-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.cpp
164 lines (116 loc) · 5.46 KB
/
matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#ifndef matrix_CPP // © 2020, Alois Pichler
#define matrix_CPP
#include <iomanip> // std::cout, std::endl
#include "matrix.h" // header file
// class MatrixException
// { public:
// std::string msg;
// MatrixException(std::string arg) : msg(arg) {}
// };
Matrix::Matrix(unsigned dim) // Construct a square matrix
{ mRows= dim; mCols= dim; mData= new double [dim* dim];}
Matrix::Matrix(unsigned dimR, unsigned dimC) // construct a rectangular matrix
{ mRows= dimR; mCols= dimC; mData= new double [dimR* dimC];}
Matrix::Matrix(const Matrix& src) // copy constructor
{ mRows= src.mRows; mCols= src.mCols;
mData= new double[mRows* mCols]; // deep copy
std::memcpy(mData, src.mData, mRows* mCols* sizeof(double));}
Matrix::Matrix(const Vector<double>& src) // construct from Vector
{ mRows= src.Length(); mCols= 1;
mData= new double[mRows]; // deep copy from Vector source
std::memcpy(mData, &src[0], mRows* sizeof(double));}
Matrix& Matrix::operator=(const Matrix& src) // assignment operator=
{ if (this != &src) // protect against invalid self-assignment
{ if (mRows* mCols != src.mRows* src.mCols)
{ delete[] mData; // destroy old stack
mData= new double[src.mRows* src.mCols];}
mRows= src.mRows; mCols= src.mCols; // deep copy
std::memcpy(mData, src.mData, mRows* mCols* sizeof(double));}
return *this;}
double* Matrix::operator[](const unsigned _row) const // row access operator
{ assert((_row < mRows) && "Matrix: Row out of range.");
return & mData[mCols* _row];} // row-major
double &Matrix::operator()(const unsigned row, const unsigned col) const // element access operator
{ assert((row > 0 && row <= mRows) && "Matrix: row out of range.");
assert((col > 0 && col <= mCols) && "Matrix: col out of range.");
return mData[(row-1)*mCols+ col-1];} // the indexes are one-based, not zero based.
Matrix Matrix::Fill(const double val= 0) // fill this matrix with entries val
{ for (unsigned i= 0; i< mRows; i++)
for (unsigned j= 0; j< mCols; j++)
(*this)[i][j]= val;
return *this;}
Matrix Eye(const unsigned mRows) // provides the identity matrix
{ Matrix c(mRows, mRows);
for(unsigned i= 0; i< mRows; i++)
{ for(unsigned j= 0; j< mRows; j++) c[i][j]= 0;
c[i][i]= 1;}
return c;}
Matrix Matrix::operator+ (double lambda) // add lambda* identity
{ Matrix c(mRows, mCols);
unsigned k= 0;
for (unsigned i= 0; i< mRows; i++)
for (unsigned j= 0; j< mCols; j++)
{ c.mData[k]= this->mData[k]; if (i==j) c.mData[k]+= lambda;
k++;}
return c;}
Matrix Matrix::operator+ (const Matrix& other) // add matrices
{ assert((other.rows()== mRows && other.cols()== mCols) && "Matrices can't be added.");
Matrix c(mRows, mCols);
for (unsigned i= 0; i< mRows* mCols; i++)
c.mData[i]= (*this).mData[i] + other.mData[i];
return c;}
Matrix Matrix::operator- (const Matrix& other) // subtract matrices
{ assert((other.rows()== mRows && other.cols()== mCols) && "Matrices cannot be substracted.");
Matrix diff(mRows, mCols);
for (unsigned i= 0; i< mRows* mCols; i++)
diff.mData[i]= (*this).mData[i] - other.mData[i];
return diff;}
Matrix Matrix::operator* (double lambda) // multiplication by scalar
{ Matrix c(mRows, mCols);
for (unsigned i= 0; i< mRows; i++)
for (unsigned j= 0; j< mCols; j++)
c.mData[i* mCols+ j]= lambda* this->mData[i* mCols+ j];
return c;}
Matrix Transpose(Matrix A) // transposition
{ Matrix ATranspose(A.cols(), A.rows());
for (unsigned i= 0; i< ATranspose.rows(); i++)
for (unsigned j= 0; j< ATranspose.cols(); j++)
ATranspose[i][j]= A[j][i];
return ATranspose;}
Matrix Matrix::operator* (const Matrix& other) // multiply this matrix and another
{ assert((mCols== other.rows()) && "Matrix multiplication: dimension mismatch.");
Matrix c(mRows, other.cols());
for (unsigned i= 0; i< mRows; i++)
for (unsigned j= 0; j< other.cols(); j++)
{ double *s= &c[i][j]; *s= 0;
for (unsigned k= 0; k< mCols; k++)
*s+= (*this).mData[i* mCols+ k] * other.mData[k* other.mCols +j];}
return c;}
Matrix Matrix::operator/ (Matrix b) // Solve A* x= b
{ return SolveQRQ(decomposeQRQ(*this), b);}
Vector<double> Matrix::operator/ (Vector<double> b) // find A/ b
{ return Vector<double>(SolveQRQ(decomposeQRQ(*this), Matrix(b)));}
double Frobenius(const Matrix& mat) // weighted Frobenius norm
{ double *tmp= mat[0]+ mat.rows()* mat.cols();
double tmpNorm= 0; // Hilbert-Schmidt
for (double* s= mat[0]; s< tmp; s++)
tmpNorm+= Square(*s);
return pow(tmpNorm/ mat.rows()/ mat.cols(), 1/ 2);}
std::ostream& operator << (std::ostream& os, const Matrix m) // output
{ os << "(" << m.rows() << "x" << m.cols() << " matrix):";
for (unsigned i= 0; i< m.rows(); i++)
{ os << std::endl; // new row
for (unsigned j= 0; j< m.cols(); j++)
os << std::setw(11) << std::right << m[i][j] << " " ;}
return os;}
// void Matrix::input() // manual input of matrix via console
// { std::cout << "Enter the elements of the " << mRows << " x " << mCols << " Matrix: " << std::endl;
// for(unsigned i=0; i< mRows ; i++)
// { std::cout << "row " << i+1 << ": ";
// for(unsigned j= 0; j< mCols; j++)
// std::cin >> mData[i* mCols+ j];}}
// Matrix input() // manual input of matrix via console
// { unsigned mRows, mCols;
// std::cout << std::endl << "Matrix input. Enter the dimension of Matrix: "; std::cin >> mRows >> mCols;
// Matrix c(mRows, mCols); c.input(); return c;}
#endif // matrix_CPP