13 std::vector<double> C;
15 if (A.size() != B.size()) {
17 std::cout <<
"Vectors are not the same dimension! The code DIDN'T run successfully." << std::endl;
18 throw std::exception();
21 std::vector<double> q_difference;
22 for (
int i = 0; i < A.size(); i++) {
24 C.push_back(A[i] - B[i]);
36 std::vector<double>
VectorSum(std::vector<double> A, std::vector<double> B) {
38 std::vector<double> C;
40 if (A.size() != B.size()) {
42 std::cout <<
"Vectors are not the same dimension! The code DIDN'T run successfully." << std::endl;
43 throw std::exception();
46 std::vector<double> q_difference;
47 for (
int i = 0; i < A.size(); i++) {
49 C.push_back(A[i] + B[i]);
55 std::vector<double> C;
59 int rowsM =
static_cast<int>(M.size());
60 int columnsM =
static_cast<int>(M[0].size());
61 int rowsA =
static_cast<int>(A.size());
63 if (columnsM != rowsA) {
65 std::cout <<
"Vector and matrix are not compatible in dimension! The code DIDN'T run successfully." << std::endl;
66 throw std::exception();
69 for (
int i = 0; i < rowsM; i++) {
71 for (
int j = 0; j < columnsM; j++) {
72 C[i] += M[i][j] * A[j];
81 if (A.size() != B.size()) {
83 std::cout <<
"Vectors are not compatible in dimension! The code DIDN'T run successfully." << std::endl;
84 throw std::exception();
87 for (
int i = 0; i < A.size(); i++) {
93 std::vector<double>
GetMatrixColumn(std::vector<std::vector<double>> M,
int column) {
94 std::vector<double> A;
95 unsigned long rows = M.size();
97 if (column > M[0].size()) {
99 std::cout <<
"Matrix index out of range. The code DIDN'T run successfully." << std::endl;
100 throw std::exception();
104 for (
unsigned long row = 0; row < rows; row++) {
105 A.push_back(M[row][column]);
110 std::vector<double>
GetMatrixRow(std::vector<std::vector<double>> M,
int row) {
111 std::vector<double> A;
112 unsigned long columns = M[0].size();
114 if (row > M.size()) {
116 std::cout <<
"Matrix index out of range. The code DIDN'T run successfully." << std::endl;
117 throw std::exception();
121 for (
unsigned long column = 0; column < columns; column++) {
122 A.push_back(M[row][column]);
127 std::vector<std::vector<double>>
TransposeMatrix(std::vector<std::vector<double> > M) {
128 unsigned long rows = M.size();
129 unsigned long columns = M[0].size();
130 std::vector<std::vector<double>> N;
133 std::vector<double> zeroRow(rows, 0);
134 N.insert(N.end(), columns, zeroRow);
136 for (
int row = 0; row < rows; row++) {
137 for (
int column = 0; column < columns; column++) {
138 N[column][row] = M[row][column];
144 std::vector<double>
MatrixTrace(std::vector<std::vector<double>> M) {
145 if (M.size() != M[0].size()) {
147 std::cout <<
"Matrix is not square, and trace doesn't exist. The code DIDN'T run successfully." << std::endl;
148 throw std::exception();
150 std::vector<double> trace;
152 for (
int i = 0; i < M.size(); i++) {
153 trace.push_back(M[i][i]);
159 for (std::vector<double> &row : M) {
160 for (
double &rowElement : row) {
161 if (rowElement != 0) {
162 rowElement = 1.0 / rowElement;
170 std::vector<std::vector<double>> M;
173 std::vector<double> zeroRow(A.size(), 0);
174 M.insert(M.end(), A.size(), zeroRow);
176 for (
int i = 0; i < A.size(); i++) {
183 std::vector<std::vector<double>>
185 std::vector<std::vector<double>> P;
186 unsigned long columnsM = M[0].size();
187 unsigned long rowsM = M.size();
188 unsigned long columnsN = N[0].size();
189 unsigned long rowsN = N.size();
191 if (columnsM != rowsN) {
193 std::cout <<
"Matrices are not compatible in dimension. The code DIDN'T run successfully." << std::endl;
194 throw std::exception();
198 std::vector<double> zeroRow(columnsN, 0);
199 P.insert(P.end(), rowsM, zeroRow);
201 for (
int rowM = 0; rowM < rowsM; rowM++) {
202 for (
int columnN = 0; columnN < columnsN; columnN++) {
204 for (
int innerDim = 0; innerDim < rowsN; innerDim++) {
205 sum += M[rowM][innerDim] * N[innerDim][columnN];
207 P[rowM][columnN] = sum;
213 std::vector<std::vector<double>>
MatrixMatrixSum(std::vector<std::vector<double>> M, std::vector<std::vector<double>> N) {
214 std::vector<std::vector<double>> S;
216 unsigned long columnsM = M[0].size();
217 unsigned long rowsM = M.size();
218 unsigned long columnsN = N[0].size();
219 unsigned long rowsN = N.size();
221 if (columnsM != columnsN || rowsM != rowsN) {
223 std::cout <<
"Matrices are not compatible in dimension. The code DIDN'T run successfully." << std::endl;
224 throw std::exception();
228 std::vector<double> zeroRow(columnsN, 0);
229 S.insert(S.end(), rowsN, zeroRow);
231 for (
int rowM = 0; rowM < rowsM; rowM++) {
232 for (
int columnM = 0; columnM < columnsM; columnM++) {
233 S[rowM][columnM] = M[rowM][columnM] + N[rowM][columnM];
240 std::vector<std::vector<double>>
operator+(std::vector<std::vector<double>> M, std::vector<std::vector<double>> N) {
245 std::vector<std::vector<double>>
operator*(std::vector<std::vector<double>> M, std::vector<std::vector<double>> N) {
250 double operator*(std::vector<double> A, std::vector<double> B) {
254 std::vector<double>
operator*(std::vector<std::vector<double>> M, std::vector<double> A) {
259 std::vector<double>
operator+(std::vector<double> A, std::vector<double> B) {
260 return VectorSum(std::move(A), std::move(B));
263 std::vector<double>
operator-(std::vector<double> A, std::vector<double> B) {
267 std::vector<double>
operator*(
double b, std::vector<double> A) {
271 std::vector<double>
operator*(std::vector<double> A,
double b) {
276 std::vector<std::vector<double>> L;
277 unsigned long rowsA = A.size();
279 std::vector<double> zeroRow(rowsA, 0);
280 L.insert(L.end(), rowsA, zeroRow);
282 L[0][0] = sqrt(A[0][0]);
284 for (
int row = 1; row < rowsA; ++row) {
285 for (
int column = 0; column <= row; ++column) {
288 for (
int k = 0; k < column; k++) {
289 sum += L[row][k] * L[column][k];
293 L[row][column] = sqrt(A[row][column] - sum);
295 L[row][column] = (1 / L[column][column]) * (A[row][column] - sum);
304 if (L.size() != L[0].size()) {
305 std::cout <<
"Lower triangular matrix is not square, and solving the triangular system can't be done. The " 306 <<
"code DIDN'T run successfully." << std::endl;
307 throw std::exception();
309 std::vector<double> y(L.size(), 0.0);
311 y[0] = x[0] / L[0][0];
312 for (
int i = 1; i < y.size(); ++i) {
315 for (
int j = 0; j < i; ++j) {
316 sum += L[i][j] * y[j];
319 y[i] = (x[i] - sum) / L[i][i];
326 if (L.size() != L[0].size()) {
327 std::cout <<
"Lower triangular matrix is not square, and inverse doesn't exist. The code DIDN'T run successfully." <<
329 throw std::exception();
332 std::vector<std::vector<double>> iL;
333 for (
int i = 0; i < L.size(); ++i) {
334 std::vector<double> RHS(L.size(), 0.0);
341 std::vector<double>
operator/(std::vector<double> A,
double b) {
346 return A / (sqrt(A * A));
std::vector< std::vector< double > > InvertLowerTriangular(std::vector< std::vector< double >> L)
Invert a lower triangular matrix by use of solving the system per column of using SolveLowerTriang...
std::vector< std::vector< double > > MatrixMatrixSum(std::vector< std::vector< double >> M, std::vector< std::vector< double >> N)
A function to calculate the sum of the entries of two matrices.
std::vector< double > MatrixTrace(std::vector< std::vector< double >> M)
Function to the trace of a square matrix .
std::vector< double > VectorSum(std::vector< double > A, std::vector< double > B)
Vector sum between two vectors.
std::vector< double > operator/(std::vector< double > A, double b)
Operator form of vector by scalar division. Uses VectorScalarProduct() with std library forwarding...
std::vector< std::vector< double > > VectorToDiagonal(std::vector< double > A)
Function which takes a std::vector of double to make a diagonal matrix of it, such that ...
std::vector< double > VectorDifference(std::vector< double > A, std::vector< double > B)
Vector difference between two vectors.
double VectorVectorProduct(std::vector< double > A, std::vector< double > B)
Dot product of vectors.
std::vector< std::vector< double > > TransposeMatrix(std::vector< std::vector< double > > M)
std::vector< double > VectorScalarProduct(std::vector< double > A, double b)
Vector scalar prodcut of vector and scalar.
std::vector< double > GetMatrixColumn(std::vector< std::vector< double >> M, int column)
Function to get a column from a matrix.
std::vector< double > GetMatrixRow(std::vector< std::vector< double >> M, int row)
Function to get a row from a matrix.
std::vector< std::vector< double > > MatrixMatrixProduct(std::vector< std::vector< double >> M, std::vector< std::vector< double >> N)
Function incorporating the standard matrix-matrix product, producing a new matrix. Matrix M should have as many columns as N has rows, otherwise an exception is thrown.
std::vector< std::vector< double > > InvertMatrixElements(std::vector< std::vector< double >> M)
Function to take the inverse of the individual matrix elements.
std::vector< double > SolveLowerTriangular(std::vector< std::vector< double >> L, std::vector< double > x)
Solve linear equation where is a lower triangular matrix and and are dimensional vectors...
std::vector< double > operator-(std::vector< double > A, std::vector< double > B)
Operator form of VectorDifference(), using std library forwarding.
std::vector< std::vector< double > > operator*(std::vector< std::vector< double >> M, std::vector< std::vector< double >> N)
Operator form of MatrixMatrixProduct(), using std library forwarding.
std::vector< double > NormalizeVector(std::vector< double > A)
Normalizes a vector to unit length.
std::vector< double > MatrixVectorProduct(std::vector< std::vector< double > > M, std::vector< double > A)
std::vector< std::vector< double > > operator+(std::vector< std::vector< double >> M, std::vector< std::vector< double >> N)
Operator form of MatrixMatrixSum(), using std library forwarding.
std::vector< std::vector< double > > CholeskyDecompose(std::vector< std::vector< double >> A)
Cholesky-decomposition of a positive definite Hermitian matrix .