Cytnx v0.9.5
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | List of all members
cytnx::Accessor Class Reference

object that mimic the python slice to access elements in C++ [this is for c++ API only]. More...

#include <Accessor.hpp>

Public Member Functions

 Accessor (const cytnx_int64 &loc)
 access the specific index at the assigned rank in Tensor.
 
template<class T >
 Accessor (const std::initializer_list< T > &list)
 
template<class T >
 Accessor (const std::vector< T > &list)
 
int type () const
 

Static Public Member Functions

static Accessor all ()
 access the whole rank, this is similar to [:] in python
 
static Accessor range (const cytnx_int64 &min, const cytnx_int64 &max, const cytnx_int64 &step=1)
 access the range at assigned rank, this is similar to min:max:step in python
 
static Accessor tilend (const cytnx_int64 &min, const cytnx_int64 &step=1)
 
static Accessor step (const cytnx_int64 &step)
 
static Accessor qns (const std::vector< std::vector< cytnx_int64 > > &qns)
 

Detailed Description

object that mimic the python slice to access elements in C++ [this is for c++ API only].

Constructor & Destructor Documentation

◆ Accessor() [1/3]

cytnx::Accessor::Accessor ( const cytnx_int64 loc)
explicit

access the specific index at the assigned rank in Tensor.

Parameters
locthe specify index

See also cytnx::Tensor.get() for how to using them.

Example:

c++ API:

#include "cytnx.hpp"
#include <iostream>
using namespace cytnx;
using namespace std;
int main() {
// 1. handy alias:
typedef Accessor ac;
/*
2. Create a Tensor with
shape (3,4,5),
dtype =Type.Double [default],
device=Device.cpu [default]
*/
A.reshape_({2, 3, 4});
cout << A << endl;
/*
3. Accessing elements using accessor
This is similar as python slices.
-> A[0,:,0:2:1]
*/
Tensor B = A(0, ac::all(), ac::range(0, 2, 1));
cout << B << endl;
/* [Note] Conversion from python slice to ac:
[::x] = ac::step(x)
[a::x] = ac::tilend(a,x)
[a::] = ac::tilend(a)
[:b:] = ac::range(0,b,1)
[a:b:x] = ac::range(a,b,x)
*/
return 0;
}
object that mimic the python slice to access elements in C++ [this is for c++ API only].
Definition Accessor.hpp:17
an tensor (multi-dimensional array)
Definition Tensor.hpp:41
void reshape_(const std::vector< cytnx_int64 > &new_shape)
reshape the Tensor, inplacely
Definition Tensor.hpp:728
Helper function to print vector with ODT:
Definition Accessor.hpp:12
Tensor arange(const cytnx_int64 &Nelem)
create an rank-1 Tensor with incremental unsigned integer elements start with [0,Nelem)

output>

Total elem: 24
type  : Double (Float64)
cytnx device: CPU
Shape : (2,3,4)
[[[0.00000e+00 1.00000e+00 2.00000e+00 3.00000e+00 ]
  [4.00000e+00 5.00000e+00 6.00000e+00 7.00000e+00 ]
  [8.00000e+00 9.00000e+00 1.00000e+01 1.10000e+01 ]]
 [[1.20000e+01 1.30000e+01 1.40000e+01 1.50000e+01 ]
  [1.60000e+01 1.70000e+01 1.80000e+01 1.90000e+01 ]
  [2.00000e+01 2.10000e+01 2.20000e+01 2.30000e+01 ]]]



Total elem: 6
type  : Double (Float64)
cytnx device: CPU
Shape : (3,2)
[[0.00000e+00 1.00000e+00 ]
 [4.00000e+00 5.00000e+00 ]
 [8.00000e+00 9.00000e+00 ]]

python API:

import sys
from pathlib import Path
home = str(Path.home())
sys.path.append(home + '/Cytnx_lib')
from cytnx import *
A = arange(24);
A.reshape_(2,3,4);
print(A)
B = A[0,:,0:2:1]
print(B)

output>

