-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulSolverTest.java
More file actions
137 lines (108 loc) · 4.98 KB
/
Copy pathSimulSolverTest.java
File metadata and controls
137 lines (108 loc) · 4.98 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
import Engineering.EngCalc;
import Scientific.SimulSolver;
import java.util.*;
public class SimulSolverTest {
private static final Scanner scanner = new Scanner(System.in);
private static final EngCalc engCalc = new EngCalc();
public static void main(String[] args) {
System.out.println("===== Simultaneous Equation Solver =====\n");
System.out.println("Choose the system to solve:");
System.out.println("1. 2x2 System of Equations");
System.out.println("2. 3x3 System of Equations");
int choice = Integer.parseInt(scanner.nextLine().trim());
switch (choice) {
case 1:
solve2x2System();
break;
case 2:
solve3x3System();
break;
default:
System.out.println("Invalid choice. Exiting...");
}
}
private static void solve2x2System() {
System.out.println("\nEnter two equations in the form: ax + by = c");
double[][] A = new double[2][2];
double[][] B = new double[2][1];
for (int i = 0; i < 2; i++) {
System.out.printf("Enter equation %d: ", i + 1);
String equation = scanner.nextLine().trim();
Map<String, Double> coefficients = parseEquation(equation, new String[]{"x", "y"});
A[i][0] = coefficients.getOrDefault("x", 0.0);
A[i][1] = coefficients.getOrDefault("y", 0.0);
B[i][0] = coefficients.get("constant");
}
try {
double[] solution = SimulSolver.solve2x2(A, B);
System.out.printf("Solution:\n x = %.6f, y = %.6f\n", solution[0], solution[1]);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
private static void solve3x3System() {
System.out.println("\nEnter three equations in the form: ax + by + cz = d");
double[][] A = new double[3][3];
double[][] B = new double[3][1];
for (int i = 0; i < 3; i++) {
System.out.printf("Enter equation %d: ", i + 1);
String equation = scanner.nextLine().trim();
Map<String, Double> coefficients = parseEquation(equation, new String[]{"x", "y", "z"});
A[i][0] = coefficients.getOrDefault("x", 0.0);
A[i][1] = coefficients.getOrDefault("y", 0.0);
A[i][2] = coefficients.getOrDefault("z", 0.0);
B[i][0] = coefficients.get("constant");
}
try {
double[] solution = SimulSolver.solve3x3(A, B);
System.out.printf("Solution:\n x = %.6f, y = %.6f, z = %.6f\n", solution[0], solution[1], solution[2]);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
private static Map<String, Double> parseEquation(String equation, String[] variables) {
Map<String, Double> coefficients = new HashMap<>();
coefficients.put("constant", 0.0); // Initialize constant term
// Normalize the equation: remove spaces and separate RHS
equation = equation.replace(" ", "");
String[] parts = equation.split("=");
if (parts.length != 2) {
throw new IllegalArgumentException("Invalid equation format. Use 'ax + by + cz = d'.");
}
String lhs = parts[0]; // Left-hand side
double rhs = Double.parseDouble(parts[1]); // Right-hand side
// Split the left-hand side into terms using regex
String[] terms = lhs.split("(?=[+-])"); // Splits at '+' or '-' but keeps them
for (String term : terms) {
term = term.trim();
if (term.isEmpty()) continue;
boolean isVariable = false;
// Check for variables (x, y, z)
for (String var : variables) {
if (term.contains(var)) {
isVariable = true;
// Extract the coefficient part
String coefficient = term.replace(var, "");
double value;
if (coefficient.isEmpty() || coefficient.equals("+")) {
value = 1.0; // Implicit +1
} else if (coefficient.equals("-")) {
value = -1.0; // Implicit -1
} else {
value = Double.parseDouble(coefficient); // Explicit coefficient
}
// Store the coefficient
coefficients.put(var, coefficients.getOrDefault(var, 0.0) + value);
break;
}
}
// If no variable is found, it's a constant term
if (!isVariable) {
coefficients.put("constant", coefficients.get("constant") + Double.parseDouble(term));
}
}
// Adjust the constant term to move RHS to LHS
coefficients.put("constant", coefficients.get("constant") - rhs);
return coefficients;
}
}