52 dmat() : _data(nullptr), _rows(0), _cols(0), _ps(nullptr)
55 dmat(
const short irows,
const short icols) : _data(nullptr), _rows(0), _cols(0), _ps(nullptr)
60 dmat(
const dmat<S> & m) : _data(nullptr), _rows(0), _cols(0), _ps(nullptr)
73 inline void set_size(
const short irows,
const short icols)
75 if(irows*icols > _rows*_cols) {
76 if(_data)
delete [] _data;
77 _data =
new S[irows*icols];
85 std::swap(_data, m._data);
86 std::swap(_rows, m._rows);
87 std::swap(_cols, m._cols);
94 for(
int idx=0; idx < _rows*_cols; idx++)
95 _data[idx] = m._data[idx];
102 return (_data +(ridx*_cols));
107 return (_data +(ridx*_cols));
112 if ((_rows == 0)||(_cols == 0))
assign(m._rows, m._cols, S());
113 assert (_rows == m._rows && _cols == m._cols);
114 for(
int i=0; i<_rows*_cols; i++) _data[i] += m._data[i];
119 if ((_rows == 0)||(_cols == 0))
assign(m._rows, m._cols, S());
120 assert (_rows == m._rows && _cols == m._cols);
121 for(
int i=0; i<_rows*_cols; i++) _data[i] -= m._data[i];
126 for(
int i=0; i<_rows*_cols; i++) _data[i] *= s;
131 for(
int i=0; i<_rows*_cols; i++) _data[i] /= s;
140 for(
short i=0; i<_rows*_cols; i++) _data[i] = ele[i];
145 for(
short i=0; i<_rows*_cols; i++) _data[i] = v;
150 for(
short i=0; i<_rows*_cols; i++) _data[i] = v[i];
154 inline void assign(
const short irows,
const short icols,
const S v)
160 inline void assign(
const short irows,
const short icols,
const S *v)
169 for(
short i=0; i<_rows; i++)
170 _data[i*_cols+i] = v;
187 for(
short i=0; i<_rows; i++) {
188 for(
short j=0; j<in.
cols(); j++) {
189 for(
short k=0; k<_cols; k++)
190 out[i][j] += _data[i*_cols+k] * in[k][j];
199 for(
short i=0; i<_rows; i++) {
200 for(
short j=0; j<in.
cols(); j++) {
201 out += _data[i*_cols+j] * in[i][j];
208 inline void mult(
const S* in, S* out)
210 for(
short i=0; i<_rows; i++) {
212 for(
short j=0; j<_cols; j++)
213 out[i] += _data[i*_cols+j] * in[j];
220 for(
short i=0; i<_rows; i++) {
222 for(
short j=0; j<_cols; j++)
223 out[i] += _data[j*_cols+i] * in[j];
232 for(
short i=0; i<_rows; i++) {
233 for(
short j=i+1; j<_cols; j++)
235 S up = _data[i*_cols+j];
236 S lo = _data[j*_cols+i];
237 _data[j*_cols+i] = up;
238 _data[i*_cols+j] = lo;
247 for(
short i=0; i<t.
rows(); i++) {
248 for(
short j=0; j<t.
cols(); j++)
249 _data[j*_cols+i] = t[i][j];
254 inline void disp(
const char* name)
256 printf(
"\n%s\n", name);
257 for(
short i=0; i<_rows; i++) {
258 for(
short j=0; j<_cols; j++)
259 printf(
" %g ", _data[i*_cols+j]);
275 assert(_rows == _cols);
276 const short n = _rows;
280 S* scales =
new S[n];
281 S pivot, biggest, mlt, tempf;
282 short pivotindex = 0;
287 for (i = 0; i < n; i++) {
290 for (j = 0; j < n; j++)
291 if (biggest < (tempf = fabs(lu[i][j])))
294 scales[i] = 1.0 / biggest;
303 for (k = 0; k < n - 1; k++) {
306 for (i = k; i < n; i++) {
307 if (biggest < (tempf = fabs(lu[_ps[i]][k]) * scales[_ps[i]])) {
317 if (pivotindex != k) {
319 _ps[k] = _ps[pivotindex];
325 pivot = lu[_ps[k]][k];
326 for (i = k + 1; i < n; i++) {
327 lu[_ps[i]][k] = mlt = lu[_ps[i]][k] / pivot;
330 for (j = k + 1; j < n; j++)
331 lu[_ps[i]][j] -= mlt * lu[_ps[k]][j];
339 return lu[_ps[n - 1]][n - 1] != 0.0;
345 const short n = _rows;
348 S *
X =
new S[n], dot_prod;
350 for (i = 0; i < n; i++) X[i] = 0.0;
353 for (i = 0; i < n; i++) {
355 for (j = 0; j < i; j++) dot_prod += lu[_ps[i]][j] * X[j];
356 X[i] = rhs[_ps[i]] - dot_prod;
360 for (i = n - 1; i >= 0; i--) {
362 for (j = i + 1; j < n; j++) dot_prod += lu[_ps[i]][j] * X[j];
363 X[i] = (X[i] - dot_prod) / lu[_ps[i]][i];
366 for (i = 0; i < n; i++) rhs[i] = X[i];
383 S* r =
new S[a.
rows()];
442 for(
short i=0; i<A.
rows(); i++) {
443 for(
short j=0; j<B.
cols(); j++) {
444 out += A[i][j] * B[i][j];
455 S e0 = ele[0], e1 = ele[1], e2 = ele[2], e3 = ele[3];
456 S e4 = ele[4], e5 = ele[5], e6 = ele[6], e7 = ele[7], e8 = ele[8];
458 det = e0*e4*e8 + e1*e5*e6 + e2*e3*e7 - e2*e4*e6 - e1*e3*e8 - e0*e5*e7;
462 ele[0] = (e4*e8 - e5*e7) * idet;
463 ele[1] = (e2*e7 - e1*e8) * idet;
464 ele[2] = (e1*e5 - e2*e4) * idet;
465 ele[3] = (e5*e6 - e3*e8) * idet;
466 ele[4] = (e0*e8 - e2*e6) * idet;
467 ele[5] = (e2*e3 - e0*e5) * idet;
468 ele[6] = (e3*e7 - e4*e6) * idet;
469 ele[7] = (e1*e6 - e0*e7) * idet;
470 ele[8] = (e0*e4 - e1*e3) * idet;
476 assert(m.
rows() == 3 && m.
cols() == 3);
485 assert(m.
rows() == 3 && m.
cols() == 3);
501 S e0 = ele[0], e1 = ele[1], e2 = ele[2], e3 = ele[3];
502 S e4 = ele[4], e5 = ele[5], e6 = ele[6], e7 = ele[7], e8 = ele[8];
504 return e0*e4*e8 + e1*e5*e6 + e2*e3*e7 - e2*e4*e6 - e1*e3*e8 - e0*e5*e7;
510 assert(m.
rows() == 2 && m.
cols() == 2);
524 S e0 = ele[0], e1 = ele[1], e2 = ele[2], e3 = ele[3];
539 assert(m.
rows() == 2 && m.
cols() == 2);
546 template<
class S,
class V>
549 m.resize(arr.
size()/9);
550 for(
size_t i=0; i<m.size(); i++)
552 m[i].assign(3, 3, &arr[i*9]);
556 template<
class S,
class V>
560 for(
size_t i=0; i<m.size(); i++)
563 arr[i*9+0] = m[i][0][0]; arr[i*9+1] = m[i][0][1]; arr[i*9+2] = m[i][0][2];
564 arr[i*9+3] = m[i][1][0]; arr[i*9+4] = m[i][1][1]; arr[i*9+5] = m[i][1][2];
565 arr[i*9+6] = m[i][2][0]; arr[i*9+7] = m[i][2][1]; arr[i*9+8] = m[i][2][2];
The vector class and related algorithms.
void tensors_to_array(const vector< dmat< S > > &m, vector< V > &arr)
void diag(const S v)
set all diagonal entries to a value
dmat(const dmat< S > &m)
constructor that deep-copies a given dmat
void assign(const short irows, const short icols, const S *v)
resize and set all entries to a value
void set_size(const short irows, const short icols)
set the matrix dimensions
dmat< S > invert_2x2(const dmat< S > &m)
void assign(const S v)
set all entries to a value
void operator*=(const S s)
dmat< S > operator-(const dmat< S > &a, const dmat< S > &b)
void assign(const short irows, const short icols, const S v)
resize and set all entries to a value
void assign(const dmat< S > &m)
copy a mtrix.
dmat< S > & operator=(dmat< S > &&m)
void mult(const S *in, S *out)
mat-vec multiplication
void operator-=(const dmat< S > &m)
void mult(const dmat< S > &in, dmat< S > &out) const
mat-mat multiplication
void disp(const char *name)
dmat< S > operator+(const dmat< S > &a, const dmat< S > &b)
S det_3x3(const dmat< S > &m)
dmat< S > operator*(const dmat< S > &a, const dmat< S > &b)
void mult_transp(const S *in, S *out)
size_t size() const
The current size of the vector.
double double_cont(const dmat< S > &in) const
mat-mat double contraction
dmat(const short irows, const short icols)
constructor that initializes the dimensions
void operator/=(const S s)
void array_to_tensors(const vector< S > &arr, vector< dmat< V > > &m)
void operator+=(const dmat< S > &m)
dmat< S > operator/(const dmat< S > &a, const S v)
void resize(size_t n)
Resize a vector.
void invert_3x3(S *ele, S &det)
const S * operator[](short ridx) const
[] operator returns the pointer to the beginning of a given row
void assign(const S *v)
set all entries to a value