-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
103 lines (90 loc) · 3.45 KB
/
Copy pathMatrix.java
File metadata and controls
103 lines (90 loc) · 3.45 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
import java.util.Scanner;
public class Matrix {
int rows; //number of rows of matrix
int cols; //numbers of columns of matrix
ComplexNum [][] complexMatrix;
// matrix constructor
public Matrix(int rows, int cols) {
complexMatrix = new ComplexNum[rows][cols];
this.rows = rows;
this.cols = cols;
}
public void CreateMatrix(int rows, int cols){
double real;
double img;
for (int i = 0; i < rows; i++){
for (int j = 0; j < cols; j++){
Scanner scanner = new Scanner(System.in);
real = scanner.nextDouble();
img = scanner.nextDouble();
complexMatrix[i][j] = new ComplexNum(real, img);
}
}
}
//representation of matrix
public void showMatrix(){
for (int i = 0; i < rows; i++){
for (int j = 0; j < cols; j++){
System.out.printf(complexMatrix[i][j] + " ");
}
System.out.println();
}
}
//transpose matrix
public Matrix transpose(){
Matrix transMatrix = new Matrix(cols, rows);
for (int i = 0; i < rows; i++){
for (int j = 0; j < cols; j++){
transMatrix.complexMatrix[j][i] = complexMatrix[i][j];
}
}
return transMatrix;
}
//sum of two matrices
public Matrix addMatrices(Matrix matrix2) throws MatrixExpection {
Matrix sumMatrix;
if (!checkSize(this, matrix2, true))
throw new MatrixExpection("Sizes of matrices are not equal");
else {
sumMatrix = new Matrix(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sumMatrix.complexMatrix[i][j] =
this.complexMatrix[i][j].sum(matrix2.complexMatrix[i][j]);
}
}
}
return sumMatrix;
}
//multiplication of two matrices
public Matrix multiplyMatrices(Matrix matrix2) throws MatrixExpection{
Matrix multMatrix;
if (!checkSize(this, matrix2, false))
throw new MatrixExpection("Number of rows of first matrix must be equal to number of columns of second matrix");
else{
multMatrix = new Matrix(this.rows, matrix2.cols);
for (int i = 0; i < this.rows; i++){
for (int j = 0; j < matrix2.cols; j++){
multMatrix.complexMatrix[i][j] = new ComplexNum(0, 0);
for (int k = 0; k < this.cols; k++) {
ComplexNum elem = this.complexMatrix[i][k].mult(matrix2.complexMatrix[k][j]);
multMatrix.complexMatrix[i][j] = multMatrix.complexMatrix[i][j].sum(elem);
}
}
}
}
return multMatrix;
}
//check size of matrices for addition and multiplication
public boolean checkSize(Matrix matrix1, Matrix matrix2, boolean forAddition){
if (forAddition)
return (matrix1.rows == matrix2.rows) && (matrix1.cols == matrix2.cols);
else
return (matrix1.cols == matrix2.rows);
}
class MatrixExpection extends Exception {
private final String issue;
MatrixExpection(String issue){ this.issue = issue; }
public String issue(){ return this.issue; }
}
}