Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sympify "N" (newton) error #26422

Open
memoriadecalculo opened this issue Mar 29, 2024 · 5 comments
Open

sympify "N" (newton) error #26422

memoriadecalculo opened this issue Mar 29, 2024 · 5 comments

Comments

@memoriadecalculo
Copy link

memoriadecalculo commented Mar 29, 2024

When trying to sympify an expression with newton unit abreviated ("N"), it raises an exception because it considered "N" as an expression and not as a unit, as you can see below:

**>>> sympify('10*N')**
ValueError: Error from parse_expr with transformed code: 'Integer (10 )*N '

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/sympy/core/sympify.py", line 472, in sympify
    expr = parse_expr(a, local_dict=locals, transformations=transformations, evaluate=evaluate)
  File "/usr/lib/python3/dist-packages/sympy/parsing/sympy_parser.py", line 1026, in parse_expr
    raise e from ValueError(f"Error from parse_expr with transformed code: {code!r}")
  File "/usr/lib/python3/dist-packages/sympy/parsing/sympy_parser.py", line 1017, in parse_expr
    rv = eval_expr(code, local_dict, global_dict)
  File "/usr/lib/python3/dist-packages/sympy/parsing/sympy_parser.py", line 911, in eval_expr
    expr = eval(
  File "<string>", line 1, in <module>
**TypeError: unsupported operand type(s) for *: 'Integer' and 'function'**

This probably happens because sympify module imports "N function" in the line 61:

from .core import (sympify, SympifyError, cacheit, Basic, Atom,
        preorder_traversal, S, Expr, AtomicExpr, UnevaluatedExpr, Symbol,
        Wild, Dummy, symbols, var, Number, Float, Rational, Integer,
        NumberSymbol, RealNumber, igcd, ilcm, seterr, E, I, nan, oo, pi, zoo,
        AlgebraicNumber, comp, mod_inverse, Pow, integer_nthroot, integer_log,
        Mul, prod, Add, Mod, Rel, Eq, Ne, Lt, Le, Gt, Ge, Equality,
        GreaterThan, LessThan, Unequality, StrictGreaterThan, StrictLessThan,
        vectorize, Lambda, WildFunction, Derivative, diff, FunctionClass,
        Function, Subs, expand, PoleError, count_ops, expand_mul, expand_log,
        expand_func, expand_trig, expand_complex, expand_multinomial, nfloat,
        expand_power_base, expand_power_exp, arity, PrecisionExhausted, **N**,
        evalf, Tuple, Dict, gcd_terms, factor_terms, factor_nc, evaluate,
        Catalan, EulerGamma, GoldenRatio, TribonacciConstant)

And again in the line 270:

__all__ = [
    # sympy.core
    'sympify', 'SympifyError', 'cacheit', 'Basic', 'Atom',
    'preorder_traversal', 'S', 'Expr', 'AtomicExpr', 'UnevaluatedExpr',
    'Symbol', 'Wild', 'Dummy', 'symbols', 'var', 'Number', 'Float',
    'Rational', 'Integer', 'NumberSymbol', 'RealNumber', 'igcd', 'ilcm',
    'seterr', 'E', 'I', 'nan', 'oo', 'pi', 'zoo', 'AlgebraicNumber', 'comp',
    'mod_inverse', 'Pow', 'integer_nthroot', 'integer_log', 'Mul', 'prod',
    'Add', 'Mod', 'Rel', 'Eq', 'Ne', 'Lt', 'Le', 'Gt', 'Ge', 'Equality',
    'GreaterThan', 'LessThan', 'Unequality', 'StrictGreaterThan',
    'StrictLessThan', 'vectorize', 'Lambda', 'WildFunction', 'Derivative',
    'diff', 'FunctionClass', 'Function', 'Subs', 'expand', 'PoleError',
    'count_ops', 'expand_mul', 'expand_log', 'expand_func', 'expand_trig',
    'expand_complex', 'expand_multinomial', 'nfloat', 'expand_power_base',
    'expand_power_exp', 'arity', 'PrecisionExhausted', **'N'**, 'evalf', 'Tuple',
    'Dict', 'gcd_terms', 'factor_terms', 'factor_nc', 'evaluate', 'Catalan',
    'EulerGamma', 'GoldenRatio', 'TribonacciConstant',

I could figured out only 3 possible solutions:

  1. rename function "N" (but I don't know from where it comes);
  2. delete function "N" from lines 61 and 270 (but I don't know the real importance of it); e
  3. not use "N" for expressions with newton unit. Instead use only use "newton" (in full form).

Despite the fact I consider the third one the worst option, since people working with physics and mathematicians expressions usually use newton force as "N", I'm adopting this option as a workaround because I hope the solution 1, 2, or other better comes fast.

INFORMATION
Ubuntu 22.04.4 LTS 64 bits
Python 3.10.12 [GCC 11.4.0]
sympy==1.9

@oscarbenjamin
Copy link
Contributor

You can use parse_expr to control the globals. It would be nice if there was a better way.

@aquazod
Copy link

aquazod commented Apr 1, 2024

if the resulting expression is "10*newton", would this be good?

@memoriadecalculo
Copy link
Author

if the resulting expression is "10*newton", would this be good?

The point is: if you input "10*N" (short form) you'll get an error and not the resulting expression you wrote.

@aquazod
Copy link

aquazod commented Apr 6, 2024

Can you give me some test inputs and the expected output for them after passing to sympify()?
try to make them as compound as you can please

@memoriadecalculo
Copy link
Author

Can you give me some test inputs and the expected output for them after passing to sympify()? try to make them as compound as you can please

For sure.
Considering:

Python 3.11.6 (main, Oct  4 2023, 09:27:52) on linux
>>> from sympy.core.sympify import sympify

Right answer using "newton" instead of "N":

>>> sympify("10*newton")
10*newton
>>> type(sympify("10*newton"))
<class 'sympy.core.mul.Mul'>

Right answer if the use of "N" was ok inside sympify:

>>> sympify("10*N")
10*newton
>>> type(sympify("10*N"))
<class 'sympy.core.mul.Mul'>

Error happening nowadays;

>>> sympify("10*N")
ValueError: Error from parse_expr with transformed code: 'Integer (10 )*N '

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/data/user/0/org.qpython.qpy/files/lib/python3.11/site-packages/sympy/core/sympify.py", line 495, in sympify
    expr = parse_expr(a, local_dict=locals, transformations=transformations, evaluate=evaluate)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/user/0/org.qpython.qpy/files/lib/python3.11/site-packages/sympy/parsing/sympy_parser.py", line 1087, in parse_expr
    raise e from ValueError(f"Error from parse_expr with transformed code: {code!r}")
  File "/data/user/0/org.qpython.qpy/files/lib/python3.11/site-packages/sympy/parsing/sympy_parser.py", line 1078, in parse_expr
    rv = eval_expr(code, local_dict, global_dict)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/user/0/org.qpython.qpy/files/lib/python3.11/site-packages/sympy/parsing/sympy_parser.py", line 906, in eval_expr
    expr = eval(
           ^^^^^
  File "<string>", line 1, in <module>
TypeError: unsupported operand type(s) for *: 'Integer' and 'function'

I hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants