2 # Copyright (C) 2012-2015 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])
33 for i in points.keys():
34 numerator=scipy.poly1d([1])
36 for j in points.keys():
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
65 return keys, [points[key] for key in keys]
69 def plot(function, start=0., stop=1., step=0.01):
71 The parameter function must be a callable.
73 arrX=numpy.arange(start, stop, step, dtype='float64')
74 # function is a callable
75 arrY=map(function,arrX)
76 pylab.plot(arrX, arrY)
81 # The points does not need to be ordered by x values
82 def TEST_lagrange_01():
83 input = {0.:5, 0.2:10, 0.9:10, 0.6:21, 1:8}
84 polynom = lagrange(input)
86 plot(function=polynom, start=0., stop=1., step=0.001)
88 def TEST_lagrange_02():
89 input = {0.:0., 0.5:1., 1.:0.}
90 polynom = lagrange(input)
92 plot(function=polynom, start=0., stop=1., step=0.001)
95 # One can create the input dictionary from arrays
96 def TEST_lagrange_usingarrays_01():
97 arrX = [0., 0.2, 0.9, 0.6, 1]
98 arrY = [5, 10, 10, 21, 8]
99 input = points_usingarray(arrX,arrY)
100 polynom = lagrange(input)
102 plot(function=polynom, start=0., stop=1., step=0.001)
104 # Another example using numpy
105 def TEST_lagrange_usingarrays_02():
106 arrX=numpy.arange(start=0., stop=1., step=0.1, dtype='float64')
107 arrY=numpy.zeros(len(arrX), dtype='float64')
109 input = points_usingarray(arrX,arrY)
110 polynom = lagrange(input)
112 plot(function=polynom, start=0., stop=1., step=0.001)
115 # One can create the input dictionary from a function applied to an
118 # simple method for mathematical functions
119 def TEST_lagrange_usingfunction_01():
120 arrX=numpy.arange(start=0., stop=1., step=0.1, dtype='float64')
121 arrY=numpy.cos(10*arrX)
122 input = points_usingarray(arrX,arrY)
123 polynom = lagrange(input)
125 plot(function=polynom, start=0., stop=1., step=0.001)
136 def TEST_lagrange_usingfunction_01():
137 arrX=numpy.arange(start=0., stop=1., step=0.1, dtype='float64')
138 input = points_usingfunction(arrX,chapeau)
139 polynom = lagrange(input)
141 plot(function=polynom, start=0., stop=1., step=0.001)
144 if __name__ == "__main__":
147 #TEST_lagrange_usingarrays_01()
148 #TEST_lagrange_usingarrays_02()
149 #TEST_lagrange_usingfunction_01()
150 #TEST_lagrange_usingfunction_01()