[sage-devel] Non-alphanumeric variable names

181 views
Skip to first unread message

Ryan Hinton

unread,
Apr 26, 2010, 11:48:26 AM4/26/10
to sage-devel
I'm using variable names with non-alphanumeric characters for
convenience. (Longer story: I have variables with two vector
subscripts.) Should the following be supported?

sage: nn = var('n(0.1)(3.)')

Creating expressions using ``nn`` seems to work fine -- as long as
everything stays in Sage. But when I try to do some non-trivial
equality comparisons, Sage punts the decision to Maxima, which chokes
on the variable name. For example,

sage: maxima(nn+2)
---------------------------------------------------------------------------
TypeError Traceback (most recent call
last)
...
TypeError: Error executing code in Maxima
CODE:
<type 'str'>
Maxima ERROR:
batchload("/home/rwh4s/.sage//temp/hostname/12453//interface//
tmp12453-940467996");1+1308080759;

<sage-display>
apply: found n(0.1) where a function was expected.
-- an error. To debug this try: debugmode(true);
<sage-display>
(%o23)
sage:

Any suggestions? Thanks!

- Ryan

--
To post to this group, send an email to sage-...@googlegroups.com
To unsubscribe from this group, send an email to sage-devel+...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

luisfe

unread,
Apr 26, 2010, 12:18:06 PM4/26/10
to sage-devel
I think that this is a problem inherent to the way that sage
communicates to maxima. And would be difficult to correct unless every
symbolic variable/function has a different maxima, pari etc. name that
does not cause this problem.

For example I would not like the following to be supported

sage: n=var('7')
sage: n
7
sage: n+1
7 + 1
sage: n^2+n
7^2 + 7
sage: maxima(n+1)
8

luisfe

unread,
Apr 26, 2010, 12:21:27 PM4/26/10
to sage-devel
Could not you use another way to use these subscripts. The following
may be ugly, but works

sage: n=var('n__0_3__0_1')
sage: maxima(n+1)
n__0_3__0_1+1


On 26 abr, 17:48, Ryan Hinton <iob...@email.com> wrote:
> For more options, visit this group athttp://groups.google.com/group/sage-devel
> URL:http://www.sagemath.org

Robert Dodier

unread,
Apr 26, 2010, 12:35:42 PM4/26/10
to sage-devel
On Apr 26, 9:48 am, Ryan Hinton <iob...@email.com> wrote:

> sage: nn = var('n(0.1)(3.)')
>
> Creating expressions using ``nn`` seems to work fine -- as long as
> everything stays in Sage. But when I try to do some non-trivial
> equality comparisons, Sage punts the decision to Maxima, which chokes
> on the variable name.

Well, you can construct a symbol in Maxima by putting
a backslash before the nonalphabetic characters.
e.g. n\(0\.1\)\(3\.\) is a symbol. Incidentally so are \123
and foo\+bar.

I guess you want some kind of subscripted variable.
In that case you could just write n[0, 1][3] (in Maxima,
dunno about Sage).

FWIW

Robert Dodier

Burcin Erocal

unread,
Apr 26, 2010, 2:47:44 PM4/26/10
to sage-...@googlegroups.com
On Mon, 26 Apr 2010 08:48:26 -0700 (PDT)
Ryan Hinton <iob...@email.com> wrote:

> I'm using variable names with non-alphanumeric characters for
> convenience. (Longer story: I have variables with two vector
> subscripts.) Should the following be supported?
>
> sage: nn = var('n(0.1)(3.)')

The variable names should just be valid python identifiers. There is
already a ticket for this:

http://trac.sagemath.org/sage_trac/ticket/7496

Unfortunately, I couldn't find a simple way to check if something is
indeed a "valid identifier" (including unicode characters etc. as well).
Python 3 seems to have a function for this, but it will be a long time
before we can migrate to that.

> Creating expressions using ``nn`` seems to work fine -- as long as
> everything stays in Sage. But when I try to do some non-trivial
> equality comparisons, Sage punts the decision to Maxima, which chokes
> on the variable name. For example,
>
> sage: maxima(nn+2)
<snip error message>

You can open a ticket for this. Even if we fix the ticket above, it
would be good to know what characters in a symbol name need to be
escaped and implement that in the conversion to maxima.


Thank you.

Cheers,
Burcin

Alex Leone

unread,
Apr 26, 2010, 3:16:30 PM4/26/10
to sage-...@googlegroups.com
One approach would be to store all the variables in a nested
dictionary, eg n[1, 2][2, 2], and use var('n_1_2_2_2',
latex_name='n_{(1,2),(2,2)}') to create the variables.

Here's an example:

nn = dict( [ ((i, j), {}) for i in [1..2] for j in [1..2] ] )
nn[(1, 1)] = dict( [ ((i, j),
var('v_1_1_%d_%d' % (i, j),
latex_name='n_{(1,1),(%d,%d)}' % (i, j)) )
for i in [1..2] for j in [1..2] ] )

show(4*nn[1, 1][2, 1] + nn[1, 1][2, 2]^2)


Which nicely shows the vector subscripts in latex.

- Alex

Ryan Hinton

unread,
May 6, 2010, 7:44:08 PM5/6/10
to sage-devel
Thanks for the good suggestions. The challenge is trying to recognize
the variables in derived expressions. I'm sure I can combine your
ideas to produce something suitable.

Thanks!

- Ryan
Reply all
Reply to author
Forward
0 new messages