Return-Path: Delivered-To: apmail-commons-commits-archive@locus.apache.org Received: (qmail 46788 invoked from network); 28 Dec 2008 18:10:01 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 28 Dec 2008 18:10:01 -0000 Received: (qmail 90021 invoked by uid 500); 28 Dec 2008 18:09:59 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 89952 invoked by uid 500); 28 Dec 2008 18:09:59 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 89943 invoked by uid 99); 28 Dec 2008 18:09:59 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 28 Dec 2008 10:09:59 -0800 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 28 Dec 2008 18:09:49 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 5450623888E6; Sun, 28 Dec 2008 10:09:29 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r729758 - in /commons/proper/math/trunk/src/java/org/apache/commons/math: ./ transform/ Date: Sun, 28 Dec 2008 18:09:29 -0000 To: commits@commons.apache.org From: luc@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20081228180929.5450623888E6@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: luc Date: Sun Dec 28 10:09:28 2008 New Revision: 729758 URL: http://svn.apache.org/viewvc?rev=729758&view=rev Log: Added a RealTransformer interface on top of cosine, sine and Hadamard transforms. The Fourier transform cannot implement this interface since it produces complex results. Improved error messages. Added: commons/proper/math/trunk/src/java/org/apache/commons/math/transform/RealTransformer.java (with props) Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/MessagesResources_fr.java commons/proper/math/trunk/src/java/org/apache/commons/math/transform/FastCosineTransformer.java commons/proper/math/trunk/src/java/org/apache/commons/math/transform/FastFourierTransformer.java commons/proper/math/trunk/src/java/org/apache/commons/math/transform/FastHadamardTransformer.java commons/proper/math/trunk/src/java/org/apache/commons/math/transform/FastSineTransformer.java Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/MessagesResources_fr.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/MessagesResources_fr.java?rev=729758&r1=729757&r2=729758&view=diff ============================================================================== --- commons/proper/math/trunk/src/java/org/apache/commons/math/MessagesResources_fr.java (original) +++ commons/proper/math/trunk/src/java/org/apache/commons/math/MessagesResources_fr.java Sun Dec 28 10:09:28 2008 @@ -343,6 +343,26 @@ { "{0} is not a power of 2", "{0} n''est pas une puissance de 2" }, + // org.apache.commons.math.transform.FastFourierTransformer + { "cannot compute 0-th root of unity, indefinite result", + "impossible de calculer la racine z\u00e9roi\u00e8me de l''unit\u00e9, r\u00e9sultat ind\u00e9fini" }, + { "number of sample is not positive: {0}", + "le nombre d''\u00e9chantillons n''est pas positif : {0}" }, + { "{0} is not a power of 2, consider padding for fix", + "{0} n''est pas une puissance de 2, ajoutez des \u00e9l\u00e9ments pour corriger" }, + { "endpoints do not specify an interval: [{0}, {1}]", + "les extr\u00e9mit\u00e9s ne constituent pas un intervalle : [{0}, {1}]" }, + { "some dimensions don't math: {0} != {1}", + "certaines dimensions sont incoh\u00e9rentes : {0} != {1}" }, + + // org.apache.commons.math.transform.FastCosineTransformer + { "{0} is not a power of 2 plus one", + "{0} n''est pas une puissance de 2 plus un" }, + + // org.apache.commons.math.transform.FastSineTransformer + { "first element is not 0: {0}", + "le premier \u00e9l\u00e9ment n''est pas nul : {0}" }, + // org.apache.commons.math.util.OpenIntToDoubleHashMap { "map has been modified while iterating", "la table d''adressage a \u00e9t\u00e9 modifi\u00e9e pendant l''it\u00e9ration" }, Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/transform/FastCosineTransformer.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/transform/FastCosineTransformer.java?rev=729758&r1=729757&r2=729758&view=diff ============================================================================== --- commons/proper/math/trunk/src/java/org/apache/commons/math/transform/FastCosineTransformer.java (original) +++ commons/proper/math/trunk/src/java/org/apache/commons/math/transform/FastCosineTransformer.java Sun Dec 28 10:09:28 2008 @@ -16,10 +16,10 @@ */ package org.apache.commons.math.transform; -import java.io.Serializable; -import org.apache.commons.math.analysis.*; -import org.apache.commons.math.complex.*; -import org.apache.commons.math.MathException; +import org.apache.commons.math.FunctionEvaluationException; +import org.apache.commons.math.MathRuntimeException; +import org.apache.commons.math.analysis.UnivariateRealFunction; +import org.apache.commons.math.complex.Complex; /** * Implements the = upper) { - throw new IllegalArgumentException - ("Endpoints do not specify an interval: [" + lower + - ", " + upper + "]"); + throw MathRuntimeException.createIllegalArgumentException("endpoints do not specify an interval: [{0}, {1}]", + new Object[] { lower, upper }); } } @@ -577,9 +571,10 @@ * @param mdca Multi-Dimensional Complex Array id est Complex[][][][] * @param forward inverseTransform2 is preformed if this is false * @return transform of mdca as a Multi-Dimensional Complex Array id est Complex[][][][] - * @throws MathException if any dimension is not a power of two + * @throws IllegalArgumentException if any dimension is not a power of two */ - public Object mdfft(Object mdca, boolean forward) throws MathException { + public Object mdfft(Object mdca, boolean forward) + throws IllegalArgumentException { MultiDimensionalComplexMatrix mdcm = (MultiDimensionalComplexMatrix) new MultiDimensionalComplexMatrix(mdca).clone(); int[] dimensionSize = mdcm.getDimensionSizes(); @@ -597,10 +592,11 @@ * @param forward inverseTransform2 is preformed if this is false * @param d index of the dimension to process * @param subVector recursion subvector - * @throws MathException if any dimension is not a power of two + * @throws IllegalArgumentException if any dimension is not a power of two */ private void mdfft(MultiDimensionalComplexMatrix mdcm, boolean forward, - int d, int[] subVector) throws MathException { + int d, int[] subVector) + throws IllegalArgumentException { int[] dimensionSize = mdcm.getDimensionSizes(); //if done if (subVector.length == dimensionSize.length) { @@ -646,8 +642,8 @@ * http://jcp.org/en/jsr/detail?id=83 * may require additional exception throws for other basic requirements. */ - private class MultiDimensionalComplexMatrix implements Serializable, - Cloneable { + private class MultiDimensionalComplexMatrix + implements Serializable, Cloneable { /** Serializable version identifier. */ private static final long serialVersionUID = 0x564FCD47EBA8169BL; @@ -693,11 +689,20 @@ * Get a matrix element. * @param vector indices of the element * @return matrix element + * @exception IllegalArgumentException if dimensions do not match */ - public Complex get(int... vector) { - if ((vector == null && dimensionSize.length > 1) || - (vector != null && vector.length != dimensionSize.length)) { - throw new IllegalArgumentException("Number of dimensions must match"); + public Complex get(int... vector) + throws IllegalArgumentException { + if (vector == null && dimensionSize.length > 1) { + throw MathRuntimeException.createIllegalArgumentException("some dimensions don't math: {0} != {1}", + new Object[] { 0, dimensionSize.length }); + } + if (vector != null && vector.length != dimensionSize.length) { + throw MathRuntimeException.createIllegalArgumentException("some dimensions don't math: {0} != {1}", + new Object[] { + vector.length, + dimensionSize.length + }); } Object lastDimension = multiDimensionalComplexArray; @@ -713,11 +718,20 @@ * @param magnitude magnitude of the element * @param vector indices of the element * @return the previous value + * @exception IllegalArgumentException if dimensions do not match */ - public Complex set(Complex magnitude, int... vector) { - if ((vector == null && dimensionSize.length > 1) || - (vector != null && vector.length != dimensionSize.length)) { - throw new IllegalArgumentException("Number of dimensions must match"); + public Complex set(Complex magnitude, int... vector) + throws IllegalArgumentException { + if (vector == null && dimensionSize.length > 1) { + throw MathRuntimeException.createIllegalArgumentException("some dimensions don't math: {0} != {1}", + new Object[] { 0, dimensionSize.length }); + } + if (vector != null && vector.length != dimensionSize.length) { + throw MathRuntimeException.createIllegalArgumentException("some dimensions don't math: {0} != {1}", + new Object[] { + vector.length, + dimensionSize.length + }); } Object lastDimension = multiDimensionalComplexArray; Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/transform/FastHadamardTransformer.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/transform/FastHadamardTransformer.java?rev=729758&r1=729757&r2=729758&view=diff ============================================================================== --- commons/proper/math/trunk/src/java/org/apache/commons/math/transform/FastHadamardTransformer.java (original) +++ commons/proper/math/trunk/src/java/org/apache/commons/math/transform/FastHadamardTransformer.java Sun Dec 28 10:09:28 2008 @@ -16,9 +16,9 @@ */ package org.apache.commons.math.transform; -import java.io.Serializable; - +import org.apache.commons.math.FunctionEvaluationException; import org.apache.commons.math.MathRuntimeException; +import org.apache.commons.math.analysis.UnivariateRealFunction; /** * Implements the Fast Hadamard Transform (FHT). @@ -26,22 +26,35 @@ * @version $Revision$ $Date$ * @since 2.0 */ -public class FastHadamardTransformer implements Serializable { +public class FastHadamardTransformer implements RealTransformer { /** Serializable version identifier. */ - private static final long serialVersionUID = 5044269102877526860L; + private static final long serialVersionUID = -710169279109099264L; - /** - * Wrapper method for fht() for double vectors - * - * @param x input vector - * @return y output vector - * @throws IllegalArgumentException - */ - public double[] transform(double x[]) throws IllegalArgumentException { - return fht(x); + /** {@inheritDoc} */ + public double[] transform(double f[]) throws IllegalArgumentException { + return fht(f); } + /** {@inheritDoc} */ + public double[] transform(UnivariateRealFunction f, + double min, double max, int n) + throws FunctionEvaluationException, IllegalArgumentException { + return fht(FastFourierTransformer.sample(f, min, max, n)); + } + + /** {@inheritDoc} */ + public double[] inversetransform(double f[]) + throws IllegalArgumentException { + return fht(f); + } + + /** {@inheritDoc} */ + public double[] inversetransform(UnivariateRealFunction f, + double min, double max, int n) + throws FunctionEvaluationException, IllegalArgumentException { + return fht(FastFourierTransformer.sample(f, min, max, n)); + } /** * The FHT (Fast Hadamard Transformation) which uses only subtraction and addition. @@ -123,16 +136,11 @@ protected double[] fht(double x[]) throws IllegalArgumentException { // n is the row count of the input vector x - int n = x.length; + final int n = x.length; + final int halfN = n / 2; // n has to be of the form n = 2^p !! - int p = 0; - int twoP = 1; - while (twoP < n) { - ++p; - twoP *= 2; - } - if (n != twoP) { + if (!FastFourierTransformer.isPowerOf2(n)) { throw MathRuntimeException.createIllegalArgumentException("{0} is not a power of 2", new Object[] { n }); } @@ -143,7 +151,7 @@ double[] yCurrent = x.clone(); // iterate from left to right (column) - for (int j = 0; j < p; j++) { + for (int j = 1; j < n; j <<= 1) { // switch columns final double[] yTmp = yCurrent; @@ -151,16 +159,17 @@ yPrevious = yTmp; // iterate from top to bottom (row) - for (int i = 0; i < n; i++) { - if (i < n / 2) { - // Dtop - // The top part works with addition - yCurrent[i] = yPrevious[i*2] + yPrevious[i*2 +1]; - } else { - // Dbottom - // The bottom part works with subtraction - yCurrent[i] = yPrevious[(i-n/2)*2] - yPrevious[(i-n/2)*2 +1]; - } + for (int i = 0; i < halfN; ++i) { + // Dtop + // The top part works with addition + final int twoI = 2 * i; + yCurrent[i] = yPrevious[twoI] + yPrevious[twoI + 1]; + } + for (int i = halfN; i < n; ++i) { + // Dbottom + // The bottom part works with subtraction + final int twoI = 2 * i; + yCurrent[i] = yPrevious[twoI - n] - yPrevious[twoI - n + 1]; } } Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/transform/FastSineTransformer.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/transform/FastSineTransformer.java?rev=729758&r1=729757&r2=729758&view=diff ============================================================================== --- commons/proper/math/trunk/src/java/org/apache/commons/math/transform/FastSineTransformer.java (original) +++ commons/proper/math/trunk/src/java/org/apache/commons/math/transform/FastSineTransformer.java Sun Dec 28 10:09:28 2008 @@ -16,10 +16,10 @@ */ package org.apache.commons.math.transform; -import java.io.Serializable; import org.apache.commons.math.analysis.*; import org.apache.commons.math.complex.*; -import org.apache.commons.math.MathException; +import org.apache.commons.math.FunctionEvaluationException; +import org.apache.commons.math.MathRuntimeException; /** * Implements the Such transforms include {@link FastSineTransformer sine transform}, + * {@link FastCosineTransformer cosine transform} or {@link + * FastHadamardTransformer Hadamard transform}. {@link FastFourierTransformer + * Fourier transform} is of a different kind and does not implement this + * interface since it produces {@link Complex complex} results instead of real + * ones. + *

