KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > polynomials > RealLagrangeBasis


1 package JSci.maths.polynomials;
2
3 import JSci.maths.fields.Field;
4
5
6 /**
7  * The Lagrange Basis for real polynomials.
8  * For a given set of sampling points {x_1, ..., x_n},
9  * the corresponding Lagrange polynomials are L_k = \kronecker_kj \forall j=1..n.
10  * The explicit form is
11  *
12  * L_k= \prod_{j=0, j\neq k}^n \frac{t-t_j}{t_k-t_j}
13  *
14  * @author b.dietrich
15  */

16 public class RealLagrangeBasis implements PolynomialBasis {
17     protected double[] _samplingsX;
18     protected int _dim;
19     private RealPolynomial[] _basis;
20     private Field.Member[] _samplings;
21
22     /** Creates a new instance of LagrangeBasis for given sampling points*/
23     public RealLagrangeBasis( Field.Member[] samplings ) {
24         if ( samplings == null ) {
25             throw new NullPointerException JavaDoc();
26         }
27         _dim = samplings.length;
28         _samplings = samplings;
29         _samplingsX = RealPolynomialRing.toDouble( _samplings );
30         buildBasis();
31     }
32
33     /**
34      * Creates a new RealLagrangeBasis object.
35      *
36      * @param samplings
37      */

38     public RealLagrangeBasis( double[] samplings ) {
39         if ( samplings == null ) {
40             throw new NullPointerException JavaDoc();
41         }
42         _dim = samplings.length;
43         _samplingsX = samplings;
44         buildBasis();
45     }
46
47     protected RealLagrangeBasis() {
48     }
49
50     /**
51      * The basis vector as described above
52      * @param k
53      */

54     public Polynomial getBasisVector( int k ) {
55         return _basis[k];
56     }
57
58     /**
59      * The dimension ( # of sampling points)
60      * @return the dimension
61      *
62      */

63     public int dimension() {
64         return _dim;
65     }
66
67     /**
68      * The sampling points used in constructor
69      * @return the sampling points
70      */

71     public Field.Member[] getSamplingPoints() {
72         if ( _samplings == null ) {
73             _samplings = RealPolynomialRing.toMathDouble( _samplingsX );
74         }
75
76         return _samplings;
77     }
78
79     /**
80      * Make a superposition of basis-vectors for a given set of coefficients.
81      * Due to the properties of a lagrange base, the result is the interpolating
82      * polynomial with values coeff[k] at sampling point k
83      * @param coeff in this case the values of the interpolation problem
84      *
85      * @return the interpolating polynomial
86      *
87      */

88     public Polynomial superposition(Field.Member[] coeff) {
89         if ( coeff == null ) {
90             throw new NullPointerException JavaDoc();
91         }
92         if ( coeff.length != _dim ) {
93             throw new IllegalArgumentException JavaDoc( "Dimensions do not match" );
94         }
95
96         double[] d = RealPolynomialRing.toDouble( coeff );
97
98         return superposition( d );
99     }
100
101     /**
102      * Same as above, but type-safe.
103      */

104     public RealPolynomial superposition( double[] c ) {
105         if ( c == null ) {
106             throw new NullPointerException JavaDoc();
107         }
108         if ( c.length != _dim ) {
109             throw new IllegalArgumentException JavaDoc( "Dimension of basis is " + _dim + ". Got "
110                                                 + c.length + " coefficients" );
111         }
112
113         RealPolynomial rp = (RealPolynomial) RealPolynomialRing.getInstance().zero();
114         for ( int k = 0; k < _dim; k++ ) {
115             RealPolynomial b = (RealPolynomial) getBasisVector( k );
116             RealPolynomial ba = b.scalarMultiply( c[k] );
117             rp = (RealPolynomial) rp.add( ba );
118         }
119
120         return rp;
121     }
122
123     
124     protected void buildBasis() {
125         _basis = new RealPolynomial[_dim];
126         for ( int k = 0; k < _dim; k++ ) {
127             _basis[k] = (RealPolynomial) RealPolynomialRing.getInstance().one();
128
129             double fac = 1.;
130             for ( int j = 0; j < _dim; j++ ) {
131                 if ( j == k ) {
132                     continue;
133                 } else {
134                     RealPolynomial n = new RealPolynomial( new double[] { -_samplingsX[j], 1. } );
135                     _basis[k] = (RealPolynomial) _basis[k].multiply( n );
136                     fac *= ( _samplingsX[k] - _samplingsX[j] );
137                 }
138             }
139
140             _basis[k] = _basis[k].scalarDivide( fac );
141         }
142     }
143 }
144
Popular Tags