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
165
166
167
168
169
170
|
// Copyright 2008 Nanorex, Inc. See LICENSE file for details.
#ifndef NX_MATRIX_H
#define NX_MATRIX_H
#include <Nanorex/Utility/NXVector.h>
namespace Nanorex {
/* CLASS: NXMatrix */
/**
* Column-major matrix of dimensions M x N
*
*/
template<typename T, int M, int N>
class NXMatrix {
public:
NXMatrix() throw() {}
~NXMatrix() throw() {}
T const *const data(void) const throw() { return elems; }
T *const data(void) throw() { return elems; }
/// Access value at m-th row, n-th column
T const& operator () (int const& m, int const& n) const throw();
/// Access value at m-th row, n-th column
T& operator () (int const& m, int const& n) throw();
/// Access n-th column
NXVectorRef<T,N> col(int const& n) throw();
template<typename T1>
NXMatrix<T,M,N>& operator += (NXMatrix<T1,M,N> const& A) throw();
template<typename T1>
NXMatrix<T,M,N>& operator -= (NXMatrix<T1,M,N> const& A) throw();
template<typename T1>
NXMatrix<T,M,N>& operator *= (T1 const& c) throw();
template<typename T1>
NXMatrix<T,M,N>& operator /= (T1 const& c) throw();
private:
T elems[M*N];
};
typedef NXMatrix<double,3,3> NXMatrix33d;
typedef NXMatrix<float,3,3> NXMatrix33f;
typedef NXMatrix<double,4,4> NXMatrix44d;
typedef NXMatrix<float,4,4> NXMatrix44f;
// Access value at m-th row, n-th column
template<typename T, int M, int N>
inline
T const& NXMatrix<T,M,N>::operator () (int const& m, int const& n) const throw()
{
return elems[n*M + m];
}
// Access value at m-th row, n-th column
template<typename T, int M, int N>
inline
T& NXMatrix<T,M,N>::operator () (int const& m, int const& n) throw()
{
return elems[n*M + m];
}
// Access n-th column
template<typename T, int M, int N>
inline
NXVectorRef<T,N> NXMatrix<T,M,N>::col(int const& n) throw()
{
return NXVectorRef<T,N>(elems + n*M);
}
template<typename T, int M, int N>
template<typename T1>
inline
NXMatrix<T,M,N>&
NXMatrix<T,M,N>::operator += (NXMatrix<T1,M,N> const& A) throw()
{
// Exploit isomorphism with M*N-dimensional vectors
NXVectorRef<T,M*N> selfRef(elems);
NXVectorRef<T1,M*N> ARef(A.data());
selfRef += ARef;
return *this;
}
template<typename T, int M, int N>
template<typename T1>
inline
NXMatrix<T,M,N>&
NXMatrix<T,M,N>::operator -= (NXMatrix<T1,M,N> const& A) throw()
{
// Exploit isomorphism with M*N-dimensional vectors
NXVectorRef<T,M*N> selfRef(elems);
NXVectorRef<T1,M*N> ARef(A.data());
selfRef -= ARef;
return *this;
}
template<typename T, int M, int N>
template<typename T1>
inline
NXMatrix<T,M,N>& NXMatrix<T,M,N>::operator *= (T1 const& c) throw()
{
// Exploit isomorphism with M*N-dimensional vectors
NXVectorRef<T,M*N> selfRef(elems);
selfRef *= c;
return *this;
}
template<typename T, int M, int N>
template<typename T1>
inline
NXMatrix<T,M,N>& NXMatrix<T,M,N>::operator /= (T1 const& c) throw()
{
// Exploit isomorphism with M*N-dimensional vectors
NXVectorRef<T,M*N> selfRef(elems);
T const one_by_c = T(1) / c;
selfRef *= one_by_c;
return *this;
}
/// Matrix-matrix multiplication (inefficient)
template<typename T, int M, int N, int K>
NXMatrix<T,M,N> operator * (NXMatrix<T,M,K> const& A,
NXMatrix<T,K,N> const& B)
{
NXMatrix<T,M,N> C; // result
for(int m=0; m<M; ++m) {
for(int n=0; n<N; ++n) {
T sum_mn(0);
for(int k=0; k<K; ++k) {
sum_mn += A(m,k) * B(k,n);
}
C(m,n) = sum_mn;
}
}
}
/// Matrix-vector multiplication (inefficient)
template<typename T, int M, int N>
NXVector<T,M> operator * (NXMatrix<T,M,N> const& A,
NXVector<T,N> const& x)
{
NXVector<T,M> y; // result
y.zero();
for(int n=0; n<N; ++n) {
for(int m=0; m<M; ++m) {
y[m] += A(m,n) * x[n];
}
}
}
} // namespace Nanorex
#endif // NX_MATRIX_H
|