+ * @version $Revision$ $Date$ + * @since 2.0 + */ +public interface RealTransformer extends Serializable { + + /** + * Transform the given real data set. + * @param f the real data array to be transformed (signal) + * @return the real transformed array (spectrum) + * @throws IllegalArgumentException if any parameters are invalid + */ + double[] transform(double f[]) + throws IllegalArgumentException; + + /** + * Transform the given real function, sampled on the given interval. + * @param f the function to be sampled and transformed + * @param min the lower bound for the interval + * @param max the upper bound for the interval + * @param n the number of sample points + * @return the real transformed array + * @throws FunctionEvaluationException if function cannot be evaluated + * at some point + * @throws IllegalArgumentException if any parameters are invalid + */ + double[] transform(UnivariateRealFunction f, double min, double max, int n) + throws FunctionEvaluationException, IllegalArgumentException; + + /** + * Inversely transform the given real data set. + * @param f the real data array to be inversely transformed (spectrum) + * @return the real inversely transformed array (signal) + * @throws IllegalArgumentException if any parameters are invalid + */ + public abstract double[] inversetransform(double f[]) + throws IllegalArgumentException; + + /** + * Inversely transform the given real function, sampled on the given interval. + * @param f the function to be sampled and inversely transformed + * @param min the lower bound for the interval + * @param max the upper bound for the interval + * @param n the number of sample points + * @return the real inversely transformed array + * @throws FunctionEvaluationException if function cannot be evaluated + * at some point + * @throws IllegalArgumentException if any parameters are invalid + */ + double[] inversetransform(UnivariateRealFunction f, double min, double max, int n) + throws FunctionEvaluationException, IllegalArgumentException; + +} \ No newline at end of file Propchange: commons/proper/math/trunk/src/java/org/apache/commons/math/transform/RealTransformer.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/math/trunk/src/java/org/apache/commons/math/transform/RealTransformer.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision