HILA
Loading...
Searching...
No Matches
Array< n, m, T > Class Template Reference

\( n\times m \) Array type More...

#include <array.h>

Public Member Functions

 Array ()=default
 Construct a new Matrix object.
 
constexpr int rows () const
 Returns number of rows.
 
constexpr int columns () const
 Returns number of columns.
 
template<int q = n, int p = m, std::enable_if_t< q==1, int > = 0>
constexpr int size () const
 Returns size of Vector Array or square Array.
 
e (const int i, const int j) const
 Standard array indexing operation for 2D and 1D Arrays.
 
Matrix< n, m, T > & asMatrix ()
 Cast Array to Matrix.
 
Vector< n, T > & asVector ()
 Cast Array1D to Vector.
 
Array< n, m, T > operator- () const
 Unary - operator.
 
Array< n, m, T > operator+ () const
 Unary + operator.
 
template<typename S , std::enable_if_t< hila::is_assignable< T &, S >::value, int > = 0>
Array< n, m, T > & operator= (const S rhs) &
 Assignment operator = to assign values to Array.
 
template<typename S , int n1, int m1>
bool operator== (const Array< n1, m1, S > &rhs) const
 Compare equality of Arrays.
 
template<typename S , int n1, int m1>
bool operator!= (const Array< n1, m1, S > &rhs) const
 Compare non-equality of two Arrays.
 
template<typename S , std::enable_if_t< std::is_convertible< S, T >::value, int > = 0>
Array< n, m, T > & operator+= (const Array< n, m, S > &rhs) &
 Add assign operator with Array or scalar.
 
template<typename S , std::enable_if_t< std::is_convertible< S, T >::value, int > = 0>
Array< n, m, T > & operator-= (const Array< n, m, S > &rhs) &
 Subtract assign operator with Array or scalar.
 
template<typename S , std::enable_if_t< std::is_convertible< hila::type_mul< T, S >, T >::value, int > = 0>
Array< n, m, T > & operator*= (const Array< n, m, S > &rhs) &
 Multiply assign scalar or array.
 
template<typename S , std::enable_if_t< std::is_convertible< hila::type_mul< T, S >, T >::value, int > = 0>
Array< n, m, T > & operator*= (const S rhs) &
 multiply assign with scalar
 
template<typename S , std::enable_if_t< std::is_convertible< hila::type_div< T, S >, T >::value, int > = 0>
Array< n, m, T > & operator/= (const Array< n, m, S > &rhs) &
 Division assign with array or scalar.
 
Array< n, m, T > conj () const
 Returns element wise Complex conjugate of Array.
 
Array< n, m, hila::arithmetic_type< T > > real () const
 Returns real part of Array.
 
Array< n, m, hila::arithmetic_type< T > > imag () const
 return imaginary part
 
hila::arithmetic_type< T > squarenorm () const
 calculate square norm - sum of squared elements
 
Array< n, m, T > & random ()
 Fill Array with random elements.
 
Array< n, m, T > & gaussian_random (double width=1.0)
 Fill Array with Gaussian random elements.
 
std::string str (int prec=8, char separator=' ') const
 Convert to string for printing.
 
template<int N>
Array< n, m, T > sort (Vector< N, int > &permutation, hila::sort order=hila::sort::ascending) const
 implement sort as casting to array
 

Detailed Description

template<const int n, const int m, typename T>
class Array< n, m, T >

\( n\times m \) Array type

Acts as array class which stores data in a simple C style array.

Main functionality which the Array class offers is to supplement the fall backs of storing information in a Matrix data structure.

For example assigning a value to each element with the Matrix class is not directly possible using the assignment operator=. This is because assignment with matrices is defined as \( M = a = a*I \) where M is a general matrix, a is a scalar and I an identity matrix. The result would only assign a to the diagonal elements.

Unlike the Matrix object, the Array object assigns element wise. This allows filling the Array with the assignment operator. Additionally element wise operations are useable as long as they are defined for the Array type. For example the operation:

sin(m);
Array< n, m, T > sin(Array< n, m, T > a)
Sine.
Definition array.h:1079
Array type
Definition array.h:43
Array< n, m, T > & random()
Fill Array with random elements.
Definition array.h:624

is defined, since the \(sin\) function is defined for doubles.

The above operation would not work for matrices, but with casting operations. Matrix::asArray and Array::asMatrix one can take full advantage of element wise operations.

Template Parameters
nNumber of rows
mNumber of columns
TArray element type

Definition at line 43 of file array.h.

