"Fossies" - the Fresh Open Source Software Archive

Member "NZMATH-1.2.0/nzmath/poly/ratfunc.py" (19 Nov 2012, 2800 Bytes) of package /linux/misc/old/NZMATH-1.2.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "ratfunc.py" see the Fossies "Dox" file reference documentation.

    1 """
    2 rational functions
    3 """
    4 
    5 from __future__ import division
    6 import nzmath.ring as ring
    7 import nzmath.poly.ring as poly_ring
    8 
    9 
   10 class RationalFunction(ring.QuotientFieldElement):
   11     """
   12     The class of rational functions.
   13     """
   14     def __init__(self, *arg, **kwd):
   15         if len(arg) == 1:
   16             if isinstance(arg[0], RationalFunction):
   17                 numerator = arg[0].numerator
   18                 denominator = arg[0].denominator
   19             else:
   20                 numerator = arg[0]
   21                 if "denominator" in kwd:
   22                     denominator = kwd["denominator"]
   23                 else:
   24                     denominator = self.numerator.getRing().one
   25         elif len(arg) == 2:
   26             numerator = arg[0]
   27             denominator = arg[1]
   28         elif len(kwd) > 0:
   29             if "numerator" in kwd:
   30                 numerator = kwd["numerator"]
   31             else:
   32                 raise ValueError("numerator must be specified.")
   33             if "denominator" in kwd:
   34                 denominator = kwd["denominator"]
   35             else:
   36                 denominator = self.numerator.getRing().one
   37         else:
   38             raise ValueError("numerator must be specified.")
   39         ring.QuotientFieldElement.__init__(self, numerator, denominator)
   40         if self.numerator.number_of_variables == self.denominator.number_of_variables:
   41             self.number_of_variables = self.numerator.number_of_variables
   42         else:
   43             raise TypeError("numerator and denominator are inconsistent")
   44 
   45     def __eq__(self, other):
   46         """
   47         equality test
   48         """
   49         try:
   50             return ring.QuotientFieldElement.__eq__(self, other)
   51         except AttributeError:
   52             return NotImplemented
   53 
   54     def __hash__(self):
   55         try:
   56             return ring.QuotientFieldElement.__hash__(self)
   57         except AttributteError:
   58             return NotImplemented
   59 
   60     def __call__(self, *args):
   61         """
   62         evaluation
   63 
   64         The type of args depends on the type of polynomials
   65         representing numerator and denominator.
   66         """
   67         return self.numerator(*args) / self.denominator(*args)
   68 
   69     def __str__(self):
   70         """
   71         Return a simple string
   72         """
   73         return str(self.numerator) + " / " + str(self.denominator)
   74 
   75     def __repr__(self):
   76         """
   77         Return a string representation.
   78         """
   79         return "%s(%s, %s)" % (self.__class__.__name__, repr(self.numerator), repr(self.denominator))
   80 
   81     def getRing(self):
   82         """
   83         Return a ring to which the rational function belongs.
   84         """
   85         nring = self.numerator.getCoefficientRing()
   86         if not nring.isfield():
   87             nring = nring.getQuotientField()
   88         return poly_ring.RationalFunctionField(nring, self.number_of_variables)