It could be useful to be able to pass assumptions to sympify which
would be applied to any symbols created.  Open an issue for it if you
want it.

Note that this could have unintendent consequences.  If you pass a
locals() dict to sympify, e.g., sympify(somestr, locals=locals()),
then any symbols in that dict will not be created at all by sympify(),
so won't have the assumptions you want unless they already do.

Also, currently, Functions in sympify() are implemented as symbols
which are called.  So if you do

sympify("f(x)"), this actually does Symbol("f")(Symbol("x')) (this is
because of the way the parser works, see
http://code.google.com/p/sympy/issues/detail?id=1612 and
http://code.google.com/p/sympy/issues/detail?id=440#c1).  So if you
were to implement this, then any "Functions" created by sympify would
also have the given assumptions on them.

If you want to apply assumptions on sympify() with arbitrary Symbols
(i.e., you don't know what they are in advance), you could do a double
pass with .atoms(Symbol).  Something like

In [12]: myexprstr = "a*b*c"

In [13]: myexpr = sympify(myexprstr)

In [14]: syms = myexpr.atoms(Symbol)

In [15]: syms
Out[15]: set(a, b, c)

In [17]: symdict = dict((i.name, Symbol(i.name, positive=True)) for i in syms)

In [18]: symdict
Out[18]: {a: a, b: b, c: c}

In [19]: myexpr = sympify(myexprstr, locals=symdict)

In [20]: myexpr
Out[20]: a⋅b⋅c

In [22]: sqrt(myexpr**2)
Out[22]: a⋅b⋅c

Aaron Meurer

On Thu, Aug 11, 2011 at 11:43 PM, Mateusz Paprocki <[email protected]> wrote:
> Hi,
>
> On 12 August 2011 04:51, Tomo Lazovich <[email protected]> wrote:
>>
>> Hi sympy-folk,
>>
>> If I want to sympify some strings to get symbols from them, but I'd also
>> like to have assumptions along with those symbols (in this case, that they
>> are real). Is it possible to pass along assumptions to sympify like you
>> could to symbols?
>
> You can't pass assumptions to sympify(), but, if you know the symbols of
> your problem, then you can pass a mapping with those symbols with proper
> assumptions set to sympify(), e.g.:
> In [1]: a, b = symbols('a,b', positive=True)
> In [2]: sympify("log(a*b)").expand()
> Out[2]: log(a⋅b)
> In [3]: sympify("log(a*b)", {'a': a, 'b': b}).expand()
> Out[3]: log(a) + log(b)
>
>>
>> Thanks!
>>
>> Tomo
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "sympy" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/sympy?hl=en.
>
> Mateusz
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to