tensor-0.1.0
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
numbers.h
1 // -*- mode: c++; fill-column: 80; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 /*
3  Copyright (c) 2010 Juan Jose Garcia Ripoll
4 
5  Tensor is free software; you can redistribute it and/or modify it
6  under the terms of the GNU Library General Public License as published
7  by the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Library General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #ifndef TENSOR_NUMBERS_H
21 #define TENSOR_NUMBERS_H
22 
23 #include <cmath>
24 #include <complex>
25 #include <iostream>
26 
27 namespace tensor {
28 
29 //
30 // REAL NUMBERS
31 //
32 
33 template<typename number>
34 inline number number_zero() { return static_cast<number>(0); }
35 
36 template<typename number>
37 inline number number_one() { return static_cast<number>(1); }
38 
39 /* Already in C++11 but they return complex */
40 inline double real(double r) { return r; }
41 inline double imag(double r) { return 0.0; }
42 inline double conj(double r) { return r; }
43 inline double abs2(double r) { return r*r; }
44 inline double abs(double r) { return std::abs(r); }
45 
46 template<class number> number square(number r) { return r*r; }
47 
48 //
49 // COMPLEX NUMBERS
50 //
51 
52 typedef std::complex<double> cdouble;
53 
54 inline double real(const cdouble &r) { return std::real(r); }
55 inline double imag(const cdouble &r) { return std::imag(r); }
56 inline cdouble conj(const cdouble &r) { return std::conj(r); }
57 inline double abs(const cdouble &r) { return std::abs(r); }
58 
59 inline cdouble to_complex(const double &r) {
60  return cdouble(r, 0);
61 }
62 
63 inline cdouble to_complex(const double &r, const double &i) {
64  return cdouble(r, i);
65 }
66 
67 inline cdouble to_complex(const cdouble &z) {
68  return z;
69 }
70 
71 template<>
72 inline cdouble number_zero<cdouble>() { return to_complex(0.0); }
73 
74 template<>
75 inline cdouble number_one<cdouble>() { return to_complex(1.0); }
76 
77 inline double abs2(cdouble z) { return abs2(real(z)) + abs2(imag(z)); }
78 
79 inline cdouble round(cdouble r) {
80  return to_complex(::round(real(r)),::round(imag(r)));
81 }
82 
83 inline std::istream &operator>>(std::istream &s, cdouble &z) {
84  double r, i;
85  s >> r >> i;
86  z = to_complex(r, i);
87  return s;
88 }
89 
90 inline std::ostream &operator<<(std::ostream &s, const cdouble &d) {
91  return s << real(d) << ' ' << imag(d);
92 }
93 
94 } // namespace tensor
95 
96 #endif // !TENSOR_NUMBERS_H
const RTensor conj(const RTensor &r)
Complex conjugate of a real tensor.
Definition: tensor.h:461