Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2015 Vehbi Sinan Tunalioglu <vst@vsthost.com>.
   *
   * 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.
  */
 
 package com.vsthost.rnd.commons.math.ext.linear;
 
 
 import java.util.List;
This class provides utilities for matrices implemented as ordinary arrays of double arrays.
 
 public class DMatrixUtils {
    
Returns the sum of the vector.

Parameters:
vector The input vector as a double array
Returns:
The sum of the vector
 
     public static double sum (double[] vector) {
         // Declare and initialize the accumulator:
         double total = 0.0;
 
         // Iterate over the vector:
         for (int i = 0; i < vector.lengthi++) {
             total += vector[i];
         }
 
         // Done, return:
         return total;
     }

    
Returns the sum of the absolute values in the vector.

Parameters:
vector The input vector as a double array
Returns:
The sum of the absolute values in the vector
 
     public static double sumOfAbsolutes (double[] vector) {
         // Declare and initialize the accumulator:
         double total = 0.0;
 
         // Iterate over the vector:
         for (int i = 0; i < vector.lengthi++) {
             total += Math.abs(vector[i]);
         }
 
         // Done, return:
         return total;
     }

    
Returns the mean of the vector.

Parameters:
vector The input vector as a double array
Returns:
The mean of the vector
 
     public static double mean(double[] vector) {
         return sum(vector) / vector.length;
     }

    
Returns the median of the vector.

Parameters:
vector The input vector as a double array
Returns:
The median of the vector
 
     public static double median (double[] vector) {
         final double[] sorted = vector.clone();
         Arrays.sort(sorted);
         if (vector.length % 2 == 1) {
             return sorted[vector.length / 2];
         }
         return (sorted[vector.length / 2 - 1] + sorted[vector.length / 2]) / 2;
     }

    
Generates a sequence of specified length from specified start to specified end.

Parameters:
start The number to start with.
end The number to end with.
length The length of the desired sequence.
Returns:
A sequence of length length starting with start and ending with end.
    public static double[] sequence (double startdouble endint length) {
        // Declare and initialize the return value:
        double[] retval = new double[length];
        // Calculate step:
        double step = (end - start) / (length == 1 ? 1 : length - 1);
        // Iterate and fill:
        for (int i = 0; i < lengthi++) {
            retval[i] = start + (i * step);
        }
        // Done, return;
        return retval;
    }

    
Creates and returns an array of length n with all values set to value.

Parameters:
value The value to be filled.
n The desired length of the array.
Returns:
An array of length n with all values set to value
    public static double[] repeat (double valueint n) {
        // Initialize the return array:
        double[] retval = new double[n];
        // Fill the array:
        Arrays.fill(retvalvalue);
        // Done, return the filled array:
        return retval;
    }

    
Clones the given matrix.

Parameters:
matrix The matrix to be cloned.
Returns:
The cloned matrix.
    public static double[][] cloneMatrix (double[][] matrix) {
        return MatrixUtils.createRealMatrix(matrix).getData();
    }

    
Creates a new array by selecting those elements marked as true in the predicate array.

Parameters:
values The array where the elements are going to be selected from.
predicate The selection mapper.
Returns:
The new array with selected items.
    public static String[] selectByPredicate (String[] valuesBoolean[] predicate) {
        // Define the filtered list:
        List<Stringfiltered = new ArrayList<String>();
        // Iterate and populate:
        for (int i = 0; i < predicate.lengthi++) {
            if (predicate[i]) {
                filtered.add(values[i]);
            }
        }
        // Save the value:
        return filtered.toArray(new String[0]);
    }

    
Creates a new array by selecting those elements marked as true in the predicate array.

Parameters:
values The array where the elements are going to be selected from.
predicate The selection mapper.
Returns:
The new array with selected items.
    public static double[] selectByPredicate (double[] valuesboolean[] predicate) {
        // Define the filtered list:
        List<Doublefiltered = new ArrayList<>();
        // Iterate and populate:
        for (int i = 0; i < predicate.lengthi++) {
            if (predicate[i]) {
                filtered.add(values[i]);
            }
        }
        // Save the value:
        return ArrayUtils.toPrimitive(filtered.toArray(new Double[0]));
    }

    
Creates a new array by selecting those elements marked as true in the predicate array.

Parameters:
values The array where the elements are going to be selected from.
predicate The selection mapper.
Returns:
The new array with selected items.
    public static double[] selectByPredicate (double[] valuesBoolean[] predicate) {
        return DMatrixUtils.selectByPredicate(values, ArrayUtils.toPrimitive(predicate));
    }

    
Get the order of the specified elements in descending or ascending order.

Parameters:
values A vector of double values.
indices The indices which will be considered for ordering.
descending Flag indicating if we go descending or not.
Returns:
A vector of indices sorted in the provided order.
    public static int[] getOrder(double[] valuesint[] indicesboolean descending) {
        // Create an index series:
        Integer[] opIndices = ArrayUtils.toObject(indices);
        // Sort indices:
        Arrays.sort(opIndicesnew Comparator<Integer>() {
            @Override
            public int compare(Integer o1Integer o2) {
                if (descending) {
                    return Double.compare(values[o2], values[o1]);
                } else {
                    return Double.compare(values[o1], values[o2]);
                }
            }
        });
        return ArrayUtils.toPrimitive(opIndices);
    }

    
Get the order of the elements in descending or ascending order.

Parameters:
values A vector of double values.
descending Flag indicating if we go descending or not.
Returns:
A vector of indices sorted in the provided order.
    public static int[] getOrder(double[] valuesboolean descending) {
        return DMatrixUtils.getOrder(values, IntStream.range(0, values.length).toArray(), descending);
    }

    
Get the order of the elements in ascending order.

Parameters:
values A vector of double values.
Returns:
A vector of indices sorted in the ascending order.
    public static int[] getOrder(double[] values) {
        return DMatrixUtils.getOrder(valuesfalse);
    }

    
Returns the DOWN rounded value of the given value for the given steps.

Parameters:
value The original value to be rounded.
steps The steps.
Returns:
The DOWN rounded value of the given value for the given steps.
    public static BigDecimal roundDownTo(double valuedouble steps) {
        final BigDecimal bValue = BigDecimal.valueOf(value);
        final BigDecimal bSteps = BigDecimal.valueOf(steps);
        if (bSteps == .) {
            return bValue;
        } else {
            return bValue.divide(bSteps, 0, .).multiply(bSteps);
        }
    }

    
Returns the UP rounded value of the given value for the given steps.

Parameters:
value The original value to be rounded.
steps The steps.
Returns:
The UP rounded value of the given value for the given steps.
    public static BigDecimal roundUpTo(double valuedouble steps) {
        final BigDecimal bValue = BigDecimal.valueOf(value);
        final BigDecimal bSteps = BigDecimal.valueOf(steps);
        if (bSteps == .) {
            return bValue;
        } else {
            return bValue.divide(bSteps, 0, .).multiply(bSteps);
        }
    }

    
Returns the closest rounded value of the given value for the given steps.

Parameters:
value The original value to be rounded.
steps The steps.
Returns:
The closest rounded value of the given value for the given steps.
    public static BigDecimal roundToClosest(double valuedouble steps) {
        final BigDecimal down = DMatrixUtils.roundDownTo(valuesteps);
        final BigDecimal up = DMatrixUtils.roundUpTo(valuesteps);
        final BigDecimal orig = new BigDecimal(String.valueOf(value));
        if (orig.subtract(down).abs().compareTo(orig.subtract(up).abs()) < 0) {
            return down;
        }
        return up;
    }
New to GrepCode? Check out our FAQ X