Package org.sadisamir.ndarray
Class NdArray
java.lang.Object
org.sadisamir.ndarray.NdArray
A multidimensional array of floats supporting 1D and 2D layouts.
Provides NumPy-style operations for scientific computing in Java.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionadd(float scalar) Returns a new array with a scalar added to every element.Returns a new array with element-wise addition.voidaddInPlace(float scalar) Adds a scalar to this array in place.voidaddInPlace(NdArray other) Adds another array to this one in place.static NdArrayarange(float stop) Creates a 1D array with values from 0 (inclusive) to stop (exclusive).static NdArrayarange(float start, float stop) Creates a 1D array with values from start (inclusive) to stop (exclusive).static NdArrayarange(float start, float stop, float step) Creates a 1D array with evenly spaced values within a given interval.static NdArrayarray(float[] data) Creates a 1D array from the given data.static NdArrayarray(float[][] data) Creates a 2D array from the given matrix.intgetNdim()Returns the number of dimensions (1 or 2).int[]getShape()Returns a copy of the shape array.intgetSize()Returns the total number of elements.reshape(int... newShape) Returns a reshaped view of this array.floatsum()Returns the sum of all elements in the array.float[]toArray()Returns a copy of the underlying data as a flat array.float[][]toMatrix()Converts a 2D array to a matrix representation.toString()static NdArrayzeros(int size) Creates a 1D array of zeros with the specified size.
-
Constructor Details
-
NdArray
public NdArray(float[] data) Creates a 1D array from flat data.- Parameters:
data- the source data (copied defensively)- Throws:
NullPointerException- if data is null
-
NdArray
public NdArray(float[][] matrix) Creates a 2D array from a matrix.- Parameters:
matrix- the source matrix (copied defensively)- Throws:
NullPointerException- if matrix is nullIllegalArgumentException- if any row is null or rows have inconsistent lengths
-
-
Method Details
-
array
Creates a 1D array from the given data. -
array
Creates a 2D array from the given matrix. -
zeros
Creates a 1D array of zeros with the specified size.- Parameters:
size- number of elements- Throws:
IllegalArgumentException- if size is negative
-
arange
Creates a 1D array with values from 0 (inclusive) to stop (exclusive). -
arange
Creates a 1D array with values from start (inclusive) to stop (exclusive). -
arange
Creates a 1D array with evenly spaced values within a given interval.- Parameters:
start- beginning of interval (inclusive)stop- end of interval (exclusive)step- spacing between values- Throws:
IllegalArgumentException- if any argument is non-finite or step is zero
-
getNdim
public int getNdim()Returns the number of dimensions (1 or 2). -
getShape
public int[] getShape()Returns a copy of the shape array. -
getSize
public int getSize()Returns the total number of elements. -
toArray
public float[] toArray()Returns a copy of the underlying data as a flat array. -
toMatrix
public float[][] toMatrix()Converts a 2D array to a matrix representation.- Throws:
IllegalStateException- if this is not a 2D array
-
add
Returns a new array with element-wise addition.- Throws:
NullPointerException- if other is nullIllegalArgumentException- if shapes don't match
-
add
Returns a new array with a scalar added to every element. -
addInPlace
Adds another array to this one in place.- Throws:
NullPointerException- if other is nullIllegalArgumentException- if shapes don't match
-
addInPlace
public void addInPlace(float scalar) Adds a scalar to this array in place. -
sum
public float sum()Returns the sum of all elements in the array. -
reshape
Returns a reshaped view of this array.- Throws:
NullPointerException- if newShape is nullIllegalArgumentException- if shape is invalid or incompatible
-
toString
-