Java Array Subtract subtractUnitsArray(int[] units1, int[] units2)

Here you can find the source of subtractUnitsArray(int[] units1, int[] units2)

Description

Add the exponent array of this token with the argument array, and return the result in a new array.

License

Open Source License

Declaration

public static int[] subtractUnitsArray(int[] units1, int[] units2) 

Method Source Code

//package com.java2s;
/* Abstract base class for tokens that contain a scalar.
    // ww  w. j  a  v  a 2  s.co m
 Copyright (c) 1997-2008 The Regents of the University of California.
 All rights reserved.
 Permission is hereby granted, without written agreement and without
 license or royalty fees, to use, copy, modify, and distribute this
 software and its documentation for any purpose, provided that the above
 copyright notice and the following two paragraphs appear in all copies
 of this software.
    
 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.
    
 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
 ENHANCEMENTS, OR MODIFICATIONS.
    
 PT_COPYRIGHT_VERSION_2
 COPYRIGHTENDKEY
    
 @Pt.ProposedRating Yellow (neuendor)
 @Pt.AcceptedRating Red (yuhong)
    
 */

public class Main {
    /** Add the exponent array of this token with the argument array,
     *  and return the result in a new array.
     */
    public static int[] subtractUnitsArray(int[] units1, int[] units2) {
        // negate the exponents of the argument token and add to
        // this token.
        int[] negation = null;

        if (!isUnitless(units2)) {
            int length = units2.length;
            negation = new int[length];

            for (int i = 0; i < length; i++) {
                negation[i] = -units2[i];
            }
        }

        return addUnitsArray(units1, negation);
    }

    /** Return true if the given unit array is null, or the exponents for
     *  each index are zero.
     */
    public static boolean isUnitless(int[] exponents) {
        if (exponents != null) {
            for (int i = 0; i < exponents.length; i++) {
                if (exponents[i] != 0) {
                    return false;
                }
            }
        }

        return true;
    }

    /** Add the given unit arrays, and return the result in a new
     *  array.  The size of the returned array will be the maximum of
     *  the size of the two input arrays, or null if both input arrays
     *  are unitless.
     */
    public static int[] addUnitsArray(int[] units1, int[] units2) {
        boolean isUnitless1 = isUnitless(units1);
        boolean isUnitless2 = isUnitless(units2);

        if (isUnitless1 && isUnitless2) {
            return null;
        } else if (isUnitless1) {
            // units2 is not unitless.
            return copyUnitsArray(units2);
        } else if (isUnitless2) {
            // units1 is not unitless.
            return copyUnitsArray(units1);
        } else {
            // both have units.
            int units1Length = units1.length;
            int units2Length = units2.length;
            int[] result;

            if (units1Length < units2Length) {
                result = new int[units2Length];
                System.arraycopy(units2, 0, result, 0, units2Length);

                for (int i = 0; i < units1Length; i++) {
                    result[i] += units1[i];
                }
            } else {
                result = new int[units1Length];
                System.arraycopy(units1, 0, result, 0, units1Length);

                for (int i = 0; i < units2Length; i++) {
                    result[i] += units2[i];
                }
            }

            if (isUnitless(result)) {
                return null;
            }

            return result;
        }
    }

    /** Return a copy of the given units array. If the given array is
     *  unitless, then return null.
     *  @return An int array that is a copy of the unit category
     *  exponents of this token.
     */
    public static int[] copyUnitsArray(int[] units) {
        if (isUnitless(units)) {
            return null;
        }

        int length = units.length;
        int[] newUnits = new int[length];
        System.arraycopy(units, 0, newUnits, 0, length);
        return newUnits;
    }
}

Related

  1. subtractKeepPositiveValues(float[] dividend, float divisor)
  2. subtractMin(Double[] array)
  3. subtractRange(double[] accumulator, int offset, double[] values)
  4. subtractSignals(double[] s1, double[] s2)
  5. subtractTo(double a[][][], double b[][][])
  6. SubtractVec2D(double[] vec2Ret, double[] vec2A, double[] vec2B)
  7. SubtractVec3D(double[] vec3Ret, double[] vec3A, double[] vec3B)