12 #ifndef TADDENSEMATRIX_H 13 #define TADDENSEMATRIX_H 33 template<
typename dtype>
44 inline int Height()
const {
return height; }
47 inline int NumRows()
const {
return height; }
50 inline int Width()
const {
return width; }
53 inline int NumCols()
const {
return width; }
66 template<
typename idtype>
69 height = m.GetHeight();
71 const int hw = height * width;
74 idtype *mdata = m.
Data();
75 MFEM_ASSERT(mdata,
"invalid source matrix");
78 for (
int i = 0; i < hw; i++)
96 const int hw = height * width;
99 double *mdata = m.
Data();
100 MFEM_ASSERT(mdata,
"invalid source matrix");
101 data =
new dtype[hw];
103 for (
int i = 0; i < hw; i++)
120 MFEM_ASSERT(
s >= 0,
"invalid DenseMatrix size: " <<
s);
126 data =
new dtype[capacity]();
137 MFEM_ASSERT(m >= 0 && n >= 0,
138 "invalid DenseMatrix size: " << m <<
" x " << n);
144 data =
new dtype[capacity]();
154 height = mat.
Width();
156 capacity = height * width;
159 data =
new dtype[capacity];
161 for (
int i = 0; i < height; i++)
163 for (
int j = 0; j < width; j++)
165 (*this)(i, j) = mat(j, i);
181 MFEM_ASSERT(h >= 0 && w >= 0,
182 "invalid DenseMatrix size: " << h <<
" x " << w);
189 const int hw = h * w;
190 if (hw > std::abs(capacity))
197 data =
new dtype[hw]();
202 inline dtype *
Data()
const {
return data; }
204 inline dtype *
GetData()
const {
return data; }
206 inline bool OwnsData()
const {
return (capacity > 0); }
211 MFEM_ASSERT(data && i >= 0 && i < height && j >= 0 && j < width,
"");
212 return data[i + j * height];
217 MFEM_ASSERT(data && i >= 0 && i < height && j >= 0 && j < width,
"");
218 return data[i + j * height];
221 dtype &
Elem(
int i,
int j) {
return (*
this)(i, j); }
223 const dtype &
Elem(
int i,
int j)
const {
return (*
this)(i, j); }
225 void Mult(
const dtype *x, dtype *y)
const 229 for (
int row = 0; row < height; row++)
237 for (
int row = 0; row < height; row++)
239 y[row] = x_col * d_col[row];
242 for (
int col = 1; col < width; col++)
245 for (
int row = 0; row < height; row++)
247 y[row] += x_col * d_col[row];
255 MFEM_ASSERT(height == y.
Size() && width == x.
Size(),
256 "incompatible dimensions");
258 Mult((
const dtype *) x, (dtype *) y);
264 "incompatible dimensions");
266 const int hw = height * width;
268 for (
int i = 0; i < hw; i++)
270 a += data[i] * m.data[i];
279 for (
int col = 0; col < width; col++)
282 for (
int row = 0; row < height; row++)
284 y_col += x[row] * d_col[row];
294 MFEM_ASSERT(height == x.
Size() && width == y.
Size(),
295 "incompatible dimensions");
303 const double max = (double) (RAND_MAX) + 1.;
307 seed = (int) time(0);
311 srand((
unsigned) seed);
313 for (
int i = 0; i < capacity; i++)
315 data[i] = (dtype)(std::abs(rand() / max));
322 const double max = (double) (RAND_MAX) + 1.;
326 seed = (int) time(0);
330 srand((
unsigned) seed);
332 for (
int i = 0; i < std::min(height, width); i++)
334 Elem(i, i) = (dtype)(std::abs(rand() / max));
344 for (
int i = 0; i < N; i++)
346 data[i] = (dtype) 0.0;
348 for (
int i = 0; i < n; i++)
350 data[i * (n + 1)] = c;
354 template<
typename itype>
360 for (i = 0; i < N; i++)
364 for (i = 0; i < n; i++)
366 data[i * (n + 1)] = (dtype) diag[i];
378 for (i = 0; i <
Height(); i++)
379 for (j = i + 1; j <
Width(); j++)
382 (*this)(i, j) = (*
this)(j, i);
393 template<
typename itype>
398 for (
int i = 0; i <
Height(); i++)
399 for (
int j = 0; j <
Width(); j++)
401 (*this)(i, j) = (dtype) A(j, i);
411 mfem_error(
"DenseMatrix::Symmetrize() : not a square matrix!");
415 for (
int i = 0; i <
Height(); i++)
416 for (
int j = 0; j < i; j++)
418 dtype
a = 0.5 * ((*this)(i, j) + (*
this)(j, i));
419 (*this)(j, i) = (*
this)(i, j) =
a;
425 for (
int i = 0; i <
Height(); i++)
428 for (
int j = 0; j <
Width(); j++)
431 (*this)(i, j) = (dtype) 0.0;
438 template<
typename dtype>
443 if (
a.Width() >
a.Height() ||
a.Width() < 1 ||
a.Height() > 3)
447 if (
a.Width() != adja.
Height() ||
a.Height() != adja.
Width())
453 if (
a.Width() <
a.Height())
455 const dtype *d =
a.Data();
456 dtype *ad = adja.
Data();
471 e = d[0] * d[0] + d[1] * d[1] + d[2] * d[2];
472 g = d[3] * d[3] + d[4] * d[4] + d[5] * d[5];
473 f = d[0] * d[3] + d[1] * d[4] + d[2] * d[5];
475 ad[0] = d[0] * g - d[3] *
f;
476 ad[1] = d[3] * e - d[0] *
f;
477 ad[2] = d[1] * g - d[4] *
f;
478 ad[3] = d[4] * e - d[1] *
f;
479 ad[4] = d[2] * g - d[5] *
f;
480 ad[5] = d[5] * e - d[2] *
f;
487 adja(0, 0) = (dtype) 1.0;
489 else if (
a.Width() == 2)
491 adja(0, 0) =
a(1, 1);
492 adja(0, 1) = -
a(0, 1);
493 adja(1, 0) = -
a(1, 0);
494 adja(1, 1) =
a(0, 0);
498 adja(0, 0) =
a(1, 1) *
a(2, 2) -
a(1, 2) *
a(2, 1);
499 adja(0, 1) =
a(0, 2) *
a(2, 1) -
a(0, 1) *
a(2, 2);
500 adja(0, 2) =
a(0, 1) *
a(1, 2) -
a(0, 2) *
a(1, 1);
502 adja(1, 0) =
a(1, 2) *
a(2, 0) -
a(1, 0) *
a(2, 2);
503 adja(1, 1) =
a(0, 0) *
a(2, 2) -
a(0, 2) *
a(2, 0);
504 adja(1, 2) =
a(0, 2) *
a(1, 0) -
a(0, 0) *
a(1, 2);
506 adja(2, 0) =
a(1, 0) *
a(2, 1) -
a(1, 1) *
a(2, 0);
507 adja(2, 1) =
a(0, 1) *
a(2, 0) -
a(0, 0) *
a(2, 1);
508 adja(2, 2) =
a(0, 0) *
a(1, 1) -
a(0, 1) *
a(1, 0);
int NumCols() const
Get the number of columns (size of input) of the Operator. Synonym with Width().
const dtype & Elem(int i, int j) const
void SetSize(int h, int w)
Change the size of the DenseMatrix to h x w.
int Width() const
Get the width (size of input) of the Operator. Synonym with NumCols().
void CalcAdjugate(const DenseMatrix &a, DenseMatrix &adja)
Data type dense matrix using column-major storage.
double * Data() const
Returns the matrix data array.
void MultTranspose(const dtype *x, dtype *y) const
std::function< double(const Vector &)> f(double mass_coeff)
const dtype & operator()(int i, int j) const
void MultTranspose(const TAutoDiffVector< dtype > &x, TAutoDiffVector< dtype > &y) const
TAutoDiffDenseMatrix(const TAutoDiffDenseMatrix< dtype > &mat, char ch)
dtype * Data() const
Returns the matrix data array.
void mfem_error(const char *msg)
Function called when an error is encountered. Used by the macros MFEM_ABORT, MFEM_ASSERT, MFEM_VERIFY.
dtype operator*(const TAutoDiffDenseMatrix< dtype > &m) const
void Symmetrize()
(*this) = 1/2 ((*this) + (*this)^t)
void Mult(const dtype *x, dtype *y) const
void Diag(dtype c, int n)
Creates n x n diagonal matrix with diagonal elements c.
int Width() const
Get the width (size of input) of the Operator. Synonym with NumCols().
dtype * GetData() const
Returns the matrix data array.
dtype & Elem(int i, int j)
int NumRows() const
Get the number of rows (size of output) of the Operator. Synonym with Height().
int Height() const
Get the height (size of output) of the Operator. Synonym with NumRows().
void SetSize(int s)
Change the size of the DenseMatrix to s x s.
int Height() const
Get the height (size of output) of the Operator. Synonym with NumRows().
TAutoDiffDenseMatrix(int s)
Creates square matrix of size s.
int Size() const
Returns the size of the vector.
TAutoDiffDenseMatrix(int m, int n)
Creates rectangular matrix of size m x n.
TAutoDiffDenseMatrix(const TAutoDiffDenseMatrix< idtype > &m)
Copy constructor.
Templated vector data type.
void Diag(itype *diag, int n)
Creates n x n diagonal matrix with diagonal given by diag.
void Transpose(const TAutoDiffDenseMatrix< itype > &A)
(*this) = A^t
void Transpose()
(*this) = (*this)^t
void RandomizeDiag(int seed)
Templated dense matrix data type.
TAutoDiffDenseMatrix(const DenseMatrix &m)
Copy constructor using standard DenseMatrix.
void Mult(const TAutoDiffVector< dtype > &x, TAutoDiffVector< dtype > &y) const
dtype & operator()(int i, int j)
Returns reference to a_{ij}.