Package hedge :: Module polynomial
[hide private]
[frames] | no frames]

Source Code for Module hedge.polynomial

  1  """Jacobi polynomials and Vandermonde matrices.""" 
  2   
  3  from __future__ import division 
  4   
  5  __copyright__ = "Copyright (C) 2007 Andreas Kloeckner" 
  6   
  7  __license__ = """ 
  8  This program is free software: you can redistribute it and/or modify 
  9  it under the terms of the GNU General Public License as published by 
 10  the Free Software Foundation, either version 3 of the License, or 
 11  (at your option) any later version. 
 12   
 13  This program is distributed in the hope that it will be useful, 
 14  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  GNU General Public License for more details. 
 17   
 18  You should have received a copy of the GNU General Public License 
 19  along with this program.  If not, see U{http://www.gnu.org/licenses/}. 
 20  """ 
 21   
 22   
 23   
 24   
 25   
 26  import numpy 
 27  import hedge._internal 
 28  import numpy.linalg as la 
 29   
 30   
 31   
 32   
 33  JacobiFunction = hedge._internal.JacobiPolynomial 
 34  DiffJacobiFunction = hedge._internal.DiffJacobiPolynomial 
 35   
 36   
 37   
 38   
39 -class LegendreFunction(JacobiFunction):
40 - def __init__(self, N):
41 JacobiFunction.__init__(self, 0, 0, N)
42
43 -class DiffLegendreFunction(DiffJacobiFunction):
44 - def __init__(self, N):
45 DiffJacobiFunction.__init__(self, 0, 0, N)
46 47 48 49
50 -def generic_vandermonde(points, functions):
51 """Return a Vandermonde matrix. 52 53 The Vandermonde Matrix is given by M{V_{i,j} := f_j(x_i)} 54 where C{functions} is the list of M{f_j} and points is 55 the list of M{x_i}. 56 """ 57 v = numpy.zeros((len(points), len(functions))) 58 for i, x in enumerate(points): 59 for j, f in enumerate(functions): 60 v[i,j] = f(x) 61 return v
62 63 64 65
66 -def generic_multi_vandermonde(points, functions):
67 """Return multiple Vandermonde matrices. 68 69 The Vandermonde Matrix is given by M{V_{i,j} := f_j(x_i)} 70 where C{functions} is the list of M{f_j} and points is 71 the list of M{x_i}. 72 73 The functions M{f_j} are multi-valued (i.e. return iterables), and one 74 matrix is returned for each return value. 75 """ 76 count = len(functions[0](points[0])) 77 result = [numpy.zeros((len(points), len(functions))) for n in range(count)] 78 79 for i, x in enumerate(points): 80 for j, f in enumerate(functions): 81 for n, f_n in enumerate(f(x)): 82 result[n][i,j] = f_n 83 return result
84 85 86 87
88 -def legendre_vandermonde(points, N):
89 return generic_vandermonde(points, 90 [LegendreFunction(i) for i in range(N+1)])
91 92 93 94
95 -def monomial_vdm(levels):
96 class Monomial: 97 def __init__(self, expt): 98 self.expt = expt
99 def __call__(self, x): 100 return x**self.expt 101 102 return generic_vandermonde(levels, 103 [Monomial(i) for i in range(len(levels))]) 104 105 106 107
108 -def make_interpolation_coefficients(levels, tap):
109 point_eval_vec = numpy.array([ tap**n for n in range(len(levels))]) 110 return la.solve(monomial_vdm(levels).T, point_eval_vec)
111