File: t_UniVariatePolynomial_std.py

package info (click to toggle)
openturns 1.26-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,708 kB
  • sloc: cpp: 261,605; python: 67,030; ansic: 4,378; javascript: 406; sh: 185; xml: 164; makefile: 101
file content (73 lines) | stat: -rwxr-xr-x 2,039 bytes parent folder | download
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
#! /usr/bin/env python

import openturns as ot
import openturns.testing as ott

ot.TESTPREAMBLE()


# Default constructor: null polynomial
P = ot.UniVariatePolynomial()
point = 1.2
print("P=", P)
print("P(", point, ")=", P(point))

# Constructor: construct the 1D polynomial with a vector of Point
# e.g. UniVariatePolynomial P1 ( coefficients )
coefficients = ot.Point(6)
coefficients[0] = 1.3
coefficients[1] = 2.4
coefficients[2] = 2.0
coefficients[3] = -0.5
coefficients[4] = 4.0
coefficients[5] = 0.0

print("Initial coefficients=", coefficients)
P1 = ot.UniVariatePolynomial(coefficients)
print("P1=", repr(P1))
print("P1=", P1)
print("P1=", P1.__str__("Y", ""))
print("P1 degree=", P1.getDegree())
print("P1 roots=", P1.getRoots())
print("P1(", point, ")=", P1(point))

# Multiplication by a scalar (postfix only)
print("P1*2.0 =", P1 * 2.0)

# P1.incrementDegree(incDeg) : multiply P1 by X^incDeg
print("P1.incrementDegree(3)=", P1.incrementDegree(3))
print("P1.incrementDegree(0)=", P1.incrementDegree(0))
print("P1.incrementDegree(1)=", P1.incrementDegree(1))
print("P1.incrementDegree()=", P1.incrementDegree())

# Sum of two polynomials
coefficients = ot.Point(3)
coefficients[0] = -2.5
coefficients[1] = 3.5
coefficients[2] = -1.2
P2 = ot.UniVariatePolynomial(coefficients)
print("P2=", P2)
print("P1+P2=", P1 + P2)

# Subtraction of two polynomials
print("P1-P2=", P1 - P2)

# Multiplication of two polynomials
print("P1*P2=", P1 * P2)

# Multiplication of two polynomials using FFT
print("P1*P2=", P1 * P2)

# Create a polynomial with a leading term equal to 0 (do not print, then
# a first term with unity coefficient (hide the coefficient when printing),
# and then several coefficients equal to 1 or 0
P1 = ot.UniVariatePolynomial([0.0, 1.0, -2.0, -1.0, 0.0, 1.0])
print("P1=", P1)
print("(repr) P1=", repr(P1))
print("(html) P1=", P1._repr_html_())

# Evaluate a polynomial over a sample
x = [[1.0], [2.0], [3.0], [4.0], [5.0]]
y = P2(x)
yRef = [[-0.2], [-0.3], [-2.8], [-7.7], [-15.0]]
ott.assert_almost_equal(y, yRef)