Ask Your Question

MadG's profile - activity

2018-04-01 02:43:20 +0200 received badge  Student (source)
2018-03-31 20:08:51 +0200 asked a question how to define constants?

is this a good way to define constants?

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()
2018-03-31 20:08:51 +0200 asked a question Can I use custom Constant Values in SAGE?

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?