You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ps...@apache.org on 2006/12/19 23:11:11 UTC

svn commit: r488828 [1/6] - in /jakarta/commons/proper/math/trunk/src/mantissa: src/org/spaceroots/mantissa/ src/org/spaceroots/mantissa/algebra/ src/org/spaceroots/mantissa/fitting/ src/org/spaceroots/mantissa/functions/scalar/ src/org/spaceroots/mant...

Author: psteitz
Date: Tue Dec 19 14:11:07 2006
New Revision: 488828

URL: http://svn.apache.org/viewvc?view=rev&rev=488828
Log:
Updated contributed sources - Mantissa 7 upgrade. JIRA: MATH-162

Modified:
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/MessagesResources.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/MessagesResources_fr.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/Chebyshev.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/Hermite.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/Laguerre.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/Legendre.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/OrthogonalPolynomial.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/Polynomial.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/PolynomialFraction.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/RationalNumber.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/fitting/AbstractCurveFitter.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/fitting/F2FP2Iterator.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/fitting/HarmonicCoefficientsGuesser.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/fitting/HarmonicFitter.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/fitting/PolynomialFitter.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/functions/scalar/ComputableFunction.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/functions/scalar/SampledFunction.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/functions/vectorial/ComputableFunction.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/functions/vectorial/SampledFunction.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/functions/vectorial/VectorialValuedPair.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/geometry/Rotation.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/geometry/RotationOrder.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/geometry/Vector3D.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/linalg/DiagonalMatrix.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/linalg/GeneralMatrix.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/linalg/GeneralSquareMatrix.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/linalg/LowerTriangularMatrix.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/linalg/Matrix.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/linalg/NonNullRange.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/linalg/SquareMatrix.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/linalg/SymetricalMatrix.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/linalg/UpperTriangularMatrix.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/ode/AbstractStepInterpolator.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/ode/ClassicalRungeKuttaIntegrator.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/ode/DormandPrince54Integrator.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/ode/DormandPrince54StepInterpolator.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/ode/DormandPrince853Integrator.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/ode/DummyStepInterpolator.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/ode/EulerIntegrator.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/ode/GillIntegrator.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/ode/GraggBulirschStoerIntegrator.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/ode/HighamHall54Integrator.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/ode/MidpointIntegrator.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/ode/StepNormalizer.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/ode/SwitchState.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/ode/SwitchingFunction.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/ode/ThreeEighthesIntegrator.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/optimization/DirectSearchOptimizer.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/optimization/MultiDirectional.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/optimization/NelderMead.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/optimization/PointCostPair.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/quadrature/vectorial/EnhancedSimpsonIntegrator.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/quadrature/vectorial/EnhancedSimpsonIntegratorSampler.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/quadrature/vectorial/RiemannIntegrator.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/quadrature/vectorial/RiemannIntegratorSampler.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/quadrature/vectorial/TrapezoidIntegrator.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/quadrature/vectorial/TrapezoidIntegratorSampler.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/random/CorrelatedRandomVectorGenerator.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/random/GaussianRandomGenerator.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/random/NormalizedRandomGenerator.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/random/UncorrelatedRandomVectorGenerator.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/random/UniformRandomGenerator.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/random/VectorialSampleStatistics.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/utilities/ArrayMapper.java
    jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/utilities/MappableArray.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/algebra/ChebyshevTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/algebra/HermiteTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/algebra/LaguerreTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/algebra/LegendreTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/algebra/PolynomialDoubleTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/algebra/PolynomialFractionTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/algebra/PolynomialRationalTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/algebra/RationalNumberTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/estimation/AllTests.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/estimation/GaussNewtonEstimatorTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/estimation/LevenbergMarquardtEstimatorTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/estimation/WeightedMeasurementTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/fitting/AbstractCurveFitterTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/fitting/HarmonicFitterTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/fitting/PolynomialFitterTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/functions/scalar/BasicSampledFunctionIteratorTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/functions/scalar/ComputableFunctionSamplerTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/functions/vectorial/BasicSampledFunctionIteratorTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/functions/vectorial/ComputableFunctionSamplerTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/functions/vectorial/VectorialValuedPairTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/geometry/RotationTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/geometry/Vector3DTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/linalg/DiagonalMatrixTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/linalg/GeneralMatrixTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/linalg/GeneralSquareMatrixTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/linalg/LowerTriangularMatrixTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/linalg/UpperTriangularMatrixTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/ode/ClassicalRungeKuttaIntegratorTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/ode/ContinuousOutputModelTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/ode/DormandPrince54IntegratorTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/ode/DormandPrince853IntegratorTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/ode/EulerIntegratorTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/ode/EulerStepInterpolatorTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/ode/FirstOrderConverterTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/ode/GillIntegratorTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/ode/GraggBulirschStoerIntegratorTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/ode/HighamHall54IntegratorTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/ode/MidpointIntegratorTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/ode/StepNormalizerTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/ode/TestProblem1.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/ode/TestProblem2.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/ode/TestProblem3.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/ode/TestProblem4.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/ode/TestProblemAbstract.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/ode/ThreeEighthesIntegratorTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/optimization/MultiDirectionalTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/optimization/NelderMeadTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/quadrature/scalar/GaussLegendreIntegratorTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/quadrature/vectorial/GaussLegendreIntegratorTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/random/CorrelatedRandomVectorGeneratorTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/random/ScalarSampleStatisticsTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/random/UncorrelatedRandomVectorGeneratorTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/random/VectorialSampleStatisticsTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/roots/BrentSolverTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/roots/TestProblem.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/utilities/ArrayMapperTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/utilities/MappableArrayTest.java
    jakarta/commons/proper/math/trunk/src/mantissa/tests-src/org/spaceroots/mantissa/utilities/MappableScalarTest.java

