Skip to content

Utilities

Sambit Paul edited this page Jun 16, 2020 · 21 revisions

Linspace

Code
int start = 2;
int stop = 3;
int samples = 5;
boolean includeEnd = true;
double[] out1 = UtilMethods.linspace(start, stop, samples, includeEnd);
Output: [2.0, 2.25, 2.50, 2.75, 3.0]

Linspace with Repeated Elements

Code
int start = 2;
int stop = 3;
int samples = 5;
int repeats = 2;
double[] out = UtilMethods.linspace(start, stop, samples, repeats);
Output: [2.0, 2.25, 2.50, 2.75, 3.0, 2.0, 2.25, 2.50, 2.75, 3.0]

Arange

Code
double start = 3.0; //Can be int
double stop = 9.0; //Can be int
double step = 0.5; //Can be int
double[] out = UtilMethods.arange(start, stop, step);
Output: [3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5]

Reverse Array

Code
double[] arr = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0}; //Can be int[]
double[] out = UtilMethods.reverse(arr);
Output: [6.0, 5.0, 4.0, 3.0, 2.0, 1.0]

Concatenate Arrays

Code
double[] arr1 = {1.0, 2.0}; //Can be int[]
double[] arr2 = {3.0, 4.0, 5.0, 6.0}; //Can be int[]

double[] out = UtilMethods.concatenateArray(arr1, arr2);
Output: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]

Split Arrays By Index

Code
double[] signal = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0}; //Can be int[]
int start = 2;
int stop = 4;
double[] out = UtilMethods.splitByIndex(signal, start, stop);
Output: [3.0, 4.0]

Pseudo-Inverse of 2-D Matrix

Code
double[][] matrix = {{1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}, {7.0, 8.0, 9.0}};
double[][] out = UtilMethods.pseudoInverse(matrix);
Output:

\begin{bmatrix} -0.639 & -0.167 & 0.306\ -0.056 & 0.000 & 0.056\ 0.528 & 0.167 & -0.194 \end{bmatrix}


Padding Array

Works in 5 modes:

  1. Reflect
  2. Constant
  3. Nearest
  4. Mirror
  5. Wrap
Code
double[] signal = {2, 8, 0, 4, 1, 9, 9, 0};
double[] reflect = UtilMethods.padSignal(signal, "reflect");
double[] constant = UtilMethods.padSignal(signal, "constant");
double[] nearest = UtilMethods.padSignal(signal, "nearest");
double[] mirror = UtilMethods.padSignal(signal, "mirror");
double[] wrap = UtilMethods.padSignal(signal, "wrap");
Output:
reflect: [0, 9, 9, 1, 4, 0, 8, 2, 2, 8, 0, 4, 1, 9, 9, 0, 0, 9, 9, 1, 4, 0, 8, 2]
constant: [0, 0, 0, 0, 0, 0, 0, 0, 2, 8, 0, 4, 1, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0]
nearest: [2, 2, 2, 2, 2, 2, 2, 2, 2, 8, 0, 4, 1, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0]
mirror: [9, 0, 9, 9, 1, 4, 0, 8, 2, 8, 0, 4, 1, 9, 9, 0, 9, 9, 1, 4, 0, 8, 2, 8]
wrap: [2, 8, 0, 4, 1, 9, 9, 0, 2, 8, 0, 4, 1, 9, 9, 0, 2, 8, 0, 4, 1, 9, 9, 0]

Diff

Calculates the deltas between elements in an array.

Code
double[] seq = {1, 2, 3, 4, 6, -4};
double[] out = UtilMethods.diff(seq);
Output: [1, 1, 1, 2, -10]

Unwrap

(by changing deltas between values to 2*pi complement)

Code
double[] seq = {0.0 , 0.78539816, 1.57079633, 5.49778714, 6.28318531};
double[] out = UtilMethods.unwrap(seq);
Output: [0.0, 0.785, 1.571, -0.785, 0.0]

Round

Helps in rounding a number to nth decimal place.

Code
double val = 123.45667;
double out = UtilMethods.round(val, 1);
Output: 123.5

Python version of modulo

Code
double divisor = -2;
double dividend = 4;
double out = UtilMethods.modulo(divisor, dividend);
Output: 2
Clone this wiki locally