MFEM  v4.4.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
array.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2022, Lawrence Livermore National Security, LLC. Produced
2 // at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3 // LICENSE and NOTICE for details. LLNL-CODE-806117.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability visit https://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the BSD-3 license. We welcome feedback and contributions, see file
10 // CONTRIBUTING.md for details.
11 
12 
13 // Abstract array data type
14 
15 #include "array.hpp"
16 #include "../general/forall.hpp"
17 #include <fstream>
18 
19 namespace mfem
20 {
21 
22 template <class T>
23 void Array<T>::Print(std::ostream &os, int width) const
24 {
25  for (int i = 0; i < size; i++)
26  {
27  os << data[i];
28  if ( !((i+1) % width) || i+1 == size )
29  {
30  os << '\n';
31  }
32  else
33  {
34  os << " ";
35  }
36  }
37 }
38 
39 template <class T>
40 void Array<T>::Save(std::ostream &os, int fmt) const
41 {
42  if (fmt == 0)
43  {
44  os << size << '\n';
45  }
46  for (int i = 0; i < size; i++)
47  {
48  os << operator[](i) << '\n';
49  }
50 }
51 
52 template <class T>
53 void Array<T>::Load(std::istream &in, int fmt)
54 {
55  if (fmt == 0)
56  {
57  int new_size;
58  in >> new_size;
59  SetSize(new_size);
60  }
61  for (int i = 0; i < size; i++)
62  {
63  in >> operator[](i);
64  }
65 }
66 
67 template <class T>
68 T Array<T>::Max() const
69 {
70  MFEM_ASSERT(size > 0, "Array is empty with size " << size);
71 
72  T max = operator[](0);
73  for (int i = 1; i < size; i++)
74  {
75  if (max < operator[](i))
76  {
77  max = operator[](i);
78  }
79  }
80 
81  return max;
82 }
83 
84 template <class T>
85 T Array<T>::Min() const
86 {
87  MFEM_ASSERT(size > 0, "Array is empty with size " << size);
88 
89  T min = operator[](0);
90  for (int i = 1; i < size; i++)
91  {
92  if (operator[](i) < min)
93  {
94  min = operator[](i);
95  }
96  }
97 
98  return min;
99 }
100 
101 // Partial Sum
102 template <class T>
104 {
105  T sum = static_cast<T>(0);
106  for (int i = 0; i < size; i++)
107  {
108  sum+=operator[](i);
109  operator[](i) = sum;
110  }
111 }
112 
113 // Sum
114 template <class T>
116 {
117  T sum = static_cast<T>(0);
118  for (int i = 0; i < size; i++)
119  {
120  sum+=operator[](i);
121  }
122 
123  return sum;
124 }
125 
126 template <class T>
128 {
129  T val_prev = operator[](0), val;
130  for (int i = 1; i < size; i++)
131  {
132  val=operator[](i);
133  if (val < val_prev)
134  {
135  return 0;
136  }
137  val_prev = val;
138  }
139 
140  return 1;
141 }
142 
143 
144 template <class T>
145 void Array2D<T>::Load(const char *filename, int fmt)
146 {
147  std::ifstream in;
148  in.open(filename, std::ifstream::in);
149  MFEM_VERIFY(in.is_open(), "File " << filename << " does not exist.");
150  Load(in, fmt);
151  in.close();
152 }
153 
154 template <class T>
155 void Array2D<T>::Print(std::ostream &os, int width_)
156 {
157  int height = this->NumRows();
158  int width = this->NumCols();
159 
160  for (int i = 0; i < height; i++)
161  {
162  os << "[row " << i << "]\n";
163  for (int j = 0; j < width; j++)
164  {
165  os << (*this)(i,j);
166  if ( (j+1) == width_ || (j+1) % width_ == 0 )
167  {
168  os << '\n';
169  }
170  else
171  {
172  os << ' ';
173  }
174  }
175  }
176 }
177 
178 template class Array<char>;
179 template class Array<int>;
180 template class Array<long long>;
181 template class Array<double>;
182 template class Array2D<int>;
183 template class Array2D<double>;
184 
185 } // namespace mfem
void Load(std::istream &in, int fmt=0)
Read an Array from the stream in using format fmt. The format fmt can be:
Definition: array.cpp:53
void Load(std::istream &in, int fmt=0)
Read an Array2D from the stream in using format fmt. The format fmt can be:
Definition: array.hpp:412
void Save(std::ostream &out, int fmt=0) const
Save the Array to the stream out using the format fmt. The format fmt can be:
Definition: array.cpp:40
T Sum()
Return the sum of all the array entries using the &#39;+&#39;&#39; operator for class &#39;T&#39;.
Definition: array.cpp:115
T Min() const
Find the minimal element in the array, using the comparison operator &lt; for class T.
Definition: array.cpp:85
T Max() const
Find the maximal element in the array, using the comparison operator &lt; for class T.
Definition: array.cpp:68
void Print(std::ostream &out=mfem::out, int width=4)
Prints array to stream with width elements per row.
Definition: array.cpp:155
int IsSorted()
Return 1 if the array is sorted from lowest to highest. Otherwise return 0.
Definition: array.cpp:127
void PartialSum()
Fill the entries of the array with the cumulative sum of the entries.
Definition: array.cpp:103
void Print(std::ostream &out=mfem::out, int width=4) const
Prints array to stream with width elements per row.
Definition: array.cpp:23