Total elem: 24
type  : Double (Float64)
cytnx device: CPU
Shape : (2,3,4)
[[[0.00000e+00 1.00000e+00 2.00000e+00 3.00000e+00 ]
  [4.00000e+00 5.00000e+00 6.00000e+00 7.00000e+00 ]
  [8.00000e+00 9.00000e+00 1.00000e+01 1.10000e+01 ]]
 [[1.20000e+01 1.30000e+01 1.40000e+01 1.50000e+01 ]
  [1.60000e+01 1.70000e+01 1.80000e+01 1.90000e+01 ]
  [2.00000e+01 2.10000e+01 2.20000e+01 2.30000e+01 ]]]




Total elem: 6
type  : Double (Float64)
cytnx device: CPU
Shape : (3,2)
[[0.00000e+00 1.00000e+00 ]
 [4.00000e+00 5.00000e+00 ]
 [8.00000e+00 9.00000e+00 ]]

◆ Accessor() [2/3]

template<class T >
cytnx::Accessor::Accessor ( const std::initializer_list< T > &  list)
inlineexplicit

◆ Accessor() [3/3]

template<class T >
cytnx::Accessor::Accessor ( const std::vector< T > &  list)
inlineexplicit

Member Function Documentation

◆ all()

static Accessor cytnx::Accessor::all ( )
inlinestatic

access the whole rank, this is similar to [:] in python

Example:

c++ API:

#include "cytnx.hpp"
#include <iostream>
using namespace cytnx;
using namespace std;
int main() {
// 1. handy alias:
typedef Accessor ac;
/*
2. Create a Tensor with
shape (3,4,5),
dtype =Type.Double [default],
device=Device.cpu [default]
*/
A.reshape_({2, 3, 4});
cout << A << endl;
/*
3. Accessing elements using accessor
This is similar as python slices.
-> A[0,:,0:2:1]
*/
Tensor B = A(0, ac::all(), ac::range(0, 2, 1));
cout << B << endl;
/* [Note] Conversion from python slice to ac:
[::x] = ac::step(x)
[a::x] = ac::tilend(a,x)
[a::] = ac::tilend(a)
[:b:] = ac::range(0,b,1)
[a:b:x] = ac::range(a,b,x)
*/
return 0;
}

output>

Total elem: 24
type  : Double (Float64)
cytnx device: CPU
Shape : (2,3,4)
[[[0.00000e+00 1.00000e+00 2.00000e+00 3.00000e+00 ]
  [4.00000e+00 5.00000e+00 6.00000e+00 7.00000e+00 ]
  [8.00000e+00 9.00000e+00 1.00000e+01 1.10000e+01 ]]
 [[1.20000e+01 1.30000e+01 1.40000e+01 1.50000e+01 ]
  [1.60000e+01 1.70000e+01 1.80000e+01 1.90000e+01 ]
  [2.00000e+01 2.10000e+01 2.20000e+01 2.30000e+01 ]]]



Total elem: 6
type  : Double (Float64)
cytnx device: CPU
Shape : (3,2)
[[0.00000e+00 1.00000e+00 ]
 [4.00000e+00 5.00000e+00 ]
 [8.00000e+00 9.00000e+00 ]]

python API:

import sys
from pathlib import Path
home = str(Path.home())
sys.path.append(home + '/Cytnx_lib')
from cytnx import *
A = arange(24);
A.reshape_(2,3,4);
print(A)
B = A[0,:,0:2:1]
print(B)

output>

Total elem: 24
type  : Double (Float64)
cytnx device: CPU
Shape : (2,3,4)
[[[0.00000e+00 1.00000e+00 2.00000e+00 3.00000e+00 ]
  [4.00000e+00 5.00000e+00 6.00000e+00 7.00000e+00 ]
  [8.00000e+00 9.00000e+00 1.00000e+01 1.10000e+01 ]]
 [[1.20000e+01 1.30000e+01 1.40000e+01 1.50000e+01 ]
  [1.60000e+01 1.70000e+01 1.80000e+01 1.90000e+01 ]
  [2.00000e+01 2.10000e+01 2.20000e+01 2.30000e+01 ]]]




