-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_tools.cpp
More file actions
170 lines (156 loc) · 3.68 KB
/
matrix_tools.cpp
File metadata and controls
170 lines (156 loc) · 3.68 KB
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
165
166
167
168
169
170
/**
* \file matrix_tools.cpp
* \brief a set of matrix operation tools
* \author Alp Dener <alp.dener@gmail.com>
* \version 1.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <ostream>
#include <iostream>
#include <math.h>
#include "./matrix_tools.hpp"
using namespace std;
// ======================================================================
void matrixMult(vector< vector<double> >& A, int rowA, int colA,
vector< vector<double> >& B, int rowB, int colB,
vector< vector<double> >& AB)
{
// Sanity check on matrix dimensions.
if (colA != rowB)
{
printf("ERROR: Matrix inner dimensions don't match.\
Multiplication failed!\n");
exit(EXIT_FAILURE);
}
// Perform the multiplication.
for (int i = 0; i < rowA; i++)
{
for (int j = 0; j < colB; j++)
{
AB[i][j] = 0;
for (int k = 0; k < colA; k++)
{
AB[i][j] += A[i][k]*B[k][j];
}
}
}
}
// ======================================================================
void matrixVecMult(vector< vector<double> >& A, int rowA, int colA,
vector<double>& B, int rowB,
vector<double>& AB)
{
// Sanity check on matrix dimensions.
if (colA != rowB)
{
printf("ERROR: Matrix inner dimensions don't match.\
Multiplication failed!\n");
exit(EXIT_FAILURE);
}
// Perform the multiplication.
for (int i = 0; i < rowA; i++)
{
AB[i] = 0;
for (int j = 0; j < colA; j++)
{
AB[i] += A[i][j]*B[j];
}
}
}
// ======================================================================
void matrixTranspose(vector< vector<double> >& A, int rowA, int colA,
vector< vector<double> >& Atrans)
{
for (int i = 0; i < rowA; i++)
{
for (int j = 0; j < colA; j++)
{
Atrans[j][i] = A[i][j];
}
}
}
// ======================================================================
void printMatrix(vector< vector<double> >& A, int rowA, int colA)
{
for (int r = 0; r < rowA; r++)
{
printf("| ");
for (int c = 0; c < colA; c++)
{
double val = A[r][c];
printf("%f ", val);
if (c == colA-1)
{printf("|\n");}
}
}
}
// ======================================================================
int CGSolve(vector< vector<double> >& K, int rowK, int colK,
vector<double>& F, int rowF, int maxIt, vector<double>& Disp,
bool info)
{
// Sanity check for system dimensions
if (colK != rowF)
{
printf("ERROR: Matrix dimensions don't match. Cannot solve!\n");
exit(EXIT_FAILURE);
}
double tol = 0;
// Zero out the output vector
for(int i = 0; i < rowF ;i++)
{
Disp[i] = 0;
}
double r[rowF];
double p[rowF];
double rtr = 0;
for(int i = 0; i < rowF; i++)
{
r[i] = F[i];
p[i] = r[i];
rtr = rtr + r[i]*r[i];
}
double alpha=0;
double Ap[rowF];
double ptAp=0;
int iter = 0;
// Start CG iterations
for(int k = 0; k < maxIt; k++)
{
iter++;
for(int i = 0; i<rowF ;i++)
{Ap[i] = 0;}
ptAp = 0;
for(int i = 0; i < rowF; i++)
{
for(int j = 0; j< rowF; j++)
{
Ap[i] = Ap[i] + K[i][j]*p[j];
}
ptAp = ptAp + p[i]*Ap[i];
}
alpha = rtr/(ptAp);
double rtr_new = 0;
for(int i = 0; i < rowF; i++)
{
Disp[i] = Disp[i]+alpha*p[i];
r[i] = r[i] - alpha*Ap[i];
rtr_new = rtr_new + r[i]*r[i];
}
if(rtr_new < 1e-6)
{
tol = rtr_new;
break;
}
for(int i = 0; i < rowF; i++)
{
p[i] = r[i] + rtr_new*p[i]/rtr;
}
rtr = rtr_new;
tol = rtr_new;
}
// return number of iterations
if (info) std::cout << "CGSolve: tol = " << sqrt(tol) << std::endl;
return iter;
}