File: matrixUtils.cpp

package info (click to toggle)
fastml 3.11-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,772 kB
  • sloc: cpp: 48,522; perl: 3,588; ansic: 819; makefile: 386; python: 83; sh: 55
file content (374 lines) | stat: -rw-r--r-- 8,171 bytes parent folder | download | duplicates (10)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "matrixUtils.h"
#include "errorMsg.h"
#include <cmath>
#include <string>
#include <ctype.h>
#include <cctype>
#include <cstdlib>


Vdouble getDiagonalFromMatrix(VVdouble &mat){
	Vdouble diagonal;
	for (int i=0; i<mat.size(); i++)
		diagonal.push_back(mat[i][i]);
	return diagonal;
}

Vdouble getSubDiagonalFromMatrix(VVdouble &mat){
	Vdouble diagonal;
	for (int i=0; i<mat.size()-1; i++)
		diagonal.push_back(mat[i+1][i]);
	return diagonal;
}






void readMatrixFromFile(VVdouble &mat,string fileName){
	ifstream in(fileName.c_str());
	if (!in){
		string err="in function readMatrixFromFile, empty file or non-existant:";
		err+=fileName;
		errorMsg::reportError(err); 
	}
	int i=0;
	mat.resize(1);
	while (!in.eof()) {
		string row;
		int k=0;
		getline(in,row,'\n');
		while (k<row.size()){
			string value;
			while (row[k]!=' ' && k<row.size()){
				value+=row[k];
				k++;
			}
		k++;
		mat[i].push_back(atof(value.c_str()));
		//j++;
		//mat.resize(j);
		}
	if (!in.eof())
		mat.resize(++i+1);
	} 
	in.close();
}


void printMatrix(const VVdouble &mat, ostream &out) {
	int num=mat.size();
	for (int row=0; row<num; row++) {
		for (int position=0; position<mat[row].size(); position++) {
			out << mat[row][position] << '\t';
		}
		out << endl ;
	}
	out << endl ;

}

void printMatrix(const VVint &mat, ostream &out) {
	int num=mat.size();
	for (int row=0; row<num; row++) {
		for (int position=0; position<mat[row].size(); position++) {
			out << mat[row][position] << '\t';
		}
		out << endl ;
	}
	out << endl ;
	out<<"---------------------------------------------"<<endl;

}



VVdouble transpose(const VVdouble &mat){
	VVdouble matT;
	int n=mat.size();
	resizeMatrix(matT,n,n);
	for (int i=0; i<n;i++){
		for (int j=0; j<n;j++) {
			matT[i][j]=mat[j][i];
		}
	}
	return matT;
}




VVdouble subtract(const VVdouble &mat1,const VVdouble &mat2){
	VVdouble newMat=add(mat1,reverseSign(mat2));
	return newMat;
}

VVdouble reverseSign(const VVdouble &mat1){
	VVdouble newMat(mat1.size());
	for (int i=0;i<mat1.size();i++){
		newMat[i].resize(mat1[i].size());
		for (int j=0;j<mat1.size();j++){
			newMat[i][j]=-mat1[i][j];
		}
	}
	return newMat;

}


void findMaxInVector(const Vdouble &vec, MDOUBLE &maxValue, int &argmax){
	MDOUBLE tempMax=VERYSMALL;
	int tempArgMax=0;
	for (int i=0; i<vec.size(); i++){
		if (vec[i]>tempMax){	// TEST DEBUG!!! if '>' is used, the first Max is chosen, if '>' the last max 
			tempMax=vec[i];
			tempArgMax=i;
		}
	}
	maxValue=tempMax;
	argmax=tempArgMax;
}

void findMinInVector(const Vdouble &vec, MDOUBLE &minValue, int &argmin) {
	Vdouble minusCopy(vec.size());
	for (int i=0; i<vec.size(); i++){
		minusCopy[i] = -vec[i];
	}
	findMaxInVector(minusCopy, minValue, argmin);
	minValue = -minValue;
}

