You're right that you have to define the Python variable name to
access F like that. See
https://docs.sympy.org/latest/tutorial/gotchas.html.

You can get all the symbols in an expression with eq.free_symbols. Or
if you know the symbol is F you can just set

F = symbols('F')

since symbols with the same name are equal, so F will be the same as
the symbol F in the expression from parse_latex.

Aaron Meurer

On Wed, May 27, 2020 at 2:37 PM Ben <ben.is.loca...@gmail.com> wrote:
>
> Hello,
>
> I have a string written in Latex for which I know the dimensions of each 
> symbol. My goal is to validate the dimensional consistency of the expression. 
> I'm having trouble with substitution. For example,
>
> >>> from sympy.physics.units import mass, length, time
> >>> from sympy.physics.units.systems.si import dimsys_SI
> >>> from sympy.parsing.latex import parse_latex
> >>> eq = parse_latex("F = m a")
> >>> eq
> Eq(F, a*m)
>
> I can get the symbols from that expression
> >>> set_of_symbols_in_eq = eq.free_symbols
>
> And for each symbol in the set I know what dimensions each has:
> >>> Fdim = mass * length / time**2
> >>> mdim = mass
> >>> adim = length / time**2
>
> When I try substituting the dimensions into the original expression, I get an 
> error
> >>> eq.subs({F: Fdim, m: mdim, a: adim})
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> NameError: name 'F' is not defined
>
> That is surprising, because F is a Symbol:
> >>> eq.lhs
> F
> >>> type(eq.lhs)
> <class 'sympy.core.symbol.Symbol'>
>
> I think that error means that although F is a Symbol, there isn't a variable 
> named F that points to the Symbol F?
> If that's the case, I don't know how to access the symbols in the abstract 
> syntax tree provided by eq.
> How would I indicate to SymPy that "F = m a" in eq has variables with certain 
> dimensions?
>
> My goal is to run
> >>> dimsys_SI.equivalent_dims(Fdim, mdim * adim)
> True
> without retyping the expression.
>
> I think I want something like the following, except with dimensions 
> substituted for each symbol.
> >>> dimsys_SI.equivalent_dims( eq.lhs, eq.rhs )
> False
>
> --
> You received this message because you are subscribed to the Google Groups 
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sympy+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sympy/a4e2b3fe-27b2-45b8-a7f6-598caea772de%40googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sympy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/CAKgW%3D6LsN9%2B6y34_4TAY_FvAuErE%2B1UYd6jNeuS1GcxADMJBZw%40mail.gmail.com.

Reply via email to