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)
= array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
arr = HDataType$Float32
dtype $new_from_values(arr, dtype) HArray
len
len() -> integer
source
Returns the number of elements of this Harray
.
Returns
An integer.
Examples
library(harmonium)
= array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
arr = HDataType$Float32
dtype = HArray$new_from_values(arr, dtype)
harray $len() harray
shape
shape() -> integeratomicvector
source
Returns the shape of this HArray
.
Returns
An integer atomic vector.
Examples
library(harmonium)
= array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
arr = HDataType$Float32
dtype = HArray$new_from_values(arr, dtype)
harray $shape() harray
ndim
ndim() -> integer
source
Returns the number of dimensions of this HArray
.
Returns
An integer.
Examples
library(harmonium)
= array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
arr = HDataType$Float32
dtype = HArray$new_from_values(arr, dtype)
harray $ndim() harray
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)
= 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))
arr = HDataType$Float32
dtype = 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
harray= c(NA_integer_, NA_integer_, NA_integer_)
x $slice(list(x, x)) == harray # TRUE harray
print()
source
Prints the HArray
.
Differently from R’s normal behaviour, print
doesn’t return the value invisibly.
Examples
library(harmonium)
= array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
arr = HDataType$Float32
dtype = HArray$new_from_values(arr, dtype)
harray $print()
harray
# 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)
= array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
arr = HDataType$Float32
dtype = HArray$new_from_values(arr, dtype)
harray1
= array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
arr = HDataType$Float32
dtype = HArray$new_from_values(arr, dtype)
harray2
$eq(harray2) # TRUE
harray1
# or similarly:
== harray2 harray1
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)
= array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
arr = HDataType$Float32
dtype = HArray$new_from_values(arr, dtype)
harray1
= array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
arr = HDataType$Float32
dtype = HArray$new_from_values(arr, dtype)
harray2
$ne(harray2) # FALSE
harray1
# or similarly:
!= harray2 harray1
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)
= array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
arr = HDataType$Float32
dtype = HArray$new_from_values(arr, dtype)
harray1 = harray1$clone()
harray2 == harray2 # TRUE harray1
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)
= array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
arr = HDataType$Float32
dtype = HArray$new_from_values(arr, dtype)
harray $collect() harray
dtype
dtype() -> HDataType
source
Gets the HArray
’s dtype as an HDataType
.
Returns
An HDataType
.
Examples
library(harmonium)
= array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
arr = HDataType$Float32
dtype = HArray$new_from_values(arr, dtype)
harray $dtype() harray
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)
= array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
arr = HDataType$Float32
dtype = HArray$new_from_values(arr, dtype)
harray $mem_adress() harray
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)
= array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
arr = HDataType$Float32
dtype = HArray$new_from_values(arr, dtype)
harray $is_standard_layout() # TRUE, contiguous data
harray= harray$slice(list(c(0L, 2L, 1L), c(1L, 3L, 1L)))
sliced_harray $is_standard_layout() # FALSE, non contiguous data sliced_harray
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)
= array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
arr = HDataType$Float32
dtype = HArray$new_from_values(arr, dtype)
harray1 $is_unique() # TRUE.
harray1
= harray1$clone()
harray2 $is_unique() # FALSE, HArray object shared with harray2. harray1
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)
= array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3,4))
arr = HDataType$Float32
dtype = HArray$new_from_values(arr, dtype)
harray $invalidate() harray