"x"

65 views
Skip to first unread message

Little Grey Men

unread,
Jan 6, 2026, 6:34:33 AM (2 days ago) Jan 6
to sympy
Would like some advice on use of string "x" as expression source.
"x**1" can work ok with sympify
"1x" can work with parse_expr

We try "x" with sympify and can see it return a type that is not sympy.Expr
Apparently "x" can never work by design <- will that change?,
is some option combo to parse "x"?

thanks for any comments
Daniel.

Raghav Bhola

unread,
Jan 6, 2026, 7:21:21 AM (2 days ago) Jan 6
to sympy
sympify("x") --> always returns a Symbol("x") not an Expr 

sympify("x**1")--> work fine because it become a proper expression ( pow object).

parse_expr("x") --> is more flexible, and with (local_dict) or transformation you can treat "x" as an expression.

here is the solution: use parse_expr instead of sympify.
      from sympy import Symbol
      from sympy.parsing.sympy_parser import parse_expr

      x = Symbol("x")
      expr = parse_expr("x", local_dict={"x": x})
       print(expr)   # outputs: x

Oscar Benjamin

unread,
Jan 6, 2026, 7:23:00 AM (2 days ago) Jan 6
to sy...@googlegroups.com
It is not clear what you mean here:

In [15]: isinstance(sympify("x"), Expr)
Out[15]: True

--
Oscar

Little Grey Men

unread,
Jan 6, 2026, 8:06:24 AM (2 days ago) Jan 6
to sympy
Thank you both for your helpful answers...
Certain blogs/observations had led me to the way I asked the question here.
like cannot use "x" and parse_expr has symbolize built in etc

Anyhow I will assimilate your info into the problem and it seems like it quite possible...

thanks
Daniel.

Little Grey Men

unread,
Jan 6, 2026, 9:15:01 AM (2 days ago) Jan 6
to sympy
OK I am able to see working the version with parse_expr with a local_dict
the previous sympify syntax used which failed with "x" was slightly more complex in that it also used a dict(subs).

Should i be able to call parse_expr with the local_dict to declare x AND the dict(subs) to evaluate.
In such a case if it were valid, it would make sense the evaluate flag would be set to True.

Does any of the above make sense ?
Thanks for any comments,
Daniel.

Oscar Benjamin

unread,
Jan 6, 2026, 9:24:39 AM (2 days ago) Jan 6
to sy...@googlegroups.com
On Tue, 6 Jan 2026 at 14:15, Little Grey Men <daniel...@gmail.com> wrote:
>
> OK I am able to see working the version with parse_expr with a local_dict
> the previous sympify syntax used which failed with "x" was slightly more complex in that it also used a dict(subs).
>
> Should i be able to call parse_expr with the local_dict to declare x AND the dict(subs) to evaluate.
> In such a case if it were valid, it would make sense the evaluate flag would be set to True.
>
> Does any of the above make sense ?

It is too imprecise to understand. Show the actual code and explain
very clearly what happens when you run the code and what it is that
you want to happen.

--
Oscar

Little Grey Men

unread,
Jan 6, 2026, 9:52:25 AM (2 days ago) Jan 6
to sympy
no problem i think what i asking like :
input_str="2x"
transformations = ... implicit_mult ...
expr = parse_expr (input_str, local_dict={"x":x}, transformations=transformations).subs("x", 4.0)

I think above is the approx replacement i looking for.
cheers
Daniel.

Little Grey Men

unread,
Jan 7, 2026, 9:50:58 AM (17 hours ago) Jan 7
to sympy
ok so I had to switch back to simpify since of the tokenize approach with parse_expr too different.
"x" also started working without a local_dict defined ?
I think "2x" cannot be turned into an expression <- if you have an answer for that ?!
The overall approach seems create the expression first and then use it's subs method if required.
cheers!

Little Grey Men

unread,
Jan 7, 2026, 10:08:13 AM (17 hours ago) Jan 7
to sympy
parse_expr with the Symbol and local_dict allows for "2x" so no need to reply further thanks !
Reply all
Reply to author
Forward
0 new messages