-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatricesLab.java
More file actions
180 lines (180 loc) · 6.45 KB
/
Copy pathMatricesLab.java
File metadata and controls
180 lines (180 loc) · 6.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//Nancy
import java.util.*;
public class MatricesLab
{
//question 1
public static void main (String [] args){
Scanner s = new Scanner(System.in);
System.out.println("How many rows in your first matrix?");
int r1 = s.nextInt();
System.out.println("How many columns in your first matrix?");
int c1 = s.nextInt();
System.out.println("How many rows in your second matrix?");
int r2 = s.nextInt();
System.out.println("How many columns in your second matrix?");
int c2 = s.nextInt();
double [] [] m1 = new double [r1] [c1];
double [] [] m2 = new double [r2] [c2];
System.out.println();
System.out.println("Now fill your first matrix");
for (int i = 0; i < m1.length; i++){
for (int j = 0; j < m1 [i].length; j++){
System.out.println("Input a double.");
m1 [i] [j] = s.nextDouble();
System.out.println();
}
}
System.out.println("Great! Now fill your second matrix");
for (int i = 0; i < m2.length; i++){
for (int j = 0; j < m2 [i].length; j++){
System.out.println("Input a double.");
m2 [i] [j] = s.nextDouble();
System.out.println();
}
}
boolean running = true;
while (running){
System.out.println();
System.out.println("Your options are: ");
System.out.println("(1) Scalar Multiplication");
System.out.println("(2) Adding the Matrices");
System.out.println("(3) Subtracting the Matrices");
System.out.println("(4) Multiplying the Matrices");
System.out.println("(5) Finding the Inverse");
System.out.println("(6) Quitting the Program");
int option = s.nextInt();
boolean answer = true;
double [] [] ans = m1;
if (option == 6){
running = false;
answer = false;
}
else if (option == 1){
System.out.println("Which matrix? (1 or 2)");
int m = s.nextInt();
System.out.println("Pick a scalar.");
double scalar = s.nextDouble();
if (m == 1){ ans = scalarMult(scalar, m1);}
else{ ans = scalarMult(scalar, m2);}
}
else if (option == 2){
if (r1 != r2 || c1 != c2){
System.out.println("These matrices can't be added");
answer = false;
}
else{
ans = addEm(m1, m2);
}
}
else if (option == 3){
if (r1 != r2 || c1 != c2){
System.out.println("These matrices can't be subtracted");
answer = false;
}
else{
System.out.println("Which matrix should be subtract" +
"ed? (1 or 2)");
if (s.nextInt() == 2){ans = subEm(m1, m2);}
else{ans = subEm(m2, m1);}
}
}
else if (option == 4){
if (c1 != r2){
System.out.println("These matrices can't be multiplied");
answer = false;
}
else{
System.out.println("Which matrix should be first" +
"? (1 or 2)");
if (s.nextInt() == 1){ans = multEm(m1, m2);}
else{ans = multEm(m2, m1);}
}
}
else{
System.out.println("Which matrix? (1 or 2)");
int p = s.nextInt();
double [] [] matrix;
if (p == 1){ matrix = m1;}
else{ matrix = m2;}
if (matrix.length != 2 || matrix [0].length != 2){
System.out.println("Sorry, I can only do 2x2 matrices");
answer = false;
}
else{ans = invert(matrix);}
}
if (answer){
for (double [] x: ans){
System.out.println();
for (double y: x){
System.out.print(y + " ");
}
}
}
}
}
// question 2
public static double [] []
scalarMult (double k, double [] [] m1){
int r = m1.length;
int c = m1 [0].length;
double [] [] ans = new double [r] [c];
for (int i = 0; i < m1.length; i++){
for (int j = 0; j < m1 [i].length; j++){
ans [i] [j] = k * m1 [i] [j];
}
}
return ans;
}
//question 3
public static double [] [] addEm (double [] [] m1, double [] [] m2){
int r = m1.length;
int c = m1 [0].length;
double [] [] ans = new double [r] [c];
for (int i = 0; i < m1.length; i++){
for (int j = 0; j < m1 [i].length; j++){
ans [i] [j] = m1 [i] [j] + m2 [i] [j];
}
}
return ans;
}
//question 4
public static double [] [] subEm (double [] [] m1, double [] [] m2){
int r = m1.length;
int c = m1 [0].length;
double [] [] ans = new double [r] [c];
for (int i = 0; i < m1.length; i++){
for (int j = 0; j < m1 [i].length; j++){
ans [i] [j] = m1 [i] [j] - m2 [i] [j];
}
}
return ans;
}
//question 5
public static double [] [] multEm (double [] [] m1, double [] [] m2){
int r = m1.length;
int c = m2 [0].length;
double [] [] ans = new double [r] [c];
for (int i = 0; i < r; i++){
for (int j = 0; j < c; j++){
double sumRow = 0;
for (int k = 0; k < m1 [0].length; k++){
sumRow = m1 [i] [k] * m2 [k] [j];
}
ans [i] [j] = sumRow;
}
}
return ans;
}
//question 6
public static double [] [] invert (double [] [] m1){
int r = m1.length;
int c = m1 [0].length;
double [] [] ans = new double [r] [c];
double det = (m1 [0] [0] * m1 [1] [1]) - (m1 [0] [1] * m1 [1] [0]);
ans [1] [1] = m1 [0] [0] / det;
ans [0] [0] = m1 [1] [1] / det;
ans [1] [0] = -1 * m1 [1] [0] / det;
ans [0] [1] = -1 * m1 [0] [1] / det;
return ans;
}
}