Ask Your Question

Can I use custom Constant Values in SAGE?

asked 2018-03-31 17:29:24 +0200

MadG gravatar image

show(pi) will show a nice representation of pi, while pi.n() will yield a useable value

how am I supposed to achieve the same functionality for a custom 'symbol' ?


Az = 5.5 # cm the surface area of my Cylinder

but showing 'Az' as representation, not the value

I solved this with a custom Class, but this feels wrong:

class Myconst(Constant):
    def __init__(self, name='Az', value=1.0):
        Constant.__init__(self, name,
                          latex=None, domain='positive')
        self.v = value
    def __float__(self):
        return self.v

    def _real_double_(self, R):
        return self.v

    def _mpfr_(self,R):
        return self.v
Az = Myconst(name='Az', value = 2.0).expression()
#As = Myconst(name='As', value = 5.0).expression()

now these work like I want them to :) Az.n() show(1/Az)

But is this the intended approach?

edit retag flag offensive close merge delete


If it is a short life constant, than the above is ok.

Else, one has to integrate the constant with the sage code, this does not seem to be easy:

sage: pi.parent()
Symbolic Ring
sage: pi?
Type:           Expression
String form:    pi
Length:         0
File:           /usr/lib/python2.7/site-packages/sage/symbolic/expression.pyx
   Nearly all expressions are created by calling
   new_Expression_from_*, but we need to make sure this at least does
   not leave self._gobj uninitialized and segfault.

and so on.

pi?? gives more information. (Or just open the py-file...)

Note: To have in-line code displayed as code, use the ticks to markdown. (Or mark it in editor and press either Control+K or that button with 101 and 010.)

dan_fulea gravatar imagedan_fulea ( 2018-03-31 21:45:15 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2018-04-01 02:43:12 +0200

tmonteil gravatar image

Yes, you can compare your code with the one of Sage source code :

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


Asked: 2018-03-31 17:29:24 +0200

Seen: 175 times

Last updated: Apr 01 '18