bool isMinEQMaxInVector(const Vdouble &vec){
	bool isMinEQMaxInVector = false;
	MDOUBLE maxValue, minValue;
	int argmax, argmin;
	findMaxInVector(vec,maxValue,argmax);
	findMinInVector(vec,minValue,argmin);
	if(maxValue == minValue)
		isMinEQMaxInVector = true;
	return isMinEQMaxInVector;
}

MDOUBLE averageElementInVector(const Vdouble &vec) {
	MDOUBLE sum=0.0;
	for (int i=0; i<vec.size(); i++){
		sum+=vec[i];
	}
	return sum/vec.size();
}

void appendBinaryVectors(Vint &vec1, const Vint &vec2){
    for (int i=0; i < vec2.size(); i++) 
        if (vec2[i]==1)
			vec1[i]=1;
}

void appendVectors(Vint &vec1, const Vint &vec2) {
	for (int i=0; i<vec2.size();i++)
		vec1.push_back(vec2[i]);
}

void appendVectors(VVdouble &vec1, const VVdouble &vec2) {
	for (int i=0; i<vec2.size();i++)
		for (int j=0; j<vec2[i].size();j++)
			vec1[i].push_back(vec2[i][j]);
}

Vint complementBinaryVec(Vint&bufferVec) {
	for (int i=0; i<bufferVec.size(); i++) 
		bufferVec[i]=abs(bufferVec[i]-1);
	return bufferVec;
}


//reads a vertical vector of float numbers(separated by \n)
void readDoubleVecFromFile(Vdouble &vec,string fileName){
	ifstream in(fileName.c_str());
	if (!in){
		string err="in function readDoubleVecFromFile, empty file or non-existant:";
		err+=fileName;
		errorMsg::reportError(err); 
	}
	string row;
	while (!in.eof()){
		getline(in,row,'\n');
		//if (isalnum(*(row.c_str())) || (row[0]=="."))
		if (isspace(*(row.c_str())) || row=="") continue;
			vec.push_back(atof(row.c_str()));
	}

	in.close();
}

void normalize(Vdouble &vec){
	MDOUBLE sum=0.0;
	MDOUBLE squareSum=0.0;
	int N=vec.size();
	int i=0;
	for (i=0;i<N;i++) sum+=vec[i];
	for (i=0;i<N;i++) squareSum+=(vec[i]*vec[i]);
	MDOUBLE avg=sum/N;
	MDOUBLE sqrAvg=squareSum/N;
	MDOUBLE stdDev=sqrt(sqrAvg-avg*avg);
	for (i=0;i<N;i++) vec[i]=(vec[i]-avg)/stdDev;

}

void scaleByAverage(Vdouble &vec){
	MDOUBLE sum=0.0;
	MDOUBLE squareSum=0.0;
	int N=vec.size();
	int i=0;
	for (i=0;i<N;i++) sum+=vec[i];
	for (i=0;i<N;i++) squareSum+=(vec[i]*vec[i]);
	MDOUBLE avg=sum/N;
	for (i=0;i<N;i++) vec[i]=(vec[i])/avg;
}

Vdouble solveLinearEquations(VVdouble A,Vdouble b){
//	VVdouble Acopy=A; //creating a copy, since ludcmp&lubksb destroy the input
//	Vdouble bcopy=b;
	MDOUBLE d; //required for ludcmp; irrelevant for us.
	Vdouble indx; //required for ludcmp; irrelevant for us.
	ludcmp(A,indx,d); //decomposes A into product of diagonal matrices
	lubksb(A,indx,b); //solves
	return b;
}


