Ricardo,
I think you are confusing SymPy symbols with Python variables here. In the
previous case you store the symbol 'a' in the python variable 'a' and then
you overwrite the python variable 'a' by storing the square root of two,
which makes it an expression. In general, you don't get to access Python
variable names because the same Python object can be assigned to any number
of variables. For example:
a = Symbol('a')
b = Symbol('a')
c = Symbol('a')
my_sym = Symbol('a')
All of the above Python variables (a, b, c, my_sym) store the exact same
SymPy symbol object.
Check out this page for some more info:
http://docs.sympy.org/dev/gotchas.html#variables
Jason
Jason
moorepants.info
+01 530-601-9791
On Sun, Nov 29, 2015 at 2:44 AM, Riccardo Rossi <[email protected]> wrote:
> Dear Thomas,
>
> the suggestion of Mateusz appears to work fine for my needs. I don't mind
> if some optimization opportunities are left out... the common factors are
> even too many for me as of now :-)
>
> one more little question though:
>
> after collecting the factors i have to do some C code generation for them,
> and i am doing some string magic to have a form i like
> for my C++ FE program. Nothing too clever really, but i would need to have
> access somehow to the name of the symbol.
>
> i thought at first that __str__() would do, but no
>
> imagine i have a symbol
>
> a = symbol('a')
> a = sqrt(2)
>
> i would like something that returns me "a"
>
> while
>
> a.__str__() will return sqrt(2)
>
> is there a method to give this? (surely yes, but i can not find it in the
> documentation
>
> cheers
> Riccardo
>
>
>
> On Sunday, November 29, 2015 at 10:08:21 AM UTC+1, Thomas Hisch wrote:
>>
>> Take a look at https://github.com/sympy/sympy/pull/7318. I remember that
>> this PR didn't work for all matrices (I guess matices including expressions
>> with sqrt(2)). If we find a better way to determine the common factor, I'll
>> update the PR.
>>
>> On Saturday, November 28, 2015 at 8:08:52 PM UTC+1, Riccardo Rossi wrote:
>>>
>>> Dear Mateusz,
>>>
>>> what you suggest is exactly what i was hoping for and could not find by
>>> googling
>>>
>>> i'll definitely try that out :-)
>>>
>>> Dzienkuje Bardzo! (hope spelling is correct and...that i guessed
>>> correctly your nationalty)
>>>
>>> cheers
>>> Riccardo
>>>
>>> On Saturday, November 28, 2015 at 11:07:20 AM UTC+1, Mateusz Paprocki
>>> wrote:
>>>>
>>>> Hi,
>>>>
>>>> On 27 November 2015 at 19:34, Riccardo Rossi <[email protected]>
>>>> wrote:
>>>> > Dear list,
>>>> >
>>>> > i am a newby to sympy, and i should say that i liked what i found, so
>>>> ...
>>>> > first of all kudos to the developers.
>>>> >
>>>> > as of now i can succesfully generate my finite element matrices using
>>>> sympy,
>>>> > which saves me quite a lot of work.
>>>> >
>>>> > the point is that now i would like to optimize a bit what i did, and
>>>> i would
>>>> > like to collect some common factors between the entries of a matrix.
>>>> >
>>>> > for example imagine that i have (pseudocode and just an example, no
>>>> physics
>>>> > behind)
>>>> >
>>>> > a,b = symbols('a b')
>>>> >
>>>> > A = Matrix(2,1)
>>>> > A[0] = a*(exp(a+b)+exp(b^2))
>>>> > A[1] = b*(exp(a+b)+exp(b^2))
>>>> >
>>>> > i would like a way to detect that the term
>>>> > (exp(a+b)+exp(b^2))
>>>> >
>>>> > is common to the different entries and eventually later on do
>>>> something of
>>>> > the type
>>>> >
>>>> > aux = (exp(a+b)+exp(b^2))
>>>> > A[0] = a*aux
>>>> > A[1] = b*aux
>>>> >
>>>> > note that later on for me it would be still interesting to do
>>>> something
>>>> > similar on SOME of the entries of the matrix
>>>> >
>>>> > for example if i had
>>>> >
>>>> > A = Matrix(3,1)
>>>> > A[0] = a*(exp(a+b)+exp(b^2))
>>>> > A[1] = b*(exp(a+b)+exp(b^2))
>>>> > A[2] = a+b
>>>> >
>>>> > i would still love to have
>>>> >
>>>> >
>>>> > aux = (exp(a+b)+exp(b^2))
>>>> > A[0] = a*aux
>>>> > A[1] = b*aux
>>>> > A[2] = a+b
>>>> >
>>>>
>>>> you could use cse() (common subexpression elimination) for this, e.g.:
>>>>
>>>> In [1]: from sympy import *
>>>>
>>>> In [2]: var('a,b')
>>>> Out[2]: (a, b)
>>>>
>>>> In [3]: aux = exp(a + b) + exp(b**2)
>>>>
>>>> In [4]: Matrix([a*aux, b*aux, a + b])
>>>> Out[4]:
>>>> Matrix([
>>>> [a*(exp(b**2) + exp(a + b))],
>>>> [b*(exp(b**2) + exp(a + b))],
>>>> [ a + b]])
>>>>
>>>> In [5]: replacements, (M,) = cse(_)
>>>>
>>>> In [6]: M
>>>> Out[6]:
>>>> Matrix([
>>>> [a*x1],
>>>> [b*x1],
>>>> [ x0]])
>>>>
>>>> In [7]: replacements
>>>> Out[7]: [(x0, a + b), (x1, exp(b**2) + exp(x0))]
>>>>
>>>> In [8]: M.subs(list(reversed(replacements)))
>>>> Out[8]:
>>>> Matrix([
>>>> [a*(exp(b**2) + exp(a + b))],
>>>> [b*(exp(b**2) + exp(a + b))],
>>>> [ a + b]])
>>>>
>>>> However, this may not be exactly what you want, because it eliminates
>>>> `a + b` as well.
>>>>
>>>> Mateusz
>>>>
>>>> >
>>>> >
>>>> > thanks in advance for any suggestion.
>>>> >
>>>> > cheers
>>>> > Riccardo
>>>> >
>>>> > --
>>>> > 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 [email protected].
>>>> > To post to this group, send email to [email protected].
>>>> > Visit this group at http://groups.google.com/group/sympy.
>>>> > To view this discussion on the web visit
>>>> >
>>>> https://groups.google.com/d/msgid/sympy/a72b7481-c0c8-49a2-9a1f-3e88ae8f5f3d%40googlegroups.com.
>>>>
>>>> > For more options, visit https://groups.google.com/d/optout.
>>>>
>>> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/1c8e9ddf-94fa-46ea-88a6-b79d77deec02%40googlegroups.com
> <https://groups.google.com/d/msgid/sympy/1c8e9ddf-94fa-46ea-88a6-b79d77deec02%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/CAP7f1Aj%3DXnyLJb%2B6yxYAD2mD1gpLcr5myvoOJZ5PKLprBqy4kQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.