HArray

An array representation.

Methods

new_from_values

new_from_values(arr: array, dtype: HDataType) -> HArray source

Creates a new HArray from an R array.

Arguments

  • arr

A double or complex array.

  • dtype

An HDataType to indicate which type of HArray to be created.

For float dtypes, the atomic vector must be a double. For complex dtypes, a complex atomic vector.

Returns

An HArray.

Examples

library(harmonium)
arr = array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
dtype = HDataType$Float32
HArray$new_from_values(arr, dtype)

len

len() -> integer source

Returns the number of elements of this Harray.

Returns

An integer.

Examples

library(harmonium)
arr = array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
dtype = HDataType$Float32
harray = HArray$new_from_values(arr, dtype)
harray$len()

shape

shape() -> integeratomicvector source

Returns the shape of this HArray.

Returns

An integer atomic vector.

Examples

library(harmonium)
arr = array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
dtype = HDataType$Float32
harray = HArray$new_from_values(arr, dtype)
harray$shape()

ndim

ndim() -> integer source

Returns the number of dimensions of this HArray.

Returns

An integer.

Examples

library(harmonium)
arr = array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
dtype = HDataType$Float32
harray = HArray$new_from_values(arr, dtype)
harray$ndim()

slice

slice(range: list[atomicvector]) -> HArray source

Slices the HArray.

This operation has a COW (clone-on-write) behaviour. The created slice shares the inner data with the original array until one of them is modified.

Arguments

  • range

A list of vectors of integers.

The number of vectors in the list must be equal to the number of dimensions in the original HArray as they represent the slice information for each axis.

Each vector must be composed of 1 or 3 elements

For 1 element: A single index. An index to use for taking a subview with respect to that axis. The index is selected, then the axis is removed.

For 3 elements: [start, end, step]. All 3 values can be positive or negative, although step can’t be 0. Negative start or end indexes are counted from the back of the axis. If end is None, the slice extends to the end of the axis. A c(NA_integer_, NA_integer_, NA_integer_) value for start will mean start = 0, end = axis_length, step = 1.

Returns

An HArray.

Examples

library(harmonium)
arr = array(c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20), c(4,5))
dtype = HDataType$Float32
harray = HArray$new_from_values(arr, dtype)
harray$slice(list(c(0L, 2L, 1L), c(1L, 3L, 1L)))
harray$slice(list(c(0L, 4L, 1L), c(1L, NA, 1L)))
harray$slice(list(c(0L, NA, 1L), c(1L, 3L, 1L)))
harray$slice(list(0L, c(NA_integer_, NA, NA))) # using index
x = c(NA_integer_, NA_integer_, NA_integer_)
harray$slice(list(x, x)) == harray # TRUE

print

print() source

Prints the HArray.

Differently from R’s normal behaviour, print doesn’t return the value invisibly.

Examples

library(harmonium)
arr = array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
dtype = HDataType$Float32
harray = HArray$new_from_values(arr, dtype)
harray$print()

# or similarly:
print(harray)

eq

eq(other: HArray) -> bool source

Equality with another HArray.

The comparison only checks if the dtype and the values are the same. To compare if the underlying data is the same in memory, check mem_adress.

Arguments

  • other

An HArray.

Returns

A bool.

Examples

library(harmonium)
arr = array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
dtype = HDataType$Float32
harray1 = HArray$new_from_values(arr, dtype)

arr = array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
dtype = HDataType$Float32
harray2 = HArray$new_from_values(arr, dtype)

harray1$eq(harray2) # TRUE

# or similarly:
harray1 == harray2

ne

ne(other: HArray) -> bool source

Difference with another HArray.

The comparison only checks if the dtype and the values are the same. To compare if the underlying data is the same in memory, check mem_adress.

Arguments

  • other

An HArray.

Returns

A bool.

Examples

library(harmonium)
arr = array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
dtype = HDataType$Float32
harray1 = HArray$new_from_values(arr, dtype)

arr = array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
dtype = HDataType$Float32
harray2 = HArray$new_from_values(arr, dtype)

harray1$ne(harray2) # FALSE

# or similarly:
harray1 != harray2

clone

clone() -> HArray source

Creates a new HArray, with the underlying data pointing to the same place in memory.

Returns

An HArray.

Examples

library(harmonium)
arr = array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
dtype = HDataType$Float32
harray1 = HArray$new_from_values(arr, dtype)
harray2 = harray1$clone()
harray1 == harray2 # TRUE

collect

collect() -> array source

Creates an R array from an HArray. The type of the array created (double or complex) will depend on the HArray’s dtype.

Returns

An array of type double or complex.

Examples

library(harmonium)
arr = array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
dtype = HDataType$Float32
harray = HArray$new_from_values(arr, dtype)
harray$collect()

dtype

dtype() -> HDataType source

Gets the HArray’s dtype as an HDataType.

Returns

An HDataType.

Examples

library(harmonium)
arr = array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
dtype = HDataType$Float32
harray = HArray$new_from_values(arr, dtype)
harray$dtype()

mem_adress

mem_adress() -> string source

The memory adress of the first element of the inner array.

This is useful to check if different objects share the same underlying data.

Returns

A string.

Examples

library(harmonium)
arr = array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
dtype = HDataType$Float32
harray = HArray$new_from_values(arr, dtype)
harray$mem_adress()

is_standard_layout

is_standard_layout() -> bool source

Returns true if the array data is laid out in contiguous “C order” in memory (where the last index is the most rapidly varying).

Returns false otherwise, i.e. the array is possibly not contiguous in memory, it has custom strides, etc.

This function is useful mainly to check if an HArray is contiguous after some operation as, for example, slice().

Returns

A bool.

Examples

library(harmonium)
arr = array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
dtype = HDataType$Float32
harray = HArray$new_from_values(arr, dtype)
harray$is_standard_layout() # TRUE, contiguous data
sliced_harray = harray$slice(list(c(0L, 2L, 1L), c(1L, 3L, 1L)))
sliced_harray$is_standard_layout() # FALSE, non contiguous data

is_unique

is_unique() -> bool source

Checks if the object is shared.

Since HArray has a COW (clone-on-write) behaviour, this function is useful to check if a new object will be created or if the change will be done in-place.

Returns

A bool.

Examples

library(harmonium)
arr = array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
dtype = HDataType$Float32
harray1 = HArray$new_from_values(arr, dtype)
harray1$is_unique() # TRUE.

harray2 = harray1$clone()
harray1$is_unique() # FALSE, HArray object shared with harray2.

invalidate

invalidate() source

Replaces the inner value of the external pointer, invalidating it. This function is useful to remove one of the shared references of the inner pointer in rust.

Examples

library(harmonium)
arr = array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
dtype = HDataType$Float32
harray = HArray$new_from_values(arr, dtype)
harray$invalidate()