@@ -124,9 +124,10 @@ def test_term_check(self):
124124 terms4 = [["c" , "x" , "a" ], [1 , "b" , "y" ]]
125125 with pytest .raises (AssertionError ):
126126 le .order_terms (terms4 )
127- terms5 = [[1 , "a" , "b" ], [1 , "b" , "y" ]]
128- with pytest .raises (AssertionError ):
129- le .order_terms (terms5 )
127+ terms5 = [["a" , "b" ], [1 , "b" , "y" ]]
128+ terms = le .order_terms (terms5 )
129+ assert len (terms ) == 1
130+ assert le .additive_offset == 8
130131
131132 def test_eval (self ):
132133 le = linsolve .LinearEquation ("a*x-b*y" , a = 2 , b = 4 )
@@ -138,6 +139,9 @@ def test_eval(self):
138139 sol = {"x" : 3 + 3j * np .ones (10 ), "y" : 7 + 2j * np .ones (10 )}
139140 ans = np .conj (sol ["x" ]) - sol ["y" ]
140141 np .testing .assert_equal (ans , le .eval (sol ))
142+ le = linsolve .LinearEquation ("a*b+a*x-b*y" , a = 2 , b = 4 )
143+ sol = {'x' : 3 , 'y' : 7 }
144+ assert 2 * 4 + 2 * 3 - 4 * 7 == le .eval (sol )
141145
142146
143147class TestLinearSolver :
@@ -276,6 +280,23 @@ def test_eval(self):
276280 result = ls .eval (sol , "a*x+b*y" )
277281 np .testing .assert_almost_equal (3 * 1 + 1 * 2 , list (result .values ())[0 ])
278282
283+ def test_eval_const_term (self ):
284+ x , y = 1.0 , 2.0
285+ a , b = 3.0 * np .ones (4 ), 1.0
286+ eqs = ["a*b+a*x+y" , "a+x+b*y" ]
287+ d , w = {}, {}
288+ for eq in eqs :
289+ d [eq ], w [eq ] = eval (eq ) * np .ones (4 ), np .ones (4 )
290+ ls = linsolve .LinearSolver (d , w , a = a , b = b , sparse = self .sparse )
291+ sol = ls .solve ()
292+ np .testing .assert_almost_equal (sol ["x" ], x * np .ones (4 , dtype = np .float64 ))
293+ np .testing .assert_almost_equal (sol ["y" ], y * np .ones (4 , dtype = np .float64 ))
294+ result = ls .eval (sol )
295+ for eq in d :
296+ np .testing .assert_almost_equal (d [eq ], result [eq ])
297+ result = ls .eval (sol , "a*b+a*x+b*y" )
298+ np .testing .assert_almost_equal (3 * 1 + 3 * 1 + 1 * 2 , list (result .values ())[0 ])
299+
279300 def test_chisq (self ):
280301 x = 1.0
281302 d = {"x" : 1 , "a*x" : 2 }
@@ -297,6 +318,14 @@ def test_chisq(self):
297318 chisq = ls .chisq (sol )
298319 np .testing .assert_almost_equal (sol ["x" ], 5.0 / 3.0 , 6 )
299320 np .testing .assert_almost_equal (ls .chisq (sol ), 1.0 / 3.0 )
321+ x = 1.0
322+ d = {"1*x+1" : 3.0 , "x" : 1.0 }
323+ w = {"1*x+1" : 1.0 , "x" : 0.5 }
324+ ls = linsolve .LinearSolver (d , wgts = w , sparse = self .sparse )
325+ sol = ls .solve ()
326+ chisq = ls .chisq (sol )
327+ np .testing .assert_almost_equal (sol ["x" ], 5.0 / 3.0 , 6 )
328+ np .testing .assert_almost_equal (ls .chisq (sol ), 1.0 / 3.0 )
300329
301330 def test_dtypes (self ):
302331 ls = linsolve .LinearSolver ({"x_" : 1.0 + 1.0j }, sparse = self .sparse )
0 commit comments