Sphinx API Example 0.1 documentation

quadratic

Contents

quadratic

This is an example on how to document Python modules using doc strings and the sphinx tool. The doc strings can make use of the reStructuredText format, see http://docutils.sourceforge.net/docs/user/rst/quickstart.html. It is recommended to document Python modules according to the rules of the numpy package. See also A Guide to NumPy/SciPy Documentation.

quadratic.roots(a, b, c, verbose=False)[source]

Return the two roots in the quadratic equation:

a*x**2 + b*x + c = 0

or written with math typesetting

\[ax^2 + bx + c = 0\]

The returned roots are real or complex numbers, depending on the values of the arguments a, b, and c.

Parameters :

a: int, real, complex :

coefficient of the quadratic term

b: int, real, complex :

coefficient of the linear term

c: int, real, complex :

coefficient of the constant term

verbose: bool, optional :

prints the quantity b**2 - 4*a*c and if the roots are real or complex

Returns :

root1, root2: real, complex :

the roots of the quadratic polynomial.

Raises :

ValueError: :

when a is zero

See also

Quadratic
which is a class for quadratic polynomials that also has a Quadratic.roots() method for computing the roots of a quadratic polynomial. There is also a class linear.Linear in the module linear.

Notes

The algorithm is a straightforward implementation of a very well known formula [R1].

References

[R1](1, 2) Any textbook on mathematics or Wikipedia.

Examples

>>> roots(-1, 2, 10)
(-5.3166247903553998, 1.3166247903553998)
>>> roots(-1, 2, -10)
((-2-3j), (-2+3j))

Alternatively, we can in a doc string list the arguments and return values in a table

Parameter Type Description
a float/complex coefficient for quadratic term
b float/complex coefficient for linear term
c float/complex coefficient for constant term
r1, r2 float/complex return: the two roots of the quadratic polynomial
class quadratic.Quadratic(a, b, c)[source]

Representation of a quadratic polynomial:

\[ax^2 + bx + c\]

Example:

>>> q = Quadratic(a=2, b=4, c=-16)
>>> print q
2*x**2 + 4*x - 16
>>> r1, r2 = q.roots()
>>> r1
2.0
>>> r2
-4.0
>>> q(r1), q(r2)  # check
(0.0, 0.0)
>>> repr(q)
'Quadratic(a=2, b=4, c=-16)'

Methods

__call__(x)
roots() Return the two roots of the quadratic polynomial.
value(x) Return the value of the quadratic polynomial for x.
__call__(x)[source]
__init__(a, b, c)[source]

The arguments a, b, and c are coefficients in the quadratic polynomial:

a*x**2 + b*x + c

or

\[ax^2 + bx + c\]
Argument Type Description
a float/complex coefficient for quadratic term
b float/complex coefficient for linear term
c float/complex coefficient for constant term
Raises :

ValueError: :

When a is too close to zero.

__module__ = 'quadratic'
__repr__()[source]
__str__()[source]
roots()[source]

Return the two roots of the quadratic polynomial. The roots are real or complex, depending on the coefficients in the polynomial.

Let us define a quadratic polynomial:

>>> q = Quadratic(a=2, b=4, c=-16)
>>> print q
2*x**2 + 4*x - 16

The roots are then found by

>>> r1, r2 = q.roots()
>>> r1
2.0
>>> r2
-4.0
value(x)[source]

Return the value of the quadratic polynomial for x.

Example:

>>> q = Quadratic(a=2, b=4, c=-16)
>>> print q
2*x**2 + 4*x - 16
>>> q(1)
-10
>>> q(-2.5)
-13.5
class quadratic.Cubic(a, b, c, d)[source]

Bases: quadratic.Quadratic

Evaluation of a cubic polynomial \(ax^3 + bx^2 + cx + d\).

Methods

__call__(x)
roots()
value(x)
__call__(x)
__init__(a, b, c, d)[source]
__module__ = 'quadratic'
__repr__()
__str__()[source]
roots()[source]
value(x)[source]

Contents