Class NdArray

java.lang.Object
org.sadisamir.ndarray.NdArray

public class NdArray extends Object
A multidimensional array of floats supporting 1D and 2D layouts. Provides NumPy-style operations for scientific computing in Java.
  • Constructor Summary

    Constructors
    Constructor
    Description
    NdArray(float[] data)
    Creates a 1D array from flat data.
    NdArray(float[][] matrix)
    Creates a 2D array from a matrix.
  • Method Summary

    Modifier and Type
    Method
    Description
    add(float scalar)
    Returns a new array with a scalar added to every element.
    add(NdArray other)
    Returns a new array with element-wise addition.
    void
    addInPlace(float scalar)
    Adds a scalar to this array in place.
    void
    Adds another array to this one in place.
    static NdArray
    arange(float stop)
    Creates a 1D array with values from 0 (inclusive) to stop (exclusive).
    static NdArray
    arange(float start, float stop)
    Creates a 1D array with values from start (inclusive) to stop (exclusive).
    static NdArray
    arange(float start, float stop, float step)
    Creates a 1D array with evenly spaced values within a given interval.
    static NdArray
    array(float[] data)
    Creates a 1D array from the given data.
    static NdArray
    array(float[][] data)
    Creates a 2D array from the given matrix.
    int
    Returns the number of dimensions (1 or 2).
    int[]
    Returns a copy of the shape array.
    int
    Returns the total number of elements.
    reshape(int... newShape)
    Returns a reshaped view of this array.
    float
    sum()
    Returns the sum of all elements in the array.
    float[]
    Returns a copy of the underlying data as a flat array.
    float[][]
    Converts a 2D array to a matrix representation.
     
    static NdArray
    zeros(int size)
    Creates a 1D array of zeros with the specified size.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • 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 null
      IllegalArgumentException - if any row is null or rows have inconsistent lengths
  • Method Details

    • array

      public static NdArray array(float[] data)
      Creates a 1D array from the given data.
    • array

      public static NdArray array(float[][] data)
      Creates a 2D array from the given matrix.
    • zeros

      public static NdArray zeros(int size)
      Creates a 1D array of zeros with the specified size.
      Parameters:
      size - number of elements
      Throws:
      IllegalArgumentException - if size is negative
    • arange

      public static NdArray arange(float stop)
      Creates a 1D array with values from 0 (inclusive) to stop (exclusive).
    • arange

      public static NdArray arange(float start, float stop)
      Creates a 1D array with values from start (inclusive) to stop (exclusive).
    • arange

      public static NdArray arange(float start, float stop, float step)
      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

      public NdArray add(NdArray other)
      Returns a new array with element-wise addition.
      Throws:
      NullPointerException - if other is null
      IllegalArgumentException - if shapes don't match
    • add

      public NdArray add(float scalar)
      Returns a new array with a scalar added to every element.
    • addInPlace

      public void addInPlace(NdArray other)
      Adds another array to this one in place.
      Throws:
      NullPointerException - if other is null
      IllegalArgumentException - 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

      public NdArray reshape(int... newShape)
      Returns a reshaped view of this array.
      Throws:
      NullPointerException - if newShape is null
      IllegalArgumentException - if shape is invalid or incompatible
    • toString

      public String toString()
      Overrides:
      toString in class Object