1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#!/usr/bin/env python3
# -*- coding: utf-8
#
# This tests checks that basic polynomial solving is working from the Python
# module. It is not aimed at detecting all bugs, but should fail in case there
# is something that prevents the Python module to work at all.
#
# Note that while this catch almost all regressions (the bugs are usually buried
# in the C code that is tested from src/tests) it would be nice to have a complete
# test suite here.
#
# Author: Leonardo Robol <leonardo.robol@sns.it>
# Date: 13/02/2014
import mpsolve
def simple_polynomial_example():
"""Solve a very simple polynomial. """
n = 100
ctx = mpsolve.Context()
poly = mpsolve.MonomialPoly(ctx, n)
poly.set_coefficient (0, -1)
poly.set_coefficient (n, 1)
print ("x^%d - 1 roots: " % n)
for root in ctx.solve (poly):
print (" - " + str(root))
print("")
def simple_polynomial_int_coefficients():
n = 2
ctx = mpsolve.Context()
poly = mpsolve.MonomialPoly(ctx, n)
poly.set_coefficient (0, -1)
poly.set_coefficient (1, 0)
poly.set_coefficient (2, 2)
ctx.solve (poly)
print("")
def simple_polynomial_float_coefficients():
n = 3
ctx = mpsolve.Context()
poly = mpsolve.MonomialPoly(ctx, n)
poly.set_coefficient (0, -1.0)
poly.set_coefficient (1, 0.0)
poly.set_coefficient (2, 2.5)
ctx.solve (poly)
print("")
def simple_chebyshev_example():
"""Here is a simple example of a polynomial expressed in the
Chebyshev basis."""
n = 100
ctx = mpsolve.Context()
poly = mpsolve.ChebyshevPoly(ctx, n)
poly.set_coefficient (n, 1)
for root in ctx.solve (poly):
print (root)
if __name__ == "__main__":
simple_polynomial_example()
simple_polynomial_int_coefficients()
simple_polynomial_float_coefficients()
simple_chebyshev_example()
|