Skip to content

Utilities

Sambit Paul edited this page Jul 18, 2021 · 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]

Absolute Value of 1-D Array

Code
double[] a = {1.22, -3.41, -0.22, 5.44, -9.28};
double[] out = UtilMethods.absoluteArray(a);
Output: [1.22, 3.41, 0.22, 5.44, 9.28]

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}


Matrix Multiplication

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

\begin{bmatrix} 30 & 36 & 42\ 66 & 81 & 96\ 102 & 126 & 150 \end{bmatrix}


Matrix Transpose

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

\begin{bmatrix} 1 & 4 & 7\ 2 & 5 & 8\ 3 & 6 & 9 \end{bmatrix}


Absolute Value of a Matrix

Code
double[][] m1 = {{1.22, -3.41, -0.22}, {-0.89, 1.6, 7.65}};
double[][] out = UtilMethods.absoluteArray(m1);
Output:

\begin{bmatrix} 1.22 & 3.41 & 0.22\ 0.89 & 1.6 & 7.65\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

Rescale

Scales the input array between the new limits provided in the arguments

Code
double[] arr1 = {12, 14, 15, 15, 16};
double[] out1 = UtilMethods.rescale(arr1, 10, 20);
Output: [10, 15, 17.5, 17.5, 20]

Standardize

Standardizes the input array between the range 0 and 1

Code
double[] arr1 = {12, 14, 15, 15, 16};
double[] out1 = UtilMethods.standardize(arr1);
Output: [0, 0.5, 0.75, 0.75, 1]

Normalize

Normalizes the input array with the mean and standard deviation

Code
double[] arr1 = {12, 14, 15, 15, 16};
double[] out1 = UtilMethods.normalize(arr1);
Output: [-1.583, -0.264, 0.396, 0.396, 1.055]

Zero-Center

Zero-Centres the input array

Code
double[] arr1 = {12, 14, 15, 15, 16};
double[] out1 = UtilMethods.zeroCenter(arr1);
Output: [-2.4, -0.4, 0.6, 0.6, 1.6]

Almost Equals

Check if 2 double[] arrays are almost equals

Code
double[] test1 = {1.23320, 1.23321};
double[] test2 = {1.23310, 1.23320};
boolean test = UtilMethods.almostEquals(test1[0], test1[1], 0.0001);
Output: True

Convert To Primitive

Converts ArrayList in or to double[] or int[] respectively

Code
ArrayList<Integer> integers = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4, 5));
ArrayList<Double> numbers = new ArrayList<Double>(Arrays.asList(1.1, 2.22, 3.3, 4.4, 5.55));

int[] out1 = UtilMethods.convertToPrimitiveInt(integers);
double[] out2 = UtilMethods.convertToPrimitiveDouble(numbers);
Output:
[1, 2, 3, 4, 5]
[1.1, 2.22, 3.3, 4.4, 5.55]

Argument Operations

Provides functions like argmin(), argmax() and argsort().

Code
double[] arr = {1, 2, 5, 3, 4, 6, 1, 6};
int min1 = UtilMethods.argmin(arr, false); //Returns the last occurrence index if more than 1 min value
int min2 = UtilMethods.argmin(arr, true); //Returns the first occurrence index if more than 1 min value
int max1 = UtilMethods.argmax(arr, false); //Returns the last occurrence index if more than 1 max value
int max2 = UtilMethods.argmax(arr, true); //Returns the first occurrence index if more than 1 max value

double[] test1 = {1.23, 4.55, -1.33, 2.45, 6.78, 1.29};
int[] sortedIndices = UtilMethods.argsort(test1, true);
Output:
Min Indices: 0, 6
Max Indices: 5, 7
sortedIndices: [2, 0, 5, 3, 1, 4]

Scalar Operations on 1-D Arrays

Allows scalar operations of a single value to an array

Code
double[] signal = {1.23, 6.54, 4.56, 9.04, 2.88};
double[] addArr = UtilMethods.scalarArithmetic(signal, 1.02, "add");
double[] subArr = UtilMethods.scalarArithmetic(signal, 1.02, "sub");
double[] mulArr = UtilMethods.scalarArithmetic(signal, 1.02, "mul");
double[] divArr = UtilMethods.scalarArithmetic(signal, 1.02, "div");
Output:
addArr: [2.25, 7.56, 5.58, 10.06, 3.9]
subArr: [0.21, 5.52, 3.54, 8.02, 1.86]
mulArr: [1.2546, 6.6708, 4.6512, 9.2208, 2.9376]
divArr: [1.2059, 6.4118, 4.4706, 8.8627, 2.8235]

Trigonometric Operations on 1-D Arrays

Allows scalar operations of a single value to an array

Code
double[] signal = {1.23, 6.54, 4.56, 9.04, 2.88};
double[] addArr = UtilMethods.scalarArithmetic(signal, 1.02, "add");
double[] subArr = UtilMethods.scalarArithmetic(signal, 1.02, "sub");
double[] mulArr = UtilMethods.scalarArithmetic(signal, 1.02, "mul");
double[] divArr = UtilMethods.scalarArithmetic(signal, 1.02, "div");
Output:
addArr: [2.25, 7.56, 5.58, 10.06, 3.9]
subArr: [0.21, 5.52, 3.54, 8.02, 1.86]
mulArr: [1.2546, 6.6708, 4.6512, 9.2208, 2.9376]
divArr: [1.2059, 6.4118, 4.4706, 8.8627, 2.8235]

ECG Signal

Returns an electrocardiogram as an example for a 1-D signal.

Code
double[] data = UtilMethods.electrocardiogram();

Log and Antilog

Hankel and Toeplitx Matrices

Sinc Function

Chebyshev Evaluation

Element-by-Element Operations of Matrices

Code
abc
Output:

\begin{bmatrix} 1.22 & 3.41 & 0.22\ 0.89 & 1.6 & 7.65\end{bmatrix}


Clone this wiki locally