How to name variables with commas?

137 views
Skip to first unread message

Kevin Hunter

unread,
Jan 6, 2012, 2:09:14 PM1/6/12
to sy...@googlegroups.com
Hullo Sympy Group,

I'm wrapping some Sympy variables into a group via a class object.  The object is attached to another object, and from here, I'm able to collect the name of the group (via __setattr__).  Thus, I can have constructs like this:

-----
M.X = Var()        # Var is the wrapper class that holds a group variables.
M.X[1,2,'a'] = 1   # automatically create variable
-----

That automatically creates the variable 'X[1,2,a]' via the Var.__getitem__.  To get the name with commas, I have to do this within the __getitem__ function:

-----
1. self.m_varsInUse += 1
2. vname = '%s[%d]' % ( self.m_name, self.m_varsInUse )
3. var = symbols( vname, **assumptions )
4. var.name = '%s(%s)' % (self.m_name, str(index).replace(' ', ''))
-----

Lines 1 and 2 guarantee a new variable, so that Sympy doesn't returned a cached copy of an already created variable, and line 4 puts in a name that potentially has commas.  What I'd like to be able to do is forgo lines one and two, like something akin to this:

-----
vname = '%s(%s)' % (self.m_name, str(index).replace(' ', ''))
var = symbols( vname, **assumptions )
-----

This seems to return names like 'X[1', '2', and 'a]'.  I'd hazard splitting based on commas is correct behavior, but I'm wondering if there's something I can pass to the constructor that says "No, the commas are part of the name."

Thanks,

Kevin

Aaron Meurer

unread,
Jan 6, 2012, 11:50:17 PM1/6/12
to sy...@googlegroups.com

Maybe I'm misunderstanding what you're doing, but if you just want
exactly one Symbol, why don't you just do var = Symbol(vname,
**assumptions)?

Aaron Meurer

Kevin Hunter

unread,
Jan 7, 2012, 2:38:27 AM1/7/12
to sy...@googlegroups.com
Err, is this perhaps a bug that has been fixed since the 0.7.1 release?  When I do that, vname can't have commas, else it creates multiple variables:

-----
# For a mnemonic, here is an example variable from a model with which I'm
# working.  The variable format is <VarName>[year, tech, vintage].
>>> from sympy import *
>>> assumptions = dict()
>>> index = (2010, 'Prius', 2008)
>>> vname = 'Car[%s]' % ','.join(str(i) for i in index)

     # I'm looking to create _single_ variable
>>> v = symbols( vname, **assumptions )

     # but instead I get three
>>> type( v )
<type 'tuple'>
>>> len( v )
3
>>> print "Variable 1: '%s'" % v[0]
Variable 1: 'Car[2010'
>>> print "Variable 2: '%s'" % v[1]
Variable 2: 'Prius'
>>> print "Variable 3: '%s'" % v[2]
Variable 3: '2008]'
-----

The only way that I currently know how to get a single variable with commas in the name is by updating it's name after it's been created:

-----
>>> v = symbols( 'some_unique_string', **assumptions )
>>> v.name = vname
>>> type( v )
<class 'sympy.core.symbol.Symbol'>
>>> len( v )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object of type 'Symbol' has no len()
>>> print "Variable 4: '%s'" % v
Variable 4: 'X[2010,Prius,2008]'
-----

Does that help elucidate what I'm asking?  What I'm looking for is more direct method to create variables that have names with commas.

Aaron Meurer

unread,
Jan 7, 2012, 3:30:03 AM1/7/12
to sy...@googlegroups.com
It works in 0.7.1:

In [1]: Symbol('x, y')
Out[1]: x, y

In [2]: type(Symbol('x, y'))
Out[2]: sympy.core.symbol.Symbol

In [10]: Symbol("Car[2010, 'prius', 2008]")
Out[10]: Car[2010, 'prius', 2008]

In [11]: type(Symbol("Car[2010, 'prius', 2008]"))
Out[11]: sympy.core.symbol.Symbol

By the way, changing the name of a Symbol after it is created is a bad
idea. It will cause it to compare equal to other symbols of the same
name, but the Symbol's hash will not be updated.

Aaron Meurer

> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/sympy/-/I-qi5iBc8-8J.
>
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to
> sympy+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.

Kevin Hunter

unread,
Jan 7, 2012, 3:33:08 AM1/7/12
to sy...@googlegroups.com
Mea culpa.  I see now the difference you were trying to impart: Symbol (the class) vs symbols (the function).  Pardon me as I misstep more than a few times while learning Sympy.

Aaron Meurer

unread,
Jan 7, 2012, 3:38:02 AM1/7/12
to sy...@googlegroups.com
No worries. Let us know if you have any other questions.

Aaron Meurer

> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/sympy/-/eBanxs0F_dEJ.

Reply all
Reply to author
Forward
0 new messages