Multik 0.3.0 Help

Copies and views

Overview

A view is an ndarray that references the same underlying data as another array. A copy owns its own storage and is independent from the original. Views are fast and memory-efficient, but changes to the base array are visible through the view.

Creating views

Most indexing and slicing operations return views:

val a = mk.ndarray(mk[mk[1, 2], mk[3, 4]]) val v = a[0] // view of the first row println(v) // [1, 2] a[0, 0] = 99 println(v) // [99, 2]

You can also create views explicitly with view and slice. Use base to check whether an array is a view:

val s = a.slice<Int, D2, D2>(0..1, axis = 1) println(s.base != null) // true for views

Copies: copy and deepCopy

Use these methods to detach from a view and get independent data:

  • copy() duplicates the underlying storage and preserves the current layout (offset/strides).

  • deepCopy() materializes only the visible elements into a new contiguous array.

val view = a[0] val c1 = view.copy() val c2 = view.deepCopy() a[0, 0] = 111 println(view) // changed: [111, 2] println(c1) // unchanged: [1, 2] println(c2) // unchanged: [1, 2]

deepCopy() is usually the safer choice when you need a compact, standalone array or want to avoid copying data that is outside a view.

Views vs copies in common operations

Typical view-producing operations:

  • Indexing and slicing

  • view(...) and slice(...)

  • transpose() and squeeze()

Operations that always allocate new storage:

  • copy() and deepCopy()

  • flatten()

  • cat(...) and mk.stack(...)

Operations like reshape() and unsqueeze() return views when the array is contiguous; otherwise they materialize a copy to keep data consistent.

28 February 2026