CommonInline.cxx
1 // Copyright (C) 2001-2012 Vivien Mallet
2 //
3 // This file is part of the linear-algebra library Seldon,
4 // http://seldon.sourceforge.net/.
5 //
6 // Seldon is free software; you can redistribute it and/or modify it under the
7 // terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 2.1 of the License, or (at your option)
9 // any later version.
10 //
11 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
14 // more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with Seldon. If not, see http://www.gnu.org/licenses/.
18 
19 
20 #ifndef SELDON_FILE_COMMON_INLINE_CXX
21 
22 
23 namespace std
24 {
25  template<class T>
26  inline T conjugate(const T& x)
27  {
28  return x;
29  }
30 
31  template<class T>
32  inline complex<T> conjugate(const complex<T>& x)
33  {
34  return conj(x);
35  }
36 
37  template<class T>
38  inline T realpart(const T& x)
39  {
40  return x;
41  }
42 
43  template<class T>
44  inline T realpart(const complex<T>& x)
45  {
46  return real(x);
47  }
48 }
49 
50 namespace Seldon
51 {
52 
53  template <class T>
54  inline void PrintArray(T* v, int lgth)
55  {
56  for (int k = 0; k < lgth - 1; k++)
57  std::cout << v[k] << " | ";
58  std::cout << v[lgth - 1] << std::endl;
59  }
60 
61 
63  inline Str::Str()
64  {
65  }
66 
67 
69 
72  inline Str::Str(const Str& s)
73  {
74  string s_input = s;
75  output_ << s_input;
76  }
77 
78 
80  inline Str::operator std::string() const
81  {
82  return output_.str();
83  }
84 
85 
87 
90  template <class T>
91  inline Str& Str::operator << (const T& input)
92  {
93  output_ << input;
94  return *this;
95  }
96 
97 
99 
103  template <class T>
104  inline Str operator + (const Str& s, const T& input)
105  {
106  string s_input = s;
107  Str output;
108  output << s_input << input;
109  return output;
110  }
111 
112 
114  inline ostream& operator << (ostream& out, Str& in)
115  {
116  string output = in;
117  out << output;
118  return out;
119  }
120 
121 
123  inline ostream& operator << (ostream& out, Str in)
124  {
125  string output = in;
126  out << output;
127  return out;
128  }
129 
130 
132 
136  template<typename T>
137  inline std::string to_str(const T& input)
138  {
139  std::ostringstream output;
140  output.precision(cout.precision());
141  output << input;
142  return output.str();
143  }
144 
145 
147 
151  template <class T>
152  inline void to_num(std::string s, T& num)
153  {
154  std::istringstream str(s);
155  str >> num;
156  }
157 
158 
160 
164  template <class T>
165  inline T to_num(std::string s)
166  {
167  T num;
168  std::istringstream str(s);
169  str >> num;
170  return num;
171  }
172 
173 
175 
178  template <class T>
179  inline void SetComplexZero(T& number)
180  {
181  number = T(0);
182  }
183 
184 
186 
189  template <class T>
190  inline void SetComplexZero(std::complex<T>& number)
191  {
192  number = std::complex<T>(T(0), T(0));
193  }
194 
195 
197 
200  template <class T>
201  inline void SetComplexOne(T& number)
202  {
203  number = T(1);
204  }
205 
206 
208 
211  template <class T>
212  inline void SetComplexOne(std::complex<T>& number)
213  {
214  number = complex<T>(T(1), T(0));
215  }
216 
217 
219 
222  template <class T>
223  inline void SetComplexReal(int n, T& number)
224  {
225  number = T(n);
226  }
227 
228 
230 
233  template <class T>
234  inline void SetComplexReal(int n, std::complex<T>& number)
235  {
236  number = std::complex<T>(n, 0);
237  }
238 
239 
241 
244  template <class T>
245  inline void SetComplexReal(bool n, std::complex<T>& number)
246  {
247  number = std::complex<T>(n, 0);
248  }
249 
250 
252  inline void SetComplexReal(int x, std::complex<int>& number)
253  {
254  number = std::complex<int>(x, 0);
255  }
256 
257 
259 
262  template <class T>
263  inline void SetComplexReal(const T& x, std::complex<T>& number)
264  {
265  number = std::complex<T>(x, 0);
266  }
267 
268 
270 
273  template <class T>
274  inline void SetComplexReal(const std::complex<T>& x, T& number)
275  {
276  number = std::realpart(x);
277  }
278 
279 
281 
284  template <class T0, class T1>
285  inline void SetComplexReal(const T0& x, T1& number)
286  {
287  number = x;
288  }
289 
290 
292  template <class T>
293  inline bool IsComplexNumber(const T& number)
294  {
295  return false;
296  }
297 
298 
300  template <class T>
301  inline bool IsComplexNumber(const std::complex<T>& number)
302  {
303  return true;
304  }
305 
306 
308  template<class T>
309  inline T ComplexAbs(const T& val)
310  {
311  return abs(val);
312  }
313 
314 
316  template<class T>
317  inline T ComplexAbs(const std::complex<T>& val)
318  {
319 #if defined(SELDON_WITH_BLAS) && !defined(SELDON_WITH_LAPACK)
320  // we choose Blas convention
321  return abs(real(val)) + abs(imag(val));
322 #else
323  // otherwise usual modulus
324  return abs(val);
325 #endif
326  }
327 
328 
330  template<class T>
331  inline T absSquare(const std::complex<T>& z)
332  {
333  // more optimal than real(z * conj(z))
334  return real(z)*real(z) + imag(z)*imag(z);
335  }
336 
337 
339  template<class T>
340  inline T absSquare(const T& z)
341  {
342  return z*z;
343  }
344 
345 } // namespace Seldon.
346 
347 #define SELDON_FILE_COMMON_INLINE_CXX
348 #endif
Seldon::operator+
const TinyMatrixSum< T, m, n, E1, E2 > operator+(const TinyMatrixExpression< T, m, n, E1 > &u, const TinyMatrixExpression< T, m, n, E2 > &v)
returns u+v
Definition: TinyMatrixExpressionInline.cxx:197
Seldon::to_str
std::string to_str(const T &input)
Converts most types to string.
Definition: CommonInline.cxx:137
Seldon::Str::operator<<
Str & operator<<(const T &input)
Adds an element to the string.
Definition: CommonInline.cxx:91
Seldon::Str
This class helps formatting C++ strings on the fly.
Definition: Common.hxx:57
Seldon::Str::Str
Str()
Default constructor.
Definition: CommonInline.cxx:63
Seldon::IsComplexNumber
bool IsComplexNumber(const T &number)
Returns true for a complex number.
Definition: CommonInline.cxx:293
Seldon::absSquare
T absSquare(const T &x)
returns the square modulus of z
Definition: CommonInline.cxx:340
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::operator<<
ostream & operator<<(ostream &out, const Array< T, N, Allocator > &A)
operator<< overloaded for a 3D array.
Definition: Array.cxx:1617
Seldon::SetComplexReal
void SetComplexReal(int n, std::complex< T > &number)
Sets a complex number to (n, 0).
Definition: CommonInline.cxx:234
Seldon::ComplexAbs
T ComplexAbs(const T &val)
returns absolute value of val
Definition: CommonInline.cxx:309