MFEM  v3.3.2
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
densemat.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
11 
12 #ifndef MFEM_DENSEMAT
13 #define MFEM_DENSEMAT
14 
15 #include "../config/config.hpp"
16 #include "../general/globals.hpp"
17 #include "matrix.hpp"
18 
19 namespace mfem
20 {
21 
22 /// Data type dense matrix using column-major storage
23 class DenseMatrix : public Matrix
24 {
25  friend class DenseTensor;
26  friend class DenseMatrixInverse;
27 
28 private:
29  double *data;
30  int capacity; // zero or negative capacity means we do not own the data.
31 
32  void Eigensystem(Vector &ev, DenseMatrix *evect = NULL);
33 
34  // Auxiliary method used in FNorm2() and FNorm()
35  void FNorm(double &scale_factor, double &scaled_fnorm2) const;
36 
37 public:
38  /** Default constructor for DenseMatrix.
39  Sets data = NULL and height = width = 0. */
40  DenseMatrix();
41 
42  /// Copy constructor
43  DenseMatrix(const DenseMatrix &);
44 
45  /// Creates square matrix of size s.
46  explicit DenseMatrix(int s);
47 
48  /// Creates rectangular matrix of size m x n.
49  DenseMatrix(int m, int n);
50 
51  /// Creates rectangular matrix equal to the transpose of mat.
52  DenseMatrix(const DenseMatrix &mat, char ch);
53 
54  /** Construct a DenseMatrix using existing data array. The DenseMatrix does
55  not assume ownership of the data array, i.e. it will not delete the
56  array. */
57  DenseMatrix(double *d, int h, int w) : Matrix(h, w)
58  { data = d; capacity = -h*w; }
59 
60  /// Change the data array and the size of the DenseMatrix.
61  /** The DenseMatrix does not assume ownership of the data array, i.e. it will
62  not delete the data array @a d. This method should not be used with
63  DenseMatrix that owns its current data array. */
64  void UseExternalData(double *d, int h, int w)
65  { data = d; height = h; width = w; capacity = -h*w; }
66 
67  /// Change the data array and the size of the DenseMatrix.
68  /** The DenseMatrix does not assume ownership of the data array, i.e. it will
69  not delete the new array @a d. This method will delete the current data
70  array, if owned. */
71  void Reset(double *d, int h, int w)
72  { if (OwnsData()) { delete [] data; } UseExternalData(d, h, w); }
73 
74  /** Clear the data array and the dimensions of the DenseMatrix. This method
75  should not be used with DenseMatrix that owns its current data array. */
76  void ClearExternalData() { data = NULL; height = width = 0; capacity = 0; }
77 
78  /// Delete the matrix data array (if owned) and reset the matrix state.
79  void Clear()
80  { if (OwnsData()) { delete [] data; } ClearExternalData(); }
81 
82  /// For backward compatibility define Size to be synonym of Width()
83  int Size() const { return Width(); }
84 
85  /// Change the size of the DenseMatrix to s x s.
86  void SetSize(int s) { SetSize(s, s); }
87 
88  /// Change the size of the DenseMatrix to h x w.
89  void SetSize(int h, int w);
90 
91  /// Returns the matrix data array.
92  inline double *Data() const { return data; }
93  /// Returns the matrix data array.
94  inline double *GetData() const { return data; }
95 
96  inline bool OwnsData() const { return (capacity > 0); }
97 
98  /// Returns reference to a_{ij}.
99  inline double &operator()(int i, int j);
100 
101  /// Returns constant reference to a_{ij}.
102  inline const double &operator()(int i, int j) const;
103 
104  /// Matrix inner product: tr(A^t B)
105  double operator*(const DenseMatrix &m) const;
106 
107  /// Trace of a square matrix
108  double Trace() const;
109 
110  /// Returns reference to a_{ij}.
111  virtual double &Elem(int i, int j);
112 
113  /// Returns constant reference to a_{ij}.
114  virtual const double &Elem(int i, int j) const;
115 
116  /// Matrix vector multiplication.
117  void Mult(const double *x, double *y) const;
118 
119  /// Matrix vector multiplication.
120  virtual void Mult(const Vector &x, Vector &y) const;
121 
122  /// Multiply a vector with the transpose matrix.
123  void MultTranspose(const double *x, double *y) const;
124 
125  /// Multiply a vector with the transpose matrix.
126  virtual void MultTranspose(const Vector &x, Vector &y) const;
127 
128  /// y += A.x
129  void AddMult(const Vector &x, Vector &y) const;
130 
131  /// y += A^t x
132  void AddMultTranspose(const Vector &x, Vector &y) const;
133 
134  /// y += a * A.x
135  void AddMult_a(double a, const Vector &x, Vector &y) const;
136 
137  /// y += a * A^t x
138  void AddMultTranspose_a(double a, const Vector &x, Vector &y) const;
139 
140  /// Compute y^t A x
141  double InnerProduct(const double *x, const double *y) const;
142 
143  /// LeftScaling this = diag(s) * this
144  void LeftScaling(const Vector & s);
145  /// InvLeftScaling this = diag(1./s) * this
146  void InvLeftScaling(const Vector & s);
147  /// RightScaling: this = this * diag(s);
148  void RightScaling(const Vector & s);
149  /// InvRightScaling: this = this * diag(1./s);
150  void InvRightScaling(const Vector & s);
151  /// SymmetricScaling this = diag(sqrt(s)) * this * diag(sqrt(s))
152  void SymmetricScaling(const Vector & s);
153  /// InvSymmetricScaling this = diag(sqrt(1./s)) * this * diag(sqrt(1./s))
154  void InvSymmetricScaling(const Vector & s);
155 
156  /// Compute y^t A x
157  double InnerProduct(const Vector &x, const Vector &y) const
158  { return InnerProduct((const double *)x, (const double *)y); }
159 
160  /// Returns a pointer to the inverse matrix.
161  virtual MatrixInverse *Inverse() const;
162 
163  /// Replaces the current matrix with its inverse
164  void Invert();
165 
166  /// Replaces the current matrix with its square root inverse
167  void SquareRootInverse();
168 
169  /// Calculates the determinant of the matrix
170  /// (optimized for 2x2, 3x3, and 4x4 matrices)
171  double Det() const;
172 
173  double Weight() const;
174 
175  /** @brief Set the matrix to alpha * A, assuming that A has the same
176  dimensions as the matrix and uses column-major layout. */
177  void Set(double alpha, const double *A);
178  /// Set the matrix to alpha * A.
179  void Set(double alpha, const DenseMatrix &A)
180  {
181  SetSize(A.Height(), A.Width());
182  Set(alpha, A.GetData());
183  }
184 
185  /// Adds the matrix A multiplied by the number c to the matrix
186  void Add(const double c, const DenseMatrix &A);
187 
188  /// Sets the matrix elements equal to constant c
189  DenseMatrix &operator=(double c);
190 
191  /// Copy the matrix entries from the given array
192  DenseMatrix &operator=(const double *d);
193 
194  /// Sets the matrix size and elements equal to those of m
195  DenseMatrix &operator=(const DenseMatrix &m);
196 
197  DenseMatrix &operator+=(const double *m);
199 
201 
202  DenseMatrix &operator*=(double c);
203 
204  /// (*this) = -(*this)
205  void Neg();
206 
207  /// Take the 2-norm of the columns of A and store in v
208  void Norm2(double *v) const;
209 
210  /// Compute the norm ||A|| = max_{ij} |A_{ij}|
211  double MaxMaxNorm() const;
212 
213  /// Compute the Frobenius norm of the matrix
214  double FNorm() const { double s, n2; FNorm(s, n2); return s*sqrt(n2); }
215 
216  /// Compute the square of the Frobenius norm of the matrix
217  double FNorm2() const { double s, n2; FNorm(s, n2); return s*s*n2; }
218 
219  void Eigenvalues(Vector &ev)
220  { Eigensystem(ev); }
221 
222  void Eigenvalues(Vector &ev, DenseMatrix &evect)
223  { Eigensystem(ev, &evect); }
224 
225  void Eigensystem(Vector &ev, DenseMatrix &evect)
226  { Eigensystem(ev, &evect); }
227 
228  void SingularValues(Vector &sv) const;
229  int Rank(double tol) const;
230 
231  /// Return the i-th singular value (decreasing order) of NxN matrix, N=1,2,3.
232  double CalcSingularvalue(const int i) const;
233 
234  /** Return the eigenvalues (in increasing order) and eigenvectors of a
235  2x2 or 3x3 symmetric matrix. */
236  void CalcEigenvalues(double *lambda, double *vec) const;
237 
238  void GetRow(int r, Vector &row);
239  void GetColumn(int c, Vector &col) const;
240  double *GetColumn(int col) { return data + col*height; }
241  const double *GetColumn(int col) const { return data + col*height; }
242 
243  void GetColumnReference(int c, Vector &col)
244  { col.SetDataAndSize(data + c * height, height); }
245 
246  void SetRow(int r, const Vector &row);
247  void SetCol(int c, const Vector &col);
248 
249  /// Set all entries of a row to the specified value.
250  void SetRow(int row, double value);
251  /// Set all entries of a column to the specified value.
252  void SetCol(int col, double value);
253 
254  /// Returns the diagonal of the matrix
255  void GetDiag(Vector &d) const;
256  /// Returns the l1 norm of the rows of the matrix v_i = sum_j |a_ij|
257  void Getl1Diag(Vector &l) const;
258  /// Compute the row sums of the DenseMatrix
259  void GetRowSums(Vector &l) const;
260 
261  /// Creates n x n diagonal matrix with diagonal elements c
262  void Diag(double c, int n);
263  /// Creates n x n diagonal matrix with diagonal given by diag
264  void Diag(double *diag, int n);
265 
266  /// (*this) = (*this)^t
267  void Transpose();
268  /// (*this) = A^t
269  void Transpose(const DenseMatrix &A);
270  /// (*this) = 1/2 ((*this) + (*this)^t)
271  void Symmetrize();
272 
273  void Lump();
274 
275  /** Given a DShape matrix (from a scalar FE), stored in *this, returns the
276  CurlShape matrix. If *this is a N by D matrix, then curl is a D*N by
277  D*(D-1)/2 matrix. The size of curl must be set outside. The dimension D
278  can be either 2 or 3. */
279  void GradToCurl(DenseMatrix &curl);
280  /** Given a DShape matrix (from a scalar FE), stored in *this,
281  returns the DivShape vector. If *this is a N by dim matrix,
282  then div is a dim*N vector. The size of div must be set
283  outside. */
284  void GradToDiv(Vector &div);
285 
286  /// Copy rows row1 through row2 from A to *this
287  void CopyRows(const DenseMatrix &A, int row1, int row2);
288  /// Copy columns col1 through col2 from A to *this
289  void CopyCols(const DenseMatrix &A, int col1, int col2);
290  /// Copy the m x n submatrix of A at row/col offsets Aro/Aco to *this
291  void CopyMN(const DenseMatrix &A, int m, int n, int Aro, int Aco);
292  /// Copy matrix A to the location in *this at row_offset, col_offset
293  void CopyMN(const DenseMatrix &A, int row_offset, int col_offset);
294  /// Copy matrix A^t to the location in *this at row_offset, col_offset
295  void CopyMNt(const DenseMatrix &A, int row_offset, int col_offset);
296  /** Copy the m x n submatrix of A at row/col offsets Aro/Aco to *this at
297  row_offset, col_offset */
298  void CopyMN(const DenseMatrix &A, int m, int n, int Aro, int Aco,
299  int row_offset, int col_offset);
300  /// Copy c on the diagonal of size n to *this at row_offset, col_offset
301  void CopyMNDiag(double c, int n, int row_offset, int col_offset);
302  /// Copy diag on the diagonal of size n to *this at row_offset, col_offset
303  void CopyMNDiag(double *diag, int n, int row_offset, int col_offset);
304  /// Copy All rows and columns except m and n from A
305  void CopyExceptMN(const DenseMatrix &A, int m, int n);
306 
307  /// Perform (ro+i,co+j)+=A(i,j) for 0<=i<A.Height, 0<=j<A.Width
308  void AddMatrix(DenseMatrix &A, int ro, int co);
309  /// Perform (ro+i,co+j)+=a*A(i,j) for 0<=i<A.Height, 0<=j<A.Width
310  void AddMatrix(double a, DenseMatrix &A, int ro, int co);
311 
312  /// Add the matrix 'data' to the Vector 'v' at the given 'offset'
313  void AddToVector(int offset, Vector &v) const;
314  /// Get the matrix 'data' from the Vector 'v' at the given 'offset'
315  void GetFromVector(int offset, const Vector &v);
316  /** If (dofs[i] < 0 and dofs[j] >= 0) or (dofs[i] >= 0 and dofs[j] < 0)
317  then (*this)(i,j) = -(*this)(i,j). */
318  void AdjustDofDirection(Array<int> &dofs);
319 
320  /// Replace small entries, abs(a_ij) <= eps, with zero.
321  void Threshold(double eps);
322 
323  /** Count the number of entries in the matrix for which isfinite
324  is false, i.e. the entry is a NaN or +/-Inf. */
325  int CheckFinite() const { return mfem::CheckFinite(data, height*width); }
326 
327  /// Prints matrix to stream out.
328  virtual void Print(std::ostream &out = mfem::out, int width_ = 4) const;
329  virtual void PrintMatlab(std::ostream &out = mfem::out) const;
330  /// Prints the transpose matrix to stream out.
331  virtual void PrintT(std::ostream &out = mfem::out, int width_ = 4) const;
332 
333  /// Invert and print the numerical conditioning of the inversion.
334  void TestInversion();
335 
336  long MemoryUsage() const { return std::abs(capacity) * sizeof(double); }
337 
338  /// Destroys dense matrix.
339  virtual ~DenseMatrix();
340 };
341 
342 /// C = A + alpha*B
343 void Add(const DenseMatrix &A, const DenseMatrix &B,
344  double alpha, DenseMatrix &C);
345 
346 /// C = alpha*A + beta*B
347 void Add(double alpha, const double *A,
348  double beta, const double *B, DenseMatrix &C);
349 
350 /// C = alpha*A + beta*B
351 void Add(double alpha, const DenseMatrix &A,
352  double beta, const DenseMatrix &B, DenseMatrix &C);
353 
354 /// Matrix matrix multiplication. A = B * C.
355 void Mult(const DenseMatrix &b, const DenseMatrix &c, DenseMatrix &a);
356 
357 /// Matrix matrix multiplication. A += B * C.
358 void AddMult(const DenseMatrix &b, const DenseMatrix &c, DenseMatrix &a);
359 
360 /** Calculate the adjugate of a matrix (for NxN matrices, N=1,2,3) or the matrix
361  adj(A^t.A).A^t for rectangular matrices (2x1, 3x1, or 3x2). This operation
362  is well defined even when the matrix is not full rank. */
363 void CalcAdjugate(const DenseMatrix &a, DenseMatrix &adja);
364 
365 /// Calculate the transposed adjugate of a matrix (for NxN matrices, N=1,2,3)
366 void CalcAdjugateTranspose(const DenseMatrix &a, DenseMatrix &adjat);
367 
368 /** Calculate the inverse of a matrix (for NxN matrices, N=1,2,3) or the
369  left inverse (A^t.A)^{-1}.A^t (for 2x1, 3x1, or 3x2 matrices) */
370 void CalcInverse(const DenseMatrix &a, DenseMatrix &inva);
371 
372 /// Calculate the inverse transpose of a matrix (for NxN matrices, N=1,2,3)
373 void CalcInverseTranspose(const DenseMatrix &a, DenseMatrix &inva);
374 
375 /** For a given Nx(N-1) (N=2,3) matrix J, compute a vector n such that
376  n_k = (-1)^{k+1} det(J_k), k=1,..,N, where J_k is the matrix J with the
377  k-th row removed. Note: J^t.n = 0, det([n|J])=|n|^2=det(J^t.J). */
378 void CalcOrtho(const DenseMatrix &J, Vector &n);
379 
380 /// Calculate the matrix A.At
381 void MultAAt(const DenseMatrix &a, DenseMatrix &aat);
382 
383 /// ADAt = A D A^t, where D is diagonal
384 void MultADAt(const DenseMatrix &A, const Vector &D, DenseMatrix &ADAt);
385 
386 /// ADAt += A D A^t, where D is diagonal
387 void AddMultADAt(const DenseMatrix &A, const Vector &D, DenseMatrix &ADAt);
388 
389 /// Multiply a matrix A with the transpose of a matrix B: A*Bt
390 void MultABt(const DenseMatrix &A, const DenseMatrix &B, DenseMatrix &ABt);
391 
392 /// ADBt = A D B^t, where D is diagonal
393 void MultADBt(const DenseMatrix &A, const Vector &D,
394  const DenseMatrix &B, DenseMatrix &ADBt);
395 
396 /// ABt += A * B^t
397 void AddMultABt(const DenseMatrix &A, const DenseMatrix &B, DenseMatrix &ABt);
398 
399 /// ADBt = A D B^t, where D is diagonal
400 void AddMultADBt(const DenseMatrix &A, const Vector &D,
401  const DenseMatrix &B, DenseMatrix &ADBt);
402 
403 /// ABt += a * A * B^t
404 void AddMult_a_ABt(double a, const DenseMatrix &A, const DenseMatrix &B,
405  DenseMatrix &ABt);
406 
407 /// Multiply the transpose of a matrix A with a matrix B: At*B
408 void MultAtB(const DenseMatrix &A, const DenseMatrix &B, DenseMatrix &AtB);
409 
410 /// AAt += a * A * A^t
411 void AddMult_a_AAt(double a, const DenseMatrix &A, DenseMatrix &AAt);
412 
413 /// AAt = a * A * A^t
414 void Mult_a_AAt(double a, const DenseMatrix &A, DenseMatrix &AAt);
415 
416 /// Make a matrix from a vector V.Vt
417 void MultVVt(const Vector &v, DenseMatrix &vvt);
418 
419 void MultVWt(const Vector &v, const Vector &w, DenseMatrix &VWt);
420 
421 /// VWt += v w^t
422 void AddMultVWt(const Vector &v, const Vector &w, DenseMatrix &VWt);
423 
424 /// VWt += a * v w^t
425 void AddMult_a_VWt(const double a, const Vector &v, const Vector &w,
426  DenseMatrix &VWt);
427 
428 /// VVt += a * v v^t
429 void AddMult_a_VVt(const double a, const Vector &v, DenseMatrix &VVt);
430 
431 
432 /** Class that can compute LU factorization of external data and perform various
433  operations with the factored data. */
435 {
436 public:
437  double *data;
438  int *ipiv;
439 #ifdef MFEM_USE_LAPACK
440  static const int ipiv_base = 1;
441 #else
442  static const int ipiv_base = 0;
443 #endif
444 
445  /** With this constructor, the (public) data and ipiv members should be set
446  explicitly before calling class methods. */
447  LUFactors() { }
448 
449  LUFactors(double *data_, int *ipiv_) : data(data_), ipiv(ipiv_) { }
450 
451  /** Factorize the current data of size (m x m) overwriting it with the LU
452  factors. The factorization is such that L.U = P.A, where A is the
453  original matrix and P is a permutation matrix represented by ipiv. */
454  void Factor(int m);
455 
456  /** Assuming L.U = P.A factored data of size (m x m), compute |A|
457  from the diagonal values of U and the permutation information. */
458  double Det(int m) const;
459 
460  /** Assuming L.U = P.A factored data of size (m x m), compute X <- A X,
461  for a matrix X of size (m x n). */
462  void Mult(int m, int n, double *X) const;
463 
464  /** Assuming L.U = P.A factored data of size (m x m), compute
465  X <- L^{-1} P X, for a matrix X of size (m x n). */
466  void LSolve(int m, int n, double *X) const;
467 
468  /** Assuming L.U = P.A factored data of size (m x m), compute
469  X <- U^{-1} X, for a matrix X of size (m x n). */
470  void USolve(int m, int n, double *X) const;
471 
472  /** Assuming L.U = P.A factored data of size (m x m), compute X <- A^{-1} X,
473  for a matrix X of size (m x n). */
474  void Solve(int m, int n, double *X) const;
475 
476  /// Assuming L.U = P.A factored data of size (m x m), compute X <- A^{-1}.
477  void GetInverseMatrix(int m, double *X) const;
478 
479  /** Given an (n x m) matrix A21, compute X2 <- X2 - A21 X1, for matrices X1,
480  and X2 of size (m x r) and (n x r), respectively. */
481  static void SubMult(int m, int n, int r, const double *A21,
482  const double *X1, double *X2);
483 
484  /** Assuming P.A = L.U factored data of size (m x m), compute the 2x2 block
485  decomposition:
486  | P 0 | | A A12 | = | L 0 | | U U12 |
487  | 0 I | | A21 A22 | | L21 I | | 0 S22 |
488  where A12, A21, and A22 are matrices of size (m x n), (n x m), and
489  (n x n), respectively. The blocks are overwritten as follows:
490  A12 <- U12 = L^{-1} P A12
491  A21 <- L21 = A21 U^{-1}
492  A22 <- S22 = A22 - L21 U12.
493  The block S22 is the Schur complement. */
494  void BlockFactor(int m, int n, double *A12, double *A21, double *A22) const;
495 
496  /** Given BlockFactor()'d data, perform the forward block solve for the
497  linear system:
498  | A A12 | | X1 | = | B1 |
499  | A21 A22 | | X2 | | B2 |
500  written in the factored form:
501  | L 0 | | U U12 | | X1 | = | P 0 | | B1 |
502  | L21 I | | 0 S22 | | X2 | | 0 I | | B2 |.
503  The resulting blocks Y1, Y2 solve the system:
504  | L 0 | | Y1 | = | P 0 | | B1 |
505  | L21 I | | Y2 | | 0 I | | B2 |
506  The blocks are overwritten as follows:
507  B1 <- Y1 = L^{-1} P B1
508  B2 <- Y2 = B2 - L21 Y1 = B2 - A21 A^{-1} B1
509  The blocks B1/Y1 and B2/Y2 are of size (m x r) and (n x r), respectively.
510  The Schur complement system is given by: S22 X2 = Y2. */
511  void BlockForwSolve(int m, int n, int r, const double *L21,
512  double *B1, double *B2) const;
513 
514  /** Given BlockFactor()'d data, perform the backward block solve in
515  | U U12 | | X1 | = | Y1 |
516  | 0 S22 | | X2 | | Y2 |.
517  The input is the solution block X2 and the block Y1 resulting from
518  BlockForwSolve(). The result block X1 overwrites input block Y1:
519  Y1 <- X1 = U^{-1} (Y1 - U12 X2). */
520  void BlockBackSolve(int m, int n, int r, const double *U12,
521  const double *X2, double *Y1) const;
522 };
523 
524 
525 /** Data type for inverse of square dense matrix.
526  Stores LU factors */
528 {
529 private:
530  const DenseMatrix *a;
531  LUFactors lu;
532 
533 public:
534  /// Default constructor.
535  DenseMatrixInverse() : a(NULL), lu(NULL, NULL) { }
536 
537  /** Creates square dense matrix. Computes factorization of mat
538  and stores LU factors. */
539  DenseMatrixInverse(const DenseMatrix &mat);
540 
541  /// Same as above but does not factorize the matrix.
542  DenseMatrixInverse(const DenseMatrix *mat);
543 
544  /// Get the size of the inverse matrix
545  int Size() const { return Width(); }
546 
547  /// Factor the current DenseMatrix, *a
548  void Factor();
549 
550  /// Factor a new DenseMatrix of the same size
551  void Factor(const DenseMatrix &mat);
552 
553  virtual void SetOperator(const Operator &op);
554 
555  /// Matrix vector multiplication with the inverse of dense matrix.
556  virtual void Mult(const Vector &x, Vector &y) const;
557 
558  /// Multiply the inverse matrix by another matrix: X = A^{-1} B.
559  void Mult(const DenseMatrix &B, DenseMatrix &X) const;
560 
561  /// Compute and return the inverse matrix in Ainv.
562  void GetInverseMatrix(DenseMatrix &Ainv) const
563  {
564  Ainv.SetSize(width);
565  lu.GetInverseMatrix(width, Ainv.Data());
566  }
567 
568  /// Compute the determinant of the original DenseMatrix using the LU factors.
569  double Det() const { return lu.Det(width); }
570 
571  /// Print the numerical conditioning of the inversion: ||A^{-1} A - I||.
572  void TestInversion();
573 
574  /// Destroys dense inverse matrix.
575  virtual ~DenseMatrixInverse();
576 };
577 
578 
580 {
581  DenseMatrix &mat;
582  Vector EVal;
583  DenseMatrix EVect;
584  Vector ev;
585  int n;
586 
587 #ifdef MFEM_USE_LAPACK
588  double *work;
589  char jobz, uplo;
590  int lwork, info;
591 #endif
592 
593 public:
594 
596  void Eval();
597  Vector &Eigenvalues() { return EVal; }
598  DenseMatrix &Eigenvectors() { return EVect; }
599  double Eigenvalue(int i) { return EVal(i); }
600  const Vector &Eigenvector(int i)
601  {
602  ev.SetData(EVect.Data() + i * EVect.Height());
603  return ev;
604  }
606 };
607 
608 
610 {
611  Vector sv;
612  int m, n;
613 
614 #ifdef MFEM_USE_LAPACK
615  double *work;
616  char jobu, jobvt;
617  int lwork, info;
618 #endif
619 
620  void Init();
621 public:
622 
624  DenseMatrixSVD(int h, int w);
625  void Eval(DenseMatrix &M);
626  Vector &Singularvalues() { return sv; }
627  double Singularvalue(int i) { return sv(i); }
628  ~DenseMatrixSVD();
629 };
630 
631 class Table;
632 
633 /// Rank 3 tensor (array of matrices)
635 {
636 private:
637  DenseMatrix Mk;
638  double *tdata;
639  int nk;
640  bool own_data;
641 
642 public:
644  {
645  nk = 0;
646  tdata = NULL;
647  own_data = true;
648  }
649 
650  DenseTensor(int i, int j, int k)
651  : Mk(NULL, i, j)
652  {
653  nk = k;
654  tdata = new double[i*j*k];
655  own_data = true;
656  }
657 
658  int SizeI() const { return Mk.Height(); }
659  int SizeJ() const { return Mk.Width(); }
660  int SizeK() const { return nk; }
661 
662  void SetSize(int i, int j, int k)
663  {
664  if (own_data) { delete [] tdata; }
665  Mk.UseExternalData(NULL, i, j);
666  nk = k;
667  tdata = new double[i*j*k];
668  own_data = true;
669  }
670 
671  void UseExternalData(double *ext_data, int i, int j, int k)
672  {
673  if (own_data) { delete [] tdata; }
674  Mk.UseExternalData(NULL, i, j);
675  nk = k;
676  tdata = ext_data;
677  own_data = false;
678  }
679 
680  /// Sets the tensor elements equal to constant c
681  DenseTensor &operator=(double c);
682 
683  DenseMatrix &operator()(int k) { Mk.data = GetData(k); return Mk; }
684  const DenseMatrix &operator()(int k) const
685  { return const_cast<DenseTensor&>(*this)(k); }
686 
687  double &operator()(int i, int j, int k)
688  { return tdata[i+SizeI()*(j+SizeJ()*k)]; }
689  const double &operator()(int i, int j, int k) const
690  { return tdata[i+SizeI()*(j+SizeJ()*k)]; }
691 
692  double *GetData(int k) { return tdata+k*Mk.Height()*Mk.Width(); }
693 
694  double *Data() { return tdata; }
695 
696  /** Matrix-vector product from unassembled element matrices, assuming both
697  'x' and 'y' use the same elem_dof table. */
698  void AddMult(const Table &elem_dof, const Vector &x, Vector &y) const;
699 
700  void Clear()
701  { UseExternalData(NULL, 0, 0, 0); }
702 
703  long MemoryUsage() const { return nk*Mk.MemoryUsage(); }
704 
706  {
707  if (own_data) { delete [] tdata; }
708  }
709 };
710 
711 
712 // Inline methods
713 
714 inline double &DenseMatrix::operator()(int i, int j)
715 {
716  MFEM_ASSERT(data && i >= 0 && i < height && j >= 0 && j < width, "");
717  return data[i+j*height];
718 }
719 
720 inline const double &DenseMatrix::operator()(int i, int j) const
721 {
722  MFEM_ASSERT(data && i >= 0 && i < height && j >= 0 && j < width, "");
723  return data[i+j*height];
724 }
725 
726 } // namespace mfem
727 
728 #endif
int Size() const
For backward compatibility define Size to be synonym of Width()
Definition: densemat.hpp:83
void Symmetrize()
(*this) = 1/2 ((*this) + (*this)^t)
Definition: densemat.cpp:2434
void MultABt(const DenseMatrix &A, const DenseMatrix &B, DenseMatrix &ABt)
Multiply a matrix A with the transpose of a matrix B: A*Bt.
Definition: densemat.cpp:3362
DenseMatrix & operator-=(const DenseMatrix &m)
Definition: densemat.cpp:587
void SymmetricScaling(const Vector &s)
SymmetricScaling this = diag(sqrt(s)) * this * diag(sqrt(s))
Definition: densemat.cpp:360
void SquareRootInverse()
Replaces the current matrix with its square root inverse.
Definition: densemat.cpp:737
int CheckFinite(const double *v, const int n)
Definition: vector.hpp:296
void AddMultVWt(const Vector &v, const Vector &w, DenseMatrix &VWt)
VWt += v w^t.
Definition: densemat.cpp:3764
DenseMatrix & operator*=(double c)
Definition: densemat.cpp:598
void GetDiag(Vector &d) const
Returns the diagonal of the matrix.
Definition: densemat.cpp:2326
void UseExternalData(double *ext_data, int i, int j, int k)
Definition: densemat.hpp:671
DenseTensor & operator=(double c)
Sets the tensor elements equal to constant c.
Definition: densemat.cpp:4377
void MultVWt(const Vector &v, const Vector &w, DenseMatrix &VWt)
Definition: densemat.cpp:3742
void InvRightScaling(const Vector &s)
InvRightScaling: this = this * diag(1./s);.
Definition: densemat.cpp:345
const DenseMatrix & operator()(int k) const
Definition: densemat.hpp:684
void Eigenvalues(Vector &ev)
Definition: densemat.hpp:219
void SingularValues(Vector &sv) const
Definition: densemat.cpp:1092
DenseMatrix & operator()(int k)
Definition: densemat.hpp:683
double Det() const
Definition: densemat.cpp:435
void Mult(const Table &A, const Table &B, Table &C)
C = A * B (as boolean matrices)
Definition: table.cpp:468
int Width() const
Get the width (size of input) of the Operator. Synonym with NumCols().
Definition: operator.hpp:42
int SizeK() const
Definition: densemat.hpp:660
long MemoryUsage() const
Definition: densemat.hpp:703
void BlockFactor(int m, int n, double *A12, double *A21, double *A22) const
Definition: densemat.cpp:4077
void BlockBackSolve(int m, int n, int r, const double *U12, const double *X2, double *Y1) const
Definition: densemat.cpp:4113
double InnerProduct(const double *x, const double *y) const
Compute y^t A x.
Definition: densemat.cpp:290
void CalcAdjugate(const DenseMatrix &a, DenseMatrix &adja)
Definition: densemat.cpp:3048
void AddMult(const Table &elem_dof, const Vector &x, Vector &y) const
Definition: densemat.cpp:4324
void TestInversion()
Invert and print the numerical conditioning of the inversion.
Definition: densemat.cpp:2918
Data type dense matrix using column-major storage.
Definition: densemat.hpp:23
void CopyRows(const DenseMatrix &A, int row1, int row2)
Copy rows row1 through row2 from A to *this.
Definition: densemat.cpp:2545
double Det(int m) const
Definition: densemat.cpp:3884
void SetSize(int i, int j, int k)
Definition: densemat.hpp:662
void Eval(DenseMatrix &M)
Definition: densemat.cpp:4293
Abstract data type for matrix inverse.
Definition: matrix.hpp:58
void AddMult_a_ABt(double a, const DenseMatrix &A, const DenseMatrix &B, DenseMatrix &ABt)
ABt += a * A * B^t.
Definition: densemat.cpp:3578
void GetInverseMatrix(DenseMatrix &Ainv) const
Compute and return the inverse matrix in Ainv.
Definition: densemat.hpp:562
void Factor(int m)
Definition: densemat.cpp:3832
void Factor()
Factor the current DenseMatrix, *a.
Definition: densemat.cpp:4142
void GetInverseMatrix(int m, double *X) const
Assuming L.U = P.A factored data of size (m x m), compute X &lt;- A^{-1}.
Definition: densemat.cpp:3995
double * GetData() const
Returns the matrix data array.
Definition: densemat.hpp:94
void CalcOrtho(const DenseMatrix &J, Vector &n)
Definition: densemat.cpp:3278
DenseMatrix & operator=(double c)
Sets the matrix elements equal to constant c.
Definition: densemat.cpp:537
void Set(double alpha, const double *A)
Set the matrix to alpha * A, assuming that A has the same dimensions as the matrix and uses column-ma...
Definition: densemat.cpp:519
void Mult_a_AAt(double a, const DenseMatrix &A, DenseMatrix &AAt)
AAt = a * A * A^t.
Definition: densemat.cpp:3719
static void SubMult(int m, int n, int r, const double *A21, const double *X1, double *X2)
Definition: densemat.cpp:4060
const Vector & Eigenvector(int i)
Definition: densemat.hpp:600
virtual void Print(std::ostream &out=mfem::out, int width_=4) const
Prints matrix to stream out.
Definition: densemat.cpp:2847
virtual void Mult(const Vector &x, Vector &y) const
Matrix vector multiplication with the inverse of dense matrix.
Definition: densemat.cpp:4175
const double & operator()(int i, int j, int k) const
Definition: densemat.hpp:689
void Set(double alpha, const DenseMatrix &A)
Set the matrix to alpha * A.
Definition: densemat.hpp:179
void Add(const DenseMatrix &A, const DenseMatrix &B, double alpha, DenseMatrix &C)
C = A + alpha*B.
Definition: densemat.cpp:2942
double & operator()(int i, int j)
Returns reference to a_{ij}.
Definition: densemat.hpp:714
double Weight() const
Definition: densemat.cpp:492
void USolve(int m, int n, double *X) const
Definition: densemat.cpp:3962
double FNorm() const
Compute the Frobenius norm of the matrix.
Definition: densemat.hpp:214
void MultTranspose(const double *x, double *y) const
Multiply a vector with the transpose matrix.
Definition: densemat.cpp:194
void CalcAdjugateTranspose(const DenseMatrix &a, DenseMatrix &adjat)
Calculate the transposed adjugate of a matrix (for NxN matrices, N=1,2,3)
Definition: densemat.cpp:3120
double & operator()(int i, int j, int k)
Definition: densemat.hpp:687
void AddMult(const DenseMatrix &b, const DenseMatrix &c, DenseMatrix &a)
Matrix matrix multiplication. A += B * C.
Definition: densemat.cpp:3016
double operator*(const DenseMatrix &m) const
Matrix inner product: tr(A^t B)
Definition: densemat.cpp:179
double Singularvalue(int i)
Definition: densemat.hpp:627
void Reset(double *d, int h, int w)
Change the data array and the size of the DenseMatrix.
Definition: densemat.hpp:71
int Height() const
Get the height (size of output) of the Operator. Synonym with NumRows().
Definition: operator.hpp:36
void Add(const double c, const DenseMatrix &A)
Adds the matrix A multiplied by the number c to the matrix.
Definition: densemat.cpp:528
void AddMult_a_VWt(const double a, const Vector &v, const Vector &w, DenseMatrix &VWt)
VWt += a * v w^t.
Definition: densemat.cpp:3785
void InvSymmetricScaling(const Vector &s)
InvSymmetricScaling this = diag(sqrt(1./s)) * this * diag(sqrt(1./s))
Definition: densemat.cpp:386
void BlockForwSolve(int m, int n, int r, const double *L21, double *B1, double *B2) const
Definition: densemat.cpp:4104
DenseMatrixSVD(DenseMatrix &M)
Definition: densemat.cpp:4259
Abstract data type matrix.
Definition: matrix.hpp:27
const double * GetColumn(int col) const
Definition: densemat.hpp:241
void Norm2(double *v) const
Take the 2-norm of the columns of A and store in v.
Definition: densemat.cpp:782
void MultADBt(const DenseMatrix &A, const Vector &D, const DenseMatrix &B, DenseMatrix &ADBt)
ADBt = A D B^t, where D is diagonal.
Definition: densemat.cpp:3445
void Invert()
Replaces the current matrix with its inverse.
Definition: densemat.cpp:627
virtual ~DenseMatrixInverse()
Destroys dense inverse matrix.
Definition: densemat.cpp:4198
void LSolve(int m, int n, double *X) const
Definition: densemat.cpp:3937
void LeftScaling(const Vector &s)
LeftScaling this = diag(s) * this.
Definition: densemat.cpp:308
double Det() const
Compute the determinant of the original DenseMatrix using the LU factors.
Definition: densemat.hpp:569
void CopyMNDiag(double c, int n, int row_offset, int col_offset)
Copy c on the diagonal of size n to *this at row_offset, col_offset.
Definition: densemat.cpp:2635
double * GetData(int k)
Definition: densemat.hpp:692
void AddMult_a_VVt(const double a, const Vector &v, DenseMatrix &VVt)
VVt += a * v v^t.
Definition: densemat.cpp:3807
void Neg()
(*this) = -(*this)
Definition: densemat.cpp:608
virtual void SetOperator(const Operator &op)
Set/update the solver for the given operator.
Definition: densemat.cpp:4168
void Solve(int m, int n, double *X) const
Definition: densemat.cpp:3981
void SetRow(int r, const Vector &row)
Definition: densemat.cpp:2817
void Getl1Diag(Vector &l) const
Returns the l1 norm of the rows of the matrix v_i = sum_j |a_ij|.
Definition: densemat.cpp:2340
void AddToVector(int offset, Vector &v) const
Add the matrix &#39;data&#39; to the Vector &#39;v&#39; at the given &#39;offset&#39;.
Definition: densemat.cpp:2752
void SetData(double *d)
Definition: vector.hpp:87
long MemoryUsage() const
Definition: densemat.hpp:336
DenseMatrix & Eigenvectors()
Definition: densemat.hpp:598
void GetColumn(int c, Vector &col) const
Definition: densemat.cpp:2312
void AddMult(const Vector &x, Vector &y) const
y += A.x
Definition: densemat.cpp:217
void Threshold(double eps)
Replace small entries, abs(a_ij) &lt;= eps, with zero.
Definition: densemat.cpp:2833
int SizeI() const
Definition: densemat.hpp:658
void CalcInverse(const DenseMatrix &a, DenseMatrix &inva)
Definition: densemat.cpp:3156
void TestInversion()
Print the numerical conditioning of the inversion: ||A^{-1} A - I||.
Definition: densemat.cpp:4187
double MaxMaxNorm() const
Compute the norm ||A|| = max_{ij} |A_{ij}|.
Definition: densemat.cpp:795
double * Data() const
Returns the matrix data array.
Definition: densemat.hpp:92
void Transpose()
(*this) = (*this)^t
Definition: densemat.cpp:2401
void MultVVt(const Vector &v, DenseMatrix &vvt)
Make a matrix from a vector V.Vt.
Definition: densemat.cpp:3733
double Trace() const
Trace of a square matrix.
Definition: densemat.cpp:411
void AddMultABt(const DenseMatrix &A, const DenseMatrix &B, DenseMatrix &ABt)
ABt += A * B^t.
Definition: densemat.cpp:3485
DenseTensor(int i, int j, int k)
Definition: densemat.hpp:650
void ClearExternalData()
Definition: densemat.hpp:76
int CheckFinite() const
Definition: densemat.hpp:325
void MultAAt(const DenseMatrix &a, DenseMatrix &aat)
Calculate the matrix A.At.
Definition: densemat.cpp:3304
void GetColumnReference(int c, Vector &col)
Definition: densemat.hpp:243
void AddMatrix(DenseMatrix &A, int ro, int co)
Perform (ro+i,co+j)+=A(i,j) for 0&lt;=i&lt;A.Height, 0&lt;=j&lt;A.Width.
Definition: densemat.cpp:2692
DenseMatrix(double *d, int h, int w)
Definition: densemat.hpp:57
void Clear()
Delete the matrix data array (if owned) and reset the matrix state.
Definition: densemat.hpp:79
void CalcInverseTranspose(const DenseMatrix &a, DenseMatrix &inva)
Calculate the inverse transpose of a matrix (for NxN matrices, N=1,2,3)
Definition: densemat.cpp:3239
void SetDataAndSize(double *d, int s)
Set the Vector data and size.
Definition: vector.hpp:94
void MultADAt(const DenseMatrix &A, const Vector &D, DenseMatrix &ADAt)
ADAt = A D A^t, where D is diagonal.
Definition: densemat.cpp:3346
int height
Dimension of the output / number of rows in the matrix.
Definition: operator.hpp:24
virtual void PrintT(std::ostream &out=mfem::out, int width_=4) const
Prints the transpose matrix to stream out.
Definition: densemat.cpp:2892
void CopyCols(const DenseMatrix &A, int col1, int col2)
Copy columns col1 through col2 from A to *this.
Definition: densemat.cpp:2556
int SizeJ() const
Definition: densemat.hpp:659
virtual MatrixInverse * Inverse() const
Returns a pointer to the inverse matrix.
Definition: densemat.cpp:430
double * GetColumn(int col)
Definition: densemat.hpp:240
bool OwnsData() const
Definition: densemat.hpp:96
void AddMultADBt(const DenseMatrix &A, const Vector &D, const DenseMatrix &B, DenseMatrix &ADBt)
ADBt = A D B^t, where D is diagonal.
Definition: densemat.cpp:3542
virtual ~DenseMatrix()
Destroys dense matrix.
Definition: densemat.cpp:2932
void CopyMNt(const DenseMatrix &A, int row_offset, int col_offset)
Copy matrix A^t to the location in *this at row_offset, col_offset.
Definition: densemat.cpp:2592
void AddMultTranspose(const Vector &x, Vector &y) const
y += A^t x
Definition: densemat.cpp:235
void CopyExceptMN(const DenseMatrix &A, int m, int n)
Copy All rows and columns except m and n from A.
Definition: densemat.cpp:2666
void Diag(double c, int n)
Creates n x n diagonal matrix with diagonal elements c.
Definition: densemat.cpp:2371
void Mult(int m, int n, double *X) const
Definition: densemat.cpp:3901
static const int ipiv_base
Definition: densemat.hpp:440
void GradToCurl(DenseMatrix &curl)
Definition: densemat.cpp:2465
DenseMatrixInverse()
Default constructor.
Definition: densemat.hpp:535
double CalcSingularvalue(const int i) const
Return the i-th singular value (decreasing order) of NxN matrix, N=1,2,3.
Definition: densemat.cpp:1758
void GetRowSums(Vector &l) const
Compute the row sums of the DenseMatrix.
Definition: densemat.cpp:2357
void GetRow(int r, Vector &row)
Definition: densemat.cpp:2296
void CalcEigenvalues(double *lambda, double *vec) const
Definition: densemat.cpp:2056
void Eigenvalues(Vector &ev, DenseMatrix &evect)
Definition: densemat.hpp:222
DenseMatrixEigensystem(DenseMatrix &m)
Definition: densemat.cpp:4205
int Rank(double tol) const
Definition: densemat.cpp:1131
const double alpha
Definition: ex15.cpp:337
LUFactors(double *data_, int *ipiv_)
Definition: densemat.hpp:449
void AddMult_a(double a, const Vector &x, Vector &y) const
y += a * A.x
Definition: densemat.cpp:253
void RightScaling(const Vector &s)
RightScaling: this = this * diag(s);.
Definition: densemat.cpp:330
void MultAtB(const DenseMatrix &A, const DenseMatrix &B, DenseMatrix &AtB)
Multiply the transpose of a matrix A with a matrix B: At*B.
Definition: densemat.cpp:3637
Vector data type.
Definition: vector.hpp:41
void Mult(const double *x, double *y) const
Matrix vector multiplication.
Definition: densemat.cpp:143
void AddMultTranspose_a(double a, const Vector &x, Vector &y) const
y += a * A^t x
Definition: densemat.cpp:271
void AddMultADAt(const DenseMatrix &A, const Vector &D, DenseMatrix &ADAt)
ADAt += A D A^t, where D is diagonal.
Definition: densemat.cpp:3318
void GetFromVector(int offset, const Vector &v)
Get the matrix &#39;data&#39; from the Vector &#39;v&#39; at the given &#39;offset&#39;.
Definition: densemat.cpp:2763
void CopyMN(const DenseMatrix &A, int m, int n, int Aro, int Aco)
Copy the m x n submatrix of A at row/col offsets Aro/Aco to *this.
Definition: densemat.cpp:2567
double * Data()
Definition: densemat.hpp:694
void InvLeftScaling(const Vector &s)
InvLeftScaling this = diag(1./s) * this.
Definition: densemat.cpp:319
void UseExternalData(double *d, int h, int w)
Change the data array and the size of the DenseMatrix.
Definition: densemat.hpp:64
void SetCol(int c, const Vector &col)
Definition: densemat.cpp:2825
void Eigensystem(Vector &ev, DenseMatrix &evect)
Definition: densemat.hpp:225
void SetSize(int s)
Change the size of the DenseMatrix to s x s.
Definition: densemat.hpp:86
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
Definition: globals.hpp:64
Abstract operator.
Definition: operator.hpp:21
int Size() const
Get the size of the inverse matrix.
Definition: densemat.hpp:545
Vector & Singularvalues()
Definition: densemat.hpp:626
Rank 3 tensor (array of matrices)
Definition: densemat.hpp:634
double InnerProduct(const Vector &x, const Vector &y) const
Compute y^t A x.
Definition: densemat.hpp:157
double FNorm2() const
Compute the square of the Frobenius norm of the matrix.
Definition: densemat.hpp:217
virtual double & Elem(int i, int j)
Returns reference to a_{ij}.
Definition: densemat.cpp:133
void AdjustDofDirection(Array< int > &dofs)
Definition: densemat.cpp:2774
void GradToDiv(Vector &div)
Definition: densemat.cpp:2524
void AddMult_a_AAt(double a, const DenseMatrix &A, DenseMatrix &AAt)
AAt += a * A * A^t.
Definition: densemat.cpp:3694
int width
Dimension of the input / number of columns in the matrix.
Definition: operator.hpp:25
virtual void PrintMatlab(std::ostream &out=mfem::out) const
Definition: densemat.cpp:2873
double * data
Definition: densemat.hpp:437
DenseMatrix & operator+=(const double *m)
Definition: densemat.cpp:570