Java Array Cross Product crossProduct(Object[] firstArray, Object[]... otherArrays)

Here you can find the source of crossProduct(Object[] firstArray, Object[]... otherArrays)

Description

Cross product many arrays

License

Apache License

Parameter

Parameter Description
firstArray first array that you want to cross product
otherArrays other arrays that you want to cross product

Return

cross product

Declaration

public static Object[][] crossProduct(Object[] firstArray, Object[]... otherArrays) 

Method Source Code

//package com.java2s;
/**/*from   w ww. j a  v  a 2  s .c  o  m*/
 * 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.
 */

public class Main {
    /**
     * Cross product many arrays
     * @param firstArray first array that you want to cross product
     * @param otherArrays other arrays that you want to cross product
     * @return cross product
     */
    public static Object[][] crossProduct(Object[] firstArray, Object[]... otherArrays) {
        if (otherArrays == null || otherArrays.length == 0) {
            Object[][] result = new Object[firstArray.length][1];
            for (int i = 0; i < firstArray.length; ++i) {
                result[i][0] = firstArray[i];
            }
            return result;
        }
        // computing cross product for the rest of the arrays
        Object[][] restArray = new Object[otherArrays.length - 1][];
        System.arraycopy(otherArrays, 1, restArray, 0, otherArrays.length - 1);
        Object[][] restCrossProduct = crossProduct(otherArrays[0], restArray);
        //creating and initializing result array
        Object[][] result = new Object[firstArray.length * restCrossProduct.length][];
        for (int i = 0; i < result.length; ++i) {
            result[i] = new Object[otherArrays.length + 1];
        }
        //doing the final cross product
        for (int i = 0; i < firstArray.length; ++i) {
            for (int j = 0; j < restCrossProduct.length; ++j) {
                //computing one row of result
                final int rowIdx = i * restCrossProduct.length + j;
                result[rowIdx][0] = firstArray[i];
                System.arraycopy(restCrossProduct[j], 0, result[rowIdx], 1, otherArrays.length);
            }
        }
        return result;
    }
}

Related

  1. crossMult(int value, int maximum, int coefficient)
  2. crossProduct(double x1, double y1, double x2, double y2, double x3, double y3)
  3. crossProduct(double[] output, double[] a, double[] b)
  4. CrossProduct(final double[] vect1, final double[] vect2)
  5. crossProduct(int ax, int ay, int bx, int by, int cx, int cy)