NUMCXX  0.13.20181108
Numerical library for small projects and teaching purposes
util.ixx
Go to the documentation of this file.
1 #include <ctime>
2 #include <fstream>
3 
4 namespace numcxx
5 {
6 
7 
8  template <typename T>
9  inline std::shared_ptr<TArray1<T>> linspace(T x0, T x1, int n)
10  {
11  auto pX=std::make_shared<TArray1<T>>(n);
12  auto &X=*pX;
13  T h=1.0/(T)(n-1);
14  T x=0.0;
15  for (int i=0;i<n;i++,x+=h) X[i]=x;
16  return pX;
17  }
18 
19  template <typename A> inline TArray1<typename A::value_type> arrayexpr(const A& a)
20  {
22  return std::move(v);
23  }
24 
25 
26  template <typename A> inline typename A::value_type normi(const A& a)
27  {
28  typename A::value_type norm=std::abs(a[0]);
29  for(index i=1; i<a.size(); i++ )
30  {
31  typename A::value_type x=std::abs(a[i]);
32  if (x>norm) norm=x;
33  }
34  return norm;
35  }
36 
37  template <typename A> inline typename A::value_type norm1(const A& a)
38  {
39  typename A::value_type norm=std::abs(a[0]);
40  for(index i=1; i<a.size(); i++ )
41  {
42  norm+=std::abs(a[i]);
43  }
44  return norm;
45  }
46 
47  template <typename A> inline typename A::value_type norm2(const A& a)
48  {
49  typename A::value_type norm=0.0;
50  for(index i=0; i<a.size(); i++ )
51  {
52  typename A::value_type x=a[i];
53  norm+=x*x;
54  }
55  return sqrt(norm);
56  }
57 
58 
59  template <typename A> inline typename A::value_type min(const A&a)
60  {
61  typename A::value_type m=a[0];
62  for(index i=1; i<a.size(); i++ )
63  {
64  typename A::value_type x=a[i];
65  if (x<m) m=x;
66  }
67  return m;
68  }
69 
70  template <typename A> inline typename A::value_type max(const A&a)
71  {
72  typename A::value_type m=a[0];
73  for(index i=1; i<a.size(); i++ )
74  {
75  typename A::value_type x=a[i];
76  if (x>m) m=x;
77  }
78  return m;
79  }
80 
81  template <typename A> inline typename A::value_type sum(const A&a)
82  {
83  typename A::value_type s=a[0];
84  for(index i=1; i<a.size(); i++ )
85  {
86  s+=a[i];
87  }
88  return s;
89  }
90 
91  template <typename A, typename B> inline typename A::value_type dot(const A& a, const B&b)
92  {
93  typename A::value_type dot=0.0;
94  for(index i=0; i<a.size(); i++ )
95  {
96  dot+=a[i]*b[i];
97  }
98  return dot;
99  }
100 
101  inline double cpu_clock()
102  {
103  return (double)std::clock()/(double)CLOCKS_PER_SEC;
104  }
105 
106  inline double wall_clock()
107  {
108  time_t t;
109  t=time(&t);
110  return ((double)t);
111  }
112 
113  template <typename T>
114  inline void savetxt(const std::string fname, const TArray<T> &a)
115  {
116  std::filebuf fb;
117  fb.open (fname,std::ios::out);
118  std::ostream s(&fb);
119  a.savetxt(s);
120  fb.close();
121  }
122 
123  template <typename T>
124  inline void savetxt(std::ostream &s, const TArray<T> &a)
125  {
126  a.savetxt(s);
127  }
128 
129 
130 }
void savetxt(std::ostream &s) const
Definition: tarray.ixx:351
A::value_type max(const A &a)
Maximum of array or expression.
Definition: util.ixx:70
A::value_type normi(const A &a)
Maximum norm of array or expression.
Definition: util.ixx:26
void savetxt(const std::string fname, const TArray< T > &a)
Definition: util.ixx:114
TArray1< typename A::value_type > arrayexpr(const A &a)
Evaluate expression as array.
Definition: util.ixx:19
A::value_type norm2(const A &a)
Euklidean norm of array or expression.
Definition: util.ixx:47
TArray is the common template base class for arrays and dense matrices of the numcxx project...
Definition: tarray.hxx:17
A::value_type min(const A &a)
Minimum of of array or expression.
Definition: util.ixx:59
std::shared_ptr< TArray1< T > > linspace(T x0, T x1, int n)
Create array of $n$ equally spaced entries.
Definition: util.ixx:9
double wall_clock()
wall clock time in seconds
Definition: util.ixx:106
unsigned int index
Definition: numcxx.hxx:21
One dimensional array class.
Definition: tarray1.hxx:31
Real abs(Real x)
Definition: gmres.h:43
A::value_type sum(const A &a)
Sum of array or expression.
Definition: util.ixx:81
double cpu_clock()
cpu time in seconds
Definition: util.ixx:101
double B(double x)
Numcxx template library.
Definition: expression.ixx:41
A::value_type dot(const A &a, const B &b)
Dot product of array or expression.
Definition: util.ixx:91
A::value_type norm(const A &a)
Euklidean norm of array or expression.
Definition: util.hxx:54
A::value_type norm1(const A &a)
Sum norm of array or expression.
Definition: util.ixx:37