![]() System : Linux absol.cf 5.4.0-198-generic #218-Ubuntu SMP Fri Sep 27 20:18:53 UTC 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.33 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, Directory : /usr/local/lib/python3.6/dist-packages/sympy/core/tests/ |
Upload File : |
from sympy import Symbol, Mul, symbols, Basic from sympy.testing.pytest import XFAIL class SymbolInMulOnce(Symbol): # Test class for a symbol that can only appear once in a `Mul` expression. pass Basic._constructor_postprocessor_mapping[SymbolInMulOnce] = { "Mul": [lambda x: x], "Pow": [lambda x: x.base if isinstance(x.base, SymbolInMulOnce) else x], "Add": [lambda x: x], } def _postprocess_SymbolRemovesOtherSymbols(expr): args = tuple(i for i in expr.args if not isinstance(i, Symbol) or isinstance(i, SymbolRemovesOtherSymbols)) if args == expr.args: return expr return Mul.fromiter(args) class SymbolRemovesOtherSymbols(Symbol): # Test class for a symbol that removes other symbols in `Mul`. pass Basic._constructor_postprocessor_mapping[SymbolRemovesOtherSymbols] = { "Mul": [_postprocess_SymbolRemovesOtherSymbols], } class SubclassSymbolInMulOnce(SymbolInMulOnce): pass class SubclassSymbolRemovesOtherSymbols(SymbolRemovesOtherSymbols): pass def test_constructor_postprocessors1(): x = SymbolInMulOnce("x") y = SymbolInMulOnce("y") assert isinstance(3*x, Mul) assert (3*x).args == (3, x) assert x*x == x assert 3*x*x == 3*x assert 2*x*x + x == 3*x assert x**3*y*y == x*y assert x**5 + y*x**3 == x + x*y w = SymbolRemovesOtherSymbols("w") assert x*w == w assert (3*w).args == (3, w) assert set((w + x).args) == {x, w} def test_constructor_postprocessors2(): x = SubclassSymbolInMulOnce("x") y = SubclassSymbolInMulOnce("y") assert isinstance(3*x, Mul) assert (3*x).args == (3, x) assert x*x == x assert 3*x*x == 3*x assert 2*x*x + x == 3*x assert x**3*y*y == x*y assert x**5 + y*x**3 == x + x*y w = SubclassSymbolRemovesOtherSymbols("w") assert x*w == w assert (3*w).args == (3, w) assert set((w + x).args) == {x, w} @XFAIL def test_subexpression_postprocessors(): # The postprocessors used to work with subexpressions, but the # functionality was removed. See #15948. a = symbols("a") x = SymbolInMulOnce("x") w = SymbolRemovesOtherSymbols("w") assert 3*a*w**2 == 3*w**2 assert 3*a*x**3*w**2 == 3*w**2 x = SubclassSymbolInMulOnce("x") w = SubclassSymbolRemovesOtherSymbols("w") assert 3*a*w**2 == 3*w**2 assert 3*a*x**3*w**2 == 3*w**2