-
Notifications
You must be signed in to change notification settings - Fork 10
/
Matrix.hx
80 lines (76 loc) · 2.15 KB
/
Matrix.hx
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
package mathlib;
class Matrix<T>{
var row:Int;
var col:Int;
var data:Array<Array<T>>;
public function new(row:Int,col:Int){
this.col=col;
this.row=row;
}
public function indentity():Matrix<T> {
for(i in 0...this.col){
for(j in 0...this.row){
if(i==j){
this.data[i][j]=null;
}else{
this.data[i][j]=null;
}
}
}
return this;
}
public function product(matrix:Matrix<T>):Matrix<T> {
var tempMatrix:Matrix<T> = new Matrix(this.row,matrix.col);
for(i in 0...this.row){
for(j in 0...matrix.col){
tempMatrix.data[i][j]=null;
for(k in 0...this.col){
// tempMatrix.data[i][j]+= this.data[i][k]*matrix.data[k][j];
}
}
}
return tempMatrix;
}
public function hardamard(data:Matrix<T>):Matrix<T> {
var tempMatrix:Matrix<T> = new Matrix(data.row,data.col);
for(i in 0...data.col){
for(j in 0...data.row){
// tempMatrix.data[i][j]=this.data[i][j]*data.data[i][j];
}
}
return tempMatrix;
}
public function tr(matrix:Matrix<Float>):Float{
var total:Float;
for(i in 0...matrix.col){
for(j in 0...matrix.row){
if(i==j){
total+=matrix.data[i][j];
}
}
}
return total;
}
public function frobenius(A:Matrix<Float>):Float {
var tempNumbers:Float;
for(i in 0...A.row){
for(j in 0...A.col){
tempNumbers+=Math.pow(A.data[i][j],2.0);
}
}
return Math.pow(tempNumbers,(1/2));
}
public function tran(A:Matrix<T>):Matrix<T>{
var NMatrix = new Matrix(A.col,A.row);
for(i in 0...A.row){
for(j in 0...A.col){
NMatrix.data[j][i]=A.data[i][j];
}
}
return NMatrix;
}
@:forwardStatics
public function name() {
}
}
var smax:Matrix<Int> = new Matrix(2,4);