2 # Copyright (C) 2012-2022 CEA/DEN, EDF R&D
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 This returns a polynom that fits the points specified in the input
25 dictionary (lagrange interpolation). In this dictionary, the keys
26 are x value and the values are y corresponding values
27 (i.e. y=polynom(x)). The polynom is a scipy polynom and then is a
28 callable (can be used as a function).
30 tmp = scipy.poly1d([0])
31 result=scipy.poly1d([0])
34 numerator=scipy.poly1d([1])
38 tmp = scipy.poly1d([1,-j])
39 numerator = numerator * tmp
40 denom = denom * (i - j)
41 tmp = (numerator/denom) * points.get(i)
46 def points_usingfunction(arrX,function):
49 points[x] = function(x)
52 def points_usingarray(arrX,arrY):
54 for i in range(len(arrX)):
61 # Sort this dictionary by keys and returns 2 lists, the list of X
62 # and the list of Y, the whole ordered by X
63 keys = sorted(points.keys())
64 return keys, [points[key] for key in keys]
68 def plot(function, start=0., stop=1., step=0.01):
70 The parameter function must be a callable.
72 arrX=numpy.arange(start, stop, step, dtype='float64')
73 # function is a callable
74 arrY=list(map(function,arrX))
75 pylab.plot(arrX, arrY)
80 # The points does not need to be ordered by x values
81 def TEST_lagrange_01():
82 input = {0.:5, 0.2:10, 0.9:10, 0.6:21, 1:8}
83 polynom = lagrange(input)
85 plot(function=polynom, start=0., stop=1., step=0.001)
87 def TEST_lagrange_02():
88 input = {0.:0., 0.5:1., 1.:0.}
89 polynom = lagrange(input)
91 plot(function=polynom, start=0., stop=1., step=0.001)
94 # One can create the input dictionary from arrays
95 def TEST_lagrange_usingarrays_01():
96 arrX = [0., 0.2, 0.9, 0.6, 1]
97 arrY = [5, 10, 10, 21, 8]
98 input = points_usingarray(arrX,arrY)
99 polynom = lagrange(input)
101 plot(function=polynom, start=0., stop=1., step=0.001)
103 # Another example using numpy
104 def TEST_lagrange_usingarrays_02():
105 arrX=numpy.arange(start=0., stop=1., step=0.1, dtype='float64')
106 arrY=numpy.zeros(len(arrX), dtype='float64')
108 input = points_usingarray(arrX,arrY)
109 polynom = lagrange(input)
111 plot(function=polynom, start=0., stop=1., step=0.001)
114 # One can create the input dictionary from a function applied to an
117 # simple method for mathematical functions
118 def TEST_lagrange_usingfunction_01():
119 arrX=numpy.arange(start=0., stop=1., step=0.1, dtype='float64')
120 arrY=numpy.cos(10*arrX)
121 input = points_usingarray(arrX,arrY)
122 polynom = lagrange(input)
124 plot(function=polynom, start=0., stop=1., step=0.001)
135 def TEST_lagrange_usingfunction_01():
136 arrX=numpy.arange(start=0., stop=1., step=0.1, dtype='float64')
137 input = points_usingfunction(arrX,chapeau)
138 polynom = lagrange(input)
140 plot(function=polynom, start=0., stop=1., step=0.001)
143 if __name__ == "__main__":
146 #TEST_lagrange_usingarrays_01()
147 #TEST_lagrange_usingarrays_02()
148 #TEST_lagrange_usingfunction_01()
149 #TEST_lagrange_usingfunction_01()