11package algolib .maths ;
22
33import java .util .Arrays ;
4+ import java .util .stream .Collectors ;
45
56/** Structure of system of linear equations. */
67public final class EquationSystem
@@ -38,6 +39,14 @@ public int hashCode()
3839 return Arrays .hashCode (equations );
3940 }
4041
42+ @ Override
43+ public String toString ()
44+ {
45+ return "{ %s }" .formatted (Arrays .stream (equations )
46+ .map (Equation ::toString )
47+ .collect (Collectors .joining (" ; " )));
48+ }
49+
4150 /**
4251 * Gets the number of equations in this system.
4352 * @return the number of equations
@@ -68,29 +77,29 @@ public double[] solve()
6877 {
6978 gaussianReduce ();
7079
71- if (equations [equations .length - 1 ].getCoefficient (equations .length - 1 ) == 0
72- && equations [equations .length - 1 ].getFreeTerm () == 0 )
80+ int lastIndex = equations .length - 1 ;
81+
82+ if (equations [lastIndex ].getCoefficients ()[lastIndex ] == 0
83+ && equations [lastIndex ].getFreeTerm () == 0 )
7384 throw new InfiniteSolutionsException ();
7485
75- if (equations [equations . length - 1 ]. getCoefficient ( equations . length - 1 ) == 0
76- && equations [equations . length - 1 ].getFreeTerm () != 0 )
86+ if (equations [lastIndex ]. getCoefficients ()[ lastIndex ] == 0
87+ && equations [lastIndex ].getFreeTerm () != 0 )
7788 throw new NoSolutionException ();
7889
7990 double [] solution = new double [equations .length ];
8091
81- solution [equations .length - 1 ] =
82- equations [equations .length - 1 ].getFreeTerm () / equations [equations .length
83- - 1 ].getCoefficient (
84- equations .length - 1 );
92+ solution [lastIndex ] = equations [lastIndex ].getFreeTerm ()
93+ / equations [lastIndex ].getCoefficients ()[lastIndex ];
8594
8695 for (int i = equations .length - 2 ; i >= 0 ; --i )
8796 {
8897 double value = equations [i ].getFreeTerm ();
8998
90- for (int j = equations . length - 1 ; j > i ; --j )
91- value -= equations [i ].getCoefficient ( j ) * solution [j ];
99+ for (int j = lastIndex ; j > i ; --j )
100+ value -= equations [i ].getCoefficients ()[ j ] * solution [j ];
92101
93- solution [i ] = value / equations [i ].getCoefficient ( i ) ;
102+ solution [i ] = value / equations [i ].getCoefficients ()[ i ] ;
94103 }
95104
96105 return solution ;
@@ -101,24 +110,16 @@ public void gaussianReduce()
101110 {
102111 for (int i = 0 ; i < equations .length - 1 ; ++i )
103112 {
104- int indexMin = i ;
105-
106- for (int j = i + 1 ; j < equations .length ; ++j )
107- {
108- double minCoef = equations [indexMin ].getCoefficient (i );
109- double actCoef = equations [j ].getCoefficient (i );
110-
111- if (actCoef != 0 && (minCoef == 0 || Math .abs (actCoef ) < Math .abs (minCoef )))
112- indexMin = j ;
113- }
113+ int indexMin = getMinimalCoefficientIndex (i );
114114
115- if (equations [indexMin ].getCoefficient ( i ) != 0 )
115+ if (equations [indexMin ].getCoefficients ()[ i ] != 0 )
116116 {
117117 swap (indexMin , i );
118118
119119 for (int j = i + 1 ; j < equations .length ; ++j )
120120 {
121- double param = equations [j ].getCoefficient (i ) / equations [i ].getCoefficient (i );
121+ double param =
122+ equations [j ].getCoefficients ()[i ] / equations [i ].getCoefficients ()[i ];
122123
123124 if (param != 0 )
124125 equations [j ] = equations [j ].add (equations [i ].multiply (-param ));
@@ -149,4 +150,22 @@ public boolean hasSolution(double[] solution)
149150 {
150151 return Arrays .stream (equations ).allMatch (eq -> eq .hasSolution (solution ));
151152 }
153+
154+ private int getMinimalCoefficientIndex (int startingIndex )
155+ {
156+ int indexMin = startingIndex ;
157+
158+ for (int i = startingIndex + 1 ; i < equations .length ; ++i )
159+ {
160+ double minCoefficient = equations [indexMin ].getCoefficients ()[startingIndex ];
161+ double currentCoefficient = equations [i ].getCoefficients ()[startingIndex ];
162+
163+ if (currentCoefficient != 0 && (minCoefficient == 0
164+ || Math .abs (currentCoefficient ) < Math .abs (
165+ minCoefficient )))
166+ indexMin = i ;
167+ }
168+
169+ return indexMin ;
170+ }
152171}
0 commit comments