Ask Your Question

method overloading error?

asked 2012-01-18 13:51:49 -0500

araichev gravatar image

updated 2012-01-18 13:57:03 -0500

Hi all:

I made a class called RationalExpression that encodes a rational expression (ratio of polynomials). It contains a class method called 'numerator' that returns the numerator of a RationalExpression instance, but the method doesn't work when i work with a multivariate polynomial ring. The 'numerator' method appears to clash with another 'numerator' method. Do you know how i can fix my bug?

Here's the relevant code snippet from /Users/arai021/Dropbox/sage_projects/

class RationalExpression(object):
    Represents a rational expression `P/(Q_1^e_1 \cdots Q_m^e_m)` by storing the
    parts `P` and `[[Q_1,e_1], \ldots, [Q_m,e_m]]`, where
   `P, Q_1, \ldots, Q_m` are elements of a common polynomial ring, and
   `e_1, \ldots, e_m` are positive integers.
    def __init__(self, num, *denoms):
        self.numerator = num
        self.base_ring = num.parent() 
        self.denominators = sorted(denoms) 

    def __str__(self):
        return str([self.numerator, self.denominators])

    def numerator(self):
        return self.numerator

And here's a failing example:

sage: attach("/Users/arai021/Dropbox/sage_projects/")
sage: P = x*y**2
sage: Qs = [x*y,3], [x,1]
sage: f = RationalExpression(P, *Qs)
sage: print f
[x*y^2, [[x, 1], [x*y, 3]]]
sage: type(f)
<class '__main__.RationalExpression'>
sage: f.numerator()
Traceback (most recent call last):    print f
  File "", line 1, in <module>

  File "/private/var/folders/bc/bc2aJOjCE20MqpWiFIh58++++TQ/-Tmp-/tmpcbitNd/", line 10, in <module>
    exec compile(u'print f.numerator()' + '\n', '', 'single')
  File "", line 1, in <module>

  File "multi_polynomial_libsingular.pyx", line 2025, in     sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular.__call__ (sage/rings/polynomial/multi_polynomial_libsingular.cpp:14808)
TypeError: number of arguments does not match number of variables in parent
edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2012-01-18 15:35:01 -0500

DSM gravatar image

updated 2012-01-18 15:35:22 -0500

I think I see the problem. You're using numerator for two things: the method numerator, and the instance variable holding the num value.

IOW when __init__ gets executed, you're replacing the method .numerator() with the polynomial in the line "self.numerator = num". Change the name of the stored value and you should be fine.

edit flag offensive delete link more


Oh, woops. Thanks! That worked.

araichev gravatar imagearaichev ( 2012-01-22 10:13:06 -0500 )edit

Your Answer


Add Answer

Question Tools

1 follower


Asked: 2012-01-18 13:51:49 -0500

Seen: 366 times

Last updated: Jan 18 '12