Modified: jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/MessagesResources.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/MessagesResources.java?view=diff&rev=488828&r1=488827&r2=488828
==============================================================================
--- jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/MessagesResources.java (original)
+++ jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/MessagesResources.java Tue Dec 19 14:11:07 2006
@@ -33,7 +33,7 @@
   }
 
   public Object[][] getContents() {
-    return contents;
+    return (Object[][]) contents.clone();
   }
 
   static final Object[][] contents = {

Modified: jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/MessagesResources_fr.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/MessagesResources_fr.java?view=diff&rev=488828&r1=488827&r2=488828
==============================================================================
--- jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/MessagesResources_fr.java (original)
+++ jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/MessagesResources_fr.java Tue Dec 19 14:11:07 2006
@@ -32,7 +32,7 @@
   }
 
   public Object[][] getContents() {
-    return contents;
+    return (Object[][]) contents.clone();
   }
 
   static final Object[][] contents = {

Modified: jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/Chebyshev.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/Chebyshev.java?view=diff&rev=488828&r1=488827&r2=488828
==============================================================================
--- jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/Chebyshev.java (original)
+++ jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/Chebyshev.java Tue Dec 19 14:11:07 2006
@@ -17,18 +17,15 @@
 
 package org.spaceroots.mantissa.algebra;
 
-import java.util.ArrayList;
-import java.util.List;
-
 /**
  * This class implements Chebyshev polynomials.
 
  * <p>Chebyshev polynomials can be defined by the following recurrence
  * relations:
  * <pre>
- *  T0(X)   = 1
- *  T1(X)   = X
- *  Tk+1(X) = 2X Tk(X) - Tk-1(X)
+ *  T<sub>0</sub>(X)   = 1
+ *  T<sub>1</sub>(X)   = X
+ *  T<sub>k+1</sub>(X) = 2X T<sub>k</sub>(X) - T<sub>k-1</sub>(X)
  * </pre></p>
 
  * @version $Id: Chebyshev.java 1705 2006-09-17 19:57:39Z luc $
@@ -39,67 +36,35 @@
 public class Chebyshev
   extends OrthogonalPolynomial {
 
+  /** Generator for the Chebyshev polynomials. */
+  private static final CoefficientsGenerator generator =
+    new CoefficientsGenerator(new RationalNumber(1l),
+                              new RationalNumber(0l),
+                              new RationalNumber(1l)) {
+    public void setRecurrenceCoefficients(int k) {
+      // the recurrence relation is
+      // Tk+1(X) = 2X Tk(X) - Tk-1(X)
+      setRecurrenceCoefficients(new RationalNumber(0l),
+                                new RationalNumber(2l),
+                                new RationalNumber(1l));
+    }
+  };
+
   /** Simple constructor.
    * Build a degree 0 Chebyshev polynomial
    */
   public Chebyshev() {
-    super(0, l, maxDegree);
+    super(0, generator);
   }
 
   /** Simple constructor.
    * Build a degree d Chebyshev polynomial
-   * @param d degree of the polynomial
-   */
-  public Chebyshev(int d) {
-    super(d, l, maxDegree);
-  }
-
-  /** Initialize the recurrence coefficients.
-   * The recurrence relation is
-   *  <pre>Tk+1(X) = 2X Tk(X) - Tk-1(X)</pre>
-   * @param k index of the current step
-   * @param b2k coefficient to initialize (b2k = a2k / a1k)
-   * @param b3k coefficient to initialize (b3k = a3k / a1k)
-   * @param b4k coefficient to initialize (b4k = a4k / a1k)
+   * @param degree degree of the polynomial
    */
-  protected void initRecurrenceCoefficients(int k,
-                                            RationalNumber b2k,
-                                            RationalNumber b3k,
-                                            RationalNumber b4k) {
-    b2k.reset(0l);
-    b3k.reset(2l);
-    b4k.reset(1l);
+  public Chebyshev(int degree) {
+    super(degree, generator);
   }
 
-  /** Set the maximal degree of already computed polynomials.
-   * @param d maximal degree of already computed polynomials
-   */
-  protected void setMaxDegree(int d) {
-    maxDegree = d;
-  }
-
-  private static final long serialVersionUID = 8367010179599693222L;
-
-  /** List holding the coefficients of the polynomials computed so far. */
-  private static List l;
-
-  /** Maximal degree of the polynomials computed so far. */
-  private static int maxDegree;
-
-  /** Build the first two polynomials. */
-  static {
-
-    l = new ArrayList ();
-
-    // T0(X)  = 1
-    l.add(new RationalNumber(1l));
-
-    // T1(X)  = X
-    l.add(new RationalNumber(0l));
-    l.add(new RationalNumber(1l));
-
-    maxDegree = 1;
-
-  }
+  private static final long serialVersionUID = -893367988717182601L;
 
 }

Modified: jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/Hermite.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/Hermite.java?view=diff&rev=488828&r1=488827&r2=488828
==============================================================================
--- jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/Hermite.java (original)
+++ jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/Hermite.java Tue Dec 19 14:11:07 2006
@@ -17,18 +17,15 @@
 
 package org.spaceroots.mantissa.algebra;
 
-import java.util.ArrayList;
-import java.util.List;
-
 /**
  * This class implements Hermite polynomials.
 
  * <p>Hermite polynomials can be defined by the following recurrence
  * relations:
  * <pre>
- *  H0(X)   = 1
- *  H1(X)   = 2X
- *  Hk+1(X) = 2X Hk(X) - 2k Hk-1(X)
+ *  H<sub>0</sub>(X)   = 1
+ *  H<sub>1</sub>(X)   = 2X
+ *  H<sub>k+1</sub>(X) = 2X H<sub>k</sub>(X) - 2k H<sub>k-1</sub>(X)
  * </pre></p>
 
  * @version $Id: Hermite.java 1705 2006-09-17 19:57:39Z luc $
@@ -38,67 +35,35 @@
 public class Hermite
   extends OrthogonalPolynomial {
 
+  /** Generator for the Hermite polynomials. */
+  private static final CoefficientsGenerator generator =
+    new CoefficientsGenerator(new RationalNumber(1l),
+                              new RationalNumber(0l),
+                              new RationalNumber(2l)) {
+    public void setRecurrenceCoefficients(int k) {
+      // the recurrence relation is
+      // Hk+1(X) = 2X Hk(X) - 2k Hk-1(X)
+      setRecurrenceCoefficients(new RationalNumber(0l),
+                                new RationalNumber(2l),
+                                new RationalNumber(k * 2l));
+    }
+  };
+
   /** Simple constructor.
    * Build a degree 0 Hermite polynomial
    */
   public Hermite() {
-    super(0, l, maxDegree);
+    super(0, generator);
   }
 
   /** Simple constructor.
    * Build a degree d Hermite polynomial
-   * @param d degree of the polynomial
-   */
-  public Hermite(int d) {
-    super(d, l, maxDegree);
-  }
-
-  /** Initialize the recurrence coefficients.
-   * The recurrence relation is
-   *  <pre>Hk+1(X) = 2X Hk(X) - 2k Hk-1(X)</pre>
-   * @param k index of the current step
-   * @param b2k coefficient to initialize (b2k = a2k / a1k)
-   * @param b3k coefficient to initialize (b3k = a3k / a1k)
-   * @param b4k coefficient to initialize (b4k = a4k / a1k)
+   * @param degree degree of the polynomial
    */
-  protected void initRecurrenceCoefficients(int k,
-                                            RationalNumber b2k,
-                                            RationalNumber b3k,
-                                            RationalNumber b4k) {
-    b2k.reset(0l);
-    b3k.reset(2l);
-    b4k.reset(2l * k);
+  public Hermite(int degree) {
+    super(degree, generator);
   }
 
-  /** Set the maximal degree of already computed polynomials.
-   * @param d maximal degree of already computed polynomials
-   */
-  protected void setMaxDegree(int d) {
-    maxDegree = d;
-  }
-
-  private static final long serialVersionUID = -4639726453485128770L;
-
-  /** Table holding the coefficients of the polynomials computed so far. */
-  private static List l;
-
-  /** Maximal degree of the polynomials computed so far. */
-  private static int maxDegree;
-
-  /** Build the first two polynomials. */
-  static {
-
-    l = new ArrayList ();
-
-    // H0(X)  = 1
-    l.add(new RationalNumber(1l));
-
-    // H1(X)  = 2X
-    l.add(new RationalNumber(0l));
-    l.add(new RationalNumber(2l));
-
-    maxDegree = 1;
-
-  }
+  private static final long serialVersionUID = 7910082423686662133L;
 
 }

Modified: jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/Laguerre.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/Laguerre.java?view=diff&rev=488828&r1=488827&r2=488828
==============================================================================
--- jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/Laguerre.java (original)
+++ jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/Laguerre.java Tue Dec 19 14:11:07 2006
@@ -17,18 +17,15 @@
 
 package org.spaceroots.mantissa.algebra;
 
-import java.util.ArrayList;
-import java.util.List;
-
 /**
  * This class implements Laguerre polynomials.
 
  * <p>Laguerre polynomials can be defined by the following recurrence
  * relations:
  * <pre>
- *        L0(X)   = 1
- *        L1(X)   = 1 - X
- *  (k+1) Lk+1(X) = (2k + 1 - X) Lk(X) - k Lk-1(X)
+ *        L<sub>0</sub>(X)   = 1
+ *        L<sub>1</sub>(X)   = 1 - X
+ *  (k+1) L<sub>k+1</sub>(X) = (2k + 1 - X) L<sub>k</sub>(X) - k L<sub>k-1</sub>(X)
  * </pre></p>
 
  * @version $Id: Laguerre.java 1705 2006-09-17 19:57:39Z luc $
@@ -38,68 +35,36 @@
 public class Laguerre
   extends OrthogonalPolynomial {
 
+  /** Generator for the Laguerre polynomials. */
+  private static final CoefficientsGenerator generator =
+    new CoefficientsGenerator(new RationalNumber(1l),
+                              new RationalNumber(1l),
+                              new RationalNumber(-1l)) {
+    public void setRecurrenceCoefficients(int k) {
+      // the recurrence relation is
+      // (k+1) Lk+1(X) = (2k + 1 - X) Lk(X) - k Lk-1(X)
+      long kP1 = k + 1;
+      setRecurrenceCoefficients(new RationalNumber(2 * k + 1, kP1),
+                                new RationalNumber(-1l, kP1),
+                                new RationalNumber(k, kP1));
+    }
+  };
+
   /** Simple constructor.
    * Build a degree 0 Laguerre polynomial
    */
   public Laguerre() {
-    super(0, l, maxDegree);
+    super(0, generator);
   }
 
   /** Simple constructor.
    * Build a degree d Laguerre polynomial
-   * @param d degree of the polynomial
-   */
-  public Laguerre(int d) {
-    super(d, l, maxDegree);
-  }
-
-  /** Initialize the recurrence coefficients.
-   * The recurrence relation is
-   *  <pre>(k+1) Lk+1(X) = (2k + 1 - X) Lk(X) - k Lk-1(X)</pre>
-   * @param k index of the current step
-   * @param b2k coefficient to initialize (b2k = a2k / a1k)
-   * @param b3k coefficient to initialize (b3k = a3k / a1k)
-   * @param b4k coefficient to initialize (b4k = a4k / a1k)
+   * @param degree degree of the polynomial
    */
-  protected void initRecurrenceCoefficients(int k,
-                                            RationalNumber b2k,
-                                            RationalNumber b3k,
-                                            RationalNumber b4k) {
-    long kP1 = k + 1;
-    b2k.reset(2 * k + 1, kP1);
-    b3k.reset(-1l, kP1);
-    b4k.reset(k, kP1);
+  public Laguerre(int degree) {
+    super(degree, generator);
   }
 
-  /** Set the maximal degree of already computed polynomials.
-   * @param d maximal degree of already computed polynomials
-   */
-  protected void setMaxDegree(int d) {
-    maxDegree = d;
-  }
-
-  private static final long serialVersionUID = -750526984136835515L;
-
-  /** List holding the coefficients of the polynomials computed so far. */
-  private static List l;
-
-  /** Maximal degree of the polynomials computed so far. */
-  private static int maxDegree;
-
-  /** Build the first two polynomials. */
-  static {
-
-    l = new ArrayList ();
-
-    // L0(X)  = 1
-    l.add(new RationalNumber(1l));
-
-    // L1(X)  = 1 - X
-    l.add(new RationalNumber(1l));
-    l.add(new RationalNumber(-1l));
-
-    maxDegree = 1;
-
-  }
+  private static final long serialVersionUID = 3213856667479179710L;
 
 }

Modified: jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/Legendre.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/Legendre.java?view=diff&rev=488828&r1=488827&r2=488828
==============================================================================
--- jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/Legendre.java (original)
+++ jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/Legendre.java Tue Dec 19 14:11:07 2006
@@ -17,18 +17,15 @@
 
 package org.spaceroots.mantissa.algebra;
 
-import java.util.ArrayList;
-import java.util.List;
-
 /**
  * This class implements Legendre polynomials.
 
  * <p>Legendre polynomials can be defined by the following recurrence
  * relations:
  * <pre>
- *        P0(X)   = 1
- *        P1(X)   = X
- *  (k+1) Pk+1(X) = (2k+1) X Pk(X) - k Pk-1(X)
+ *        P<sub>0</sub>(X)   = 1
+ *        P<sub>1</sub>(X)   = X
+ *  (k+1) P<sub>k+1</sub>(X) = (2k+1) X P<sub>k</sub>(X) - k P<sub>k-1</sub>(X)
  * </pre></p>
 
  * @version $Id: Legendre.java 1705 2006-09-17 19:57:39Z luc $
@@ -39,68 +36,36 @@
 public class Legendre
   extends OrthogonalPolynomial {
 
+  /** Generator for the Legendre polynomials. */
+  private static final CoefficientsGenerator generator =
+    new CoefficientsGenerator(new RationalNumber(1l),
+                              new RationalNumber(0l),
+                              new RationalNumber(1l)) {
+    public void setRecurrenceCoefficients(int k) {
+      // the recurrence relation is
+      // (k+1) Pk+1(X) = (2k+1) X Pk(X) - k Pk-1(X)
+      long kP1 = k + 1;
+      setRecurrenceCoefficients(new RationalNumber(0l),
+                                new RationalNumber(2 * k + 1, kP1),
+                                new RationalNumber(k, kP1));
+    }
+  };
+
   /** Simple constructor.
    * Build a degree 0 Legendre polynomial
    */
   public Legendre() {
-    super(0, l, maxDegree);
+    super(0, generator);
   }
 
   /** Simple constructor.
    * Build a degree d Legendre polynomial
-   * @param d degree of the polynomial
-   */
-  public Legendre(int d) {
-    super(d, l, maxDegree);
-  }
-
-  /** Initialize the recurrence coefficients.
-   * The recurrence relation is
-   *  <pre>(k+1) Pk+1(X) = (2k+1) X Pk(X) - k Ok-1(X)</pre>
-   * @param k index of the current step
-   * @param b2k coefficient to initialize (b2k = a2k / a1k)
-   * @param b3k coefficient to initialize (b3k = a3k / a1k)
-   * @param b4k coefficient to initialize (b4k = a4k / a1k)
+   * @param degree degree of the polynomial
    */
-  protected void initRecurrenceCoefficients(int k,
-                                            RationalNumber b2k,
-                                            RationalNumber b3k,
-                                            RationalNumber b4k) {
-    long kP1 = k + 1;
-    b2k.reset(0l);
-    b3k.reset(2 * k + 1, kP1);
-    b4k.reset(k, kP1);
+  public Legendre(int degree) {
+    super(degree, generator);
   }
 
-  /** Set the maximal degree of already computed polynomials.
-   * @param d maximal degree of already computed polynomials
-   */
-  protected void setMaxDegree(int d) {
-    maxDegree = d;
-  }
-
-  private static final long serialVersionUID = 428266828791532209L;
-
-  /** List holding the coefficients of the polynomials computed so far. */
-  private static List l;
-
-  /** Maximal degree of the polynomials computed so far. */
-  private static int maxDegree;
-
-  /** Build the first two polynomials. */
-  static {
-
-    l = new ArrayList ();
-
-    // P0(X)  = 1
-    l.add(new RationalNumber(1l));
-
-    // P1(X)  = X
-    l.add(new RationalNumber(0l));
-    l.add(new RationalNumber(1l));
-
-    maxDegree = 1;
-
-  }
+  private static final long serialVersionUID = 4014485393845978429L;
 
 }

Modified: jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/OrthogonalPolynomial.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/OrthogonalPolynomial.java?view=diff&rev=488828&r1=488827&r2=488828
==============================================================================
--- jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/OrthogonalPolynomial.java (original)
+++ jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/OrthogonalPolynomial.java Tue Dec 19 14:11:07 2006
@@ -17,18 +17,17 @@
 
 package org.spaceroots.mantissa.algebra;
 
-import java.util.List;
-
 /**
  * This class is the base class for orthogonal polynomials.
 
  * <p>Orthogonal polynomials can be defined by recurrence relations like:
  * <pre>
- *      O0(X)   = some 0 degree polynomial
- *      O1(X)   = some first degree polynomial
- *  a1k Ok+1(X) = (a2k + a3k X) Ok(X) - a4k Ok-1(X)
+ *      O<sub>0</sub>(X)   = some 0 degree polynomial
+ *      O<sub>1</sub>(X)   = some first degree polynomial
+ *  a<sub>1,k</sub> O<sub>k+1</sub>(X) = (a<sub>2,k</sub> + a<sub>3,k</sub> X) O<sub>k</sub>(X) - a<sub>4,k</sub> O<sub>k-1</sub>(X)
  * </pre>
- * where a0k, a1k, a2k and a3k are simple expressions which either are
+ * where a<sub>1,k</sub>, a<sub>2,k</sub>, a<sub>3,k</sub> and
+ * a<sub>4,k</sub> are simple expressions which either are
  * constants or depend on k.</p>
 
  * @version $Id: OrthogonalPolynomial.java 1705 2006-09-17 19:57:39Z luc $
@@ -40,112 +39,11 @@
 
   /** Simple constructor.
    * Build a degree d orthogonal polynomial
-   * @param d degree of the polynomial
-   * @param l list containing all coefficients already computed
-   * @param maxDegree maximal degree of computed coefficients, this
-   * coefficient <em>must</em> be greater or equal to 1, i.e. the
-   * derived class <em>must</em> have initialized the first two
-   * polynomials of degree 0 and 1 before this constructor can be
-   * called.
-   */
-  protected OrthogonalPolynomial(int d, List l, int maxDegree) {
-    if (d > maxDegree) {
-      computeUpToDegree(d, l, maxDegree);
-    }
-
-    // coefficient  for polynomial 0 is  l [0]
-    // coefficient  for polynomial 1 are l [1] ... l [2] (degrees 0 ... 1)
-    // coefficients for polynomial 2 are l [3] ... l [5] (degrees 0 ... 2)
-    // coefficients for polynomial 3 are l [6] ... l [9] (degrees 0 ... 3)
-    // coefficients for polynomial 4 are l[10] ... l[14] (degrees 0 ... 4)
-    // coefficients for polynomial 5 are l[15] ... l[20] (degrees 0 ... 5)
-    // coefficients for polynomial 6 are l[21] ... l[27] (degrees 0 ... 6)
-    // ...
-    int start = d * (d + 1) / 2;
-
-    a = new RationalNumber[d+1];
-    for (int i = 0; i <= d; ++i) {
-      a[i] = new RationalNumber((RationalNumber) l.get(start + i));
-    }
-
-    unknown = null;
-
-  }
-
-  /** Initialize the recurrence coefficients.
-   * The recurrence relation is
-   *  <pre>a1k Ok+1(X) = (a2k + a3k X) Ok(X) - a4k Ok-1(X)</pre>
-   * @param k index of the current step
-   * @param b2k coefficient to initialize (b2k = a2k / a1k)
-   * @param b3k coefficient to initialize (b3k = a3k / a1k)
-   * @param b4k coefficient to initialize (b4k = a4k / a1k)
-   */
-  protected abstract void initRecurrenceCoefficients(int k,
-                                                     RationalNumber b2k,
-                                                     RationalNumber b3k,
-                                                     RationalNumber b4k);
-
-  /** Set the maximal degree of already computed polynomials.
-   * @param d maximal degree of already computed polynomials
+   * @param degree degree of the polynomial
+   * @param generator coefficients generator for the current type of polynomials
    */
-  protected abstract void setMaxDegree(int d);
-
-  /** Compute all the polynomial coefficients up to a given degree.
-   * @param d maximal degree
-   * @param l list containing all coefficients already computed
-   * @param maxDegree maximal degree of computed coefficients
-   */
-  protected void computeUpToDegree(int d, List l, int maxDegree) {
-
-    RationalNumber b2k = new RationalNumber();
-    RationalNumber b3k = new RationalNumber();
-    RationalNumber b4k = new RationalNumber();
-
-    int startK = (maxDegree - 1) * maxDegree / 2;
-    for (int k = maxDegree; k < d; ++k) {
-
-      // start indices of two previous polynomials Ok(X) and Ok-1(X)
-      int startKm1 = startK;
-      startK += k;
-
-      // a1k Ok+1(X) = (a2k + a3k X) Ok(X) - a4k Ok-1(X)
-      // we use bik = aik/a1k
-      initRecurrenceCoefficients(k, b2k, b3k, b4k);
-
-      RationalNumber ckPrev = null;
-      RationalNumber ck     = (RationalNumber)l.get(startK);
-      RationalNumber ckm1   = (RationalNumber)l.get(startKm1);
-
-      // degree 0 coefficient
-      RationalNumber coeff  = RationalNumber.multiply(ck, b2k);
-      coeff.multiplyAndSubtractFromSelf(ckm1, b4k);
-      l.add(coeff);
-
-      // degree 1 to degree k-1 coefficients
-      for (int i = 1; i < k; ++i) {
-        ckPrev = ck;
-        ck     = (RationalNumber)l.get(startK + i);
-        ckm1   = (RationalNumber)l.get(startKm1 + i);
-        coeff  = RationalNumber.multiply(ck, b2k);
-        coeff.multiplyAndAddToSelf(ckPrev, b3k);
-        coeff.multiplyAndSubtractFromSelf(ckm1, b4k);
-        l.add(coeff);
-      }
-
-      // degree k coefficient
-      ckPrev = ck;
-      ck     = (RationalNumber)l.get(startK + k);
-      coeff  = RationalNumber.multiply(ck, b2k);
-      coeff.multiplyAndAddToSelf(ckPrev, b3k);
-      l.add(coeff);
-
-      // degree k+1 coefficient
-      l.add(RationalNumber.multiply(ck, b3k));
-
-    }
-
-    setMaxDegree(d);
-
+  protected OrthogonalPolynomial(int degree, CoefficientsGenerator generator) {
+    a       = generator.getCoefficients(degree);
   }
 
 }

Modified: jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/Polynomial.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/Polynomial.java?view=diff&rev=488828&r1=488827&r2=488828
==============================================================================
--- jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/Polynomial.java (original)
+++ jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/Polynomial.java Tue Dec 19 14:11:07 2006
@@ -19,9 +19,9 @@
 
 import java.io.Serializable;
 import java.math.BigInteger;
+import java.util.Arrays;
 
-/**
- * This class implements polynomials with one unknown.
+/** This class implements polynomials with one unknown.
 
  * <p>This is an abstract class that only declares general methods but
  * does not hold the coefficients by themselves. Specific subclasses
@@ -34,17 +34,13 @@
  * considered (value of the polynom for a given unknown,
  * derivative).</p>
 
+ * <p>Instances of this class are immutable.</p>
+
  * @version $Id: Polynomial.java 1705 2006-09-17 19:57:39Z luc $
  * @author L. Maisonobe
 
 */
-public abstract class Polynomial
-  implements Cloneable, Serializable {
-
-  /** Create a copy of the instance.
-   * @return a copy of the instance
-   */
-  public abstract Object clone();
+public abstract class Polynomial implements Serializable {
 
   /** Check if the instance is the null polynomial.
    * @return true if the polynomial is null
@@ -67,27 +63,58 @@
   public abstract int getDegree();
 
   /** Negate the instance.
+   * @return a new polynomial
    */
-  public abstract void negateSelf();
+  public abstract Polynomial negate();
 
   /** Multiply the instance by a constant.
    * @param r constant to multiply by
+   * @return a new polynomial
    */
-  public abstract void multiplySelf(RationalNumber r);
+  public abstract Polynomial multiply(RationalNumber r);
 
   /** Multiply the instance by a constant.
    * @param l constant to multiply by
+   * @return a new Polynomial
    */
-  public abstract void multiplySelf(long l);
+  public abstract Polynomial multiply(long l);
 
   /** Multiply the instance by a constant.
    * @param i constant to multiply by
+   * @return a new Polynomial
+   */
+  public Polynomial multiply(BigInteger i) {
+    return multiply(new RationalNumber(i));
+  }
+
+  /** Divide the instance by a constant.
+   * @param l constant to multiply by
+   * @return a new polynomial
+   * @exception ArithmeticException if the constant is zero
+   */
+  public Polynomial divide(long l) {
+    return divide(new RationalNumber(l));
+  }
+
+  /** Divide the instance by a constant.
+   * @param r constant to multiply by
+   * @return a new polynomial
+   * @exception ArithmeticException if the constant is zero
+   */
+  public Polynomial divide(RationalNumber r) {
+    return multiply(r.invert());
+  }
+
+  /** Divide the instance by a constant.
+   * @param i constant to multiply by
+   * @return a new polynomial
+   * @exception ArithmeticException if the constant is zero
    */
-  public void multiplySelf(BigInteger i) {
-    multiplySelf(new RationalNumber(i));
+  public Polynomial divide(BigInteger i) {
+    return divide(new RationalNumber(i));
   }
 
-  /** Get the value of the polynomial for a specified unknown.
+ /** Get the value of the polynomial for a specified unknown.
    * @param x value of the unknown
    * @return value of the polynomial
    */
@@ -100,11 +127,6 @@
    */
   public abstract Polynomial getDerivative();
 
-  /** Set the name of the unknown (to appear during conversions to strings).
-   * @param name name to set (if null, the default 'x' value  will be used)
-   */
-  public abstract void setUnknownName(String name);
-
   /** This class implements polynomials with one unknown and rational
    * coefficients.
 
@@ -118,9 +140,7 @@
      * Build a null polynomial
      */
     public Rational() {
-      a = new RationalNumber[1];
-      a[0] = new RationalNumber(0l);
-      unknown = null;
+      a = new RationalNumber[] { RationalNumber.ZERO };
     }
 
     /** Simple constructor.
@@ -136,9 +156,7 @@
      * @param value constant value of the polynomial
      */
     public Rational(RationalNumber value) {
-      a = new RationalNumber[1];
-      a[0] = value;
-      unknown = null;
+      a = new RationalNumber[] { value };
     }
 
     /** Simple constructor.
@@ -157,13 +175,10 @@
      */
     public Rational(RationalNumber a1, RationalNumber a0) {
       if (! a1.isZero()) {
-        a = new RationalNumber[2];
-        a[1] = a1;
+        a = new RationalNumber[] { a0, a1 };
       } else {
-        a = new RationalNumber[1];
+        a = new RationalNumber[] { a0 };
       }
-      a[0] = a0;
-      unknown = null;
     }
 
     /** Simple constructor.
@@ -186,19 +201,52 @@
      */
     public Rational(RationalNumber a2, RationalNumber a1, RationalNumber a0) {
       if (! a2.isZero()) {
-        a = new RationalNumber[3];
-        a[2] = a2;
-        a[1] = a1;
+        a = new RationalNumber[] { a0, a1, a2 };
       } else {
         if (! a1.isZero()) {
-          a = new RationalNumber[2];
-          a[1] = a1;
+          a = new RationalNumber[] { a0, a1 };
         } else {
-          a = new RationalNumber[1];
+          a = new RationalNumber[] { a0 };
+        }
+      }
+    }
+
+    /** Simple constructor.
+     * Build a third degree polynomial
+     * @param a3 leeding degree coefficient
+     * @param a2 second degree coefficient
+     * @param a1 first degree coefficient
+     * @param a0 constant term
+     */
+    public Rational(long a3, long a2, long a1, long a0) {
+      this(new RationalNumber(a3),
+           new RationalNumber(a2),
+           new RationalNumber(a1),
+           new RationalNumber(a0));
+    }
+
+    /** Simple constructor.
+     * Build a third degree polynomial
+     * @param a3 leeding degree coefficient
+     * @param a2 second degree coefficient
+     * @param a1 first degree coefficient
+     * @param a0 constant term
+     */
+    public Rational(RationalNumber a3, RationalNumber a2,
+                    RationalNumber a1, RationalNumber a0) {
+      if (! a3.isZero()) {
+        a = new RationalNumber[] { a0, a1, a2, a3 };
+      } else {
+        if (! a2.isZero()) {
+          a = new RationalNumber[] { a0, a1, a2 };
+        } else {
+          if (! a1.isZero()) {
+            a = new RationalNumber[] { a0, a1 };
+          } else {
+            a = new RationalNumber[] { a0 };
+          }
         }
       }
-      a[0] = a0;
-      unknown = null;
     }
 
     /** Simple constructor.
@@ -220,8 +268,6 @@
       this.a = new RationalNumber[i + 1];
       System.arraycopy(a, 0, this.a, 0, i + 1);
 
-      unknown = null;
-
     }
 
     /** Simple constructor.
@@ -232,47 +278,15 @@
     public Rational(RationalNumber c, int degree) {
 
       if (c.isZero() || degree < 0) {
-        a = new RationalNumber[1];
-        a[0] = new RationalNumber(0l);
+        a = new RationalNumber[] { RationalNumber.ZERO };
       } else {
         a = new RationalNumber[degree + 1];
-        for (int i = 0; i < degree; ++i) {
-          a[i] = new RationalNumber(0l);
-        }
-        a[degree] = new RationalNumber(c);
-      }
-
-      unknown = null;
-
-    }
-
-    /** Copy constructor.
-     * The copy is a deep copy: the polynomials do <em>not</em> share
-     * their coefficients arrays
-     * @param p polynomial to copy
-     */
-    public Rational(Rational p) {
-
-      a = new RationalNumber[p.a.length];
-      for (int i = 0; i < a.length; ++i) {
-        a[i] = new RationalNumber(p.a[i]);
-      }
-
-      if (p.unknown == null) {
-        unknown = null;
-      } else {
-        unknown = new String(p.unknown);
+        Arrays.fill(a, 0, degree, RationalNumber.ZERO);
+        a[degree] = c;
       }
 
     }
 
-    /** Create a copy of the instance.
-     * @return a copy of the instance
-     */
-    public Object clone() {
-      return new Rational(this);
-    }
-
     /** Check if the instance is the null polynomial.
      * @return true if the polynomial is null
      */
@@ -302,207 +316,125 @@
     }
 
     /** Get the coefficients of the polynomial.
-     * @return a reference to the internal coefficients array, the array
+     * @return a copy of the coefficients array, the array
      * element at index 0 is the constant term while the element at
-     * index a.length-1 is the leeding degree coefficient
+     * index a.length-1 is the leading degree coefficient
      */
     public RationalNumber[] getCoefficients() {
-      return a;
-    }
-
-    /** Set the name of the unknown (to appear during conversions to strings).
-     * @param name name to set (if null, the default 'x' value  will be used)
-     */
-    public void setUnknownName(String name) {
-      unknown = name;
+      return (RationalNumber[]) a.clone();
     }
 
-    /** Simplify the polynomial, by removing null high degree terms.
-     */
-    private void simplify() {
-
-      int i = a.length - 1;
-      while ((i > 0) && a[i].isZero()) {
-        --i;
-      }
-
-      if (i < a.length - 1) {
-        RationalNumber[] newA = new RationalNumber[i + 1];
-        System.arraycopy(a, 0, newA, 0, i + 1);
-        a = newA;
-      }
-
-    }
-
-    /** Add a polynomial to the instance.
+    /** Add a polynomial to the instance
      * @param p polynomial to add
+     * @return a new polynomial which is the sum of the instance and p
      */
-    public void addToSelf(Rational p) {
+    public Rational add(Rational p) {
 
-      if (p.a.length > a.length) {
-        RationalNumber[] newA = new RationalNumber[p.a.length];
-        System.arraycopy(a, 0, newA, 0, a.length);
-        for (int i = a.length; i < newA.length; ++i) {
-          newA[i] = new RationalNumber(0l);
-        }
-        a = newA;
-      }
+      // identify the lowest degree polynomial
+      int lowLength  = Math.min(a.length, p.a.length);
+      int highLength = Math.max(a.length, p.a.length);
 
-      for (int i = 0; i < p.a.length; ++i) {
-        a[i].addToSelf(p.a[i]);
+      // build the coefficients array
+      RationalNumber[] newA = new RationalNumber[highLength];
+      for (int i = 0; i < lowLength; ++i) {
+        newA[i] = a[i].add(p.a[i]);
       }
+      System.arraycopy((a.length < p.a.length) ? p.a : a,
+                       lowLength, newA, lowLength, highLength - lowLength);
 
-      simplify();
+      return new Rational(newA);
 
     }
 
-    /** Add two polynomials.
-     * @param p1 first polynomial
-     * @param p2 second polynomial
-     * @return a new polynomial which is the sum of p1 and p2
-     */
-    public static Rational add(Rational p1, Rational p2) {
-      Rational copy = new Rational(p1);
-      copy.addToSelf(p2);
-      return copy;
-    }
-
     /** Subtract a polynomial from the instance.
      * @param p polynomial to subtract
+     * @return a new polynomial which is the difference the instance minus p
      */
-    public void subtractFromSelf(Rational p) {
+    public Rational subtract(Rational p) {
 
-      if (p.a.length > a.length) {
-        RationalNumber[] newA = new RationalNumber[p.a.length];
-        System.arraycopy(a, 0, newA, 0, a.length);
-        for (int i = a.length; i < newA.length; ++i) {
-          newA[i] = new RationalNumber(0l);
-        }
-        a = newA;
-      }
+      // identify the lowest degree polynomial
+      int lowLength  = Math.min(a.length, p.a.length);
+      int highLength = Math.max(a.length, p.a.length);
 
-      for (int i = 0; i < p.a.length; ++i) {
-        a[i].subtractFromSelf(p.a[i]);
+      // build the coefficients array
+      RationalNumber[] newA = new RationalNumber[highLength];
+      for (int i = 0; i < lowLength; ++i) {
+        newA[i] = a[i].subtract(p.a[i]);
+      }
+      if (a.length < p.a.length) {
+        for (int i = lowLength; i < highLength; ++i) {
+          newA[i] = p.a[i].negate();
+        }
+      } else {
+        System.arraycopy(a, lowLength, newA, lowLength, highLength - lowLength);
       }
 
-      simplify();
-
-    }
+      return new Rational(newA);
 
-    /** Subtract two polynomials.
-     * @param p1 first polynomial
-     * @param p2 second polynomial
-     * @return a new polynomial which is the difference p1 minus p2
-     */
-    public static Rational subtract(Rational p1, Rational p2) {
-      Rational copy = new Rational(p1);
-      copy.subtractFromSelf(p2);
-      return copy;
     }
 
     /** Negate the instance.
+     * @return a new polynomial
      */
-    public void negateSelf() {
+    public Polynomial negate() {
+      RationalNumber[] newA = new RationalNumber[a.length];
       for (int i = 0; i < a.length; ++i) {
-        a[i].negateSelf();
+        newA[i] = a[i].negate();
       }
-    }
-
-    /** Negate a polynomial.
-     * @param p polynomial to negate
-     * @return a new polynomial which is the opposite of p
-     */
-    public static Rational negate(Rational p) {
-      Rational copy = new Rational(p);
-      copy.negateSelf();
-      return copy;
+      return new Rational(newA);
     }
 
     /** Multiply the instance by a polynomial.
      * @param p polynomial to multiply by
+     * @return a new polynomial
      */
-    public void multiplySelf(Rational p) {
+    public Rational multiply(Rational p) {
 
       RationalNumber[] newA = new RationalNumber[a.length + p.a.length - 1];
 
       for (int i = 0; i < newA.length; ++i) {
-        newA[i] = new RationalNumber(0l);
+        newA[i] = RationalNumber.ZERO;
         for (int j = Math.max(0, i + 1 - p.a.length);
              j < Math.min(a.length, i + 1);
              ++j) {
-          newA[i].addToSelf(RationalNumber.multiply(a[j], p.a[i-j]));
+          newA[i] = newA[i].add(a[j].multiply(p.a[i-j]));
         }
       }
 
-      a = newA;
+      return new Rational(newA);
 
     }
 
-    /** Multiply two polynomials.
-     * @param p1 first polynomial
-     * @param p2 second polynomial
-     * @return a new polynomial which is the product of p1 and p2
+    /** Multiply the instance by a constant.
+     * @param l constant to multiply by
+     * @return a new polynomial
      */
-    public static Rational multiply(Rational p1, Rational p2) {
-      Rational copy = new Rational(p1);
-      copy.multiplySelf(p2);
-      return copy;
+    public Polynomial multiply(long l) {
+      return multiply(new RationalNumber(l));
     }
 
     /** Multiply the instance by a constant.
      * @param r constant to multiply by
+     * @return a new polynomial
      */
-    public void multiplySelf(RationalNumber r) {
+    public Polynomial multiply(RationalNumber r) {
 
       if (r.isZero()) {
-        a = new RationalNumber[1];
-        a[0] = new RationalNumber(0l);
+        return new Rational(new RationalNumber[] { RationalNumber.ZERO });
       }
 
-      for (int i = 0; i < a.length; ++i) {
-        a[i].multiplySelf(r);
-      }
-
-    }
-
-    /** Multiply a polynomial by a constant.
-     * @param p polynomial
-     * @param r constant
-     * @return a new polynomial which is the product of p and r
-     */
-    public static Rational multiply(Rational p, RationalNumber r) {
-      Rational copy = new Rational(p);
-      copy.multiplySelf(r);
-      return copy;
-    }
-
-    /** Multiply the instance by a constant.
-     * @param l constant to multiply by
-     */
-    public void multiplySelf(long l) {
-
-      if (l == 0l) {
-        a = new RationalNumber[1];
-        a[0] = new RationalNumber(0l);
+      if (r.isOne()) {
+        return this;
       }
 
+      RationalNumber[] newA = new RationalNumber[a.length];
       for (int i = 0; i < a.length; ++i) {
-        a[i].multiplySelf(l);
+        newA[i] = a[i].multiply(r);
       }
+      return new Rational(newA);
 
     }
 
-    /** Multiply a polynomial by a constant.
-     * @param p polynomial
-     * @param l constant
-     * @return a new polynomial which is the product of p and l
-     */
-    public static Rational multiply(Rational p, long l) {
-      Rational copy = new Rational(p);
-      copy.multiplySelf(l);
-      return copy;
-    }
-
     /** Get the value of the polynomial for a specified unknown.
      * @param x value of the unknown
      * @return value of the polynomial
@@ -521,15 +453,14 @@
      * @return a new polynomial which is the derivative of the instance
      */
     public Polynomial getDerivative() {
-      Rational derivative = new Rational();
       if (a.length == 1) {
-        return derivative;
+        return new Rational();
       }
-      derivative.a = new RationalNumber[a.length - 1];
+      RationalNumber[] newA = new RationalNumber[a.length - 1];
       for (int i = 1; i < a.length; ++i) {
-        derivative.a[i-1] = RationalNumber.multiply(a[i], i);
+        newA[i - 1] = a[i].multiply(i);
       }
-      return derivative;
+      return new Rational(newA);
     }
 
     /** Perform the euclidian division of two polynomials.
@@ -541,18 +472,18 @@
                                                    Rational divisor) {
 
       Rational quotient  = new Rational(0l);
-      Rational remainder = new Rational(dividend);
+      Rational remainder = dividend;
 
       int divisorDegree   = divisor.getDegree();
       int remainderDegree = remainder.getDegree();
       while ((! remainder.isZero()) && (remainderDegree >= divisorDegree)) {
 
-        RationalNumber c = RationalNumber.divide(remainder.a[remainderDegree],
-                                                 divisor.a[divisorDegree]);
+        RationalNumber c =
+          remainder.a[remainderDegree].divide(divisor.a[divisorDegree]);
         Rational monomial = new Rational(c, remainderDegree - divisorDegree);
 
-        remainder.subtractFromSelf(Rational.multiply(monomial, divisor));
-        quotient.addToSelf(monomial);
+        remainder = remainder.subtract(monomial.multiply(divisor));
+        quotient  = quotient.add(monomial);
 
         remainderDegree = remainder.getDegree();
 
@@ -572,7 +503,7 @@
       BigInteger lcm = BigInteger.ONE;
 
       for (int i = 0; i < a.length; ++i) {
-        RationalNumber newCoeff = RationalNumber.multiply(a[i], lcm);
+        RationalNumber newCoeff = a[i].multiply(lcm);
         if (! newCoeff.isInteger()) {
           lcm = lcm.multiply(newCoeff.getDenominator());
         }
@@ -594,10 +525,6 @@
     * but <code>1 - 3 x + x^2</code> if the negative coefficient is not
     * the first one displayed).</p>
 
-    * <p>The name of the unknown is <code>x</code> by default, but can
-    * be changed using the {@link #setUnknownName setUnknownName}
-    * method.</p>
-
     * @return a string representation of the polynomial
 
     */
@@ -634,7 +561,7 @@
             s.append(' ');
           }
 
-          s.append((unknown == null) ? defaultUnknown : unknown);
+          s.append("x");
           if (i > 1) {
             s.append('^');
             s.append(Integer.toString(i));
@@ -650,10 +577,7 @@
     /** Coefficients array. */
     protected RationalNumber[] a;
 
-    /** Name of the unknown. */
-    protected String unknown;
-
-    private static final long serialVersionUID = 3035650338772911046L;
+    private static final long serialVersionUID = -794133890636181115L;
 
   }
 
@@ -687,9 +611,15 @@
      * Build a null polynomial
      */
     public Double() {
-      a = new double[1];
-      a[0] = 0;
-      unknown = null;
+      a = new double[] { 0.0 };
+    }
+
+    /** Simple constructor.
+     * Build a constant polynomial
+     * @param value constant value of the polynomial
+     */
+    public Double(long value) {
+      this((double) value);
     }
 
     /** Simple constructor.
@@ -697,9 +627,16 @@
      * @param value constant value of the polynomial
      */
     public Double(double value) {
-      a = new double[1];
-      a[0] = value;
-      unknown = null;
+      a = new double[] { value };
+    }
+
+    /** Simple constructor.
+     * Build a first degree polynomial
+     * @param a1 leeding degree coefficient
+     * @param a0 constant term
+     */
+    public Double(long a1, long a0) {
+      this((double) a1, (double) a0);
     }
 
     /** Simple constructor.
@@ -708,14 +645,21 @@
      * @param a0 constant term
      */
     public Double(double a1, double a0) {
-      if (Math.abs(a1) > 1.0e-12) {
-        a = new double[2];
-        a[1] = a1;
+      if (a1 != 0) {
+        a = new double[] { a0, a1 };
       } else {
-        a = new double[1];
+        a = new double[] { a0 };
       }
-      a[0] = a0;
-      unknown = null;
+    }
+
+    /** Simple constructor.
+     * Build a second degree polynomial
+     * @param a2 leeding degree coefficient
+     * @param a1 first degree coefficient
+     * @param a0 constant term
+     */
+    public Double(long a2, long a1, long a0) {
+      this((double) a2, (double) a1, (double) a0);
     }
 
     /** Simple constructor.
@@ -725,20 +669,49 @@
      * @param a0 constant term
      */
     public Double(double a2, double a1, double a0) {
-      if (Math.abs(a2) > 1.0e-12) {
-        a = new double[3];
-        a[2] = a2;
-        a[1] = a1;
+      if (a2 != 0) {
+        a = new double[] { a0, a1, a2 };
       } else {
-        if (Math.abs(a1) > 1.0e-12) {
-          a = new double[2];
-          a[1] = a1;
+        if (a1 != 0) {
+          a = new double[] { a0, a1 };
         } else {
-          a = new double[1];
+          a = new double[] { a0 };
+        }
+      }
+    }
+
+    /** Simple constructor.
+     * Build a third degree polynomial
+     * @param a3 leeding degree coefficient
+     * @param a2 second degree coefficient
+     * @param a1 first degree coefficient
+     * @param a0 constant term
+     */
+    public Double(long a3, long a2, long a1, long a0) {
+      this((double) a3, (double) a2, (double) a1, (double) a0);
+    }
+
+    /** Simple constructor.
+     * Build a third degree polynomial
+     * @param a3 leeding degree coefficient
+     * @param a2 second degree coefficient
+     * @param a1 first degree coefficient
+     * @param a0 constant term
+     */
+    public Double(double a3, double a2, double a1, double a0) {
+      if (a3 != 0) {
+        a = new double[] { a0, a1, a2, a3 };
+      } else {
+        if (a2 != 0) {
+          a = new double[] { a0, a1, a2 };
+        } else {
+          if (a1 != 0) {
+            a = new double[] { a0, a1 };
+          } else {
+            a = new double[] { a0 };
+          }
         }
       }
-      a[0] = a0;
-      unknown = null;
     }
 
     /** Simple constructor.
@@ -752,7 +725,7 @@
 
       // remove null high degree coefficients
       int i = a.length - 1;
-      while ((i > 0) && (Math.abs(a[i]) <= 1.0e-12)) {
+      while ((i > 0) && (a[i] == 0)) {
         --i;
       }
 
@@ -760,8 +733,6 @@
       this.a = new double[i + 1];
       System.arraycopy(a, 0, this.a, 0, i + 1);
 
-      unknown = null;
-
     }
 
     /** Simple constructor.
@@ -770,91 +741,47 @@
      * @param degree degree associated with the coefficient
      */
     public Double(double c, int degree) {
-
-      if ((Math.abs(c) <= 1.0e-12) || degree < 0) {
-        a = new double[1];
-        a[0] = 0;
+      if ((c == 0) || degree < 0) {
+        a = new double[] { 0.0 };
       } else {
         a = new double[degree + 1];
-        for (int i = 0; i < degree; ++i) {
-          a[i] = 0;
-        }
+        Arrays.fill(a, 0, degree, 0.0);
         a[degree] = c;
       }
-
-      unknown = null;
-
-    }
-
-    /** Copy constructor.
-     * The copy is a deep copy: the polynomials do <em>not</em> share
-     * their coefficients arrays
-     * @param p polynomial to copy
-     */
-    public Double(Double p) {
-
-      a = new double[p.a.length];
-      for (int i = 0; i < a.length; ++i) {
-        a[i] = p.a[i];
-      }
-
-      if (p.unknown == null) {
-        unknown = null;
-      } else {
-        unknown = new String(p.unknown);
-      }
-
     }
 
-    /** Copy constructor.
-     * The copy is a deep copy: the polynomials do <em>not</em> share
-     * their coefficients arrays
-     * @param p polynomial to copy
-     */
-    public Double(Rational p) {
-
-      RationalNumber[] pA = p.getCoefficients();
-      a = new double[pA.length];
+    /** Simple constructor.
+     * Build a {@link Polynomial.Double Polynomial.Double} from a
+     * {@link Polynomial.Rational Polynomial.Rational}
+     * @param r a rational polynomial
+     */
+    public Double(Rational r) {
+      // convert the coefficients
+      a = new double[r.a.length];
       for (int i = 0; i < a.length; ++i) {
-        a[i] = pA[i].doubleValue();
+        a[i] = r.a[i].doubleValue();
       }
-
-      if (p.unknown == null) {
-        unknown = null;
-      } else {
-        unknown = new String(p.unknown);
-      }
-
-    }
-
-    /** Create a copy of the instance.
-     * @return a copy of the instance
-     */
-    public Object clone() {
-      return new Double(this);
     }
 
     /** Check if the instance is the null polynomial.
      * @return true if the polynomial is null
      */
     public boolean isZero() {
-      return (a.length == 1) && (Math.abs(a[0]) < 1.0e-12);
+      return (a.length == 1) && (a[0] == 0);
     }
 
     /** Check if the instance is the constant unit polynomial.
      * @return true if the polynomial is the constant unit polynomial
      */
     public boolean isOne() {
-      return (a.length == 1) && (Math.abs(a[0] - 1) < 1.0e-12);
+      return (a.length == 1) && ((a[0] - 1.0) == 0);
     }
 
     /** Check if the instance is the identity polynomial.
      * @return true if the polynomial is the identity polynomial
      */
     public boolean isIdentity() {
-      return (a.length == 2)
-        && (Math.abs(a[0]) < 1.0e-12)
-        && (Math.abs(a[1] - 1) < 1.0e-12);
+      return (a.length == 2) && (a[0] == 0) && ((a[1] - 1.0) == 0);
     }
 
     /** Get the polynomial degree.
@@ -865,124 +792,84 @@
     }
 
     /** Get the coefficients of the polynomial.
-     * @return a reference to the internal coefficients array, the array
+     * @return a copy of the coefficients array, the array
      * element at index 0 is the constant term while the element at
-     * index a.length-1 is the leeding degree coefficient
+     * index a.length-1 is the leading degree coefficient
      */
     public double[] getCoefficients() {
-      return a;
-    }
-
-    /** Simplify the polynomial, by removing null high degree terms.
-     */
-    private void simplify() {
-
-      int i = a.length - 1;
-      while ((i > 0) && (Math.abs(a[i]) <= 1.0e-12)) {
-        --i;
-      }
-
-      if (i < a.length - 1) {
-        double[] newA = new double[i + 1];
-        System.arraycopy(a, 0, newA, 0, i + 1);
-        a = newA;
-      }
-
+      return (double[]) a.clone();
     }
 
-    /** Add a polynomial to the instance.
+    /** Add a polynomial to the instance
      * @param p polynomial to add
+     * @return a new polynomial which is the sum of the instance and p
      */
-    public void addToSelf(Double p) {
+    public Double add(Double p) {
 
-      if (p.a.length > a.length) {
-        double[] newA = new double[p.a.length];
-        System.arraycopy(a, 0, newA, 0, a.length);
-        for (int i = a.length; i < newA.length; ++i) {
-          newA[i] = 0;
-        }
-        a = newA;
-      }
+      // identify the lowest degree polynomial
+      int lowLength  = Math.min(a.length, p.a.length);
+      int highLength = Math.max(a.length, p.a.length);
 
-      for (int i = 0; i < p.a.length; ++i) {
-        a[i] += p.a[i];
+      // build the coefficients array
+      double[] newA = new double[highLength];
+      for (int i = 0; i < lowLength; ++i) {
+        newA[i] = a[i] + p.a[i];
       }
+      System.arraycopy((a.length < p.a.length) ? p.a : a,
+                       lowLength, newA, lowLength, highLength - lowLength);
 
-      simplify();
+      return new Double(newA);
 
     }
 
-    /** Add two polynomials.
-     * @param p1 first polynomial
-     * @param p2 second polynomial
-     * @return a new polynomial which is the sum of p1 and p2
-     */
-    public static Double add(Double p1, Double p2) {
-      Double copy = new Double(p1);
-      copy.addToSelf(p2);
-      return copy;
-    }
-
     /** Subtract a polynomial from the instance.
      * @param p polynomial to subtract
+     * @return a new polynomial which is the difference the instance minus p
      */
-    public void subtractFromSelf(Double p) {
+    public Double subtract(Double p) {
 
-      if (p.a.length > a.length) {
-        double[] newA = new double[p.a.length];
-        System.arraycopy(a, 0, newA, 0, a.length);
-        for (int i = a.length; i < newA.length; ++i) {
-          newA[i] = 0;
-        }
-        a = newA;
-      }
+      // identify the lowest degree polynomial
+      int lowLength  = Math.min(a.length, p.a.length);
+      int highLength = Math.max(a.length, p.a.length);
 
-      for (int i = 0; i < p.a.length; ++i) {
-        a[i] -= p.a[i];
+      // build the coefficients array
+      double[] newA = new double[highLength];
+      for (int i = 0; i < lowLength; ++i) {
+        newA[i] = a[i] - p.a[i];
+      }
+      if (a.length < p.a.length) {
+        for (int i = lowLength; i < highLength; ++i) {
+          newA[i] = -p.a[i];
+        }
+      } else {
+        System.arraycopy(a, lowLength, newA, lowLength, highLength - lowLength);
       }
 
-      simplify();
-
-    }
+      return new Double(newA);
 
-    /** Subtract two polynomials.
-     * @param p1 first polynomial
-     * @param p2 second polynomial
-     * @return a new polynomial which is the difference p1 minus p2
-     */
-    public static Double subtract(Double p1, Double p2) {
-      Double copy = new Double(p1);
-      copy.subtractFromSelf(p2);
-      return copy;
     }
 
     /** Negate the instance.
+     * @return a new polynomial
      */
-    public void negateSelf() {
+    public Polynomial negate() {
+      double[] newA = new double[a.length];
       for (int i = 0; i < a.length; ++i) {
-        a[i] = -a[i];
+        newA[i] = -a[i];
       }
-    }
-
-    /** Negate a polynomial.
-     * @param p polynomial to negate
-     * @return a new polynomial which is the opposite of p
-     */
-    public static Double negate(Double p) {
-      Double copy = new Double(p);
-      copy.negateSelf();
-      return copy;
+      return new Double(newA);
     }
 
     /** Multiply the instance by a polynomial.
      * @param p polynomial to multiply by
+     * @return a new polynomial
      */
-    public void multiplySelf(Double p) {
+    public Double multiply(Double p) {
 
       double[] newA = new double[a.length + p.a.length - 1];
 
       for (int i = 0; i < newA.length; ++i) {
-        newA[i] = 0;
+        newA[i] = 0.0;
         for (int j = Math.max(0, i + 1 - p.a.length);
              j < Math.min(a.length, i + 1);
              ++j) {
@@ -990,92 +877,44 @@
         }
       }
 
-      a = newA;
-
-    }
+      return new Double(newA);
 
-    /** Multiply two polynomials.
-     * @param p1 first polynomial
-     * @param p2 second polynomial
-     * @return a new polynomial which is the product of p1 and p2
-     */
-    public static Double multiply(Double p1, Double p2) {
-      Double copy = new Double(p1);
-      copy.multiplySelf(p2);
-      return copy;
     }
 
     /** Multiply the instance by a constant.
-     * @param r constant to multiply by
-     */
-    public void multiplySelf(double r) {
-
-      if (Math.abs(r) < 1.0e-12) {
-        a = new double[1];
-        a[0] = 0;
-      }
-
-      for (int i = 0; i < a.length; ++i) {
-        a[i] *= r;
-      }
-
-    }
-
-    /** Multiply a polynomial by a constant.
-     * @param p polynomial
-     * @param r constant
-     * @return a new polynomial which is the product of p and r
+     * @param l constant to multiply by
+     * @return a new polynomial
      */
-    public static Double multiply(Double p, double r) {
-      Double copy = new Double(p);
-      copy.multiplySelf(r);
-      return copy;
+    public Polynomial multiply(long l) {
+      return multiply((double) l);
     }
 
     /** Multiply the instance by a constant.
      * @param r constant to multiply by
+     * @return a new polynomial
      */
-    public void multiplySelf(RationalNumber r) {
-
-      if (r.isZero()) {
-        a = new double[1];
-        a[0] = 0;
-      }
-
-      double rValue = r.doubleValue();
-      for (int i = 0; i < a.length; ++i) {
-        a[i] *= rValue;
-      }
-
+    public Polynomial multiply(RationalNumber r) {
+      return multiply(r.doubleValue());
     }
 
     /** Multiply the instance by a constant.
-     * @param l constant to multiply by
+     * @param r constant to multiply by
+     * @return a new polynomial
      */
-    public void multiplySelf(long l) {
+    public Polynomial multiply(double r) {
 
-      if (l == 0l) {
-        a = new double[1];
-        a[0] = 0;
+      if (r == 0) {
+        return new Double(new double[] { 0.0 });
       }
 
+      double[] newA = new double[a.length];
       for (int i = 0; i < a.length; ++i) {
-        a[i] *= l;
+        newA[i] = a[i] * r;
       }
+      return new Double(newA);
 
     }
 
-    /** Multiply a polynomial by a constant.
-     * @param p polynomial
-     * @param l constant
-     * @return a new polynomial which is the product of p and l
-     */
-    public static Double multiply(Double p, long l) {
-      Double copy = new Double(p);
-      copy.multiplySelf(l);
-      return copy;
-    }
-
     /** Get the value of the polynomial for a specified unknown.
      * @param x value of the unknown
      * @return value of the polynomial
@@ -1094,22 +933,14 @@
      * @return a new polynomial which is the derivative of the instance
      */
     public Polynomial getDerivative() {
-      Double derivative = new Double();
       if (a.length == 1) {
-        return derivative;
+        return new Double();
       }
-      derivative.a = new double[a.length - 1];
+      double[] newA = new double[a.length - 1];
       for (int i = 1; i < a.length; ++i) {
-        derivative.a[i-1] = a[i] * i;
+        newA[i - 1] = a[i] * i;
       }
-      return derivative;
-    }
-
-    /** Set the name of the unknown (to appear during conversions to strings).
-     * @param name name to set (if null, the default 'x' value  will be used)
-     */
-    public void setUnknownName(String name) {
-      unknown = name;
+      return new Double(newA);
     }
 
     /** Returns a string representation of the polynomial.
@@ -1124,36 +955,23 @@
     * but <code>1 - 3 x + x^2</code> if the negative coefficient is not
     * the first one displayed).</p>
 
-    * <p>The name of the unknown is <code>x</code> by default, but can
-    * be changed using the {@link #setUnknownName setUnknownName}
-    * method.</p>
-
     * @return a string representation of the polynomial
 
     */
     public String toString() {
 
-      double maxCoeff = 0;
-      for (int i = 0; i < a.length; ++i) {
-        double abs = Math.abs(a[i]);
-        if (abs > maxCoeff) {
-          maxCoeff = abs;
-        }
-      }
-      double epsilon = 1.0e-12 * maxCoeff;
-
       StringBuffer s = new StringBuffer();
-      if (Math.abs(a[0]) <= epsilon) {
+      if (a[0] == 0.0) {
         if (a.length == 1) {
           return "0";
         }
       } else {
-        s.append(a[0]);
+        s.append(java.lang.Double.toString(a[0]));
       }
 
       for (int i = 1; i < a.length; ++i) {
 
-        if (Math.abs(a[i]) > epsilon) {
+        if (a[i] != 0) {
 
           if (s.length() > 0) {
             if (a[i] < 0) {
@@ -1168,12 +986,12 @@
           }
 
           double absAi = Math.abs(a[i]);
-          if (Math.abs(absAi - 1) > 1.0e-12) {
-            s.append(absAi);
+          if ((absAi - 1) != 0) {
+            s.append(java.lang.Double.toString(absAi));
             s.append(' ');
           }
 
-          s.append((unknown == null) ? defaultUnknown : unknown);
+          s.append("x");
           if (i > 1) {
             s.append('^');
             s.append(Integer.toString(i));
@@ -1189,14 +1007,8 @@
     /** Coefficients array. */
     protected double[] a;
 
-    /** Name of the unknown. */
-    protected String unknown;
-
-    private static final long serialVersionUID = -5907669461605191069L;
+    private static final long serialVersionUID = -4210522025715687648L;
 
   }
-
-  /** Default name of unknowns. */
-  protected static String defaultUnknown = new String("x");
 
 }

Modified: jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/PolynomialFraction.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/PolynomialFraction.java?view=diff&rev=488828&r1=488827&r2=488828
==============================================================================
--- jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/PolynomialFraction.java (original)
+++ jakarta/commons/proper/math/trunk/src/mantissa/src/org/spaceroots/mantissa/algebra/PolynomialFraction.java Tue Dec 19 14:11:07 2006
@@ -17,26 +17,28 @@
 
 package org.spaceroots.mantissa.algebra;
 
+import java.io.Serializable;
 import java.math.BigInteger;
 
 /**
  * This class implements fractions of polynomials with one unknown and
  * rational coefficients.
+ * <p>Instances of this class are immutable.</p>
 
  * @version $Id: PolynomialFraction.java 1705 2006-09-17 19:57:39Z luc $
  * @author L. Maisonobe
 
  */
 
-public class PolynomialFraction {
+public class PolynomialFraction implements Serializable {
 
   /**
    * Simple constructor.
    * Build a constant null fraction
    */
   public PolynomialFraction() {
-    this(new Polynomial.Rational(new RationalNumber(0l)),
-         new Polynomial.Rational(new RationalNumber(1l)));
+    this(new Polynomial.Rational(RationalNumber.ZERO),
+         new Polynomial.Rational(RationalNumber.ONE));
   }
 
   /**
@@ -47,8 +49,8 @@
    * @exception ArithmeticException if the denominator is null
    */
   public PolynomialFraction(long numerator, long denominator) {
-    this(new Polynomial.Rational(new RationalNumber(numerator)),
-         new Polynomial.Rational(new RationalNumber(denominator)));
+    this(new Polynomial.Rational(numerator),
+         new Polynomial.Rational(denominator));
   }
 
   /**
@@ -90,21 +92,20 @@
       throw new ArithmeticException("null denominator");
     }
 
-    p = new Polynomial.Rational(numerator);
-    q = new Polynomial.Rational(denominator);
+    p = numerator;
+    q = denominator;
 
     RationalNumber[] a = q.getCoefficients();
     if (a[a.length - 1].isNegative()) {
-      p.negateSelf();
-      q.negateSelf();
+      p = (Polynomial.Rational) p.negate();
+      q = (Polynomial.Rational) q.negate();
     }
 
     simplify();
 
   }
 
-  /**
-   * Simple constructor.
+  /** Simple constructor.
    * Build a fraction from a single integer
    * @param l value of the fraction
    */
@@ -121,8 +122,7 @@
     this(i, BigInteger.ONE);
   }
 
-  /**
-   * Simple constructor.
+  /** Simple constructor.
    * Build a fraction from a single rational number
    * @param r value of the fraction
    */
@@ -130,178 +130,95 @@
     this(r.getNumerator(), r.getDenominator());
   }
 
-  /**
-   * Simple constructor.
-   * Build a fraction from a single Polynom
+  /** Simple constructor.
+   * Build a fraction from a single Polynomial
    * @param p value of the fraction
    */
   public PolynomialFraction(Polynomial.Rational p) {
-    this(p, new Polynomial.Rational(new RationalNumber(1l)));
+    this(p, new Polynomial.Rational(1l));
   }
 
-  /**
-   * Copy-constructor.
-   * @param f fraction to copy
+  /** Negate the instance.
+   * @return a new polynomial fraction opposite to the instance
    */
-  public PolynomialFraction(PolynomialFraction f) {
-    p = new Polynomial.Rational(f.p);
-    q = new Polynomial.Rational(f.q);
+  public PolynomialFraction negate() {
+    return new PolynomialFraction((Polynomial.Rational) p.negate(), q);
   }
 
-  /**
-   * Negate the instance
+  /** Add a polynomial fraction to the instance.
+   * @param f polynomial fraction to add.
+   * @return a new polynomial fraction
    */
-  public void negateSelf() {
-    p.negateSelf();
+  public PolynomialFraction add(PolynomialFraction f) {
+    return new PolynomialFraction(p.multiply(f.q).add(f.p.multiply(q)),
+                                  q.multiply(f.q));
   }
 
-  /**
-   * Negate a fraction.
-   * @param f fraction to negate
-   * @return a new fraction which is the opposite of f
-   */
-  public static PolynomialFraction negate(PolynomialFraction f) {
-    PolynomialFraction copy = new PolynomialFraction(f);
-    copy.negateSelf();
-    return copy;
-  }
-
-  /**
-   * Add a fraction to the instance.
-   * @param f fraction to add.
+  /** Subtract a fraction from the instance.
+   * @param f polynomial fraction to subtract.
+   * @return a new polynomial fraction
    */
-  public void addToSelf(PolynomialFraction f) {
-    PolynomialFraction sum = add(this, f);
-    p = sum.p;
-    q = sum.q;
-  }
-
-  /** Add two fractions.
-   * @param f1 first fraction
-   * @param f2 second fraction
-   * @return a new fraction which is the sum of f1 and f2
-   */
-  public static PolynomialFraction add(PolynomialFraction f1,
-                                       PolynomialFraction f2) {
-    Polynomial.Rational num =
-      Polynomial.Rational.add(Polynomial.Rational.multiply(f1.p, f2.q),
-                              Polynomial.Rational.multiply(f2.p, f1.q));
-    Polynomial.Rational den = Polynomial.Rational.multiply(f1.q, f2.q);
-    return new PolynomialFraction(num, den);
+  public PolynomialFraction subtract(PolynomialFraction f) {
+    return new PolynomialFraction(p.multiply(f.q).subtract(f.p.multiply(q)),
+                                  q.multiply(f.q));
   }
 
-  /**
-   * Subtract a fraction to the instance.
-   * @param f fraction to subtract.
+  /** Multiply the instance by a polynomial fraction.
+   * @param f polynomial fraction to multiply by
+   * @return a new polynomial fraction
    */
-  public void subtractFromSelf(PolynomialFraction f) {
-    PolynomialFraction diff = subtract(this, f);
-    p = diff.p;
-    q = diff.q;
-  }
-
-  /** Subtract two fractions.
-   * @param f1 first fraction
-   * @param f2 second fraction
-   * @return a new fraction which is the difference f1 minus f2
-   */
-  public static PolynomialFraction subtract(PolynomialFraction f1,
-                                            PolynomialFraction f2) {
-    Polynomial.Rational num =
-      Polynomial.Rational.subtract(Polynomial.Rational.multiply(f1.p, f2.q),
-                                   Polynomial.Rational.multiply(f2.p, f1.q));
-    Polynomial.Rational den = Polynomial.Rational.multiply(f1.q, f2.q);
-    return new PolynomialFraction(num, den);
-  }
-
-  /** Multiply the instance by a fraction.
-   * @param f fraction to multiply by
-   */
-  public void multiplySelf(PolynomialFraction f) {
-    p.multiplySelf(f.p);
-    q.multiplySelf(f.q);
-    simplify();
+  public PolynomialFraction multiply(PolynomialFraction f) {
+    PolynomialFraction product =
+      new PolynomialFraction(p.multiply(f.p), q.multiply(f.q));
+    product.simplify();
+    return product;
   }
 
-  /** Multiply two fractions.
-   * @param f1 first fraction
-   * @param f2 second fraction
-   * @return a new fraction which is the product of f1 and f2
-   */
-  public static PolynomialFraction multiply(PolynomialFraction f1,
-                                            PolynomialFraction f2) {
-    PolynomialFraction copy = new PolynomialFraction(f1);
-    copy.multiplySelf(f2);
-    return copy;
-  }
-
-  /** Divide the instance by a fraction.
-   * @param f fraction to divide by
+  /** Divide the instance by a polynomial fraction.
+   * @param f polynomial fraction to divide by
+   * @return a new polynomial fraction
    * @exception ArithmeticException if f is null
    */
-  public void divideSelf(PolynomialFraction f) {
+  public PolynomialFraction divide(PolynomialFraction f) {
 
     if (f.p.isZero()) {
       throw new ArithmeticException("divide by zero");
     }
 
-    p.multiplySelf(f.q);
-    q.multiplySelf(f.p);
+    Polynomial.Rational newP = p.multiply(f.q);
+    Polynomial.Rational newQ = q.multiply(f.p);
 
-    RationalNumber[] a = q.getCoefficients();
+    RationalNumber[] a = newQ.getCoefficients();
     if (a[a.length - 1].isNegative()) {
-      p.negateSelf();
-      q.negateSelf();
+      newP = (Polynomial.Rational) newP.negate();
+      newQ = (Polynomial.Rational) newQ.negate();
     }
 
-    simplify();
+    PolynomialFraction result = new PolynomialFraction(newP, newQ);
+    result.simplify();
+    return result;
 
   }
 
-  /** Divide two fractions.
-   * @param f1 first fraction
-   * @param f2 second fraction
-   * @return a new fraction which is the quotient of f1 by f2
-   */
-  public static PolynomialFraction divide(PolynomialFraction f1,
-                                          PolynomialFraction f2) {
-    PolynomialFraction copy = new PolynomialFraction(f1);
-    copy.divideSelf(f2);
-    return copy;
-  }
-
   /** Invert the instance.
-   * Replace the instance by its inverse.
-   * @exception ArithmeticException if the instance is null
+   * @return the inverse of the instance
+   * @exception ArithmeticException if the instance is zero
    */
-  public void invertSelf() {
+  public PolynomialFraction invert() {
 
     if (p.isZero()) {
       throw new ArithmeticException("divide by zero");
     }
 
-    Polynomial.Rational tmp = p;
-    p = q;
-    q = tmp;
+    RationalNumber[] a = p.getCoefficients();
+    PolynomialFraction inverse =
+      (a[a.length - 1].isNegative())
+      ? new PolynomialFraction((Polynomial.Rational) q.negate(),
+                               (Polynomial.Rational) p.negate())
+      : new PolynomialFraction(q, p);
+    inverse.simplify();
+    return inverse;
 
-    RationalNumber[] a = q.getCoefficients();
-    if (a[a.length - 1].isNegative()) {
-      p.negateSelf();
-      q.negateSelf();
-    }
-
-    simplify();
-
-  }
-
-  /** Invert a fraction.
-   * @param f fraction to invert
-   * @return a new fraction which is the inverse of f
-   */
-  public static PolynomialFraction invert(PolynomialFraction f) {
-    PolynomialFraction copy = new PolynomialFraction(f);
-    copy.invertSelf();
-    return copy;
   }
 
   /** Simplify a fraction.
@@ -318,8 +235,8 @@
    */
   private void simplify() {
 
-    Polynomial.Rational a = new Polynomial.Rational(p);
-    Polynomial.Rational b = new Polynomial.Rational(q);
+    Polynomial.Rational a = p;
+    Polynomial.Rational b = q;
     if (a.getDegree() < b.getDegree()) {
       Polynomial.Rational tmp = a;
       a = b;
@@ -342,30 +259,28 @@
 
     if (q.getDegree() == 0) {
       if (! q.isOne()) {
-        RationalNumber f = q.getCoefficients()[0];
-        f.invertSelf();
-        p.multiplySelf(f);
+        p = (Polynomial.Rational) p.divide(q.getCoefficients()[0]);
         q = new Polynomial.Rational(1l);
       }
     } else {
 
       BigInteger lcm = p.getDenominatorsLCM();
       if (lcm.compareTo(BigInteger.ONE) != 0) {
-        p.multiplySelf(lcm);
-        q.multiplySelf(lcm);
+        p = (Polynomial.Rational) p.multiply(lcm);
+        q = (Polynomial.Rational) q.multiply(lcm);
       }
 
       lcm = q.getDenominatorsLCM();
       if (lcm.compareTo(BigInteger.ONE) != 0) {
-        p.multiplySelf(lcm);
-        q.multiplySelf(lcm);
+        p = (Polynomial.Rational) p.multiply(lcm);
+        q = (Polynomial.Rational) q.multiply(lcm);
       }
 
     }
 
     if (q.getCoefficients()[q.getDegree()].isNegative()) {
-      p.negateSelf();
-      q.negateSelf();
+      p = (Polynomial.Rational) p.negate();
+      q = (Polynomial.Rational) q.negate();
     }
 
   }
@@ -386,16 +301,6 @@
     return q;
   }
 
-  /** Set the name of the unknown (to appear during conversions to
-   * strings).
-   * @param name name to set (if null, the default 'x' value will be
-   * used)
-   */
-  public void setUnknownName(String name) {
-    p.setUnknownName(name);
-    q.setUnknownName(name);
-  }
-
   public String toString() {
     if (p.isZero()) {
       return "0";
@@ -435,5 +340,7 @@
 
   /** Denominator. */
   private Polynomial.Rational q;
+
+  private static final long serialVersionUID = 6033909492898954748L;
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org