11package com .rae .formicapi .math_tests .solvers ;
22
3+ import com .rae .formicapi .foundation .math .operators .backend .cpu .CpuDoubleVector ;
34import com .rae .formicapi .foundation .math .operators .linear .DynamicCSRMatrix ;
45import com .rae .formicapi .foundation .math .solvers .LeastSquare ;
56import org .junit .jupiter .api .Test ;
@@ -28,11 +29,11 @@ public void testIdentitySystem() {
2829 {0 , 1 , 0 },
2930 {0 , 0 , 1 }
3031 };
31- double [] b = {3 , 7 , 2 };
32+ CpuDoubleVector b = new CpuDoubleVector ( new double [] {3 , 7 , 2 }) ;
3233 double [] expected = {3 , 7 , 2 };
33- double [] x = new double [3 ];
34+ CpuDoubleVector x = new CpuDoubleVector ( new double [3 ]) ;
3435 LeastSquare .solve (denseToSparce (A ),b , x , MAX_ITER , TOL );
35- assertVectorEquals (expected , x , 1e-6 , "identity system" );
36+ assertVectorEquals (expected , x . array () , 1e-6 , "identity system" );
3637 }
3738
3839 /**
@@ -65,11 +66,11 @@ public void testDiagonalSystem() {
6566 {0 , 4 , 0 },
6667 {0 , 0 , 8 }
6768 };
68- double [] b = {4 , 8 , 16 };
69+ CpuDoubleVector b = new CpuDoubleVector ( new double [] {4 , 8 , 16 }) ;
6970 double [] expected = {2 , 2 , 2 };
70- double [] x = new double [3 ];
71+ CpuDoubleVector x = new CpuDoubleVector ( new double [3 ]) ;
7172 LeastSquare .solve (denseToSparce (A ), b , x , MAX_ITER , TOL );
72- assertVectorEquals (expected , x , 1e-6 , "diagonal system" );
73+ assertVectorEquals (expected , x . array () , 1e-6 , "diagonal system" );
7374 }
7475
7576 // ------------------------------------------------
@@ -85,10 +86,10 @@ public void testSymmetricPositiveDefinite() {
8586 {0 , -1 , 4 }
8687 };
8788 double [] x_exact = {1 , 2 , 3 };
88- double [] b = multiply (A , x_exact );
89- double [] x =new double [3 ];
89+ CpuDoubleVector b = new CpuDoubleVector ( multiply (A , x_exact ) );
90+ CpuDoubleVector x = new CpuDoubleVector ( new double [3 ]) ;
9091 LeastSquare .solve (denseToSparce (A ), b , x ,MAX_ITER , TOL );
91- assertVectorEquals (x_exact , x , 1e-6 , "SPD system" );
92+ assertVectorEquals (x_exact , x . array () , 1e-6 , "SPD system" );
9293 }
9394
9495 /**
@@ -112,10 +113,10 @@ public void testGeneralSquare() {
112113 {-2 , 1 , 2 }
113114 };
114115 double [] x_exact = {1 , 2 , 3 };
115- double [] b = multiply (A , x_exact );
116- double [] x = new double [3 ];
117- LeastSquare .solve (denseToSparce (A ), b ,new double [ 3 ] , MAX_ITER , TOL );
118- assertVectorEquals (x_exact , x , 1e-6 , "general square system" );
116+ CpuDoubleVector b = new CpuDoubleVector ( multiply (A , x_exact ) );
117+ CpuDoubleVector x = new CpuDoubleVector ( new double [3 ]) ;
118+ LeastSquare .solve (denseToSparce (A ), b ,x , MAX_ITER , TOL );
119+ assertVectorEquals (x_exact , x . array () , 1e-6 , "general square system" );
119120 }
120121
121122 @ Test
@@ -127,10 +128,10 @@ public void testOverdeterminedConsistent() {
127128 {1 , 1 }
128129 };
129130 double [] x_exact = {3 , 5 };
130- double [] b = multiply (A , x_exact );
131- double [] x = new double [2 ];
132- LeastSquare .solve (denseToSparce (A ), b , x ,MAX_ITER , TOL );
133- assertVectorEquals (x_exact , x , 1e-6 , "overdetermined consistent" );
131+ CpuDoubleVector b = new CpuDoubleVector ( multiply (A , x_exact ) );
132+ CpuDoubleVector x = new CpuDoubleVector ( new double [2 ]) ;
133+ LeastSquare .solve (denseToSparce (A ), b , x ,MAX_ITER , TOL );
134+ assertVectorEquals (x_exact , x . array () , 1e-6 , "overdetermined consistent" );
134135 }
135136
136137 // ------------------------------------------------
@@ -145,20 +146,20 @@ public void testOverdeterminedInconsistent() {
145146 { 1 , 3 },
146147 { 1 , 4 }
147148 };
148- double [] b = { 6 , 5 , 7 , 10 };
149- double [] x = new double [2 ];
149+ CpuDoubleVector b = new CpuDoubleVector ( new double [] { 6 , 5 , 7 , 10 }) ;
150+ CpuDoubleVector x = new CpuDoubleVector ( new double [2 ]) ;
150151 LeastSquare .solve (denseToSparce (A ), b , x , MAX_ITER , TOL );
151152
152- double residual = residualNorm (A , x , b );
153+ double residual = residualNorm (A , x . array () , b . array () );
153154
154155 // Verify optimality: perturbing the solution in any direction should not reduce the residual
155156 Random rng = new Random (42 );
156157 for (int trial = 0 ; trial < 100 ; trial ++) {
157- double [] perturbed = x .clone ();
158+ double [] perturbed = x .array (). clone ();
158159 for (int i = 0 ; i < perturbed .length ; i ++)
159160 perturbed [i ] += (rng .nextDouble () - 0.5 ) * 0.1 ;
160161
161- double perturbedResidual = residualNorm (A , perturbed , b );
162+ double perturbedResidual = residualNorm (A , perturbed , b . array () );
162163 assertTrue (residual <= perturbedResidual + 1e-6 ,
163164 "least-squares solution should be optimal — perturbation trial " + trial );
164165 }
@@ -187,12 +188,12 @@ public void testHilbertMatrix3x3() {
187188 {1.0 / 3 , 1.0 / 4 , 1.0 / 5 }
188189 };
189190 double [] x_exact = {1 , 1 , 1 };
190- double [] b = multiply (A , x_exact );
191- double [] x = new double [3 ];
191+ CpuDoubleVector b = new CpuDoubleVector ( multiply (A , x_exact ) );
192+ CpuDoubleVector x = new CpuDoubleVector ( new double [3 ]) ;
192193 LeastSquare .solve (denseToSparce (A ), b , x , MAX_ITER , TOL );
193194
194195 // Loose tolerance — Hilbert matrices amplify errors
195- assertVectorEquals (x_exact , x , 1e-4 , "Hilbert 3x3" );
196+ assertVectorEquals (x_exact , x . array () , 1e-4 , "Hilbert 3x3" );
196197 }
197198
198199 // ------------------------------------------------
@@ -202,11 +203,11 @@ public void testHilbertMatrix3x3() {
202203 @ Test
203204 public void testSingleElement () {
204205 double [][] A = {{5.0 }};
205- double [] b = {15.0 };
206+ CpuDoubleVector b = new CpuDoubleVector ( new double [] {15.0 }) ;
206207
207- double [] x = new double [1 ];
208+ CpuDoubleVector x = new CpuDoubleVector ( new double [1 ]) ;
208209 LeastSquare .solve (denseToSparce (A ), b , x , MAX_ITER , TOL );
209- assertVectorEquals (new double []{3.0 }, x , 1e-9 , "single element" );
210+ assertVectorEquals (new double []{3.0 }, x . array () , 1e-9 , "single element" );
210211 }
211212
212213 @ Test
@@ -245,7 +246,7 @@ public void testWrongRHSLengthThrows() {
245246 double [] b = {1 , 2 , 3 }; // wrong length
246247
247248 assertThrows (IllegalArgumentException .class ,
248- () -> LeastSquare .solve (denseToSparce (A ), new double [2 ], b , MAX_ITER , TOL ),
249+ () -> LeastSquare .solve (denseToSparce (A ), b , new double [2 ], MAX_ITER , TOL ),
249250 "should throw on RHS length mismatch" );
250251 }
251252
0 commit comments