Ask Your Question

[style] Choose between multiple inheritance

asked 2013-11-03 07:22:42 -0500

Noud gravatar image

updated 2013-11-06 00:52:12 -0500

Dear Sage people,

I want to create a new (mathematical) object that sometimes is an Expression and sometimes a SymbolicFunction, depending on the arguments. You can think of this for example like $f(a, b, t) = \int_0^t a^b e^{-x^2} dx$. For special values of $t$ I would like to see it as an Expression ($t=0$ or $t=\infty$), but in all other cases I want it to be a BuiltinFunction (or something alike).

In Sage I can do something like:

class MyObjectExpression(Expression):
    def __init__(self, a, b, t):
        Expression.__init__(self, integral(a**b*e**(-x**2), x, 0, t))
    # More (override) stuff below

class MyObjectFunction(BuiltinFunction):
    def __init__(self, a, b, t):
        BuiltinFunction.__init__(self, 'f(a,b,t)', nargs=1)
    # More (override) stuff below

def MyObject(a, b, t):
    if t == 0 or t == infty:
        return MyObjectExpression(a, b, t)
        return MyObjectFunction(a, b, t)

Is it possible to combine these three things into one class? So I want to create a class which is sometimes an Expression and sometimes an much more abstract class, is this possible?

Best, Noud

Edit: What I actually want to do is programming Askey-Wilson polynomials and give them extra options, like a three term recurrence relation. But this depends on $n$. I already programmed this.

class Askey_Wilson(SageObject):
    def __init__(self, SR, n, z, a, b, c, d, q):
        self.n = n
        self.z = z
        self.q = q
        self.a = a
        self.b = b
        self.c = c
        self.d = d
        self.param = [a, b, c, d]

        if self.n in ZZ:
            self.I = self.evaluate()
            self.I = var('askey_wilson')

    def __repr__(self):
        return 'p_%i(%s;%s,%s,%s,%s|%s)' % (
            self.n, self.z, self.a, self.b, self.c, self.d, self.q

    def evaluate(self):
        n, q, z, a, b, c, d = [self.n, self.q, self.z] + self.param

        lc = qPochhammerSymbol(SR, [a*b, a*c, a*d], q, n) / a**n
        poly = BasicHypergeometricSeries(SR,
            [q**(-n), a*b*c*d*q**(n-1), a*z, a*z**(-1)],
            [a*b, a*c, a*d], q, q)
        return lc*poly

    def three_term_recurrence(self):
        A, B, C = 0, 0, 0
        # compute three term recurrence relation
        return A, B, C

But now every time I want to know the explicit value of the Askey-Wilson polynomials I have to call askey_wilson.I. I want to get rid of the I.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-11-03 09:48:34 -0500

tmonteil gravatar image

updated 2013-11-03 09:49:59 -0500

Inheritance is used when you want to build a class that is a particular case of an existing class (by adding some features). In your case, this looks more like a "mix", hence inheritance seems not very appropriate.

I am not sure i understand your wish clearly, does something along this way makes sense for you ?

class MyObject(SageObject):
    def __init__(self, a, b, t):
        self.a = a
        self.b = b
        self.t = t
        if t == 0 or t == infinity:
            self.I = var('t') == t # self.I takes the value one of the expressions "(t == 0)" or "(t == infinity)"
            self.I = integral(a**b*e**(-x**2), x, 0, t)

Perhaps could you describe the behaviour you want to see as an example of creation and interaction with the object, so we can help you more.

edit flag offensive delete link more


I actually want to implement Askey-Wilson polynomials (, but I want to give the polynomials some extra functions so that you can do something like: Askey_Wilson(n, z, a, b, c, d, q).three_term() and you get a triple with the three term recurrence relation. So if n is an integer I can use Expression, but if n is a variable I want to use an other object. Your answer does work, except that I don't want to call askey_wilson.I all the time, but just without the I.

Noud gravatar imageNoud ( 2013-11-06 00:46:53 -0500 )edit

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: 2013-11-03 07:22:42 -0500

Seen: 71 times

Last updated: Nov 06 '13