diff --git a/src/java/org/apache/commons/math/complex/Complex.java b/src/java/org/apache/commons/math/complex/Complex.java index 1ed680490..d928ccc19 100644 --- a/src/java/org/apache/commons/math/complex/Complex.java +++ b/src/java/org/apache/commons/math/complex/Complex.java @@ -17,10 +17,10 @@ package org.apache.commons.math.complex; -import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import org.apache.commons.math.FieldElement; import org.apache.commons.math.MathRuntimeException; import org.apache.commons.math.util.MathUtils; @@ -40,11 +40,11 @@ import org.apache.commons.math.util.MathUtils; * * @version $Revision$ $Date$ */ -public class Complex implements Serializable { +public class Complex implements FieldElement { /** Serializable version identifier */ - private static final long serialVersionUID = -6530173849413811929L; - + private static final long serialVersionUID = -6195664516687396620L; + /** The square root of -1. A number representing "0.0 + 1.0i" */ public static final Complex I = new Complex(0.0, 1.0); @@ -953,4 +953,10 @@ public class Complex implements Serializable { protected Complex createComplex(double real, double imaginary) { return new Complex(real, imaginary); } + + /** {@inheritDoc} */ + public ComplexField getField() { + return ComplexField.getInstance(); + } + } diff --git a/src/java/org/apache/commons/math/complex/ComplexField.java b/src/java/org/apache/commons/math/complex/ComplexField.java new file mode 100644 index 000000000..65ce97352 --- /dev/null +++ b/src/java/org/apache/commons/math/complex/ComplexField.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.math.complex; + +import org.apache.commons.math.Field; + +/** + * Representation of the complex numbers field. + *

+ * This class is a singleton. + *

+ * @see Complex + * @version $Revision$ $Date$ + * @since 2.0 + */ +public class ComplexField implements Field { + + /** Serializable version identifier. */ + private static final long serialVersionUID = -6130362688700788798L; + + /** Private constructor for the singleton. + */ + private ComplexField() { + } + + /** Get the unique instance. + * @return the unique instance + */ + public static ComplexField getInstance() { + return LazyHolder.INSTANCE; + } + + /** {@inheritDoc} */ + public Complex getOne() { + return Complex.ONE; + } + + /** {@inheritDoc} */ + public Complex getZero() { + return Complex.ZERO; + } + + /** Holder for the instance. + *

We use here the Initialization On Demand Holder Idiom.

+ */ + private static class LazyHolder { + /** Cached field instance. */ + private static final ComplexField INSTANCE = new ComplexField(); + } + +} diff --git a/src/java/org/apache/commons/math/fraction/BigFraction.java b/src/java/org/apache/commons/math/fraction/BigFraction.java index da95b76b1..ce48cef58 100644 --- a/src/java/org/apache/commons/math/fraction/BigFraction.java +++ b/src/java/org/apache/commons/math/fraction/BigFraction.java @@ -19,6 +19,7 @@ package org.apache.commons.math.fraction; import java.math.BigDecimal; import java.math.BigInteger; +import org.apache.commons.math.FieldElement; import org.apache.commons.math.MathRuntimeException; import org.apache.commons.math.util.MathUtils; @@ -29,7 +30,7 @@ import org.apache.commons.math.util.MathUtils; * @version $Revision$ $Date$ * @since 2.0 */ -public class BigFraction extends Number implements Comparable { +public class BigFraction extends Number implements FieldElement, Comparable { /** A fraction representing "2 / 1". */ public static final BigFraction TWO = new BigFraction(2); @@ -74,7 +75,7 @@ public class BigFraction extends Number implements Comparable { public static final BigFraction TWO_THIRDS = new BigFraction(2, 3); /** Serializable version identifier. */ - private static final long serialVersionUID = -130662482360701382L; + private static final long serialVersionUID = -5630213147331578515L; /** BigInteger representation of 100. */ private static final BigInteger ONE_HUNDRED_DOUBLE = BigInteger.valueOf(100); @@ -1119,4 +1120,10 @@ public class BigFraction extends Number implements Comparable { } return str; } + + /** {@inheritDoc} */ + public BigFractionField getField() { + return BigFractionField.getInstance(); + } + } diff --git a/src/java/org/apache/commons/math/fraction/BigFractionField.java b/src/java/org/apache/commons/math/fraction/BigFractionField.java new file mode 100644 index 000000000..25131fc1e --- /dev/null +++ b/src/java/org/apache/commons/math/fraction/BigFractionField.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.math.fraction; + +import org.apache.commons.math.Field; + +/** + * Representation of the fractional numbers without any overflow field. + *

+ * This class is a singleton. + *

+ * @see Fraction + * @version $Revision$ $Date$ + * @since 2.0 + */ +public class BigFractionField implements Field { + + /** Serializable version identifier */ + private static final long serialVersionUID = -1699294557189741703L; + + /** Private constructor for the singleton. + */ + private BigFractionField() { + } + + /** Get the unique instance. + * @return the unique instance + */ + public static BigFractionField getInstance() { + return LazyHolder.INSTANCE; + } + + /** {@inheritDoc} */ + public BigFraction getOne() { + return BigFraction.ONE; + } + + /** {@inheritDoc} */ + public BigFraction getZero() { + return BigFraction.ZERO; + } + + /** Holder for the instance. + *

We use here the Initialization On Demand Holder Idiom.

+ */ + private static class LazyHolder { + /** Cached field instance. */ + private static final BigFractionField INSTANCE = new BigFractionField(); + } + +} diff --git a/src/java/org/apache/commons/math/fraction/Fraction.java b/src/java/org/apache/commons/math/fraction/Fraction.java index f4e95f8c6..1177ec30b 100644 --- a/src/java/org/apache/commons/math/fraction/Fraction.java +++ b/src/java/org/apache/commons/math/fraction/Fraction.java @@ -18,6 +18,7 @@ package org.apache.commons.math.fraction; import java.math.BigInteger; +import org.apache.commons.math.FieldElement; import org.apache.commons.math.MathRuntimeException; import org.apache.commons.math.util.MathUtils; @@ -27,7 +28,7 @@ import org.apache.commons.math.util.MathUtils; * @since 1.1 * @version $Revision$ $Date$ */ -public class Fraction extends Number implements Comparable { +public class Fraction extends Number implements FieldElement, Comparable { /** A fraction representing "2 / 1". */ public static final Fraction TWO = new Fraction(2, 1); @@ -72,7 +73,7 @@ public class Fraction extends Number implements Comparable { public static final Fraction MINUS_ONE = new Fraction(-1, 1); /** Serializable version identifier */ - private static final long serialVersionUID = 3071409609509774764L; + private static final long serialVersionUID = 3698073679419233275L; /** The denominator. */ private final int denominator; @@ -649,4 +650,9 @@ public class Fraction extends Number implements Comparable { return str; } + /** {@inheritDoc} */ + public FractionField getField() { + return FractionField.getInstance(); + } + } diff --git a/src/java/org/apache/commons/math/fraction/FractionField.java b/src/java/org/apache/commons/math/fraction/FractionField.java new file mode 100644 index 000000000..f284cbeb6 --- /dev/null +++ b/src/java/org/apache/commons/math/fraction/FractionField.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.math.fraction; + +import org.apache.commons.math.Field; + +/** + * Representation of the fractional numbers field. + *

+ * This class is a singleton. + *

+ * @see Fraction + * @version $Revision$ $Date$ + * @since 2.0 + */ +public class FractionField implements Field { + + /** Serializable version identifier */ + private static final long serialVersionUID = -1257768487499119313L; + + /** Private constructor for the singleton. + */ + private FractionField() { + } + + /** Get the unique instance. + * @return the unique instance + */ + public static FractionField getInstance() { + return LazyHolder.INSTANCE; + } + + /** {@inheritDoc} */ + public Fraction getOne() { + return Fraction.ONE; + } + + /** {@inheritDoc} */ + public Fraction getZero() { + return Fraction.ZERO; + } + + /** Holder for the instance. + *

We use here the Initialization On Demand Holder Idiom.

+ */ + private static class LazyHolder { + /** Cached field instance. */ + private static final FractionField INSTANCE = new FractionField(); + } + +} diff --git a/src/java/org/apache/commons/math/util/BigReal.java b/src/java/org/apache/commons/math/util/BigReal.java new file mode 100644 index 000000000..665f33e60 --- /dev/null +++ b/src/java/org/apache/commons/math/util/BigReal.java @@ -0,0 +1,213 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.math.util; + + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.math.MathContext; + +import org.apache.commons.math.Field; +import org.apache.commons.math.FieldElement; + +/** + * Arbitrary precision decimal number. + *

+ * This class is a simple wrapper around the standard BigDecimal + * in order to implement the {@link FieldElement} interface. + *

+ * @since 2.0 + * @version $Revision$ $Date$ + */ +public class BigReal implements FieldElement, Comparable { + + /** Serializable version identifier. */ + private static final long serialVersionUID = 7887631840434052850L; + + /** A big real representing 0. */ + public static final BigReal ZERO = new BigReal(BigDecimal.ZERO); + + /** A big real representing 1. */ + public static final BigReal ONE = new BigReal(BigDecimal.ONE); + + /** Underlying BigDecimal. */ + private final BigDecimal d; + + /** Build an instance from a BigDecimal. + * @param val value of the instance + */ + public BigReal(BigDecimal val) { + d = val; + } + + /** Build an instance from a BigInteger. + * @param val value of the instance + */ + public BigReal(BigInteger val) { + d = new BigDecimal(val); + } + + /** Build an instance from an unscaled BigInteger. + * @param unscaledVal unscaled value + * @param scale scale to use + */ + public BigReal(BigInteger unscaledVal, int scale) { + d = new BigDecimal(unscaledVal, scale); + } + + /** Build an instance from an unscaled BigInteger. + * @param unscaledVal unscaled value + * @param scale scale to use + * @param mc to used + */ + public BigReal(BigInteger unscaledVal, int scale, MathContext mc) { + d = new BigDecimal(unscaledVal, scale, mc); + } + + /** Build an instance from a BigInteger. + * @param val value of the instance + * @param mc context to use + */ + public BigReal(BigInteger val, MathContext mc) { + d = new BigDecimal(val, mc); + } + + /** Build an instance from a characters representation. + * @param in character representation of the value + */ + public BigReal(char[] in) { + d = new BigDecimal(in); + } + + /** Build an instance from a characters representation. + * @param in character representation of the value + * @param offset offset of the first character to analyze + * @param len length of the array slice to analyze + */ + public BigReal(char[] in, int offset, int len) { + d = new BigDecimal(in, offset, len); + } + + /** Build an instance from a characters representation. + * @param in character representation of the value + * @param offset offset of the first character to analyze + * @param len length of the array slice to analyze + * @param mc context to use + */ + public BigReal(char[] in, int offset, int len, MathContext mc) { + d = new BigDecimal(in, offset, len, mc); + } + + /** Build an instance from a characters representation. + * @param in character representation of the value + * @param mc context to use + */ + public BigReal(char[] in, MathContext mc) { + d = new BigDecimal(in, mc); + } + + /** Build an instance from a double. + * @param val value of the instance + */ + public BigReal(double val) { + d = new BigDecimal(val); + } + + /** Build an instance from a double. + * @param val value of the instance + * @param mc context to use + */ + public BigReal(double val, MathContext mc) { + d = new BigDecimal(val, mc); + } + + /** Build an instance from an int. + * @param val value of the instance + */ + public BigReal(int val) { + d = new BigDecimal(val); + } + + /** Build an instance from an int. + * @param val value of the instance + * @param mc context to use + */ + public BigReal(int val, MathContext mc) { + d = new BigDecimal(val, mc); + } + + /** Build an instance from a long. + * @param val value of the instance + */ + public BigReal(long val) { + d = new BigDecimal(val); + } + + /** Build an instance from a long. + * @param val value of the instance + * @param mc context to use + */ + public BigReal(long val, MathContext mc) { + d = new BigDecimal(val, mc); + } + + /** Build an instance from a String representation. + * @param val character representation of the value + */ + public BigReal(String val) { + d = new BigDecimal(val); + } + + /** Build an instance from a String representation. + * @param val character representation of the value + * @param mc context to use + */ + public BigReal(String val, MathContext mc) { + d = new BigDecimal(val, mc); + } + + /** {@inheritDoc} */ + public BigReal add(BigReal a) { + return new BigReal(d.add(a.d)); + } + + /** {@inheritDoc} */ + public BigReal subtract(BigReal a) { + return new BigReal(d.subtract(a.d)); + } + + /** {@inheritDoc} */ + public BigReal divide(BigReal a) throws ArithmeticException { + return new BigReal(d.divide(a.d)); + } + + /** {@inheritDoc} */ + public BigReal multiply(BigReal a) { + return new BigReal(d.multiply(a.d)); + } + + /** {@inheritDoc} */ + public int compareTo(BigReal a) { + return d.compareTo(a.d); + } + + /** {@inheritDoc} */ + public Field getField() { + return BigRealField.getInstance(); + } + +} diff --git a/src/java/org/apache/commons/math/util/BigRealField.java b/src/java/org/apache/commons/math/util/BigRealField.java new file mode 100644 index 000000000..5e20a6b24 --- /dev/null +++ b/src/java/org/apache/commons/math/util/BigRealField.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.math.util; + +import org.apache.commons.math.Field; + +/** + * Representation of real numbers with arbitrary precision field. + *

+ * This class is a singleton. + *

+ * @see BigReal + * @version $Revision$ $Date$ + * @since 2.0 + */ +public class BigRealField implements Field { + + /** Serializable version identifier */ + private static final long serialVersionUID = 4756431066541037559L; + + /** Private constructor for the singleton. + */ + private BigRealField() { + } + + /** Get the unique instance. + * @return the unique instance + */ + public static BigRealField getInstance() { + return LazyHolder.INSTANCE; + } + + /** {@inheritDoc} */ + public BigReal getOne() { + return BigReal.ONE; + } + + /** {@inheritDoc} */ + public BigReal getZero() { + return BigReal.ZERO; + } + + /** Holder for the instance. + *

We use here the Initialization On Demand Holder Idiom.

+ */ + private static class LazyHolder { + /** Cached field instance. */ + private static final BigRealField INSTANCE = new BigRealField(); + } + +} diff --git a/src/site/xdoc/changes.xml b/src/site/xdoc/changes.xml index b818d424d..d12ac575a 100644 --- a/src/site/xdoc/changes.xml +++ b/src/site/xdoc/changes.xml @@ -39,6 +39,11 @@ The type attribute can be add,update,fix,remove. + + Added general Field and FieldElement interfaces to allow generic algorithms + to operate on fields. The library already provides several implementations: + Complex, Fraction, BigFraction and BigReal + Fixed inconsistent access to multidimensional array in FastFourierTransformer