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