MCPcopy Create free account
hub / github.com/numpy/numpy / test_operations

Method test_operations

numpy/f2py/tests/test_symbolic.py:212–274  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

210 assert as_ge(x, y).tostring(language=language) == "x >= y"
211
212 def test_operations(self):
213 x = as_symbol("x")
214 y = as_symbol("y")
215 z = as_symbol("z")
216
217 assert x + x == Expr(Op.TERMS, {x: 2})
218 assert x - x == Expr(Op.INTEGER, (0, 4))
219 assert x + y == Expr(Op.TERMS, {x: 1, y: 1})
220 assert x - y == Expr(Op.TERMS, {x: 1, y: -1})
221 assert x * x == Expr(Op.FACTORS, {x: 2})
222 assert x * y == Expr(Op.FACTORS, {x: 1, y: 1})
223
224 assert +x == x
225 assert -x == Expr(Op.TERMS, {x: -1}), repr(-x)
226 assert 2 * x == Expr(Op.TERMS, {x: 2})
227 assert 2 + x == Expr(Op.TERMS, {x: 1, as_number(1): 2})
228 assert 2 * x + 3 * y == Expr(Op.TERMS, {x: 2, y: 3})
229 assert (x + y) * 2 == Expr(Op.TERMS, {x: 2, y: 2})
230
231 assert x**2 == Expr(Op.FACTORS, {x: 2})
232 assert (x + y)**2 == Expr(
233 Op.TERMS,
234 {
235 Expr(Op.FACTORS, {x: 2}): 1,
236 Expr(Op.FACTORS, {y: 2}): 1,
237 Expr(Op.FACTORS, {
238 x: 1,
239 y: 1
240 }): 2,
241 },
242 )
243 assert (x + y) * x == x**2 + x * y
244 assert (x + y)**2 == x**2 + 2 * x * y + y**2
245 assert (x + y)**2 + (x - y)**2 == 2 * x**2 + 2 * y**2
246 assert (x + y) * z == x * z + y * z
247 assert z * (x + y) == x * z + y * z
248
249 assert (x / 2) == as_apply(ArithOp.DIV, x, as_number(2))
250 assert (2 * x / 2) == x
251 assert (3 * x / 2) == as_apply(ArithOp.DIV, 3 * x, as_number(2))
252 assert (4 * x / 2) == 2 * x
253 assert (5 * x / 2) == as_apply(ArithOp.DIV, 5 * x, as_number(2))
254 assert (6 * x / 2) == 3 * x
255 assert ((3 * 5) * x / 6) == as_apply(ArithOp.DIV, 5 * x, as_number(2))
256 assert (30 * x**2 * y**4 / (24 * x**3 * y**3)) == as_apply(
257 ArithOp.DIV, 5 * y, 4 * x)
258 assert ((15 * x / 6) / 5) == as_apply(ArithOp.DIV, x,
259 as_number(2)), (15 * x / 6) / 5
260 assert (x / (5 / x)) == as_apply(ArithOp.DIV, x**2, as_number(5))
261
262 assert (x / 2.0) == Expr(Op.TERMS, {x: 0.5})
263
264 s = as_string('"ABC"')
265 t = as_string('"123"')
266
267 assert s // t == Expr(Op.STRING, ('"ABC123"', 1))
268 assert s // x == Expr(Op.CONCAT, (s, x))
269 assert x // s == Expr(Op.CONCAT, (x, s))

Callers

nothing calls this directly

Calls 7

as_symbolFunction · 0.90
ExprClass · 0.90
as_numberFunction · 0.90
as_applyFunction · 0.90
as_stringFunction · 0.90
as_complexFunction · 0.90
as_exprFunction · 0.90

Tested by

no test coverage detected