-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumCpp.h
More file actions
61 lines (58 loc) · 1.2 KB
/
NumCpp.h
File metadata and controls
61 lines (58 loc) · 1.2 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
#ifndef _MATRIX_
#include "Mat.h"
#define _MATRIX_
#endif
#ifndef _VECTOR_
#include "Vec.h"
#define _VECTOR_
#endif
#ifndef _UTIL_
#include "utilMat.h"
#define _UTIL_
#endif
#ifndef _NUMCPP_
#define _NUMCPP_
#endif
Vector GaussEq(Matrix &augmentedMat,int copy=1){
Matrix mat(augmentedMat);
if (copy){
mat=augmentedMat.copy();
}
//mat 是一个增广矩阵
int row=mat.row(),col=mat.col();
if(row!=(col-1)) {printf("Error\n");return Vector();}; //满足增广矩阵
int i,j,maxIndex;
IntVector rowIndex=mat.getRowIndex();
for(i=1;i<=row-1;++i){
maxIndex=mat.argmax(i,i,-1,1);
rowIndex.swap(i,maxIndex);
for(j=i+1;j<=row;++j){
matImmerse(mat,i,j,i,col,1);
}
}
Vector solution(row);
for(i=row;i>=1;--i){
solution(i)=mat(i,col);
for(j=row;j>i;--j)
solution(i)-=solution(j)*mat(i,j);
solution(i)/=mat(i,i);
}
return solution;
}
Matrix randomMat(int row,int col){
Matrix newMat(row,col);
int i,j;
for(i=1;i<=row;++i)
for(j=1;j<=col;++j)
newMat(i,j)=rand()/(1.0*RAND_MAX)/row/col;
return newMat;
}
Matrix Cate(Matrix tag,int n){
int row=tag.row(),i,j;
Matrix cate(row,n);
for(i=1;i<=row;++i){
for(j=1;j<=n;++j)
cate(i,j)=(fabs(j-1-tag(i,1))<0.5)?1:0;
}
return cate;
}