Ask Your Question
1

how to find all special single letter symbols in sagemath?

asked 2022-09-06 10:53:35 +0200

Nasser gravatar image

updated 2022-09-06 12:05:45 +0200

I want to do var(....) and list all the single letters I could possibly use in symbolic expression. I just want to make sure I do not use a letter that has special meaning in sagemath. For example I is the sqrt(-1) so I will not include that in my var command (it causes problem when using external CAS systems from sage if I do that). Same for E is special symbol and there might be more.

How does one find what single symbols have special meaning in sage? Either lower case or upper case? To avoid using any of these in a symbolic expression?

Here is an example where by mistakes I added I to var and this causes fricas to hang when calling from sagemath

age: var('x e d c b a I')
sage: integrate((a+I*a*tan(d*x+c))^(1/2)*tan(d*x+c)^(5/2),x, algorithm="fricas")

This hangs (because I is a simple now and not sqrt(-1) it was having hard time integrating it).

But if do (from clear session)

age: var('x e d c b a')
sage: integrate((a+I*a*tan(d*x+c))^(1/2)*tan(d*x+c)^(5/2),x, algorithm="fricas")

It now works and returns result very quickly.

I googled but having hard time getting such a list and I do not know what command to use in sage to find this out.

I know I could do ? on each letter to see if sagemath shows something about it and if it says object not found or not. But I think there should be a faster way?

Btw, I think it is an error from sagemath to allow a user to do

   var(`I`)

or

 var('E')

At the very least, there should be a warning given that this now overwrites a special build-in symbol. So that I*I no longer gives -1 as expected when doing this. But this is for another discussion.

update

Thanks to the answer. These are the letters that can be used

import string
alphabet = list(string.ascii_lowercase)
print(alphabet)

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

my_vars = [i for i in alphabet if i not in globals()]
print(my_vars)
['a', 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'o', 'p', 'q', 's', 't', 'u', 'v', 'w', 'y', 'z']

alphabet = list(string.ascii_uppercase)
print(alphabet)

['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

my_vars = [i for i in alphabet if i not in globals()]
print(my_vars)

['A', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'P', 'Q', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

So the safe letters to use in var are

 'a', 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'o', 'p', 'q', 's', 't', 'u', 'v', 'w', 'y', 'z'
 'A', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'P', 'Q', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
edit retag flag offensive close merge delete

Comments

Even shorter:

import string
alphabet = string.ascii_letters
my_vars = [i for i in alphabet if i not in globals()]
print(my_vars)
Juanjo gravatar imageJuanjo ( 2022-09-06 12:22:55 +0200 )edit

I would be annoyed if Sage didn't let me use i or n for, say, indexing variables, or I for an ideal. So I do not agree with your statement that we should not be able to redefine I or E. These are important and useful for symbolic calculus, but people use Sage for lots of other things.

John Palmieri gravatar imageJohn Palmieri ( 2022-09-07 00:36:09 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-09-06 11:51:27 +0200

Juanjo gravatar image

You can check if the variables are already in the dictionary provided by globals() and reject those who are:

sage: vars_to_test = {'a', 'b', 'c', 'E', 'I', 'z'}
sage: my_vars = [i for i in vars_to_test if i not in globals()]
sage: print(my_vars)
['a', 'c', 'z', 'b']

Then you can define the variables through

var(my_vars)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2022-09-06 10:53:35 +0200

Seen: 111 times

Last updated: Sep 06 '22