-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperation.java
More file actions
118 lines (97 loc) · 3.42 KB
/
Copy pathOperation.java
File metadata and controls
118 lines (97 loc) · 3.42 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
public class Operation
{
public static Vector add( Vector v , Vector u )
{
int vectorSize = Integer.parseInt( v.getSize() );
Double[] vectorValues = new Double[vectorSize];
if( v.vectorsAreSameSize(u) )
{
for (int i = 0; i < vectorSize; i++) {
vectorValues[i] = v.getComponent(i) + u.getComponent(i);
}
}
Vector summationVector = new Vector(vectorValues);
return summationVector;
}
public static Matrix add(Matrix a,Matrix b)
{
Vector[] rowVectorsSum = new Vector[a.getNumberOfRows()];
for( int i = 0 ; i < a.getNumberOfRows() ; i++ )
{
rowVectorsSum[i] = add(a.getRowVectors(i),b.getRowVectors(i));
}
return new Matrix(rowVectorsSum);
}
public static Vector scalarMultiplication(Double c , Vector u )
{
Double[] values = new Double[ Integer.parseInt( u.getSize() ) ];
for(int i = 0 ; i < Integer.parseInt( u.getSize() ); i++ )
{
values[ i ] = c * u.getComponent( i );
}
return new Vector( values );
}
public static Matrix scalarMultiplication( Double c , Matrix a )
{
Vector[] rowVectorProduct = new Vector[ a.getNumberOfRows() ];
for( int i = 0 ; i < a.getNumberOfRows() ; i++ )
{
rowVectorProduct[ i ] = scalarMultiplication( c , a.getRowVectors( i ) );
}
return new Matrix( rowVectorProduct );
}
public static double determinant( Matrix a ) {
Double[][] matrix = a.matrixToArray();
Double minorMatrix[][];
Double determinent = 0.0;
if (matrix.length == 1) {
determinent = matrix[0][0];
return (determinent);
}
if (matrix.length == 2) {
determinent = ((matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0]));
return (determinent);
}
for (int i = 0; i < matrix[0].length; i++) {
minorMatrix = new Double[matrix.length - 1][matrix[0].length - 1];
for (int j = 1; j < matrix.length; j++) {
for (int k = 0; k < matrix[0].length; k++) {
if (k < i) {
minorMatrix[j - 1][k] = matrix[j][k];
} else if (k > i) {
minorMatrix[j - 1][k - 1] = matrix[j][k];
}
}
}
determinent += matrix[0][i] * Math.pow (-1, (double) i) * determinant(new Matrix(minorMatrix) );
}
return (determinent);
}
public static Double dotProduct(Vector u , Vector v )
{
Double dotProduct = 0.0;
for( int i = 0 ; i < Integer.parseInt( u.getSize() ) ; i++ )
{
dotProduct += u.getComponent( i ) * v.getComponent( i );
}
return dotProduct;
}
public static Double dotProduct( Matrix a , Matrix b )
{
Double dotProduct = 0.0;
for( int i = 0 ; i < a.getNumberOfRows() ; i++ )
{
dotProduct += dotProduct( a.getRowVectors( i ), b.getRowVectors( i ) );
}
return dotProduct;
}
public static Matrix transpose( Matrix a )
{
Vector[] rowVectors = new Vector[a.getNumberOfColumns()];
for(int i = 0 ; i < a.getNumberOfColumns() ; i++ )
{
rowVectors[i] = a.getColumnVectors(i);
}
return new Matrix( rowVectors );
}
}