Repository navigation
Expand file tree
/
Copy pathDiagonalMatrix.java
More file actions
156 lines (132 loc) · 4.37 KB
/
Copy pathDiagonalMatrix.java
File metadata and controls
156 lines (132 loc) · 4.37 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
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
import java.util.Scanner;
/**
* DiagonalMatrix
*/
public class DiagonalMatrix {
/** Data Members */
int n;
Object[] element;
/** Constructor */
public DiagonalMatrix(int n) {
this.n = n;
element = new Object[n];
}
/**
* Reads the matrix from the standard input.
*/
public void read() {
Scanner sc = new Scanner(System.in);
for (int i = 0; i < n; i++) {
System.out.print("Enter the element at index (" + i + ", " + i + "): ");
element[i] = sc.nextDouble();
}
}
/**
* Prints the matrix.
*/
public void print() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
System.out.print(element[i] + " ");
} else {
System.out.print("0 ");
}
}
System.out.println();
}
System.out.println();
}
/**
* Adds two matrices.
* @return The sum of the two matrices.
* @throws IllegalArgumentException if the matrices are not of the same size.
*/
public DiagonalMatrix add(DiagonalMatrix other) {
if (n != other.n) {
throw new IllegalArgumentException("Matrices are not of the same size.");
}
DiagonalMatrix sum = new DiagonalMatrix(n);
for (int i = 0; i < n; i++) {
sum.element[i] = (double) element[i] + (double) other.element[i];
}
return sum;
}
/**
* Multiplies two matrices.
* @return The product of the two matrices.
* @throws IllegalArgumentException if the matrices are not of the same size.
*/
public DiagonalMatrix multiply(DiagonalMatrix other) {
if (n != other.n) {
throw new IllegalArgumentException("Matrices are not of the same size.");
}
DiagonalMatrix product = new DiagonalMatrix(n);
for (int i = 0; i < n; i++) {
product.element[i] = (double) element[i] * (double) other.element[i];
}
return product;
}
/**
* Returns the determinant of the matrix.
* @return The determinant of the matrix.
* @throws UnsupportedOperationException if the matrix is not a square matrix.
*/
public double determinant() {
double det = 1;
for (int i = 0; i < n; i++) {
det *= (double) element[i];
}
return det;
}
/**
* Returns the inverse of the matrix.
* @return The inverse of the matrix.
* @throws UnsupportedOperationException if the matrix is not a square matrix.
* @throws UnsupportedOperationException if the matrix is singular.
*/
public DiagonalMatrix inverse() {
DiagonalMatrix inverse = new DiagonalMatrix(n);
for (int i = 0; i < n; i++) {
if ((double) element[i] == 0) {
throw new UnsupportedOperationException("Matrix is singular.");
}
inverse.element[i] = 1 / (double) element[i];
}
return inverse;
}
/**
* Returns the transpose of the matrix.
* @return The transpose of the matrix.
*/
public DiagonalMatrix transpose() {
return this;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the matrix: ");
int n = sc.nextInt();
DiagonalMatrix matrix1 = new DiagonalMatrix(n);
matrix1.read();
System.out.println("Matrix1:");
matrix1.print();
DiagonalMatrix matrix2 = new DiagonalMatrix(n);
matrix2.read();
System.out.println("Matrix2:");
matrix2.print();
DiagonalMatrix sum = matrix1.add(matrix2);
System.out.println("Sum of the two matrices:");
sum.print();
DiagonalMatrix product = matrix1.multiply(matrix2);
System.out.println("Product of the two matrices:");
product.print();
System.out.println("Determinant of matrix1: " + matrix1.determinant());
System.out.println("Determinant of matrix2: " + matrix2.determinant());
DiagonalMatrix inverse1 = matrix1.inverse();
System.out.println("Inverse of matrix1:");
inverse1.print();
DiagonalMatrix inverse2 = matrix2.inverse();
System.out.println("Inverse of matrix2:");
inverse2.print();
}
}