NZMATH  1.2.0
About: NZMATH is a Python based number theory oriented calculation system.
  Fossies Dox: NZMATH-1.2.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

ratfunc.py
Go to the documentation of this file.
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 
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)
nzmath.ring
Definition: ring.py:1
nzmath.poly.ratfunc.RationalFunction.number_of_variables
number_of_variables
Definition: ratfunc.py:41
nzmath.ring.QuotientFieldElement
Definition: ring.py:361
nzmath.poly.ring
Definition: ring.py:1
nzmath.poly.ratfunc.RationalFunction.__eq__
def __eq__(self, other)
Definition: ratfunc.py:45
nzmath.poly.ratfunc.RationalFunction.__str__
def __str__(self)
Definition: ratfunc.py:69
nzmath.ring.QuotientFieldElement.numerator
numerator
Definition: ring.py:369
nzmath.poly.ratfunc.RationalFunction.__init__
def __init__(self, *arg, **kwd)
Definition: ratfunc.py:14
nzmath.poly.ratfunc.RationalFunction.__call__
def __call__(self, *args)
Definition: ratfunc.py:60
nzmath.ring.QuotientFieldElement.denominator
denominator
Definition: ring.py:372
nzmath.poly.ratfunc.RationalFunction.__repr__
def __repr__(self)
Definition: ratfunc.py:75
nzmath.poly.ratfunc.RationalFunction.getRing
def getRing(self)
Definition: ratfunc.py:81
nzmath.poly.ratfunc.RationalFunction.__hash__
def __hash__(self)
Definition: ratfunc.py:54
nzmath.poly.ratfunc.RationalFunction
Definition: ratfunc.py:10