Itasca C++ Interface
Loading...
Searching...
No Matches
mat.h
1#pragma once
2#include "baseexception.h"
3#include "basestring.h"
4#include "symtensor.h"
5#include "matrix.h"
6#include "basememory.h"
7// mat.h
8
9// Mat.H - Header file for the matrix & vector classes.
10//
11// History: 03-Jul-96 Dave Potyondy
12// last mod. 11-Jul-96 DOP
13// last mod. 22-Jul-96 DOP added scalar multiply
14// last mod. 24-Jul-96 DOP inlined oper. () and arr_idx functions
15// last mod. 09-Aug-96 DOP Added read/write member functions
16// last mod. 03-May-11 JTD Adapted for use as FISH matrix primitive.
17//
18// =======================================================================
19
20namespace itasca {
21 /* Exception handler for Matrix Class
22 */
23 struct NegMatExcept : public Exception {
25 };
26
27 /* General matrix class for (m x n) matrices */
29 public:
30 using size_type = uint32;
31
32 Mat(); //default constructor
33 Mat(size_type m, size_type n); //constructor of (m x n) matrix
34
35 Mat(const Mat &mtx); //copy constructor
36 Mat(Mat &&mtx); // rvalue-reference copy constructor
37
38 Mat(const SymTensor &t);
39 Mat(const DVect2 &v);
40 Mat(const DVect3 &v);
41 template <unsigned SX, unsigned SY>
43 Mat(const DMatrix<2, 2> &v);
44 Mat(const DMatrix<3, 3> &v);
45
46 virtual ~Mat(); //destructor
47
48 // full access arr(i,j)
49 double &operator()(size_type i, size_type j) { return arr[arr_idx(i, j)]; }
50 // read access arr(i.j)
51 const double &operator()(size_type i, size_type j) const { return arr[arr_idx(i, j)]; }
52 inline constexpr std::partial_ordering operator<=>(const Mat &m) const;
53
54 Mat &operator =(const Mat &mtx); //assignment
55 Mat &operator= (Mat &&mtx);
56
57 Mat operator +(const Mat &mtx) const; //matrix addition
58 Mat operator -(const Mat &mtx) const; //matrix subtraction
59 Mat operator *(const Mat &mtx) const; //matrix multiply
60 Mat operator *(double scal) const; //scalar multiply
61 Mat operator* (const DVect2 &v) const; // 2d vector multiply
62 Mat operator* (const DVect3 &v) const; // 3d vector multiply
63 void operator*=(double s);
64 void operator/=(double s);
65 void operator+=(const Mat &mtx);
66 void operator-=(const Mat &mtx);
67
68 //fill matrix with given value
69 void fill(double val) { std::fill(arr, arr + len, val); }
70 void zero() { fill(0.0); }
71 void identity(); // set matrix to identity matrix
72 Mat transpose() const; // return matrix transpose
73 // multiply by given scalar
74 void scalMult(double scal) { auto *e = arr + len; for (auto *v = arr; v < e; ++v) *v *= scal; }
75
76 bool equals(const Mat &mtx) const; // are the matrices equal?
77 bool exactEquals(const Mat &mtx) const;
78 bool operator== (const Mat &mtx) const { return equals(mtx); }
79 virtual bool symmetric() const; // is the matrix symmetric?
80 virtual double maxNorm() const; // return infinity-norm: max abs(elem)
81 UVect2 size() const { UVect2 v(msize, nsize); return v; }
82
83 UVect2 blockSize() const { UVect2 v(blk_m, blk_n); return v; }
84 void setBlockSize(size_type blk_msize, size_type blk_nsize) { blk_m = blk_msize; blk_n = blk_nsize; }
85 void addBlock(const Mat &src, //source matrix
86 size_type src_bi, size_type src_bj, //block index of source
87 size_type dst_bi, size_type dst_bj); //block index of dest.
88 virtual void addGenBlock(const Mat &src, //source matrix
89 size_type src_i, size_type src_j, //u.l.-corner of source
90 size_type dst_i, size_type dst_j); //u.l.-corner of dest.
91
92 SymTensor toTensor() const;
93 DVect2 toVect2() const;
94 DVect3 toVect3() const;
95 template <unsigned SX, unsigned SY>
96 Matrix<double, SX, SY> toMatrix() const;
97 double *data() const { return arr; }
98 protected:
99 double *arr = nullptr; //store in row-major order as C-array -- (1,2,3,4) - (1,2)
100 size_type msize, nsize; //matrix is (msize x nsize) (3,4)
101 size_type len; //number of stored entries
102 size_type blk_m = 1, blk_n = 1; //block size (m x n)
103
104 bool same(const double *v1, const double *v2) const;
105 inline size_type arr_idx(size_type i, size_type j) const;
106 };
107
108 template <unsigned SX, unsigned SY>
109 Mat::Mat(const Matrix<double, SX, SY> &m) : arr(nullptr) {
110 msize = SX;
111 nsize = SY;
112 len = SX * SY;
113 arr = NEW double[SX * SY];
114 for (uint32 i = 0; i < SX; ++i)
115 for (uint32 j = 0; j < SY; ++j)
116 operator()(i, j) = m(i, j);
117 }
118
119 template <unsigned SX, unsigned SY>
120 Matrix<double, SX, SY> Mat::toMatrix() const {
122 if (msize != SX || nsize != SY) {
123 if (msize == 1 && SY == 1 && nsize == SX) { // Allow generalized vectors to switch
124 for (uint32 i = 0; i < SX; ++i)
125 ret(i, 0) = operator()(0, i);
126 return ret;
127 }
128 if (nsize == 1 && SX == 1 && msize == SY) { // Allow
129 for (uint32 i = 0; i < SY; ++i)
130 ret(0, i) = operator()(i, 0);
131 return ret;
132 }
133 throw Exception("Matrix size does not match {},{}, is {},{} instead.", SX, SY, msize, nsize);
134 }
135 for (uint32 i = 0; i < SX; ++i)
136 for (uint32 j = 0; j < SY; ++j)
137 ret(i, j) = operator()(i, j);
138 return ret;
139 }
140
141 // =========================================
142 // Returns index into the array.
143 // Assumes indexing starts at (0,0).
144 Mat::size_type Mat::arr_idx(size_type i, size_type j) const {
145#ifdef _DEBUG
146 if ((i >= msize) || (j >= nsize))
147 throw Exception("Matrix ({} x {}) : index ({},{}) is out of bounds.", msize, nsize, i, j);
148#endif
149 return nsize * (i)+j;
150 }
151
152 constexpr std::partial_ordering Mat::operator<=>(const Mat &m) const {
153 auto c = size() <=> m.size();
154 if (c!=std::partial_ordering::equivalent) return c;
155 assert(len==m.len);
156 for (size_type i=0;i<len;++i) {
157 auto j = arr[i] <=> m.arr[i];
158 if (j!=std::partial_ordering::equivalent) return j;
159 }
160 return std::partial_ordering::equivalent;
161 }
162}
163
164namespace base {
165 using itasca::Mat;
166 BASE_EXPORT string ts(const itasca::Mat &m, int width = 0, char notation = '\0', int precision = -1, char fill = ' ');
167}
168// EOF Mat.H
Comment point for memory allocation in all modules.
includes std::string and additional functions not included in the standard.
Base exception class for all Itasca code.
Definition baseexception.h:10
A template-based matrix class, size fixed at compile time. Defaults to symmetric sized matrix.
Definition matrix.h:22
A symmetric 2nd order tensor.
Definition symtensor.h:22
Definition mat.h:28
constexpr Vector3< T > toVect3(const Vector2< T > &v, const T &t=0)
Conversion between vectors of different dimension.
Definition vect.h:312
constexpr const Vector2< T > & toVect2(const Vector2< T > &v)
Conversion between vectors of different dimension.
Definition vect.h:310
#define BASE_EXPORT
Definition basedef.h:25
A template-based matrix class, size fixed at compile time.
namespace Itasca
Definition basememory.cpp:14
Definition mat.h:23
A Symmetric 2nd order tensor.