Constructor & Destructor Documentation

◆ Array()

template<const int n, const int m, typename T >
Array< n, m, T >::Array ( )
default

Construct a new Matrix object.

The following ways of constructing a matrix are:

NOTE: n,m are integers and MyType is a HILA standard type or Complex.

Default constructor:

Allocates undefined \( n\times m\) Array.

Scalar constructor:

Construct with given scalar which is assigned to all elements in array

Complex definition.
Definition cmplx.h:56
double random()
Real valued uniform random number generator.
Definition hila_gpu.cpp:121

Copy constructor:

Construction from previously defined Array.

//
// A_0 is filled with content
//

Initializer list:

Construction from c++ initializer list.

Array<2,2,int> A = {1, 0
0, 1};

Member Function Documentation

◆ asMatrix()

template<const int n, const int m, typename T >
Matrix< n, m, T > & Array< n, m, T >::asMatrix ( )
inline

Cast Array to Matrix.

Returns
Matrix<n, m, T>&

Definition at line 222 of file array.h.

◆ asVector()

template<const int n, const int m, typename T >
Vector< n, T > & Array< n, m, T >::asVector ( )
inline

Cast Array1D to Vector.

asMatrix will perform the same operation.

Returns
Vector<n, T>&

Definition at line 237 of file array.h.

◆ columns()

template<const int n, const int m, typename T >
constexpr int Array< n, m, T >::columns ( ) const
inlineconstexpr

Returns number of columns.

Returns
constexpr int

Definition at line 147 of file array.h.

◆ conj()

template<const int n, const int m, typename T >
Array< n, m, T > Array< n, m, T >::conj ( ) const
inline

Returns element wise Complex conjugate of Array.

Returns
Array<n, m, T>

Definition at line 581 of file array.h.

◆ e()

template<const int n, const int m, typename T >
T Array< n, m, T >::e ( const int  i,
const int  j 
) const
inline

Standard array indexing operation for 2D and 1D Arrays.

Accessing singular elements is insufficient, but matrix elements are often quite small.

Exammple for 2D Array:

MyType a = A.e(i,j); \\ i <= n, j <= m
T e(const int i, const int j) const
Standard array indexing operation for 2D and 1D Arrays.
Definition array.h:196

Example for Vector Array:

MyType a = A.e(i) \\ i <= n
Parameters
irow index
jcolumn index
Returns
T matrix element type

Definition at line 196 of file array.h.

◆ gaussian_random()

template<const int n, const int m, typename T >
Array< n, m, T > & Array< n, m, T >::gaussian_random ( double  width = 1.0)
inline

Fill Array with Gaussian random elements.

Parameters
width
Returns
Array<n, m, T>&

Definition at line 637 of file array.h.

◆ operator!=()

template<const int n, const int m, typename T >
template<typename S , int n1, int m1>
bool Array< n, m, T >::operator!= ( const Array< n1, m1, S > &  rhs) const
inline

Compare non-equality of two Arrays.

Negation of operator==()

Template Parameters
S
n1
m1
Parameters
rhs
Returns
true
false

Definition at line 388 of file array.h.

◆ operator*=()

template<const int n, const int m, typename T >
template<typename S , std::enable_if_t< std::is_convertible< hila::type_mul< T, S >, T >::value, int > = 0>
Array< n, m, T > & Array< n, m, T >::operator*= ( const Array< n, m, S > &  rhs) &
inline

Multiply assign scalar or array.

Multiplication works element wise

Multiply assign operator can be used in the following ways

Multiply assign Array:

A = 2;
B = 2;
A *= B; \\ A is uniformly 4

Multiply assign scalar:

A *= 2 ; \\ A is uniformly 2
Template Parameters
S
Parameters
rhs
Returns
Array<n, m, T>&

Definition at line 512 of file array.h.

◆ operator+()

template<const int n, const int m, typename T >
Array< n, m, T > Array< n, m, T >::operator+ ( ) const
inline

Unary + operator.

Equivalent to identity operator meaning that Arrat stays as is.

Returns
const Array<n, m, T>

Definition at line 291 of file array.h.

◆ operator+=()

template<const int n, const int m, typename T >
template<typename S , std::enable_if_t< std::is_convertible< S, T >::value, int > = 0>
Array< n, m, T > & Array< n, m, T >::operator+= ( const Array< n, m, S > &  rhs) &
inline

Add assign operator with Array or scalar.

Add assign operator can be used in the following ways

Add assign Array:

Requires that Arrays have same dimensions

