The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

Name

SPVM::R::NDArray - N-Dimensional Array Base Class

Description

The R::NDArray class in SPVM represetns n-dimensional array and it is the base class of classes that represents typed n-dimensional arrays.

Usage

  use R::NDArray;

Details

Row and Column Major Order

Elements of n-dimesion array that is a R::NDArray object is column major order.

Scalar

A sacalr is a n-dimensional array that "is_scalar" method returns a true value.

Vector

A vector is a n-dimensional array that "is_vector" method returns a true value.

Matrix

A matrix is a n-dimensional array that "is_matrix" method returns a true value.

Fields

data

has data : protected ro object;

An array of numeric type, multi-numeric type, or object type.

dim

has dim : int[];

Dimensions.

method dim : int[] ();

Creates a new array, copies the elements of this field, and returns the array.

method set_dim : void ($dim : int[]);

The dimensions $dim is normalized by R::Util#normalize_dim method, checks the normalized dim by R::Util#check_length method, and sets this field to the normalized dim.

Exceptions:

If "is_dim_read_only" is a true value, an exception is thrown.

Exceptions thrown by R::Util#normalize_dim method could be thrown.

Exceptions thrown by R::Util#check_length method could be thrown.

is_dim_read_only

has is_dim_read_only : ro byte;

If this field is a true value, it indicates "dim" field is read only, otherwise writable.

Instance Methods

init

protected method init : void ($options : object[] = undef);

Initialize this instance given the options $options that is the same as "new" method.

Options:

data : object = undef

An array set to "data" field.

dim : int[] = undef

Dimensions set to "dim" field.

dim option is assinged to $dim variable.

data option is assinged to $data variable.

If $data is defined, the data type is checked. The type must be same as the type of an object created by "create_default_data" method.

If $dim is not defined, $dim is created by the length of $data. $dim is set to [length of $data]. If $data is not defined, $dim is set to [].

$dim is normalized by R::Util#normalize_dim method.

If $data is not defined, $data is created by $dim. The data length is calcurated by R::Util#calc_data_length method and "create_default_data" method is called given the data length.

R::Util#check_length method is called to check integrity for $dim and $data.

"data" field is set to $data.

"dim" field is set to $dim.

Exceptions:

If checking data type failed, an exception is thrown.

Exceptions thrown by R::Util#normalize_dim method could be thrown.

Exceptions thrown by R::Util#check_length method could be thrown.

make_dim_read_only

method make_dim_read_only : void ();

Sets "dim_read_only" field to 1.

nrow

method nrow : int ();

Returns the number of rows. This is the element of "dim" field at index 0.

This instance must be a matrix "is_matrix" method returns a true value.

Exceptions:

ncol

method ncol : int ();

This instance must be a matrix. Otherwise an exception is thrown.

Returns the number of columns.

If the length of "dim" field is greater than or equal to 2, the number of columns is the element of "dim" field at index 1, otherwise the number of columns is 1.

This instance must be a matrix "is_matrix" method returns a true value.

Exceptions:

This instance must be a matrix. Otherwise an exception is thrown.

length

method length : int ();

Culcurates the array length of "data" field by R::Util#calc_data_length method and returns it.

is_empty

method is_empty : int ();

If the length of "dim" field is equal to 0, returns 1, otherwise returns 0.

is_scalar

method is_scalar : int ();

Checks if this instance can be treated as a scalar.

If the length of "dim" field is greater than or equal to 1 and the data length calcureated by R::Util#calc_data_length method is 1, returns 1, otherwise return 0.

Examples Where The Condition Is Met:

  # OK dim
  [1]
  
  [1, 1]
  
  [1, 1, 1]
  
  # Not OK dim
  []
  [2]
  [1, 2]

is_vector

method is_vector : int ();

Checks if this instance can be treated as a vector.

If the length of "dim" field is greater than or equal to 1 and the element of "dim" field at index 0 is the same as the data length calcureated by R::Util#calc_data_length method, returns 1, otherwise return 0.

Examples Where The Condition Is Met:

  # OK dim
  [1]
  
  [1, 1]
  
  [1, 1, 1]
  
  [2]
  
  [2, 1]
  
  [2, 1, 1]
  
  # Not OK dim
  []
  [2, 2]
  [1, 2]

is_matrix

method is_matrix : int ();

Checks if this instance can be treated as a matrix.

Implemntation:

$dim : "dim" field.

$dim_length : the length of $dim.

$legnth : the data length calcureated by R::Util#calc_data_length method.

If $dim_length is not greater than or equal to 1, this method returns 0.

$nrow : numbers of rows. this is $dim at index 0.

$ncol : numbers of columns. If $dim_length is equal to 1, this is 0. Otherwise this is $dim at index 1.

If $nrow multiplied by $ncol is the same as $length, this method returns 1, otherwise returns 0.

Examples Where The Condition Is Met:

  # OK dim
  [3, 2]
  
  [3, 2, 1]
  
  [2]
  
  [2, 1]
  
  [2, 1, 1]
  
  [1]
  
  [1, 1]
  
  [1, 1, 1]
  
  # Not OK dim
  []
  [1, 1, 3]
  [1, 2, 3]

is_square_matrix

method is_square_matrix : int ();

Checks if this instance can be treated as a square matrix.

If this instance is a matrix "is_matrix" method returns a true value and the return value of "nrow" is equal to the return value of "ncol", returns 1, otherwise returns 0.

drop_dim

method drop_dim : void ($index : int = -1);

expand_dim

method expand_dim : void ($index : int = -1);

create_default_data

method create_default_data : object ($length : int = 0);

elem_to_string

method elem_to_string : string ($data : object, $data_index : int);

elem_assign

method elem_assign : void ($dist_data : object, $dist_data_index : int, $src_data : object, $src_data_index : int);

elem_clone

method elem_clone : void ($dist_data : object, $dist_data_index : int, $src_data : object, $src_data_index : int);

elem_cmp

method elem_cmp : int ($data : object, $a_data_index : int, $b_data_index : int);

elem_is_na

method elem_is_na : int ($data : object, $data_index : int);

Checks if an element represets NA.

This method will be implemented in a child class.

Exceptions:

This method is not implemented.

to_string_ndarray

method to_string_ndarray : R::NDArray::String ();

Converts this n-dimensional array to a n-dimensional array of R::NDArray::String and returns it.

elem_size

method elem_size : int ();

elem_type_name

method elem_type_name : string ();

is_numeric_ndarray

method is_numeric_ndarray : int ();

is_mulnum_ndarray

method is_mulnum_ndarray : int ();

is_any_numeric_ndarray

method is_any_numeric_ndarray : int ();

is_object_ndarray

method is_object_ndarray : int ();

clone

method clone : R::NDArray ($shallow : int = 0);

Clones this n-dimensional array and returns it.

slice

method slice : R::NDArray ($asix_indexes_product : R::NDArray::Int[]);

Slices this n-dimensional array using the cartesian product of asix indexes $asix_indexes_product and returns it.

slice_set

method slice_set : void ($asix_indexes_product : R::NDArray::Int[], $ndarray : R::NDArray);

to_string

method to_string : string ();

order

method order : R::NDArray::Int ();

set_order

method set_order : void ($data_indexes_ndarray : R::NDArray::Int);

sort_asc

method sort_asc : void ();

sort_desc

method sort_desc : void ();

Well Known Child Classes

Copyright & License

Copyright (c) 2024 Yuki Kimoto

MIT License