Ask Your Question
3

How can I clear an assignment to a variable?

asked 2011-06-20 09:46:38 -0500

omoplata gravatar image

If I assign a symbolic expression to a variable, how do I clear the assignment as if I didn't assign anything to it in the first place?

For example,

sage: var('a,b')
(a, b)
sage: x=a+b
sage: x
a + b
sage: var(x)
a + b
sage: x
a + b
sage: x=x
sage: x
a + b

Here, I assigned a+b to x. But I can't 'unassign' it.

In Mathematica this can be done with Clear[x].

Thanks.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
6

answered 2011-06-20 10:01:12 -0500

kcrisman gravatar image

The reset command may do what you want.

sage: var('a,b')
(a, b)
sage: x = a+b
sage: x
a + b
sage: reset('x')
sage: x
x
edit flag offensive delete link more

Comments

Thanks. I've made a mistake in the code I posted. I've typed var(x) instead of var('x'). If var('x') is used, that removes the assignment too. I guess sage makes a completely new variable named x?

omoplata gravatar imageomoplata ( 2011-06-20 12:57:55 -0500 )edit

See the answer to your previous question at http://ask.sagemath.org/question/597/difference-between-varx-and-xvarx - I think that is the best explanation.

kcrisman gravatar imagekcrisman ( 2011-06-21 03:01:21 -0500 )edit
3

answered 2011-06-21 07:30:16 -0500

Volker Braun gravatar image

Another possibility is to use Python's del to delete an object:

sage: del x
sage: x
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)

/home/vbraun/opt/sage-4.7.1.alpha2/local/include/singular/<ipython console> in <module>()

NameError: name 'x' is not defined
sage: x = var('x')
sage: x
x
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

Stats

Asked: 2011-06-20 09:46:38 -0500

Seen: 7,468 times

Last updated: Jun 21 '11