Ask Your Question

Making my own special type of variable

asked 2011-05-11 13:35:18 +0200

paragon gravatar image

updated 2011-05-12 15:21:18 +0200

Kelvin Li gravatar image

I want to create an object that I can take formal products and sums of, and also that remembers some extra data. I'd like to use sage's symbolic tools to do it without reimplementing multiplication, etc. For example, I'd like to make a class myVar that behaves something like this:

sage: a = myVar("some data about a")
sage: b = myVar("other important stuff")
sage: p = ((a + b)^2).expand()
a^2 + 2*a*b + b^2

sage: p.operands()
[a^2, 2a*b, b^2]

sage: p.operands()[0].operands[0]

sage: p.operands()[0].operands[0].get_data()
"some data about a"

I've tried subclassing Expression, but it seems like whenever I try to do some sums or products it just turns my thing into an Expression and forgets that it was a myVar.

Is something like this possible in sage?

edit retag flag offensive close merge delete


I think my syntax is probably bad in the example-- I need to be more careful about the variable names, probably, but I hope it is clear what I am trying to do.

paragon gravatar imageparagon ( 2011-05-11 15:16:30 +0200 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2011-05-12 15:46:21 +0200

Volker Braun gravatar image

You can't solve your problem by subclassing Expression, this requires you to assign data to every expression. Whats the data associated to the Expression a+b?

Since you only want to associate something to variables, you can just use a dictionary:

sage: var('a, b')
(a, b)
mydata = { a:'foo', b:'bar' }
sage: p = ((a + b)^2).expand()
a^2 + 2*a*b + b^2
sage: p.operands()[0].operands()[0]
sage: mydata[_]
edit flag offensive delete link more


I have thought about using a dictionary, but it is kind of awkward when I want to pass these things into functions and between modules, and also I sometimes want variables with the same name but different data.

paragon gravatar imageparagon ( 2011-05-21 17:39:44 +0200 )edit

Also, I am ok if a+b is just an Expression, I just want to be able to extract 'a' using operands() and then get a myVar object out.

paragon gravatar imageparagon ( 2011-05-21 17:40:46 +0200 )edit

answered 2011-05-11 22:45:20 +0200

Jason Grout gravatar image

This is not a complete answer, but here are some useful methods of the symbolic ring and elements for retrieving wrapped python objects:

sage: a=SR(Mod(8,3))
sage: a
sage: type(a)
<type 'sage.symbolic.expression.Expression'>
sage: type(a.pyobject())
<type 'sage.rings.finite_rings.integer_mod.IntegerMod_int'>
sage: b=SR._force_pyobject('test')
sage: b
sage: type(b)
<type 'sage.symbolic.expression.Expression'>
sage: type(b.pyobject())
<type 'str'>
edit flag offensive delete link more


This is along the lines of what I want, but if I force my object into SR, then I can't add them together because the addition is not defined for them. I want to just inherit the addition and multiplication of the Symbolic Ring somehow without reimplementing.

paragon gravatar imageparagon ( 2011-05-21 17:42:26 +0200 )edit

Your Answer

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

Add Answer

Question Tools


Asked: 2011-05-11 13:35:18 +0200

Seen: 455 times

Last updated: May 12 '11