TransformUtils.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.transform;

  22. import java.util.Arrays;

  23. import org.hipparchus.complex.Complex;
  24. import org.hipparchus.exception.LocalizedCoreFormats;
  25. import org.hipparchus.exception.MathIllegalArgumentException;

  26. /**
  27.  * Useful functions for the implementation of various transforms.
  28.  *
  29.  */
  30. public class TransformUtils {
  31.     /**
  32.      * Table of the powers of 2 to facilitate binary search lookup.
  33.      *
  34.      * @see #exactLog2(int)
  35.      */
  36.     private static final int[] POWERS_OF_TWO = {
  37.         0x00000001, 0x00000002, 0x00000004, 0x00000008, 0x00000010, 0x00000020,
  38.         0x00000040, 0x00000080, 0x00000100, 0x00000200, 0x00000400, 0x00000800,
  39.         0x00001000, 0x00002000, 0x00004000, 0x00008000, 0x00010000, 0x00020000,
  40.         0x00040000, 0x00080000, 0x00100000, 0x00200000, 0x00400000, 0x00800000,
  41.         0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000,
  42.         0x40000000
  43.     };

  44.     /** Private constructor. */
  45.     private TransformUtils() {
  46.         super();
  47.     }

  48.     /**
  49.      * Multiply every component in the given real array by the
  50.      * given real number. The change is made in place.
  51.      *
  52.      * @param f the real array to be scaled
  53.      * @param d the real scaling coefficient
  54.      * @return a reference to the scaled array
  55.      */
  56.     public static double[] scaleArray(double[] f, double d) {

  57.         for (int i = 0; i < f.length; i++) {
  58.             f[i] *= d;
  59.         }
  60.         return f;
  61.     }

  62.     /**
  63.      * Multiply every component in the given complex array by the
  64.      * given real number. The change is made in place.
  65.      *
  66.      * @param f the complex array to be scaled
  67.      * @param d the real scaling coefficient
  68.      * @return a reference to the scaled array
  69.      */
  70.     public static Complex[] scaleArray(Complex[] f, double d) {

  71.         for (int i = 0; i < f.length; i++) {
  72.             f[i] = new Complex(d * f[i].getReal(), d * f[i].getImaginary());
  73.         }
  74.         return f;
  75.     }


  76.     /**
  77.      * Builds a new two dimensional array of {@code double} filled with the real
  78.      * and imaginary parts of the specified {@link Complex} numbers. In the
  79.      * returned array {@code dataRI}, the data is laid out as follows
  80.      * <ul>
  81.      * <li>{@code dataRI[0][i] = dataC[i].getReal()},</li>
  82.      * <li>{@code dataRI[1][i] = dataC[i].getImaginary()}.</li>
  83.      * </ul>
  84.      *
  85.      * @param dataC the array of {@link Complex} data to be transformed
  86.      * @return a two dimensional array filled with the real and imaginary parts
  87.      *   of the specified complex input
  88.      */
  89.     public static double[][] createRealImaginaryArray(final Complex[] dataC) {
  90.         final double[][] dataRI = new double[2][dataC.length];
  91.         final double[] dataR = dataRI[0];
  92.         final double[] dataI = dataRI[1];
  93.         for (int i = 0; i < dataC.length; i++) {
  94.             final Complex c = dataC[i];
  95.             dataR[i] = c.getReal();
  96.             dataI[i] = c.getImaginary();
  97.         }
  98.         return dataRI;
  99.     }

  100.     /**
  101.      * Builds a new array of {@link Complex} from the specified two dimensional
  102.      * array of real and imaginary parts. In the returned array {@code dataC},
  103.      * the data is laid out as follows
  104.      * <ul>
  105.      * <li>{@code dataC[i].getReal() = dataRI[0][i]},</li>
  106.      * <li>{@code dataC[i].getImaginary() = dataRI[1][i]}.</li>
  107.      * </ul>
  108.      *
  109.      * @param dataRI the array of real and imaginary parts to be transformed
  110.      * @return an array of {@link Complex} with specified real and imaginary parts.
  111.      * @throws MathIllegalArgumentException if the number of rows of the specified
  112.      *   array is not two, or the array is not rectangular
  113.      */
  114.     public static Complex[] createComplexArray(final double[][] dataRI)
  115.         throws MathIllegalArgumentException {

  116.         if (dataRI.length != 2) {
  117.             throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
  118.                                                    dataRI.length, 2);
  119.         }
  120.         final double[] dataR = dataRI[0];
  121.         final double[] dataI = dataRI[1];
  122.         if (dataR.length != dataI.length) {
  123.             throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
  124.                                                    dataI.length, dataR.length);
  125.         }

  126.         final int n = dataR.length;
  127.         final Complex[] c = new Complex[n];
  128.         for (int i = 0; i < n; i++) {
  129.             c[i] = new Complex(dataR[i], dataI[i]);
  130.         }
  131.         return c;
  132.     }


  133.     /**
  134.      * Returns the base-2 logarithm of the specified {@code int}. Throws an
  135.      * exception if {@code n} is not a power of two.
  136.      *
  137.      * @param n the {@code int} whose base-2 logarithm is to be evaluated
  138.      * @return the base-2 logarithm of {@code n}
  139.      * @throws MathIllegalArgumentException if {@code n} is not a power of two
  140.      */
  141.     public static int exactLog2(final int n)
  142.         throws MathIllegalArgumentException {

  143.         int index = Arrays.binarySearch(POWERS_OF_TWO, n);
  144.         if (index < 0) {
  145.             throw new MathIllegalArgumentException(LocalizedFFTFormats.NOT_POWER_OF_TWO_CONSIDER_PADDING,
  146.                     n);
  147.         }
  148.         return index;
  149.     }
  150. }