Cytnx v0.7.6
Loading...
Searching...
No Matches
Public Member Functions | List of all members
cytnx::LinOp Class Reference

#include <LinOp.hpp>

Public Member Functions

 LinOp (const std::string &type, const cytnx_uint64 &nx, const int &dtype=Type.Double, const int &device=Device.cpu)
 Linear Operator class for iterative solvers.
 
template<class T >
void set_elem (const cytnx_uint64 &i, const cytnx_uint64 &j, const T &elem, const bool check_exists=true)
 
Tensor::Tproxy operator() (const cytnx_uint64 &i, const cytnx_uint64 &j)
 
void set_device (const int &device)
 
void set_dtype (const int &dtype)
 
int device () const
 
int dtype () const
 
cytnx_uint64 nx () const
 
void _print ()
 
virtual Tensor matvec (const Tensor &Tin)
 

Constructor & Destructor Documentation

◆ LinOp()

cytnx::LinOp::LinOp ( const std::string &  type,
const cytnx_uint64 nx,
const int &  dtype = Type.Double,
const int &  device = Device.cpu 
)
inline

Linear Operator class for iterative solvers.

Parameters
typethe type of operator, currently it can only be "mv" (matvec) or "mv_elem" (matvec with pre-store element)
nxthe last dimension of operator, this should be the dimension of the input vector when "mv_elem" is used.
dtypethe Operator's dtype. Note that this should match the input/output Tensor's dtype.
devicethe Operator's on device.

Note:

  1. the device and dtype should be set. This should be the same as the input and output vectors. by default, we assume custom_f take input and output vector to be on CPU and Double type.

Details:

The LinOp class is a class that defines a custom Linear operation acting on a Tensor or UniTensor. To use, inherit this class and override the matvec function.
See the following examples for how to use them.

Example:

c++ API:

output>

 

python API:

from cytnx import *
# LinOp class provides a base class that defines the operation on a vector.
# This class or it's derived class are required
# for using the cytnx's iterative solver such as Lanczos and Arnodi.
#-----------------------------------------
# Suppose we want to define a custom linear operation
# acting on input vector t (dim=4) that swap the first and last element
# and add the 2nd and 3rd element with one.
t = arange(4)
print(t)
# Method 1, write a custom function, and assign into the LinOp class
# -----------------------------------
# [Note] the function should have the signature
# Tensor f(const Tensor&) as in C++
def myfunc(v):
out = v.clone();
out[0], out[3] = v[3], v[0]; #swap
out[1]+=1 #add 1
out[2]+=1 #add 1
return out;
lop = LinOp("mv",nx=4,dtype=Type.Double,device=Device.cpu,custom_f=myfunc)
print(lop.matvec(t))
# Method 2, write a custom class that inherit LinOp class.
# ------------------------------------
# [Note] Instead of writing a custom class, we overload the matvec() directly.
# Inheritance is handy if there are additional parameters/arguments
# needed for the custom operation.
class MyOp(LinOp):
AddConst = 1 # let's make it a class member.
def __init__(self,typ,nx,aconst):
LinOp.__init__(self,typ,nx,Type.Double,Device.cpu)
self.AddConst = aconst
def matvec(self,v):
out = v.clone()
out[0],out[3] = v[3],v[0]
out[1]+=self.AddConst #add the constant
out[2]+=self.AddConst #add the constant
return out
mylop = MyOp("mv",nx=4,aconst=3) # let's add 3 instead of one.
print(mylop.matvec(t))


output>

Total elem: 4
type  : Double (Float64)
cytnx device: CPU
Shape : (4)
[0.00000e+00 1.00000e+00 2.00000e+00 3.00000e+00 ]




Total elem: 4
type  : Double (Float64)
cytnx device: CPU
Shape : (4)
[3.00000e+00 2.00000e+00 3.00000e+00 0.00000e+00 ]




Total elem: 4
type  : Double (Float64)
cytnx device: CPU
Shape : (4)
[3.00000e+00 4.00000e+00 5.00000e+00 0.00000e+00 ]



Member Function Documentation

◆ _print()

void cytnx::LinOp::_print ( )

◆ device()

int cytnx::LinOp::device ( ) const
inline

◆ dtype()

int cytnx::LinOp::dtype ( ) const
inline

◆ matvec()

UniTensor cytnx::LinOp::matvec ( const Tensor Tin)
virtual

◆ nx()

cytnx_uint64 cytnx::LinOp::nx ( ) const
inline

◆ operator()()

Tensor::Tproxy cytnx::LinOp::operator() ( const cytnx_uint64 i,
const cytnx_uint64 j 
)
inline

◆ set_device()

void cytnx::LinOp::set_device ( const int &  device)
inline

◆ set_dtype()

void cytnx::LinOp::set_dtype ( const int &  dtype)
inline

◆ set_elem()

template<class T >
void cytnx::LinOp::set_elem ( const cytnx_uint64 i,
const cytnx_uint64 j,
const T &  elem,
const bool  check_exists = true 
)
inline

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