Ask Your Question
1

Can I use custom Constant Values in SAGE?

asked 2018-03-31 10:29:24 -0600

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' ?

like

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

Comments

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
Docstring:     
   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 14:45:15 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-03-31 19:43:12 -0600

tmonteil gravatar image

Yes, you can compare your code with the one of Sage source code : https://git.sagemath.org/sage.git/tre...

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: 2018-03-31 10:29:24 -0600

Seen: 41 times

Last updated: Mar 31