1 package JSci.maths.polynomials; 2 3 import JSci.maths.fields.Field; 4 5 6 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 23 public RealLagrangeBasis( Field.Member[] samplings ) { 24 if ( samplings == null ) { 25 throw new NullPointerException (); 26 } 27 _dim = samplings.length; 28 _samplings = samplings; 29 _samplingsX = RealPolynomialRing.toDouble( _samplings ); 30 buildBasis(); 31 } 32 33 38 public RealLagrangeBasis( double[] samplings ) { 39 if ( samplings == null ) { 40 throw new NullPointerException (); 41 } 42 _dim = samplings.length; 43 _samplingsX = samplings; 44 buildBasis(); 45 } 46 47 protected RealLagrangeBasis() { 48 } 49 50 54 public Polynomial getBasisVector( int k ) { 55 return _basis[k]; 56 } 57 58 63 public int dimension() { 64 return _dim; 65 } 66 67 71 public Field.Member[] getSamplingPoints() { 72 if ( _samplings == null ) { 73 _samplings = RealPolynomialRing.toMathDouble( _samplingsX ); 74 } 75 76 return _samplings; 77 } 78 79 88 public Polynomial superposition(Field.Member[] coeff) { 89 if ( coeff == null ) { 90 throw new NullPointerException (); 91 } 92 if ( coeff.length != _dim ) { 93 throw new IllegalArgumentException ( "Dimensions do not match" ); 94 } 95 96 double[] d = RealPolynomialRing.toDouble( coeff ); 97 98 return superposition( d ); 99 } 100 101 104 public RealPolynomial superposition( double[] c ) { 105 if ( c == null ) { 106 throw new NullPointerException (); 107 } 108 if ( c.length != _dim ) { 109 throw new IllegalArgumentException ( "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 |