-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtensor.hpp
More file actions
164 lines (140 loc) · 6.97 KB
/
tensor.hpp
File metadata and controls
164 lines (140 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* tensor.hpp
*
* A quick-and-dirty arithmetic wrapper class for vectors (tensors of rank 1, therefore called Tensor1)
* The first template argument specifies the data (float) type, the second the dimensions.
*
* Example:
* Tensor1<double, 3> t1({1,2,3});
* Tensor1<double, 3> t2({9,8,7});
* cout << t1.length() << endl;
* cout << t1 * t2 << endl; // dot product
* cout << t1 ^ t2 << endl; // cross product
* cout << t2 * 5 << endl; // scale (5 * t2 does not work because double does not know how to multiply Tensor1)
* cout << t2.normalized() << " Length: " << t2.normalized().length() << endl; // normalized
*
* Created on: Dec 27, 2016
* Author: Christian Huettig
*/
#pragma once
using namespace std;
// T == elemental data type, D == dimensions
template <typename T, unsigned char D>
class Tensor1 {
// Attributes
public:
/*
// This wastes storage space for D < 3... Trade for comfort.
union {
struct {
T x;
T y;
T z;
};
struct {
T u;
T v;
T w;
};
struct {
T r;
T phi;
T theta;
};
};*/
array<T, D> value{}; // braces guarantee zero init
// Constructors
Tensor1<T, D>() {}
Tensor1<T, D>(const array<T, D> &ia) { value = ia; }
Tensor1<T, D>(initializer_list<T> il) { copy(il.begin(), il.end(), value.begin()); }
Tensor1<T, D>(T s) { for (auto &e : value) e = s;}
T& x() { return value[0]; }
T& y() { return value[1]; }
T& z() { return value[2]; }
T& u() { return value[0]; }
T& v() { return value[1]; }
T& w() { return value[2]; }
T& r() { return value[0]; }
T& phi() { return value[1]; }
T& theta() { return value[2]; }
T norm() const { return (*this) * (*this); }
T length() const { return sqrt(norm()); }
T magnitude() const { return sqrt(norm()); }
T mag() const { return sqrt(norm()); }
void normalize() { (*this) *= 1./length(); }
Tensor1<T, D> normalized() { return Tensor1<T, D>((*this) /= length()); }
/* TODO
Tensor1<T, n> Sph2Cart();
Tensor1<T, n> Cart2Sph();
*/
Tensor1<T, D>& operator=(const Tensor1<T, D> &P) { value = P.value; return *this; } // assign
Tensor1<T, D>& operator=(const T &val) { for (auto &e : value) e = val; return *this; } // assign
Tensor1<T, D>& operator*= (const Tensor1<T, D>& rhs) { for (int i = 0; i < D; i++) value[i] *= rhs.value[i]; return *this;}
Tensor1<T, D>& operator/= (const Tensor1<T, D>& rhs) { for (int i = 0; i < D; i++) value[i] /= rhs.value[i]; return *this;}
Tensor1<T, D>& operator+= (const Tensor1<T, D>& rhs) { for (int i = 0; i < D; i++) value[i] += rhs.value[i]; return *this;}
Tensor1<T, D>& operator-= (const Tensor1<T, D>& rhs) { for (int i = 0; i < D; i++) value[i] -= rhs.value[i]; return *this;}
Tensor1<T, D>& operator*= (const T& val) { for (auto &e : value) e *= val; return *this; }
Tensor1<T, D>& operator/= (const T& val) { for (auto &e : value) e /= val; return *this; }
Tensor1<T, D>& operator+= (const T& val) { for (auto &e : value) e += val; return *this; }
Tensor1<T, D>& operator-= (const T& val) { for (auto &e : value) e -= val; return *this; }
T& operator[] (const unsigned i) { return value[i]; } // index access to x, y, z
// cross-product for 2 or 3 dimensions
Tensor1<T, 3> operator^(const Tensor1<T, D>& P) {
if (D != 2 && D !=3)
throw range_error("Tensor1 cross product N mismatch");
Tensor1<T, 3> result;
result.z() = x() * P.y() - y() * P.x();
if (D == 3) {
result.x() = y() * P.z() - z() * P.y();
result.y() = z() * P.x() - x() * P.z();
}
return result;
} // cross product
// for use as output stream cout << myT1...
friend ostream& operator<< (std::ostream& os, const Tensor1<T, D>& t) {
os << "T1(";
for (auto e : t.value)
os << e << ", ";
os << "[" << (int)D << "]) ";
return os;
}
};
// All non-member operator overloads to allow lhs/rhs exchange (3 * t1 same as t1 * 3)
// "Multiply" of two Tensor1 leads to dot-product. Component-wise only with *=
template <typename T, unsigned char D> T operator* (const Tensor1<T, D>& lhs, const Tensor1<T, D>& rhs)
{ T result = 0; for (int i = 0; i < D; i++) result += lhs.value[i] * rhs.value[i]; return result;}
// Compiler will complain about ambiguity
//template <typename T, unsigned char D> Tensor1<T, D> operator* (const Tensor1<T, D>& lhs, const Tensor1<T, D>& rhs)
//{ Tensor1<T, D> result = lhs; result *= rhs; return result;}
template <typename T, unsigned char D> Tensor1<T, D> operator* (const T& val, const Tensor1<T, D>& rhs)
{ Tensor1<T, D> result = rhs; result *= val; return result;}
template <typename T, unsigned char D> Tensor1<T, D> operator* (const Tensor1<T, D>& lhs, const T& val)
{ Tensor1<T, D> result = lhs; result *= val; return result;}
template <typename T, unsigned char D> Tensor1<T, D> operator/ (const Tensor1<T, D>& lhs, const Tensor1<T, D>& rhs)
{ Tensor1<T, D> result = lhs; result /= rhs; return result;}
template <typename T, unsigned char D> Tensor1<T, D> operator/ (const T& val, const Tensor1<T, D>& rhs)
{ Tensor1<T, D> result = rhs; result /= val; return result;}
template <typename T, unsigned char D> Tensor1<T, D> operator/ (const Tensor1<T, D>& lhs, const T& val)
{ Tensor1<T, D> result = lhs; result /= val; return result;}
template <typename T, unsigned char D> Tensor1<T, D> operator+ (const Tensor1<T, D>& lhs, const Tensor1<T, D>& rhs)
{ Tensor1<T, D> result = lhs; result += rhs; return result;}
template <typename T, unsigned char D> Tensor1<T, D> operator+ (const T& val, const Tensor1<T, D>& rhs)
{ Tensor1<T, D> result = rhs; result += val; return result;}
template <typename T, unsigned char D> Tensor1<T, D> operator+ (const Tensor1<T, D>& lhs, const T& val)
{ Tensor1<T, D> result = lhs; result += val; return result;}
template <typename T, unsigned char D> Tensor1<T, D> operator- (const Tensor1<T, D>& lhs, const Tensor1<T, D>& rhs)
{ Tensor1<T, D> result = lhs; result -= rhs; return result;}
template <typename T, unsigned char D> Tensor1<T, D> operator- (const T& val, const Tensor1<T, D>& rhs)
{ Tensor1<T, D> result = rhs; result -= val; return result;}
template <typename T, unsigned char D> Tensor1<T, D> operator- (const Tensor1<T, D>& lhs, const T& val)
{ Tensor1<T, D> result = lhs; result -= val; return result;}
template <typename T, unsigned char D> bool operator< (const Tensor1<T, D>& lhs, const Tensor1<T, D>& rhs)
{ return lhs.norm() < rhs.norm(); }
/*
template <typename T, unsigned char D> valarray<bool> operator== (const Tensor1<T, D>& lhs, const Tensor1<T, D>& rhs);
template <typename T, unsigned char D> valarray<bool> operator== (const T& val, const Tensor1<T, D>& rhs);
template <typename T, unsigned char D> valarray<bool> operator== (const Tensor1<T, D>& lhs, const T& val);
template <typename T, unsigned char D> valarray<bool> operator!= (const Tensor1<T, D>& lhs, const Tensor1<T, D>& rhs);
template <typename T, unsigned char D> valarray<bool> operator!= (const T& val, const Tensor1<T, D>& rhs);
template <typename T, unsigned char D> valarray<bool> operator!= (const Tensor1<T, D>& lhs, const T& val);
*/