-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDblMatrix.C
255 lines (184 loc) · 5.8 KB
/
DblMatrix.C
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/***********************************************************************
DblMatrix.C
BOOM : Bioinformatics Object Oriented Modules
Copyright (C)2012 William H. Majoros ([email protected]).
This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
License (GPL) version 3, as described at www.opensource.org.
***********************************************************************/
#include <iostream>
#include "Stacktrace.H"
#include "GaussJordan.H"
#include "DblMatrix.H"
using namespace std;
BOOM::DblMatrix::DblMatrix(int rows,int columns)
: theArray(rows,columns)
{
}
BOOM::DblMatrix::DblMatrix(const BOOM::DblMatrix &m)
: theArray(m.theArray)
{
}
ostream &operator<<(ostream &os,BOOM::DblMatrix &mat)
{
mat.printOn(os);
return os;
}
double BOOM::DblMatrix::operator()(int row,int col) const
{
BOOM::Array2D<double> &array=const_cast<BOOM::Array2D<double>&>(theArray);
return array[row][col];
}
double &BOOM::DblMatrix::operator()(int row,int col)
{
BOOM::Array2D<double> &array=const_cast<BOOM::Array2D<double>&>(theArray);
return array[row][col];
}
BOOM::DblMatrix &BOOM::DblMatrix::operator=(BOOM::DblMatrix &other)
{
int rows=theArray.getFirstDim();
int cols=theArray.getSecondDim();
for(int i=0 ; i<rows ; ++i)
for(int j=0 ; j<cols ; ++j)
theArray[i][j]=other.theArray[i][j];
}
bool BOOM::DblMatrix::invert(BOOM::DblMatrix &resultingMatrix) const
{
int rows=getNumRows(), cols=getNumColumns();
if(cols!=rows)
throw BOOM::Stacktrace(
"matrix is non-square in BOOM::DblMatrix::invert()");
if(resultingMatrix.getNumColumns()!=cols ||
resultingMatrix.getNumRows()!=rows)
throw BOOM::Stacktrace(
"resulting matrix of wrong size in BOOM::DblMatrix::invert()");
BOOM::DblMatrix &self=const_cast<BOOM::DblMatrix&>(*this);
bool success=BOOM::GaussJordan::invert(self,resultingMatrix);
return success;
}
int BOOM::DblMatrix::getNumColumns() const
{
BOOM::Array2D<double> &array=const_cast<BOOM::Array2D<double>&>(theArray);
return array.getSecondDim();
}
int BOOM::DblMatrix::getNumRows() const
{
BOOM::Array2D<double> &array=const_cast<BOOM::Array2D<double>&>(theArray);
return array.getFirstDim();
}
void BOOM::DblMatrix::addRowMultiple(int sourceRow,int destinationRow,
double factor)
{
int cols=theArray.getSecondDim();
for(int i=0 ; i<cols ; ++i)
theArray[destinationRow][i]+=theArray[sourceRow][i]*factor;
}
void BOOM::DblMatrix::getColumn(int col,BOOM::DblArray1D &into) const
{
BOOM::Array2D<double> &array=const_cast<BOOM::Array2D<double>&>(theArray);
int rows=getNumRows();
if(into.size()!=rows) into.resize(rows);
for(int i=0 ; i<rows ; ++i) into[i]=array[i][col];
}
void BOOM::DblMatrix::getColumn(int col,BOOM::Vector<double> &into) const
{
BOOM::Array2D<double> &array=const_cast<BOOM::Array2D<double>&>(theArray);
int rows=getNumRows();
if(into.size()!=rows) into.resize(rows);
for(int i=0 ; i<rows ; ++i) into[i]=array[i][col];
}
void BOOM::DblMatrix::getRow(int row,BOOM::DblArray1D &into) const
{
BOOM::Array2D<double> &array=const_cast<BOOM::Array2D<double>&>(theArray);
int cols=getNumColumns();
if(into.size()!=cols) into.resize(cols);
for(int i=0 ; i<cols ; ++i)
into[i]=array[row][i];
}
void BOOM::DblMatrix::getRow(int row,BOOM::Vector<double> &into) const
{
BOOM::Array2D<double> &array=const_cast<BOOM::Array2D<double>&>(theArray);
int cols=getNumColumns();
if(into.size()!=cols) into.resize(cols);
for(int i=0 ; i<cols ; ++i)
into[i]=array[row][i];
}
void BOOM::DblMatrix::multiplyRowBy(int whichRow,double factor)
{
int cols=theArray.getSecondDim();
for(int i=0 ; i<cols ; ++i) theArray[whichRow][i]*=factor;
}
void BOOM::DblMatrix::printOn(ostream &os)
{
int rows=theArray.getFirstDim();
int cols=theArray.getSecondDim();
for(int i=0 ; i<rows ; ++i)
{
for(int j=0 ; j<cols ; ++j)
os << theArray[i][j] << '\t';
os << endl;
}
}
void BOOM::DblMatrix::setAllTo(double to)
{
theArray.setAllTo(to);
}
void BOOM::DblMatrix::swapRows(int r1,int r2)
{
double tempCell;
int cols=theArray.getSecondDim();
for(int i=0 ; i<cols ; ++i)
{
tempCell=theArray[r2][i];
theArray[r2][i]=theArray[r1][i];
theArray[r1][i]=tempCell;
}
}
void BOOM::DblMatrix::times(const BOOM::DblMatrix &otherMatrix,
BOOM::DblMatrix &resultMatrix) const
{
BOOM::DblMatrix &thisOne=*const_cast<BOOM::DblMatrix*>(this);
BOOM::DblMatrix &thatOne=const_cast<BOOM::DblMatrix&>(otherMatrix);
multiply(thisOne,thatOne,resultMatrix);
}
void BOOM::DblMatrix::transpose(BOOM::DblMatrix &result) const
{
int nRows=getNumRows(), nCols=getNumColumns();
if(nCols!=result.getNumRows() ||
nRows!=result.getNumColumns())
throw BOOM::Stacktrace(
"wrong-sized matrix in BOOM::DblMatrix::transpose");
BOOM::Array2D<double> &thatArray=result.theArray;
BOOM::Array2D<double> &thisArray=
const_cast<BOOM::Array2D<double>&>(theArray);
for(int i=0 ; i<nRows ; ++i)
for(int j=0 ; j<nCols ; ++j)
thatArray[j][i]=thisArray[i][j];
}
void BOOM::multiply(BOOM::DblMatrix &leftM,BOOM::DblMatrix &rightM,
BOOM::DblMatrix &resultMatrix)
{
int rCols=resultMatrix.getNumColumns();
int rRows=resultMatrix.getNumRows();
int rowLength=leftM.getNumColumns();
if(resultMatrix.getNumRows()!=leftM.getNumRows() ||
resultMatrix.getNumColumns()!=rightM.getNumColumns())
throw BOOM::Stacktrace("wrong-sized matrix in BOOM::DblMatrix.C");
if(rightM.getNumRows()!=leftM.getNumColumns())
throw BOOM::Stacktrace("multiplying differently-sized matrices");
double s;
for(int i=0 ; i<rRows ; ++i)
for(int j=0 ; j<rCols ; ++j)
{
s=0.0;
for(int k=0 ; k<rowLength ; ++k)
{
double x=rightM(k,j)*leftM(i,k);
s+=x;
}
resultMatrix(i,j)=s;
}
}
void BOOM::DblMatrix::resize(int r,int c)
{
theArray.resize(r,c);
}