Revision history [back]

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?

 2 retagged Kelvin Li 503 ●5 ●12 ●17

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?