Total elem: 6
type  : Double (Float64)
cytnx device: CPU
Shape : (3,2)
[[0.00000e+00 1.00000e+00 ]
 [4.00000e+00 5.00000e+00 ]
 [8.00000e+00 9.00000e+00 ]]

◆ qns()

static Accessor cytnx::Accessor::qns ( const std::vector< std::vector< cytnx_int64 > > &  qns)
inlinestatic

◆ range()

static Accessor cytnx::Accessor::range ( const cytnx_int64 min,
const cytnx_int64 max,
const cytnx_int64 step = 1 
)
inlinestatic

access the range at assigned rank, this is similar to min:max:step in python

Parameters
min
max
step

Example:

c++ API:

#include "cytnx.hpp"
#include <iostream>
using namespace cytnx;
using namespace std;
int main() {
// 1. handy alias:
typedef Accessor ac;
/*
2. Create a Tensor with
shape (3,4,5),
dtype =Type.Double [default],
device=Device.cpu [default]
*/
A.reshape_({2, 3, 4});
cout << A << endl;
/*
3. Accessing elements using accessor
This is similar as python slices.
-> A[0,:,0:2:1]
*/
Tensor B = A(0, ac::all(), ac::range(0, 2, 1));
cout << B << endl;
/* [Note] Conversion from python slice to ac:
[::x] = ac::step(x)
[a::x] = ac::tilend(a,x)
[a::] = ac::tilend(a)
[:b:] = ac::range(0,b,1)
[a:b:x] = ac::range(a,b,x)
*/
return 0;
}

output>

Total elem: 24
type  : Double (Float64)
cytnx device: CPU
Shape : (2,3,4)
[[[0.00000e+00 1.00000e+00 2.00000e+00 3.00000e+00 ]
  [4.00000e+00 5.00000e+00 6.00000e+00 7.00000e+00 ]
  [8.00000e+00 9.00000e+00 1.00000e+01 1.10000e+01 ]]
 [[1.20000e+01 1.30000e+01 1.40000e+01 1.50000e+01 ]
  [1.60000e+01 1.70000e+01 1.80000e+01 1.90000e+01 ]
  [2.00000e+01 2.10000e+01 2.20000e+01 2.30000e+01 ]]]



Total elem: 6
type  : Double (Float64)
cytnx device: CPU
Shape : (3,2)
[[0.00000e+00 1.00000e+00 ]
 [4.00000e+00 5.00000e+00 ]
 [8.00000e+00 9.00000e+00 ]]

python API:

import sys
from pathlib import Path
home = str(Path.home())
sys.path.append(home + '/Cytnx_lib')
from cytnx import *
A = arange(24);
A.reshape_(2,3,4);
print(A)
B = A[0,:,0:2:1]
print(B)

output>

Total elem: 24
type  : Double (Float64)
cytnx device: CPU
Shape : (2,3,4)
[[[0.00000e+00 1.00000e+00 2.00000e+00 3.00000e+00 ]
  [4.00000e+00 5.00000e+00 6.00000e+00 7.00000e+00 ]
  [8.00000e+00 9.00000e+00 1.00000e+01 1.10000e+01 ]]
 [[1.20000e+01 1.30000e+01 1.40000e+01 1.50000e+01 ]
  [1.60000e+01 1.70000e+01 1.80000e+01 1.90000e+01 ]
  [2.00000e+01 2.10000e+01 2.20000e+01 2.30000e+01 ]]]




Total elem: 6
type  : Double (Float64)
cytnx device: CPU
Shape : (3,2)
[[0.00000e+00 1.00000e+00 ]
 [4.00000e+00 5.00000e+00 ]
 [8.00000e+00 9.00000e+00 ]]

◆ step()

static Accessor cytnx::Accessor::step ( const cytnx_int64 step)
inlinestatic

◆ tilend()

static Accessor cytnx::Accessor::tilend ( const cytnx_int64 min,
const cytnx_int64 step = 1 
)
inlinestatic

◆ type()

int cytnx::Accessor::type ( ) const
inline

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