Skip to content

Utilities

psambit9791 edited this page Dec 2, 2023 · 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]

Flatten a 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.flattenMatrix(matrix);
Output: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]

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 trigonometric operations on an array element-wise

Code
double[] arr1 = {1.23, 6.54, 4.56, 9.04, 2.88};
double[] sinArr = UtilMethods.trigonometricArithmetic(arr1, "sin");
double[] cosArr = UtilMethods.trigonometricArithmetic(arr1, "cos");
double[] tanArr = UtilMethods.trigonometricArithmetic(arr1, "tan");

double[] arr2 = {-0.92, -0.38, 0.25, 0.55, 0.98};
double[] asinArr = UtilMethods.trigonometricArithmetic(arr2, "asin");
double[] acosArr = UtilMethods.trigonometricArithmetic(arr2, "acos");
double[] atanArr = UtilMethods.trigonometricArithmetic(arr2, "atan");
Output:
sinArr: [0.9425 , 0.2540 , -0.9884, 0.3754, 0.2586]
cosArr: [0.3342, 0.9672, -0.1518, -0.9269 , -0.9660]
tanArr: [2.8198, 0.2626, 6.5113, -0.4050, -0.2677]
asinArr: [-1.1681, -0.3898, 0.2527, 0.5824, 1.3705]
acosArr: [2.7389, 1.9606, 1.3181, 0.9884, 0.2003]
atanArr: [-0.7438, -0.3631, 0.2450, 0.5028, 0.7753]

ECG Signal

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

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

Log and Antilog

Computes the log and antilog of a value using the base.

Log Code
int base = 2;
int input_val = 256;
double out = UtilMethods.log(input_val, base);
Output:
out: 8.0
Antilog Code
int base = 10;
int input_val = 122.0;
double out = UtilMethods.log(input_val, base);
Output:
out: 2.08636

Toeplitz and Hankel Matrices

These functions are used to construct a Toeplitz and Hankel matrix from the input vector. For Hankel, the last row can also be defined.

Toeplitz Code
double[] c = {1,2,3,4};
double[][] output = UtilMethods.toeplitz(c);
Output:
$$\begin{bmatrix} 1 & 2 & 3 & 4\\ 2 & 1 & 2 & 3\\ 3 & 2 & 1 & 2\\ 4 & 3 & 2 & 1\end{bmatrix}$$
Hankel Code
double[] c = {1, 2, 3, 4};
double[] r = {4, 7, 7, 8, 9};
double[][] output = UtilMethods.hankel(c, r);
Output:
$$\begin{bmatrix} 1 & 2 & 3 & 4 & 7\\ 2 & 3 & 4 & 7 & 7\\ 3 & 4 & 7 & 7 & 8\\ 4 & 7 & 7 & 8 & 9\end{bmatrix}$$

Sinc Function

Computes the normalised sinc function for the input value.

Code
double x_num = 0.23;
double out_num = UtilMethods.sinc(x_num);
double[] x_arr = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9};
double[] out_arr = UtilMethods.sinc(x_arr);
Output:
out_num: 0.9152265417
out_arr: [0.9836, 0.9355, 0.8584, 0.7568, 0.6366, 0.5046, 0.3679, 0.2339, 0.1093]

Chebyshev Evaluation

Evaluate a Chebyshev series at point(s) x

Code
double[] arr = {1000.0, 2.0, 3.4, 17.0, 50.0};

double x_val = 2.0;
double out_val = UtilMethods.chebyEval(x_val, arr);

double[] x_arr = {1.0, 2.0, 3.0, 4.0, 5.0};
double[] out_arr = UtilMethods.chebyEval(x_arr, arr);
Output:
out_val: 1047.4
out_arr: [-470.2, 1047.4, 23580.4, 97134.8, 263716.6]

Element-by-Element Operations of Matrices

These set of function allows operating on a RealMatrix at the element level. The operations are: multiplication, addition, subtraction and division. It operates in 3 modes: EBE (element-by-element) in which both matrices must have the same shape, Row-wise in which both matrices must have same number of columns and, Column-wise in which both matrices must have the same number of rows.

