Ask Your Question
1

Weird behavior BuiltinFunction

asked 2012-10-05 07:22:43 +0100

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

Dear Sage,

I'm trying to program qPochhammer functions which can interact with sage in a nice way. However I encounter some weird behavior of these new BuiltinFunctions. This is what I made: from sage.symbolic.function import BuiltinFunction

class qPochhammer(BuiltinFunction):

    def __init__(self, m=1, expand_exp=False):
        self.m = m
        self.expand_exp = expand_exp
        self.has_arguments = False

        BuiltinFunction.__init__(self, 'qPochhammer(%i)' % self.m, nargs=(m+2))

    def _latex_(self):
        if self.has_arguments:
            return '(%s; %s)_{%s}' % (self.a, self.q, self.n)
        return 'qPochhammer'

    def _qPochhammer1(self, a, q, n):
        if n == 0:
            return 1
        return (1 - a*q**(n-1))*self._qPochhammer1(a, q, n-1)

    def _expand(self):
        return prod([self._qPochhammer1(elm, self.q, self.n) \
                     for elm in self.a])

    def _eval_(self, *args):
        if len(args) != self.m + 2:
            raise RuntimeError, args

        if self.expand_exp:
            return self.evaluate(args[:self.m], args[-2], args[-1])

        return None

    def _evalf_(self, *args, **kwds):
        if type(self.a) == tuple:
            return self._expand()
        return self._qPochhammer1(self.a, self.q, self.n)

    def evaluate(self, a, q, n):
        self.a = a
        self.q = q
        self.n = n

        self.has_arguments = True

        if type(self.a) == tuple:
            return self._expand()
        return self._qPochhammer1(self.a, self.q, self.n)

Now I try the following the the sage console:

sage: a, b, c, q = var('a b c q')
sage: qshift3 = qPochhammer(m=3, expand_exp=True)
sage: qshift3(a, b, c, q, 2)
(c - 1)*(b - 1)*(a - 1)*(c*q - 1)*(b*q - 1)*(a*q - 1)
sage: qshift2 = qPochhammer(m=2, expand_exp=True)
sage: qshift2(a, b, q, 2)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)

/home/noud/work/sage/local/lib/python2.7/site-packages/sage/all_cmdline.pyc in  <module>()
----> 1 
      2 
      3 
      4 
      5 

/home/noud/work/sage/local/lib/python2.7/site-packages/sage/symbolic/function.so in sage.symbolic.function.Function.__call__ (sage/symbolic/function.cpp:4941)()
    430 
    431 
--> 432 
    433 
    434 

/home/noud/work/sage/local/lib/python2.7/site-packages/sage/all_cmdline.pyc in _eval_(self, *args)
     27 
     28 
---> 29 
     30 
     31 

RuntimeError: (a, b, q, 2)

Can someone explain to my why I get a RuntimeError and how to solve this?

Best regards, Noud

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2012-10-08 12:41:49 +0100

Some debug output after the lines pasted in the question:

sage: %debug                                     
> /home/burcin/sage/ask_qpoch.py(28)_eval_()
     27         if len(args) != self.m + 2:
---> 28             raise RuntimeError, args
     29 

ipdb> print self
qPochhammer(3)
ipdb> self.name()
'qPochhammer(3)'

Note that the function in use is not qPochhammer(3). ATM, we expect each BuiltinFunction to be instantiated once. In other words, each class corresponds to exactly one symbolic function. This is evident in the check on line 827 of sage/symbolic/function.pyx. Changing this to also check for the name of the function fixes your problem.

Patch waiting for review on trac ticket #13586.

edit flag offensive delete link more

Comments

Nice! Thank you for this fix!

Noud gravatar imageNoud ( 2012-10-10 08:20:46 +0100 )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

Stats

Asked: 2012-10-05 07:22:43 +0100

Seen: 410 times

Last updated: Oct 08 '12