Java Array Has arrayHasContiguousRowEntries(double[] aVector)

Here you can find the source of arrayHasContiguousRowEntries(double[] aVector)

Description

Test a vector to see if there is a block of contiguous nonzero values present.

License

Open Source License

Parameter

Parameter Description
aVector the vector to be tested

Return

true if a contiguous block exists, false otherwise. If a vector is entirely composed of zeros it returns true as the data is contiguous but not present!

Declaration

public static boolean arrayHasContiguousRowEntries(double[] aVector) 

Method Source Code

//package com.java2s;
/**//from w  ww  .  j  av  a2s  .co  m
 * Copyright (C) 2011 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */

public class Main {
    /**
     * Test a vector to see if there is a block of contiguous nonzero values present.
     * @param aVector the vector to be tested
     * @return true if a contiguous block exists, false otherwise. If a vector is entirely composed of zeros it returns true as the data is contiguous but not present!
     */
    public static boolean arrayHasContiguousRowEntries(double[] aVector) {
        int nnz = numberOfNonZeroElementsInVector(aVector);
        if (nnz == 0) {
            return true;
        }
        if (nnz == 1) {
            return true;
        }
        int dataStartsAt = 0;
        for (int i = 0; i < aVector.length; i++) {
            if (Double.doubleToLongBits(aVector[i]) != 0L) {
                dataStartsAt = i;
                break;
            }
        }
        int dataEndsAt = aVector.length - 1;
        for (int i = aVector.length - 1; i > 0; i--) {
            if (Double.doubleToLongBits(aVector[i]) != 0L) {
                dataEndsAt = i;
                break;
            }
        }
        if (dataEndsAt - dataStartsAt + 1 == nnz) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Counts number of *true* nonzero elements in a vector
     * @param aVector which is the vector (array of doubles) being tested
     * @return tmp the number of nonzero elements in the vector
     */
    public static int numberOfNonZeroElementsInVector(double[] aVector) {
        int tmp = 0;
        for (int i = 0; i < aVector.length; i++) {
            if (Double.doubleToLongBits(aVector[i]) != 0L) {
                tmp++;
            }
        }
        return tmp;
    }

    /**
     * Counts number of *true* nonzero elements in a vector
     * @param aVector which is the vector (array of doubles) being tested
     * @return tmp the number of nonzero elements in the vector
     */
    public static int numberOfNonZeroElementsInVector(int[] aVector) {
        int tmp = 0;
        for (int i = 0; i < aVector.length; i++) {
            if (Double.doubleToLongBits(aVector[i]) != 0L) {
                tmp++;
            }
        }
        return tmp;
    }
}

Related

  1. arrayHas(int[] arr, int val)
  2. arrayHasContent(Object[] array)
  3. arrayHasElements(Object[] obj)
  4. arrayHasTaints(int[] a)
  5. inArray(byte needle, byte[] haystack)
  6. inArray(byte needle, byte[] haystack)