Input:
$$\begin{bmatrix} 1.0 & 2.0\\ 2.0 & 0.5\\ 3.0 & 4.0\end{bmatrix}$$
Multiplication Code
double[][] m1 = {{1.0, 2.0}, {2.0, 0.5}, {3.0, 4.0}};

double[][] m2 = {{1.0, 2.0}, {2.0, 0.5}, {3.0, 4.0}};
RealMatrix out = UtilMethods.ebeMultiply(MatrixUtils.createRealMatrix(m1), MatrixUtils.createRealMatrix(m2));
double[][] m2_row = {{0.5, 1.0}};
RealMatrix out_row = UtilMethods.ebeMultiply(MatrixUtils.createRealMatrix(m1), MatrixUtils.createRealMatrix(m2_row), "row");
double[][] m2_col = {{0.5}, {1.0}, {2.0}};
RealMatrix out_col = UtilMethods.ebeMultiply(MatrixUtils.createRealMatrix(m1), MatrixUtils.createRealMatrix(m2_col), "column");
Multiplication Output:
EBE Output:
$$\begin{bmatrix} 1.0 & 4.0\\ 4.0 & 0.25\\ 9.0 & 16.0\end{bmatrix}$$
Row-wise Output:
$$\begin{bmatrix} 0.5 & 2.0\\ 1.0 & 0.5\\ 1.5 & 4.0\end{bmatrix}$$
Column-wise Output:
$$\begin{bmatrix} 0.5 & 1.0\\ 2.0 & 0.5\\ 6.0 & 8.0\end{bmatrix}$$
Addition Code
double[][] m1 = {{1.0, 2.0}, {2.0, 0.5}, {3.0, 4.0}};

double[][] m2 = {{1.0, 2.0}, {2.0, 0.5}, {3.0, 4.0}};
RealMatrix out = UtilMethods.ebeAdd(MatrixUtils.createRealMatrix(m1), MatrixUtils.createRealMatrix(m2));
double[][] m2_row = {{0.5, 1.0}};
RealMatrix out_row = UtilMethods.ebeAdd(MatrixUtils.createRealMatrix(m1), MatrixUtils.createRealMatrix(m2_row), "row");
double[][] m2_col = {{0.5}, {1.0}, {2.0}};
RealMatrix out_col = UtilMethods.ebeAdd(MatrixUtils.createRealMatrix(m1), MatrixUtils.createRealMatrix(m2_col), "column");
Subtraction Code
double[][] m1 = {{1.0, 2.0}, {2.0, 0.5}, {3.0, 4.0}};

double[][] m2 = {{1.0, 2.0}, {2.0, 0.5}, {3.0, 4.0}};
RealMatrix out = UtilMethods.ebeSubtract(MatrixUtils.createRealMatrix(m1), MatrixUtils.createRealMatrix(m2));
double[][] m2_row = {{0.5, 1.0}};
RealMatrix out_row = UtilMethods.ebeSubtract(MatrixUtils.createRealMatrix(m1), MatrixUtils.createRealMatrix(m2_row), "row");
double[][] m2_col = {{0.5}, {1.0}, {2.0}};
RealMatrix out_col = UtilMethods.ebeSubtract(MatrixUtils.createRealMatrix(m1), MatrixUtils.createRealMatrix(m2_col), "column");
Division Code
double[][] m1 = {{1.0, 2.0}, {2.0, 0.5}, {3.0, 4.0}};

double[][] m2 = {{1.0, 2.0}, {2.0, 0.5}, {3.0, 4.0}};
RealMatrix out = UtilMethods.ebeDivide(MatrixUtils.createRealMatrix(m1), MatrixUtils.createRealMatrix(m2));
double[][] m2_row = {{0.5, 1.0}};
RealMatrix out_row = UtilMethods.ebeDivide(MatrixUtils.createRealMatrix(m1), MatrixUtils.createRealMatrix(m2_row), "row");
double[][] m2_col = {{0.5}, {1.0}, {2.0}};
RealMatrix out_col = UtilMethods.ebeDivide(MatrixUtils.createRealMatrix(m1), MatrixUtils.createRealMatrix(m2_col), "column");

Clone this wiki locally