Java Float Number Mod modulusSq(float[][] a)

Here you can find the source of modulusSq(float[][] a)

Description

Computes the squared modulus of a complex array.

License

Apache License

Parameter

Parameter Description
a complex array

Return

modulus squared array

Declaration

public static float[][] modulusSq(float[][] a) 

Method Source Code

//package com.java2s;
/*/*from  w ww .  j  av  a2  s.  c  om*/
 * Copyright 2016 Universidad Nacional de Colombia
 * 
 * Licensed 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.
 */

public class Main {
    /**
     * Computes the squared modulus of a complex array.
     * <p>
     * {@code
     * modulusSq[i][j] = Re[i][j]^2 + Im[i][j]^2
     * }
     *
     * @param a complex array
     * @return modulus squared array
     */
    public static float[][] modulusSq(float[][] a) {
        checkDimension(a);
        int M = a.length;
        int N = a[0].length / 2;

        float[][] modulusSq = new float[M][N];

        for (int i = 0; i < M; i++) {
            for (int j = 0; j < N; j++) {
                modulusSq[i][j] = (a[i][2 * j] * a[i][2 * j]) + (a[i][2 * j + 1] * a[i][2 * j + 1]);
            }
        }
        return modulusSq;
    }

    /**
     * Computes the squared modulus of a complex array.
     * <p>
     * {@code
     * modulusSq[i][j] = Re[i][j]^2 + Im[i][j]^2
     * }
     *
     * @param a complex array
     * @return modulus squared array
     */
    public static double[][] modulusSq(double[][] a) {
        checkDimension(a);
        int M = a.length;
        int N = a[0].length / 2;

        double[][] modulusSq = new double[M][N];

        for (int i = 0; i < M; i++) {
            for (int j = 0; j < N; j++) {
                modulusSq[i][j] = (a[i][2 * j] * a[i][2 * j]) + (a[i][2 * j + 1] * a[i][2 * j + 1]);
            }
        }
        return modulusSq;
    }

    private static void checkDimension(float[][] a) {
        if (a.length == 0) {
            throw new IllegalArgumentException("Arrays dimension must be greater than 0.");
        } else if (a[0].length == 0) {
            throw new IllegalArgumentException("Arrays dimension must be greater than 0.");
        }
    }

    private static void checkDimension(double[][] a) {
        if (a.length == 0) {
            throw new IllegalArgumentException("Arrays dimension must be greater than 0.");
        } else if (a[0].length == 0) {
            throw new IllegalArgumentException("Arrays dimension must be greater than 0.");
        }
    }
}

Related

  1. mod(float a, float b)
  2. mod(float a, float b)
  3. mod(float dividend, float quotient)
  4. mod2(float value)
  5. module(float[] vector)