A = 1;
B = 1;
A += B; \\ A is uniformly 2

Add assign scalar:

Adds scalar \( a \) to Array uniformly

A += 1 ; \\ A is uniformly 2
Template Parameters
SElement type of rhs
Parameters
rhsArray to add
Returns
Array<n, m, T>

Definition at line 422 of file array.h.

◆ operator-()

template<const int n, const int m, typename T >
Array< n, m, T > Array< n, m, T >::operator- ( ) const
inline

Unary - operator.

Returns Array with the signs of all the elements in the Arrat flipped.

Returns
Array<n,m,T>

Definition at line 277 of file array.h.

◆ operator-=()

template<const int n, const int m, typename T >
template<typename S , std::enable_if_t< std::is_convertible< S, T >::value, int > = 0>
Array< n, m, T > & Array< n, m, T >::operator-= ( const Array< n, m, S > &  rhs) &
inline

Subtract assign operator with Array or scalar.

Subtract assign operator can be used in the following ways

Subtract assign Array:

A = 3;
B = 1;
A -= B; \\ A is uniformly 2

Subtract assign scalar:

Subtract scalar uniformly from square matrix

A<n,m,MyType> A = 3;
A -= 1 ; \\ A is uniformly 2
Template Parameters
S
Parameters
rhs
Returns
Array<n, m, T>&

Definition at line 456 of file array.h.

◆ operator/=()

template<const int n, const int m, typename T >
template<typename S , std::enable_if_t< std::is_convertible< hila::type_div< T, S >, T >::value, int > = 0>
Array< n, m, T > & Array< n, m, T >::operator/= ( const Array< n, m, S > &  rhs) &
inline

Division assign with array or scalar.

Division works element wise

Division assign operator can be used in the following ways

Division assign Array:

A = 2;
B = 2;
A /= B; \\ A is uniformly 1

Division assign scalar:

A /= 2 ; \\ A is uniformly 1
Template Parameters
S
Parameters
rhs
Returns
Array<n, m, T>&

Definition at line 558 of file array.h.

◆ operator=()

template<const int n, const int m, typename T >
template<typename S , std::enable_if_t< hila::is_assignable< T &, S >::value, int > = 0>
Array< n, m, T > & Array< n, m, T >::operator= ( const S  rhs) &
inline

Assignment operator = to assign values to Array.

The following ways to assign an Array are:

Assignment from Array:

.
. A_0 has values assigned to it
.
Array<n,m,MyType> A; \\ undefined matrix
A = A_0; \\ Assignment from A_0
Array()=default
Construct a new Matrix object.

Assignment from scalar:

Assignment from scalar assigns the scalar to the diagonal elements as \( A = I\cdot a\)

A = a; A = I*a
constexpr Imaginary_t< double > I(1.0)
Imaginary unit I - global variable.

Initializer list:

Assignment from c++ initializer list.

A = {1, 0
0, 1};

Definition at line 333 of file array.h.

◆ operator==()

template<const int n, const int m, typename T >
template<typename S , int n1, int m1>
bool Array< n, m, T >::operator== ( const Array< n1, m1, S > &  rhs) const
inline

Compare equality of Arrays.

Two Arrays are equal iff Arrays are of same dimension and all elements compare to equal Note: Complex == scalar if arithmetic value is equal

Template Parameters
S
Parameters
rhs
Returns
true if equal

Definition at line 362 of file array.h.

◆ random()

template<const int n, const int m, typename T >
Array< n, m, T > & Array< n, m, T >::random ( )
inline

Fill Array with random elements.

Returns
Array<n, m, T>&

Definition at line 624 of file array.h.

◆ real()

template<const int n, const int m, typename T >
Array< n, m, hila::arithmetic_type< T > > Array< n, m, T >::real ( ) const
inline

Returns real part of Array.

Returns
Array<n, m, T>

Definition at line 593 of file array.h.

◆ rows()

template<const int n, const int m, typename T >
constexpr int Array< n, m, T >::rows ( ) const
inlineconstexpr

Returns number of rows.

Returns
constexpr int

Definition at line 139 of file array.h.

◆ size()

template<const int n, const int m, typename T >
template<int q = n, int p = m, std::enable_if_t< q==1, int > = 0>
constexpr int Array< n, m, T >::size ( ) const
inlineconstexpr

Returns size of Vector Array or square Array.

Template Parameters
qNumber of rows n
pNumber of columns m
Returns
constexpr int

Definition at line 159 of file array.h.


The documentation for this class was generated from the following file: