Class Numpy

java.lang.Object
org.numpy4j.api.Numpy

public class Numpy extends Object
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static NDArray
    arange(double start, double stop, double step)
    Creates a one-dimensional NDArray containing evenly spaced values within a specified interval.
    static NDArray
    array(double[] data, int... shape)
    Creates a new NDArray from an existing one-dimensional Java array and a specified shape.
    static NDArray
    Creates a deep copy of the given NDArray.
    static NDArray
    empty(int... shape)
    Creates a new NDArray of the specified shape with uninitialized (random) values.
    static NDArray
    Element-wise exponential of all elements in the input NDArray.
    static NDArray
    eye(int n)
    Creates a two-dimensional identity matrix of size n × n.
    static NDArray
    Flattens the input NDArray to a one-dimensional array.
    static NDArray
    full(double fillValue, int... shape)
    Creates a new NDArray of the specified shape filled with a given constant value.
    static NDArray
    linspace(double start, double stop, int num)
    Generates a one-dimensional NDArray of evenly spaced numbers over a specified interval.
    static double
    Compute the mean (average) of all elements in the input NDArray.
    static NDArray
    ones(int... shape)
    Creates a new NDArray of the specified shape filled with ones.
    static NDArray
    random(int... shape)
    Generate an NDArray of random values uniformly distributed in [0, 1).
    static NDArray
    Alias for flatten(NDArray).
    static NDArray
    reshape(NDArray a, int... newShape)
    Reshapes the input NDArray to the specified new shape.
    static NDArray
    zeros(int... shape)
    Creates a new NDArray of the specified shape filled with zeros.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Numpy

      public Numpy()
  • Method Details

    • array

      public static NDArray array(double[] data, int... shape)
      Creates a new NDArray from an existing one-dimensional Java array and a specified shape.

      This method mimics the behavior of numpy.array() in Python. It wraps a given double[] array into an NDArray of the specified dimensions.

      The total number of elements implied by the shape must match the length of the input array, otherwise an IllegalArgumentException will be thrown.

      Example usage:

      
       double[] data = {1.0, 2.0, 3.0, 4.0};
       NDArray a = NDArray.array(data, 2, 2);
       System.out.println(Arrays.toString(a.getData()));
       // Output: [1.0, 2.0, 3.0, 4.0]
       
      Parameters:
      data - the one-dimensional data array containing the elements
      shape - the desired dimensions of the resulting array (e.g., (2, 2))
      Returns:
      a new NDArray wrapping the given data with the specified shape
      Throws:
      IllegalArgumentException - if the data length does not match the product of the shape dimensions
    • random

      public static NDArray random(int... shape)
      Generate an NDArray of random values uniformly distributed in [0, 1). Equivalent to Python's numpy.random.random(size) or numpy.random.rand(*shape).

      Example usage:

      
       NDArray r = Numpy.random(2, 3);
       System.out.println(r);
       // [[0.7276, 0.6832, 0.3087]
       //  [0.2771, 0.6655, 0.9033]]
       
      Parameters:
      shape - dimensions of the random array
      Returns:
      NDArray filled with random doubles in [0, 1)
    • zeros

      public static NDArray zeros(int... shape)
      Creates a new NDArray of the specified shape filled with zeros.

      This method mimics the behavior of numpy.zeros() in Python. It allocates an array of the given shape and initializes all elements to 0.0.

      Example usage:

      
       NDArray z = NDArray.zeros(2, 3);
       System.out.println(Arrays.toString(z.getData()));
       // Output: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
       
      Parameters:
      shape - the dimensions of the array (e.g., (2, 3) for a 2×3 array)
      Returns:
      a new NDArray with all elements initialized to zero
      Throws:
      IllegalArgumentException - if any dimension is non-positive
    • ones

      public static NDArray ones(int... shape)
      Creates a new NDArray of the specified shape filled with ones.

      This method mimics the behavior of numpy.ones() in Python. It allocates an array of the given shape and initializes all elements to 1.0.

      Example usage:

      
       NDArray z = NDArray.ones(2, 3);
       System.out.println(Arrays.toString(z.getData()));
       // Output: [1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
       
      Parameters:
      shape - the dimensions of the array (e.g., (2, 3) for a 2×3 array)
      Returns:
      a new NDArray with all elements initialized to one
      Throws:
      IllegalArgumentException - if any dimension is non-positive
    • full

      public static NDArray full(double fillValue, int... shape)
      Creates a new NDArray of the specified shape filled with a given constant value.

      This method mimics the behavior of numpy.full() in Python. It allocates an array of the given shape and fills all elements with fillValue.

      Example usage:

      
       NDArray f = Numpy.full(7.5, 2, 2);
       System.out.println(Arrays.toString(f.getData()));
       // Output: [7.5, 7.5, 7.5, 7.5]
       
      Parameters:
      fillValue - the value to fill the array with
      shape - the dimensions of the array
      Returns:
      a new NDArray with all elements initialized to fillValue
      Throws:
      IllegalArgumentException - if any dimension is non-positive
    • linspace

      public static NDArray linspace(double start, double stop, int num)
      Generates a one-dimensional NDArray of evenly spaced numbers over a specified interval.

      This method mimics the behavior of numpy.linspace() in Python. It generates num values starting at start and ending at stop, inclusive.

      Example usage:

      
       NDArray a = Numpy.linspace(0, 1, 5);
       System.out.println(Arrays.toString(a.getData()));
       // Output: [0.0, 0.25, 0.5, 0.75, 1.0]
       
      Parameters:
      start - the starting value of the sequence
      stop - the end value of the sequence
      num - the number of evenly spaced samples to generate
      Returns:
      a new one-dimensional NDArray containing the generated values
      Throws:
      IllegalArgumentException - if num is not positive
    • eye

      public static NDArray eye(int n)
      Creates a two-dimensional identity matrix of size n × n.

      This method mimics the behavior of numpy.eye() in Python. The diagonal elements are set to 1.0, and all others are 0.0.

      Example usage:

      
       NDArray id = Numpy.eye(3);
       System.out.println(Arrays.toString(id.getData()));
       // Output: [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]
       
      Parameters:
      n - the number of rows and columns
      Returns:
      a new NDArray representing the identity matrix
      Throws:
      IllegalArgumentException - if n is non-positive
    • copy

      public static NDArray copy(NDArray a)
      Creates a deep copy of the given NDArray.

      This method mimics the behavior of numpy.copy() in Python. The returned array contains the same data but in a separate memory location.

      Example usage:

      
       NDArray a = Numpy.ones(2,2);
       NDArray b = Numpy.copy(a);
       b.getData()[0] = 99;
       // a remains unchanged
       
      Parameters:
      a - the input NDArray to copy
      Returns:
      a new NDArray with copied data
    • flatten

      public static NDArray flatten(NDArray a)
      Flattens the input NDArray to a one-dimensional array.

      This method mimics the behavior of numpy.flatten() in Python.

      Example usage:

      
       NDArray a = Numpy.arange(1,7);
       NDArray flat = Numpy.flatten(a);
       System.out.println(Arrays.toString(flat.getData()));
       // Output: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
       
      Parameters:
      a - the input NDArray
      Returns:
      a new one-dimensional NDArray containing the same data
    • ravel

      public static NDArray ravel(NDArray a)
      Alias for flatten(NDArray).

      This method mimics the behavior of numpy.ravel() in Python.

    • reshape

      public static NDArray reshape(NDArray a, int... newShape)
      Reshapes the input NDArray to the specified new shape.

      This method mimics the behavior of numpy.reshape() in Python. The total number of elements must remain the same.

      Example usage:

      
       NDArray a = Numpy.arange(1,7); // 6 elements
       NDArray reshaped = Numpy.reshape(a, 2,3);
       System.out.println(Arrays.toString(reshaped.getData()));
       // Output: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
       
      Parameters:
      a - the input NDArray
      newShape - the desired shape
      Returns:
      a new NDArray reshaped to newShape
      Throws:
      IllegalArgumentException - if total size mismatches
    • empty

      public static NDArray empty(int... shape)
      Creates a new NDArray of the specified shape with uninitialized (random) values.

      This method mimics the behavior of numpy.empty() in Python. It allocates an array of the given shape without setting the elements to zero, which can be useful for performance when the contents will be overwritten later.
      Since Java does not expose uninitialized memory, this implementation fills the array with random values to simulate that behavior.

      Example usage:

      
       NDArray a = NDArray.empty(2, 3);
       System.out.println(Arrays.deepToString(a.reshape(2, 3).getData()));
       // Output: [[0.384, 0.912, 0.156], [0.771, 0.428, 0.623]]  (values will vary)
       
      Parameters:
      shape - the dimensions of the array (e.g., (2, 3) for a 2×3 array)
      Returns:
      a new NDArray with random values simulating uninitialized data
      Throws:
      IllegalArgumentException - if any dimension is non-positive
    • exp

      public static NDArray exp(NDArray a)
      Element-wise exponential of all elements in the input NDArray.

      Equivalent to Python's numpy.exp(x).

      Example usage:

      
       NDArray x = Numpy.arange(0, 5);
       NDArray y = Numpy.exp(x);
       System.out.println(y);
       // Output: [1.0000, 2.7183, 7.3891, 20.0855, 54.5981]
       
      Parameters:
      a - input NDArray
      Returns:
      a new NDArray with element-wise exponentials
    • mean

      public static double mean(NDArray a)
      Compute the mean (average) of all elements in the input NDArray.

      Equivalent to Python's numpy.mean(x).

      Example usage:

      
       NDArray x = Numpy.arange(1, 6);
       double mean = Numpy.mean(x);
       System.out.println(mean);
       // Output: 3.0
       
      Parameters:
      a - input NDArray
      Returns:
      mean value as a scalar double
    • arange

      public static NDArray arange(double start, double stop, double step)
      Creates a one-dimensional NDArray containing evenly spaced values within a specified interval.

      This method mimics the behavior of numpy.arange() in Python. It generates values starting from start (inclusive) up to stop (exclusive), incremented by step.

      Example usage:

      
       NDArray a = NDArray.arange(0.0, 5.0, 1.0);
       System.out.println(Arrays.toString(a.getData()));
       // Output: [0.0, 1.0, 2.0, 3.0, 4.0]
      
       NDArray b = NDArray.arange(5.0, 0.0, -1.0);
       System.out.println(Arrays.toString(b.getData()));
       // Output: [5.0, 4.0, 3.0, 2.0, 1.0]
       
      Parameters:
      start - the starting value of the sequence (inclusive)
      stop - the end value of the sequence (exclusive)
      step - the spacing between values; must not be zero
      Returns:
      a one-dimensional NDArray containing the generated values
      Throws:
      IllegalArgumentException - if step is zero