ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 21 May 2011 10:42:26 -0500Making my own special type of variablehttp://ask.sagemath.org/question/8109/making-my-own-special-type-of-variable/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]
a
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?Wed, 11 May 2011 06:35:18 -0500http://ask.sagemath.org/question/8109/making-my-own-special-type-of-variable/Comment by paragon for <p>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 <code>myVar</code> that behaves something like this:</p>
<pre><code>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]
a
sage: p.operands()[0].operands[0].get_data()
"some data about a"
</code></pre>
<p>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. </p>
<p>Is something like this possible in sage?</p>
http://ask.sagemath.org/question/8109/making-my-own-special-type-of-variable/?comment=21750#post-id-21750I 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.Wed, 11 May 2011 08:16:30 -0500http://ask.sagemath.org/question/8109/making-my-own-special-type-of-variable/?comment=21750#post-id-21750Answer by Jason Grout for <p>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 <code>myVar</code> that behaves something like this:</p>
<pre><code>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]
a
sage: p.operands()[0].operands[0].get_data()
"some data about a"
</code></pre>
<p>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. </p>
<p>Is something like this possible in sage?</p>
http://ask.sagemath.org/question/8109/making-my-own-special-type-of-variable/?answer=12343#post-id-12343This 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
2
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
'test'
sage: type(b)
<type 'sage.symbolic.expression.Expression'>
sage: type(b.pyobject())
<type 'str'>
Wed, 11 May 2011 15:45:20 -0500http://ask.sagemath.org/question/8109/making-my-own-special-type-of-variable/?answer=12343#post-id-12343Comment by paragon for <p>This is not a complete answer, but here are some useful methods of the symbolic ring and elements for retrieving wrapped python objects:</p>
<pre><code>sage: a=SR(Mod(8,3))
sage: a
2
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
'test'
sage: type(b)
<type 'sage.symbolic.expression.Expression'>
sage: type(b.pyobject())
<type 'str'>
</code></pre>
http://ask.sagemath.org/question/8109/making-my-own-special-type-of-variable/?comment=21697#post-id-21697This 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.Sat, 21 May 2011 10:42:26 -0500http://ask.sagemath.org/question/8109/making-my-own-special-type-of-variable/?comment=21697#post-id-21697Answer by Volker Braun for <p>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 <code>myVar</code> that behaves something like this:</p>
<pre><code>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]
a
sage: p.operands()[0].operands[0].get_data()
"some data about a"
</code></pre>
<p>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. </p>
<p>Is something like this possible in sage?</p>
http://ask.sagemath.org/question/8109/making-my-own-special-type-of-variable/?answer=12345#post-id-12345You 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]
a
sage: mydata[_]
'foo'
Thu, 12 May 2011 08:46:21 -0500http://ask.sagemath.org/question/8109/making-my-own-special-type-of-variable/?answer=12345#post-id-12345Comment by paragon for <p>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?</p>
<p>Since you only want to associate something to variables, you can just use a dictionary:</p>
<pre><code>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]
a
sage: mydata[_]
'foo'
</code></pre>
http://ask.sagemath.org/question/8109/making-my-own-special-type-of-variable/?comment=21698#post-id-21698Also, 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. Sat, 21 May 2011 10:40:46 -0500http://ask.sagemath.org/question/8109/making-my-own-special-type-of-variable/?comment=21698#post-id-21698Comment by paragon for <p>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?</p>
<p>Since you only want to associate something to variables, you can just use a dictionary:</p>
<pre><code>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]
a
sage: mydata[_]
'foo'
</code></pre>
http://ask.sagemath.org/question/8109/making-my-own-special-type-of-variable/?comment=21699#post-id-21699I 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. Sat, 21 May 2011 10:39:44 -0500http://ask.sagemath.org/question/8109/making-my-own-special-type-of-variable/?comment=21699#post-id-21699