Class Numpy
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic NDArrayarange(double start, double stop, double step) Creates a one-dimensionalNDArraycontaining evenly spaced values within a specified interval.static NDArrayarray(double[] data, int... shape) Creates a newNDArrayfrom an existing one-dimensional Java array and a specified shape.static NDArrayCreates a deep copy of the givenNDArray.static NDArrayempty(int... shape) Creates a newNDArrayof the specified shape with uninitialized (random) values.static NDArrayElement-wise exponential of all elements in the input NDArray.static NDArrayeye(int n) Creates a two-dimensional identity matrix of sizen × n.static NDArrayFlattens the inputNDArrayto a one-dimensional array.static NDArrayfull(double fillValue, int... shape) Creates a newNDArrayof the specified shape filled with a given constant value.static NDArraylinspace(double start, double stop, int num) Generates a one-dimensionalNDArrayof evenly spaced numbers over a specified interval.static doubleCompute the mean (average) of all elements in the input NDArray.static NDArrayones(int... shape) Creates a newNDArrayof the specified shape filled with ones.static NDArrayrandom(int... shape) Generate an NDArray of random values uniformly distributed in [0, 1).static NDArrayAlias forflatten(NDArray).static NDArrayReshapes the inputNDArrayto the specified new shape.static NDArrayzeros(int... shape) Creates a newNDArrayof the specified shape filled with zeros.
-
Constructor Details
-
Numpy
public Numpy()
-
-
Method Details
-
array
Creates a newNDArrayfrom an existing one-dimensional Java array and a specified shape.This method mimics the behavior of
numpy.array()in Python. It wraps a givendouble[]array into anNDArrayof the specified dimensions.The total number of elements implied by the shape must match the length of the input array, otherwise an
IllegalArgumentExceptionwill 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 elementsshape- the desired dimensions of the resulting array (e.g.,(2, 2))- Returns:
- a new
NDArraywrapping the given data with the specified shape - Throws:
IllegalArgumentException- if the data length does not match the product of the shape dimensions
-
random
Generate an NDArray of random values uniformly distributed in [0, 1). Equivalent to Python'snumpy.random.random(size)ornumpy.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
Creates a newNDArrayof 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 to0.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
NDArraywith all elements initialized to zero - Throws:
IllegalArgumentException- if any dimension is non-positive
-
ones
Creates a newNDArrayof 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 to1.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
NDArraywith all elements initialized to one - Throws:
IllegalArgumentException- if any dimension is non-positive
-
full
Creates a newNDArrayof 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 withfillValue.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 withshape- the dimensions of the array- Returns:
- a new
NDArraywith all elements initialized tofillValue - Throws:
IllegalArgumentException- if any dimension is non-positive
-
linspace
Generates a one-dimensionalNDArrayof evenly spaced numbers over a specified interval.This method mimics the behavior of
numpy.linspace()in Python. It generatesnumvalues starting atstartand ending atstop, 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 sequencestop- the end value of the sequencenum- the number of evenly spaced samples to generate- Returns:
- a new one-dimensional
NDArraycontaining the generated values - Throws:
IllegalArgumentException- ifnumis not positive
-
eye
Creates a two-dimensional identity matrix of sizen × n.This method mimics the behavior of
numpy.eye()in Python. The diagonal elements are set to1.0, and all others are0.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
NDArrayrepresenting the identity matrix - Throws:
IllegalArgumentException- ifnis non-positive
-
copy
Creates a deep copy of the givenNDArray.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
NDArraywith copied data
-
flatten
Flattens the inputNDArrayto 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
NDArraycontaining the same data
-
ravel
Alias forflatten(NDArray).This method mimics the behavior of
numpy.ravel()in Python. -
reshape
Reshapes the inputNDArrayto 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 NDArraynewShape- the desired shape- Returns:
- a new
NDArrayreshaped tonewShape - Throws:
IllegalArgumentException- if total size mismatches
-
empty
Creates a newNDArrayof 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
NDArraywith random values simulating uninitialized data - Throws:
IllegalArgumentException- if any dimension is non-positive
-
exp
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
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
Creates a one-dimensionalNDArraycontaining evenly spaced values within a specified interval.This method mimics the behavior of
numpy.arange()in Python. It generates values starting fromstart(inclusive) up tostop(exclusive), incremented bystep.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
NDArraycontaining the generated values - Throws:
IllegalArgumentException- ifstepis zero
-