For the complete documentation index, see llms.txt. This page is also available as Markdown.

Quickstart

Hands-on quickstart guide for ND4J — creating arrays, basic operations, indexing, and shape manipulation

ND4J is a scientific computing library for the JVM designed for production use. It provides a versatile N-dimensional array object (INDArray), linear algebra and signal processing routines, and multi-platform execution including CPU and GPU. This guide follows the structure of the NumPy quickstart so that Python users can map familiar concepts directly to ND4J equivalents.

Maven Setup

Add the following to your pom.xml. You need the core platform bundle and a backend. Use nd4j-native-platform for CPU or nd4j-cuda-11.x-platform for GPU.

<properties>
  <dl4j.version>1.0.0-M2.1</dl4j.version>
</properties>

<dependencies>
  <!-- ND4J API -->
  <dependency>
    <groupId>org.nd4j</groupId>
    <artifactId>nd4j-api</artifactId>
    <version>${dl4j.version}</version>
  </dependency>

  <!-- CPU backend — replaces this with nd4j-cuda-11.x-platform for GPU -->
  <dependency>
    <groupId>org.nd4j</groupId>
    <artifactId>nd4j-native-platform</artifactId>
    <version>${dl4j.version}</version>
  </dependency>
</dependencies>

If you are using the broader DL4J stack (DataVec, DL4J training), add the BOM instead to keep versions consistent:

Imports Used in This Guide

All examples below assume these imports are present:

Array Creation

ND4J arrays are created through static factory methods on Nd4j. You never call new INDArray(...) directly.

zeros, ones, rand

createFromArray

Nd4j.createFromArray is overloaded for float, double, int, and long up to 4D. Shape is inferred from the Java array structure.

arange and linspace

Array Properties

The default DataType for arrays created without an explicit type argument is FLOAT. In M2.1 the type is represented by the DataType enum — the old DataBuffer.Type enum was removed.

Printing Arrays

INDArray.toString() produces NumPy-style output. Call it via System.out.println or string concatenation.

Basic Operations

Copy operations (non-destructive)

Copy operations allocate a new array and leave the original unchanged.

In-place operations (i suffix)

In-place operations modify the array they are called on. They return the same Java object for method chaining.

The complete set of element-wise arithmetic methods:

Operation
Copy
In-place

Addition

add

addi

Subtraction

sub

subi

Multiplication

mul

muli

Division

div

divi

Data type requirement

Operands must share the same DataType. Mixing types throws IllegalArgumentException.

castTo returns a new array of the requested type. The original is not modified.

Reduction Operations

Reductions collapse one or more dimensions down to a scalar or a lower-rank array.

Pass a dimension argument to reduce along a specific axis. Dimension 0 collapses rows (result has one entry per column); dimension 1 collapses columns (result has one entry per row).

Transform Operations

Transforms apply a mathematical function element-wise and return a new array. They live in org.nd4j.linalg.ops.transforms.Transforms.

A second boolean argument on most transforms controls whether the result is written in-place:

Matrix Multiplication

Element-wise multiplication uses mul / muli (covered above). For true matrix products use mmul, and for dot products use Transforms.dot.

Indexing and Slicing

Single-element access

For 2D arrays, pass row and column:

Exporting to Java arrays

Slicing with NDArrayIndex

Use NDArrayIndex to extract contiguous and strided sub-arrays. Slices are views — modifying the slice modifies the source array.

For 2D arrays, pass one NDArrayIndex per dimension:

getRow(i) and getColumn(j) are convenience shortcuts for the common 2D case:

Assigning into a slice

Because slices are views, you can assign into them using assign:

Shape Manipulation

reshape and ravel

reshape returns a view — it does not copy data. The total number of elements must remain the same.

Because reshape and ravel return views, a mutation in one propagates to all:

Use dup() after reshape if you need an independent copy:

Transpose

transpose() returns a view with swapped strides. For 2D arrays this swaps rows and columns:

Stacking Arrays

vstack concatenates along dimension 0 (rows); hstack concatenates along dimension 1 (columns). Both accept varargs.

Copies vs Views

Getting this right prevents subtle bugs.

Reference assignment — no copy

Java passes objects by reference. Assigning one INDArray variable to another gives you a second reference to the same object and the same underlying memory.

Views — shallow copy

reshape, ravel, transpose, getRow, getColumn, and NDArrayIndex slices all return views: new INDArray objects that share the same backing buffer. Mutating a view mutates the original.

Deep copy with dup()

dup() allocates a new buffer and copies all data into it. After calling dup(), the two arrays are completely independent.

As a rule: if you are going to modify a slice or reshaped view and do not want the original to change, call .dup() on it first.

Quick Reference

Array creation

Method
Description

Nd4j.zeros(DataType, long...)

All-zeros array

Nd4j.ones(DataType, long...)

All-ones array

Nd4j.rand(DataType, long...)

Uniform random in [0, 1)

Nd4j.randn(DataType, long...)

Standard-normal random

Nd4j.createFromArray(float[][])

From Java array (shape inferred)

Nd4j.arange(start, stop)

Integer range, exclusive of stop

Nd4j.linspace(DataType, start, stop, count)

Evenly-spaced points

Nd4j.eye(n)

n x n identity matrix

Element-wise arithmetic

Op
Copy
In-place

Addition

a.add(b)

a.addi(b)

Subtraction

a.sub(b)

a.subi(b)

Multiplication

a.mul(b)

a.muli(b)

Division

a.div(b)

a.divi(b)

Reductions

Method
Result

x.sum()

Global sum (scalar INDArray)

x.sum(dim)

Sum along dimension dim

x.min() / x.min(dim)

Minimum

x.max() / x.max(dim)

Maximum

x.mean() / x.mean(dim)

Mean

x.sumNumber().doubleValue()

Global sum as Java double

Transforms (Transforms class)

Method
Description

Transforms.sin(x)

Sine

Transforms.cos(x)

Cosine

Transforms.exp(x)

Exponential e^x

Transforms.sqrt(x)

Square root

Transforms.log(x)

Natural logarithm

Transforms.abs(x)

Absolute value

Transforms.relu(x)

max(0, x)

Transforms.sigmoid(x)

1 / (1 + e^-x)

Shape manipulation

Method
Returns
Notes

x.reshape(long...)

INDArray

View; total elements unchanged

x.ravel()

INDArray

View; 1D flat

x.transpose()

INDArray

View; swapped strides

x.dup()

INDArray

Independent deep copy

Nd4j.vstack(a, b)

INDArray

Concatenate along rows

Nd4j.hstack(a, b)

INDArray

Concatenate along columns


For a deeper treatment of DataType, memory ordering, and workspace-based memory management, see the Tensors and NDArrays page.

Last updated

Was this helpful?