Added the generic Field/FieldElement interfaces on top

of existing Complex, Fraction and BigFraction
Added a new BigReal class wrapping a BidDecimal and implementing FieldElement

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@766483 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2009-04-19 16:34:37 +00:00
parent f802e004ae
commit d60149c7f8
9 changed files with 509 additions and 8 deletions

View File

@ -17,10 +17,10 @@
package org.apache.commons.math.complex; package org.apache.commons.math.complex;
import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.commons.math.FieldElement;
import org.apache.commons.math.MathRuntimeException; import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.util.MathUtils; import org.apache.commons.math.util.MathUtils;
@ -40,11 +40,11 @@ import org.apache.commons.math.util.MathUtils;
* *
* @version $Revision$ $Date$ * @version $Revision$ $Date$
*/ */
public class Complex implements Serializable { public class Complex implements FieldElement<Complex> {
/** Serializable version identifier */ /** 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" */ /** The square root of -1. A number representing "0.0 + 1.0i" */
public static final Complex I = new Complex(0.0, 1.0); 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) { protected Complex createComplex(double real, double imaginary) {
return new Complex(real, imaginary); return new Complex(real, imaginary);
} }
/** {@inheritDoc} */
public ComplexField getField() {
return ComplexField.getInstance();
}
} }

View File

@ -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.
* <p>
* This class is a singleton.
* </p>
* @see Complex
* @version $Revision$ $Date$
* @since 2.0
*/
public class ComplexField implements Field<Complex> {
/** 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.
* <p>We use here the Initialization On Demand Holder Idiom.</p>
*/
private static class LazyHolder {
/** Cached field instance. */
private static final ComplexField INSTANCE = new ComplexField();
}
}

View File

@ -19,6 +19,7 @@ package org.apache.commons.math.fraction;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import org.apache.commons.math.FieldElement;
import org.apache.commons.math.MathRuntimeException; import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.util.MathUtils; import org.apache.commons.math.util.MathUtils;
@ -29,7 +30,7 @@ import org.apache.commons.math.util.MathUtils;
* @version $Revision$ $Date$ * @version $Revision$ $Date$
* @since 2.0 * @since 2.0
*/ */
public class BigFraction extends Number implements Comparable<BigFraction> { public class BigFraction extends Number implements FieldElement<BigFraction>, Comparable<BigFraction> {
/** A fraction representing "2 / 1". */ /** A fraction representing "2 / 1". */
public static final BigFraction TWO = new BigFraction(2); public static final BigFraction TWO = new BigFraction(2);
@ -74,7 +75,7 @@ public class BigFraction extends Number implements Comparable<BigFraction> {
public static final BigFraction TWO_THIRDS = new BigFraction(2, 3); public static final BigFraction TWO_THIRDS = new BigFraction(2, 3);
/** Serializable version identifier. */ /** Serializable version identifier. */
private static final long serialVersionUID = -130662482360701382L; private static final long serialVersionUID = -5630213147331578515L;
/** <code>BigInteger</code> representation of 100. */ /** <code>BigInteger</code> representation of 100. */
private static final BigInteger ONE_HUNDRED_DOUBLE = BigInteger.valueOf(100); private static final BigInteger ONE_HUNDRED_DOUBLE = BigInteger.valueOf(100);
@ -1119,4 +1120,10 @@ public class BigFraction extends Number implements Comparable<BigFraction> {
} }
return str; return str;
} }
/** {@inheritDoc} */
public BigFractionField getField() {
return BigFractionField.getInstance();
}
} }

View File

@ -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.
* <p>
* This class is a singleton.
* </p>
* @see Fraction
* @version $Revision$ $Date$
* @since 2.0
*/
public class BigFractionField implements Field<BigFraction> {
/** 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.
* <p>We use here the Initialization On Demand Holder Idiom.</p>
*/
private static class LazyHolder {
/** Cached field instance. */
private static final BigFractionField INSTANCE = new BigFractionField();
}
}

View File

@ -18,6 +18,7 @@ package org.apache.commons.math.fraction;
import java.math.BigInteger; import java.math.BigInteger;
import org.apache.commons.math.FieldElement;
import org.apache.commons.math.MathRuntimeException; import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.util.MathUtils; import org.apache.commons.math.util.MathUtils;
@ -27,7 +28,7 @@ import org.apache.commons.math.util.MathUtils;
* @since 1.1 * @since 1.1
* @version $Revision$ $Date$ * @version $Revision$ $Date$
*/ */
public class Fraction extends Number implements Comparable<Fraction> { public class Fraction extends Number implements FieldElement<Fraction>, Comparable<Fraction> {
/** A fraction representing "2 / 1". */ /** A fraction representing "2 / 1". */
public static final Fraction TWO = new Fraction(2, 1); public static final Fraction TWO = new Fraction(2, 1);
@ -72,7 +73,7 @@ public class Fraction extends Number implements Comparable<Fraction> {
public static final Fraction MINUS_ONE = new Fraction(-1, 1); public static final Fraction MINUS_ONE = new Fraction(-1, 1);
/** Serializable version identifier */ /** Serializable version identifier */
private static final long serialVersionUID = 3071409609509774764L; private static final long serialVersionUID = 3698073679419233275L;
/** The denominator. */ /** The denominator. */
private final int denominator; private final int denominator;
@ -649,4 +650,9 @@ public class Fraction extends Number implements Comparable<Fraction> {
return str; return str;
} }
/** {@inheritDoc} */
public FractionField getField() {
return FractionField.getInstance();
}
} }

View File

@ -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.
* <p>
* This class is a singleton.
* </p>
* @see Fraction
* @version $Revision$ $Date$
* @since 2.0
*/
public class FractionField implements Field<Fraction> {
/** 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.
* <p>We use here the Initialization On Demand Holder Idiom.</p>
*/
private static class LazyHolder {
/** Cached field instance. */
private static final FractionField INSTANCE = new FractionField();
}
}

View File

@ -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.
* <p>
* This class is a simple wrapper around the standard <code>BigDecimal</code>
* in order to implement the {@link FieldElement} interface.
* </p>
* @since 2.0
* @version $Revision$ $Date$
*/
public class BigReal implements FieldElement<BigReal>, Comparable<BigReal> {
/** 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<BigReal> getField() {
return BigRealField.getInstance();
}
}

View File

@ -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.
* <p>
* This class is a singleton.
* </p>
* @see BigReal
* @version $Revision$ $Date$
* @since 2.0
*/
public class BigRealField implements Field<BigReal> {
/** 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.
* <p>We use here the Initialization On Demand Holder Idiom.</p>
*/
private static class LazyHolder {
/** Cached field instance. */
private static final BigRealField INSTANCE = new BigRealField();
}
}

View File

@ -39,6 +39,11 @@ The <action> type attribute can be add,update,fix,remove.
</properties> </properties>
<body> <body>
<release version="2.0" date="TBD" description="TBD"> <release version="2.0" date="TBD" description="TBD">
<action dev="luc" type="add" >
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
</action>
<action dev="luc" type="fix" issue="MATH-257" due-to="Sebb"> <action dev="luc" type="fix" issue="MATH-257" due-to="Sebb">
Fixed inconsistent access to multidimensional array in FastFourierTransformer Fixed inconsistent access to multidimensional array in FastFourierTransformer
</action> </action>