@@ -222,7 +222,7 @@ def test_getVal_with_GenExpr():
222222 m .getVal (1 / z )
223223
224224
225- def test_unary (model ):
225+ def test_unary_ufunc (model ):
226226 m , x , y , z = model
227227
228228 res = "abs(sum(0.0,prod(1.0,x)))"
@@ -276,6 +276,57 @@ def test_unary(model):
276276 # forbid modifying Variable/Expr/GenExpr in-place via out parameter
277277 np .sin (x , out = np .array ([0 ]))
278278
279+ # test np.negative
280+ assert str (np .negative (x )) == "Expr({Term(x): -1.0})"
281+
282+
283+ def test_binary_ufunc (model ):
284+ m , x , y , z = model
285+
286+ # test np.add
287+ assert str (np .add (x , 1 )) == "Expr({Term(x): 1.0, Term(): 1.0})"
288+ assert str (np .add (1 , x )) == "Expr({Term(x): 1.0, Term(): 1.0})"
289+ a = np .array ([1 ])
290+ assert str (np .add (x , a )) == "[Expr({Term(x): 1.0, Term(): 1.0})]"
291+ assert str (np .add (a , x )) == "[Expr({Term(x): 1.0, Term(): 1.0})]"
292+
293+ # test np.subtract
294+ assert str (np .subtract (x , 1 )) == "Expr({Term(x): 1.0, Term(): -1.0})"
295+ assert str (np .subtract (1 , x )) == "Expr({Term(x): -1.0, Term(): 1.0})"
296+ assert str (np .subtract (x , a )) == "[Expr({Term(x): 1.0, Term(): -1.0})]"
297+ assert str (np .subtract (a , x )) == "[Expr({Term(x): -1.0, Term(): 1.0})]"
298+
299+ # test np.multiply
300+ a = np .array ([2 ])
301+ assert str (np .multiply (x , 2 )) == "Expr({Term(x): 2.0})"
302+ assert str (np .multiply (2 , x )) == "Expr({Term(x): 2.0})"
303+ assert str (np .multiply (x , a )) == "[Expr({Term(x): 2.0})]"
304+ assert str (np .multiply (a , x )) == "[Expr({Term(x): 2.0})]"
305+
306+ # test np.divide
307+ assert str (np .divide (x , 2 )) == "Expr({Term(x): 0.5})"
308+ assert str (np .divide (2 , x )) == "prod(2.0,**(sum(0.0,prod(1.0,x)),-1))"
309+ assert str (np .divide (x , a )) == "[Expr({Term(x): 0.5})]"
310+ assert str (np .divide (a , x )) == "[prod(2.0,**(sum(0.0,prod(1.0,x)),-1))]"
311+
312+ # test np.power
313+ assert str (np .power (x , 2 )) == "Expr({Term(x, x): 1.0})"
314+ assert str (np .power (2 , x )) == "exp(prod(1.0,sum(0.0,prod(1.0,x)),log(2.0)))"
315+ assert str (np .power (x , a )) == "[Expr({Term(x, x): 1.0})]"
316+ assert str (np .power (a , x )) == "[exp(prod(1.0,sum(0.0,prod(1.0,x)),log(2.0)))]"
317+
318+ # test np.less_equal
319+ assert str (np .less_equal (x , a )) == "[ExprCons(Expr({Term(x): 1.0}), None, 2.0)]"
320+ assert str (np .less_equal (a , x )) == "[ExprCons(Expr({Term(x): 1.0}), 2.0, None)]"
321+
322+ # test np.equal
323+ assert str (np .equal (x , a )) == "[ExprCons(Expr({Term(x): 1.0}), 2.0, 2.0)]"
324+ assert str (np .equal (a , x )) == "[ExprCons(Expr({Term(x): 1.0}), 2.0, 2.0)]"
325+
326+ # test np.greater_equal
327+ assert str (np .greater_equal (x , a )) == "[ExprCons(Expr({Term(x): 1.0}), 2.0, None)]"
328+ assert str (np .greater_equal (a , x )) == "[ExprCons(Expr({Term(x): 1.0}), None, 2.0)]"
329+
279330
280331def test_mul ():
281332 m = Model ()
0 commit comments