Ask Your Question
0

method overloading error?

asked 2012-01-18 20:51:49 +0200

araichev gravatar image

updated 2012-01-18 20:57:03 +0200

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/pfd.py:

class RationalExpression(object):
    r"""
    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/pfd.py")
sage: R.<x,y>= PolynomialRing(QQ)
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/___code___.py", 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
0

answered 2012-01-18 22:35:01 +0200

DSM gravatar image

updated 2012-01-18 22:35:22 +0200

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

Comments

Oh, woops. Thanks! That worked.

araichev gravatar imagearaichev ( 2012-01-22 17:13:06 +0200 )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

1 follower

Stats

Asked: 2012-01-18 20:51:49 +0200

Seen: 607 times

Last updated: Jan 18 '12