KADATH
array_math.hpp
1 /*
2  Copyright 2017 Philippe Grandclement
3 
4  This file is part of Kadath.
5 
6  Kadath is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Kadath is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Kadath. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "math.h"
21 #include "array.hpp"
22 
23 namespace Kadath {
24 
25 template <typename T> inline Array<T> sin (const Array<T>& so) {
26  Array<T> res(so.dimensions) ;
27  for (int i=0 ; i<so.nbr ; i++)
28  res.data[i] = sin(so.data[i]) ;
29  return res ;
30 }
31 
32 template <typename T> inline Array<T> cos (const Array<T>& so) {
33  Array<T> res(so.dimensions) ;
34  for (int i=0 ; i<so.nbr ; i++)
35  res.data[i] = cos(so.data[i]) ;
36  return res ;
37 }
38 
39 template <typename T> inline Array<T> sinh (const Array<T>& so) {
40  Array<T> res(so.dimensions) ;
41  for (int i=0 ; i<so.nbr ; i++)
42  res.data[i] = sinh(so.data[i]) ;
43  return res ;
44 }
45 
46 template <typename T> inline Array<T> cosh (const Array<T>& so) {
47  Array<T> res(so.dimensions) ;
48  for (int i=0 ; i<so.nbr ; i++)
49  res.data[i] = cosh(so.data[i]) ;
50  return res ;
51 }
52 
53 template <typename T> inline Array<T> operator+ (const Array<T>& so) {return Array<T>{so};}
54 
55 
56 template <typename T> inline Array<T> operator- (const Array<T>& so) {
57  Array<T> res(so.dimensions) ;
58  for (int i=0 ; i<so.nbr ; i++)
59  res.data[i] = -so.data[i] ;
60  return res ;
61 }
62 
63 
64 template <typename T> inline Array<T> operator+ (const Array<T>& a, const Array<T>& b) {
65  assert (a.nbr==b.nbr) ;
66  Array<T> res(a.dimensions) ;
67  for (int i=0 ; i<a.nbr ; i++)
68  res.data[i] = a.data[i] + b.data[i] ;
69  return res ;
70 }
71 
72 
73 template <typename T> inline Array<T> operator+ (const Array<T>& so , T xx) {
74  Array<T> res(so.dimensions) ;
75  for (int i=0 ; i<so.nbr ; i++)
76  res.data[i] = so.data[i] + xx;
77  return res ;
78 }
79 
80 template <typename T> inline Array<T> operator+ (T xx, const Array<T>& so) {
81  Array<T> res(so.dimensions) ;
82  for (int i=0 ; i<so.nbr ; i++)
83  res.data[i] = xx + so.data[i] ;
84  return res ;
85 }
86 
87 
88 
89 template <typename T> inline Array<T> operator- (const Array<T>& a, const Array<T>& b) {
90  assert (a.nbr==b.nbr) ;
91  Array<T> res(a.dimensions) ;
92  for (int i=0 ; i<a.nbr ; i++)
93  res.data[i] = a.data[i] - b.data[i] ;
94  return res ;
95 }
96 
97 
98 template <typename T> inline Array<T> operator- (const Array<T>& so, T xx) {
99  Array<T> res(so.dimensions) ;
100  for (int i=0 ; i<so.nbr ; i++)
101  res.data[i] = so.data[i] - xx;
102  return res ;
103 }
104 
105 
106 template <typename T> inline Array<T> operator- (T xx, const Array<T>& so) {
107  Array<T> res(so.dimensions) ;
108  for (int i=0 ; i<so.nbr ; i++)
109  res.data[i] = xx - so.data[i] ;
110  return res ;
111 }
112 
113 template <typename T> inline Array<T> operator* (const Array<T>& a, const Array<T>& b) {
114  assert (a.nbr==b.nbr) ;
115  Array<T> res(a.dimensions) ;
116  for (int i=0 ; i<a.nbr ; i++)
117  res.data[i] = a.data[i] * b.data[i] ;
118  return res ;
119 }
120 
121 
122 template <typename T> inline Array<T> operator* (const Array<T>& so, T xx) {
123  Array<T> res(so.dimensions) ;
124  for (int i=0 ; i<so.nbr ; i++)
125  res.data[i] = so.data[i] * xx;
126  return res ;
127 }
128 
129 template <typename T> inline Array<T> operator* (T xx, const Array<T>& so) {
130  Array<T> res(so.dimensions) ;
131  for (int i=0 ; i<so.nbr ; i++)
132  res.data[i] = xx * so.data[i] ;
133  return res ;
134 }
135 
136 template <typename T> inline Array<T> operator/ (const Array<T>& a, const Array<T>& b ){
137  assert (a.nbr==b.nbr) ;
138  Array<T> res(a.dimensions) ;
139  for (int i=0 ; i<a.nbr ; i++)
140  res.data[i] = a.data[i] / b.data[i] ;
141  return res ;
142 }
143 
144 template <typename T> inline Array<T> operator/ (const Array<T>& so, T xx) {
145  Array<T> res(so.dimensions) ;
146  for (int i=0 ; i<so.nbr ; i++)
147  res.data[i] = so.data[i] / xx;
148  return res ;
149 }
150 
151 template <typename T> inline Array<T> operator/ (T xx, const Array<T>& so) {
152  Array<T> res(so.dimensions) ;
153  for (int i=0 ; i<so.nbr ; i++)
154  res.data[i] = xx / so.data[i] ;
155  return res;
156 }
157 
158 template <typename T> inline Array<T> pow (const Array<T>& so, int n) {
159  Array<T> res(so.dimensions) ;
160  for (int i=0 ; i<so.nbr ; i++)
161  res.data[i] = pow(so.data[i],n) ;
162  return res;
163 }
164 
165 template <typename T> inline Array<T> pow (const Array<T>& so, double nn) {
166  Array<T> res(so.dimensions) ;
167  for (int i=0 ; i<so.nbr ; i++)
168  res.data[i] = pow(so.data[i],nn) ;
169  return res;
170 }
171 
172 template <typename T> inline Array<T> sqrt (const Array<T>& so) {
173  Array<T> res(so.dimensions) ;
174  for (int i=0 ; i<so.nbr ; i++)
175  res.data[i] = sqrt(so.data[i]) ;
176  return res;
177 }
178 
179 template <typename T> inline Array<T> exp (const Array<T>& so) {
180  Array<T> res(so.dimensions) ;
181  for (int i=0 ; i<so.nbr ; i++)
182  res.data[i] = exp(so.data[i]) ;
183  return res;
184 }
185 
186 template <typename T> inline Array<T> log (const Array<T>& so) {
187  Array<T> res(so.dimensions) ;
188  for (int i=0 ; i<so.nbr ; i++)
189  res.data[i] = log(so.data[i]) ;
190  return res;
191 }
192 
193 template <typename T> inline Array<T> atanh (const Array<T>& so) {
194  Array<T> res(so.dimensions) ;
195  for (int i=0 ; i<so.nbr ; i++)
196  res.data[i] = atanh(so.data[i]) ;
197  return res;
198 }
199 
200 template <typename T> inline Array<T> fabs (const Array<T>& so) {
201  Array<T> res(so.dimensions) ;
202  for (int i=0 ; i<so.nbr ; i++)
203  res.data[i] = fabs(so.data[i]) ;
204  return res;
205 }
206 
207 template <typename T> inline T scal (const Array<T>& a, const Array<T>& b) {
208  T res = a.data[0] * b.data[0] ;
209  for (int i=1 ; i<a.get_size(0) ; i++)
210  res += a.data[i] * b.data[i] ;
211  return res ;
212 }
213 
214 
215 template <typename T> inline T diffmax (const Array<T>& a, const Array<T>& b) {
216  assert (a.nbr==b.nbr) ;
217  T res = 0 ;
218  T diff ;
219  for (int i=0 ; i<a.nbr ; i++) {
220  diff = fabs(a.data[i]-b.data[i]) ;
221  if (diff> res)
222  res = diff ;
223  }
224  return res ;
225 }
226 
227 template <typename T> inline T max (const Array<T>& so) {
228  T res = so.data[0] ;
229  for (int i=0 ; i<so.nbr ; i++)
230  if (so.data[i]>res)
231  res = so.data[i] ;
232  return res;
233 }
234 
235 template <typename T> inline T min (const Array<T>& so) {
236  T res = so.data[0] ;
237  for (int i=0 ; i<so.nbr ; i++)
238  if (so.data[i]<res)
239  res = so.data[i] ;
240  return res;
241 }
242 
243 template <typename T> inline T sum (const Array<T>& so) {
244  T res = 0 ;
245  for (int i=0 ; i<so.nbr ; i++)
246  res += so.data[i] ;
247  return res;
248 }
249 
250 template <typename T> inline Array<T> atan (const Array<T>& so) {
251  Array<T> res(so.dimensions) ;
252  for (int i=0 ; i<so.nbr ; i++)
253  res.data[i] = atan(so.data[i]) ;
254  return res;
255 }
256 }
Template class for arrays.
Definition: array.hpp:86
int get_size(int i) const
Returns the size of a given dimension.
Definition: array.hpp:331
Dim_array dimensions
Dimensions of the Array.
Definition: array.hpp:88
int nbr
Total number of elements.
Definition: array.hpp:91
Memory_mapped_array< T > data
Elements of the Array.
Definition: array.hpp:92