37 dmat() : _data(nullptr), _rows(0), _cols(0), _ps(nullptr)
40 dmat(
const short irows,
const short icols) : _data(nullptr), _rows(0), _cols(0), _ps(nullptr)
45 dmat(
const dmat<S> & m) : _data(nullptr), _rows(0), _cols(0), _ps(nullptr)
58 inline void set_size(
const short irows,
const short icols)
60 if(irows*icols > _rows*_cols) {
61 if(_data)
delete [] _data;
62 _data =
new S[irows*icols];
70 std::swap(_data, m._data);
71 std::swap(_rows, m._rows);
72 std::swap(_cols, m._cols);
79 for(
int idx=0; idx < _rows*_cols; idx++)
80 _data[idx] = m._data[idx];
87 return (_data +(ridx*_cols));
92 return (_data +(ridx*_cols));
97 if ((_rows == 0)||(_cols == 0))
assign(m._rows, m._cols, S());
98 assert (_rows == m._rows && _cols == m._cols);
99 for(
int i=0; i<_rows*_cols; i++) _data[i] += m._data[i];
104 if ((_rows == 0)||(_cols == 0))
assign(m._rows, m._cols, S());
105 assert (_rows == m._rows && _cols == m._cols);
106 for(
int i=0; i<_rows*_cols; i++) _data[i] -= m._data[i];
111 for(
int i=0; i<_rows*_cols; i++) _data[i] *= s;
116 for(
int i=0; i<_rows*_cols; i++) _data[i] /= s;
125 for(
short i=0; i<_rows*_cols; i++) _data[i] = ele[i];
130 for(
short i=0; i<_rows*_cols; i++) _data[i] = v;
135 for(
short i=0; i<_rows*_cols; i++) _data[i] = v[i];
139 inline void assign(
const short irows,
const short icols,
const S v)
145 inline void assign(
const short irows,
const short icols,
const S *v)
154 for(
short i=0; i<_rows; i++)
155 _data[i*_cols+i] = v;
172 for(
short i=0; i<_rows; i++) {
173 for(
short j=0; j<in.
cols(); j++) {
174 for(
short k=0; k<_cols; k++)
175 out[i][j] += _data[i*_cols+k] * in[k][j];
184 for(
short i=0; i<_rows; i++) {
185 for(
short j=0; j<in.
cols(); j++) {
186 out += _data[i*_cols+j] * in[i][j];
193 inline void mult(
const S* in, S* out)
195 for(
short i=0; i<_rows; i++) {
197 for(
short j=0; j<_cols; j++)
198 out[i] += _data[i*_cols+j] * in[j];
205 for(
short i=0; i<_rows; i++) {
207 for(
short j=0; j<_cols; j++)
208 out[i] += _data[j*_cols+i] * in[j];
217 for(
short i=0; i<_rows; i++) {
218 for(
short j=i+1; j<_cols; j++)
220 S up = _data[i*_cols+j];
221 S lo = _data[j*_cols+i];
222 _data[j*_cols+i] = up;
223 _data[i*_cols+j] = lo;
232 for(
short i=0; i<t.
rows(); i++) {
233 for(
short j=0; j<t.
cols(); j++)
234 _data[j*_cols+i] = t[i][j];
239 inline void disp(
const char* name)
241 printf(
"\n%s\n", name);
242 for(
short i=0; i<_rows; i++) {
243 for(
short j=0; j<_cols; j++)
244 printf(
" %g ", _data[i*_cols+j]);
260 assert(_rows == _cols);
261 const short n = _rows;
265 S* scales =
new S[n];
266 S pivot, biggest, mlt, tempf;
267 short pivotindex = 0;
272 for (i = 0; i < n; i++) {
275 for (j = 0; j < n; j++)
276 if (biggest < (tempf = fabs(lu[i][j])))
279 scales[i] = 1.0 / biggest;
288 for (k = 0; k < n - 1; k++) {
291 for (i = k; i < n; i++) {
292 if (biggest < (tempf = fabs(lu[_ps[i]][k]) * scales[_ps[i]])) {
302 if (pivotindex != k) {
304 _ps[k] = _ps[pivotindex];
310 pivot = lu[_ps[k]][k];
311 for (i = k + 1; i < n; i++) {
312 lu[_ps[i]][k] = mlt = lu[_ps[i]][k] / pivot;
315 for (j = k + 1; j < n; j++)
316 lu[_ps[i]][j] -= mlt * lu[_ps[k]][j];
324 return lu[_ps[n - 1]][n - 1] != 0.0;
330 const short n = _rows;
333 S *
X =
new S[n], dot_prod;
335 for (i = 0; i < n; i++)
X[i] = 0.0;
338 for (i = 0; i < n; i++) {
340 for (j = 0; j < i; j++) dot_prod += lu[_ps[i]][j] *
X[j];
341 X[i] = rhs[_ps[i]] - dot_prod;
345 for (i = n - 1; i >= 0; i--) {
347 for (j = i + 1; j < n; j++) dot_prod += lu[_ps[i]][j] *
X[j];
348 X[i] = (
X[i] - dot_prod) / lu[_ps[i]][i];
351 for (i = 0; i < n; i++) rhs[i] =
X[i];
368 S* r =
new S[a.
rows()];
427 for(
short i=0; i<A.
rows(); i++) {
428 for(
short j=0; j<B.
cols(); j++) {
429 out += A[i][j] * B[i][j];
440 S e0 = ele[0], e1 = ele[1], e2 = ele[2], e3 = ele[3];
441 S e4 = ele[4], e5 = ele[5], e6 = ele[6], e7 = ele[7], e8 = ele[8];
443 det = e0*e4*e8 + e1*e5*e6 + e2*e3*e7 - e2*e4*e6 - e1*e3*e8 - e0*e5*e7;
447 ele[0] = (e4*e8 - e5*e7) * idet;
448 ele[1] = (e2*e7 - e1*e8) * idet;
449 ele[2] = (e1*e5 - e2*e4) * idet;
450 ele[3] = (e5*e6 - e3*e8) * idet;
451 ele[4] = (e0*e8 - e2*e6) * idet;
452 ele[5] = (e2*e3 - e0*e5) * idet;
453 ele[6] = (e3*e7 - e4*e6) * idet;
454 ele[7] = (e1*e6 - e0*e7) * idet;
455 ele[8] = (e0*e4 - e1*e3) * idet;
461 assert(m.
rows() == 3 && m.
cols() == 3);
470 assert(m.
rows() == 3 && m.
cols() == 3);
486 S e0 = ele[0], e1 = ele[1], e2 = ele[2], e3 = ele[3];
487 S e4 = ele[4], e5 = ele[5], e6 = ele[6], e7 = ele[7], e8 = ele[8];
489 return e0*e4*e8 + e1*e5*e6 + e2*e3*e7 - e2*e4*e6 - e1*e3*e8 - e0*e5*e7;
495 assert(m.
rows() == 2 && m.
cols() == 2);
509 S e0 = ele[0], e1 = ele[1], e2 = ele[2], e3 = ele[3];
524 assert(m.
rows() == 2 && m.
cols() == 2);
531 template<
class S,
class V>
534 m.resize(arr.
size()/9);
535 for(
size_t i=0; i<m.size(); i++)
537 m[i].assign(3, 3, &arr[i*9]);
541 template<
class S,
class V>
545 for(
size_t i=0; i<m.size(); i++)
548 arr[i*9+0] = m[i][0][0]; arr[i*9+1] = m[i][0][1]; arr[i*9+2] = m[i][0][2];
549 arr[i*9+3] = m[i][1][0]; arr[i*9+4] = m[i][1][1]; arr[i*9+5] = m[i][1][2];
550 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 assign(const short irows, const short icols, const S *v)
resize and set all entries to a value
dmat(const short irows, const short icols)
constructor that initializes the dimensions
void operator-=(const dmat< S > &m)
dmat< S > & operator=(dmat< S > &&m)
void diag(const S v)
set all diagonal entries to a value
void assign(const dmat< S > &m)
copy a mtrix.
void assign(const S v)
set all entries to a value
dmat(const dmat< S > &m)
constructor that deep-copies a given dmat
double double_cont(const dmat< S > &in) const
mat-mat double contraction
void set_size(const short irows, const short icols)
set the matrix dimensions
void assign(const S *v)
set all entries to a value
void assign(const short irows, const short icols, const S v)
resize and set all entries to a value
void operator/=(const S s)
void operator+=(const dmat< S > &m)
void disp(const char *name)
void mult(const S *in, S *out)
mat-vec multiplication
void operator*=(const S s)
const S * operator[](short ridx) const
[] operator returns the pointer to the beginning of a given row
void mult_transp(const S *in, S *out)
void mult(const dmat< S > &in, dmat< S > &out) const
mat-mat multiplication
size_t size() const
The current size of the vector.
void resize(size_t n)
Resize a vector.
void invert_3x3(S *ele, S &det)
dmat< S > operator-(const dmat< S > &a, const dmat< S > &b)
dmat< S > operator/(const dmat< S > &a, const S v)
double double_cont(const dmat< S > &A, const dmat< S > &B)
mat-mat double contraction A:B
dmat< S > invert_2x2(const dmat< S > &m)
dmat< S > operator*(const dmat< S > &a, const dmat< S > &b)
dmat< S > transpose(const dmat< S > &a)
void tensors_to_array(const vector< dmat< S > > &m, vector< V > &arr)
dmat< S > operator+(const dmat< S > &a, const dmat< S > &b)
void array_to_tensors(const vector< S > &arr, vector< dmat< V > > &m)
S det_3x3(const dmat< S > &m)