void ludcmp(VVdouble &a, Vdouble &indx, MDOUBLE &d)
{
	const MDOUBLE TINY=1.0e-20;
	int i,imax=0,j,k;
	MDOUBLE big,dum,sum,temp;

	int n=a.size();
	Vdouble vv(n);
	indx.resize(n);//my addition
	d=1.0;
	for (i=0;i<n;i++) {
		big=0.0;
		for (j=0;j<n;j++)
			if ((temp=fabs(a[i][j])) > big) big=temp;
		if (big == 0.0) errorMsg::reportError("Singular matrix in routine ludcmp");
		vv[i]=1.0/big;
	}
	for (j=0;j<n;j++) {
		for (i=0;i<j;i++) {
			sum=a[i][j];
			for (k=0;k<i;k++) sum -= a[i][k]*a[k][j];
			a[i][j]=sum;
		}
		big=0.0;
		for (i=j;i<n;i++) {
			sum=a[i][j];
			for (k=0;k<j;k++) sum -= a[i][k]*a[k][j];
			a[i][j]=sum;
			if ((dum=vv[i]*fabs(sum)) >= big) {
				big=dum;
				imax=i;
			}
		}
		if (j != imax) {
			for (k=0;k<n;k++) {
				dum=a[imax][k];
				a[imax][k]=a[j][k];
				a[j][k]=dum;
			}
			d = -d;
			vv[imax]=vv[j];
		}
		indx[j]=imax;
		if (a[j][j] == 0.0) a[j][j]=TINY;
		if (j != n-1) {
			dum=1.0/(a[j][j]);
			for (i=j+1;i<n;i++) a[i][j] *= dum;
		}
	}
}



void lubksb(VVdouble &a, Vdouble &indx, Vdouble &b)
{
	int i,ii=0,ip,j;
	MDOUBLE sum;

	int n=a.size();
	for (i=0;i<n;i++) {
		ip=(int)(indx[i]);
		sum=b[ip];
		b[ip]=b[i];
		if (ii != 0)
			for (j=ii-1;j<i;j++) sum -= a[i][j]*b[j];
		else if (sum != 0.0)
			ii=i+1;
		b[i]=sum;
	}
	for (i=n-1;i>=0;i--) {
		sum=b[i];
		for (j=i+1;j<n;j++) sum -= a[i][j]*b[j];
		b[i]=sum/a[i][i];
	}
}

//get the first norm sum{abs(Mij)}
MDOUBLE getMatrixNorm(const VVdouble &mat) {
	MDOUBLE res(0.0);
	for (int i=0; i<mat.size(); i++){
		for (int j=0; j<mat[i].size();j++){
			res += fabs(mat[i][j]);
		}
	}
	return res;
}

//get the first norm sum{abs(Mij)} from vector of Matrices
MDOUBLE getVMatrixNorm(const VVVdouble &mat) {
	MDOUBLE res(0.0);
	for (int i=0; i<mat.size(); i++){
		for (int j=0; j<mat[i].size();j++){
			for (int k=0; k<mat[i][j].size();k++){
				res += fabs(mat[i][j][k]);
			}
		}
	}
	return res;
}

//get the specific coordinates sum from vector of Matrices
MDOUBLE getVMatrixJK(const VVVdouble &mat, const int j, const int k) {
	MDOUBLE res(0.0);
	for (int i=0; i<mat.size(); i++){
		res += fabs(mat[i][j][k]);	
	}
	return res;
}




/********************************************************************************************
*********************************************************************************************/
void resize_VVVV(int dim1, int dim2, int dim3, int dim4,  VVVVdouble& vetor){
	
	vetor.resize(dim1);
	for (int posNum=0;posNum<vetor.size();++posNum){
		vetor[posNum].resize(dim2);
		for (int n=0;n<vetor[posNum].size();++n){
			resizeMatrix(vetor[posNum][n],dim3,dim4);
		}
	}
}
/********************************************************************************************
*********************************************************************************************/
void resize_VVV(int dim1, int dim2, int dim3, VVVdouble& vetor){	
	vetor.resize(dim1);
	for (int n=0;n<vetor.size();++n){
		resizeMatrix(vetor[n],dim2,dim3);
	}
}