3 from cython.operator cimport dereference 
as deref, preincrement 
as inc
     4 from libc.stdint cimport int64_t
     5 from libc.stdint cimport int32_t
     6 from libc.stdint cimport int16_t
     7 from libc.stdint cimport int8_t
    10 from libcpp.complex cimport *
    11 ctypedef double complex complex128_t
    12 ctypedef float complex complex64_t
    13 from libc.stdlib cimport malloc, free
    17 from copy 
import deepcopy
    23 from libcpp cimport bool
    24 from libc.stdint cimport int64_t
    26 cdef extern 
from "<functional>" namespace 
"std":
    27     cdef cppclass function[dtype]:
    36 cdef extern 
from "mpi.h":
    37     void MPI_Init(int * argc, char *** argv)
    38     int MPI_Initialized(int *)
    42 type_index[np.bool] = 1
    43 type_index[np.int32] = 2
    44 type_index[np.int64] = 3
    45 type_index[np.float32] = 4
    46 type_index[np.float64] = 5
    47 type_index[np.complex64] = 6
    48 type_index[np.complex128] = 7
    49 type_index[np.int16] = 8
    50 type_index[np.int8] = 9
    52 cdef int is_mpi_init=0
    53 MPI_Initialized(<int*>&is_mpi_init)
    55   MPI_Init(&is_mpi_init, <char***>NULL)
    59     Kill all working nodes.    63 cdef extern 
from "ctf.hpp" namespace 
"CTF_int":
    64     cdef cppclass algstrct:
    68     cdef cppclass ctensor 
"CTF_int::tensor":
    73         ctensor(ctensor * other, bool copy, bool alloc_data)
    74         ctensor(ctensor * other, int * new_sym)
    77         int read(int64_t num_pair,
    81         int read(int64_t num_pair,
    86         int write(int64_t   num_pair,
    91         int write(int64_t num_pair,
    95         int read_local(int64_t * num_pair,
    97         int read_local(int64_t * num_pair,
   100         int read_local_nnz(int64_t * num_pair,
   103         void allread(int64_t * num_pair, char * data, bool unpack)
   104         void slice(int *, int *, char *, ctensor *, int *, int *, char *)
   105         int64_t get_tot_size(bool packed)
   106         void get_raw_data(char **, int64_t * size)
   107         int 
permute(ctensor * A, int ** permutation_A, char * alpha, int ** permutation_B, char * beta)
   108         void conv_type[dtype_A,dtype_B](ctensor * B)
   109         void compare_elementwise[dtype](ctensor * A, ctensor * B)
   110         void not_equals[dtype](ctensor * A, ctensor * B)
   111         void smaller_than[dtype](ctensor * A, ctensor * B)
   112         void smaller_equal_than[dtype](ctensor * A, ctensor * B)
   113         void larger_than[dtype](ctensor * A, ctensor * B)
   114         void larger_equal_than[dtype](ctensor * A, ctensor * B)
   115         void exp_helper[dtype_A,dtype_B](ctensor * A)
   116         void read_sparse_from_file[dtype](char * fpath, bool with_vals)
   117         void write_sparse_to_file[dtype](char * fpath, bool with_vals)
   118         void read_dense_from_file(char *)
   119         void write_dense_to_file(char *)
   120         void true_divide[dtype](ctensor * A)
   121         void pow_helper_int[dtype](ctensor * A, int p)
   122         int sparsify(char * threshold, int take_abs)
   126         Contract_Term operator*(double scl);
   127         Contract_Term operator*(Term A);
   128         Sum_Term operator+(Term A);
   129         Sum_Term operator-(Term A);
   130         void operator<<(double scl);
   131         void operator<<(Term B);
   132         void mult_scl(char *);
   135     cdef cppclass Sum_Term(Term):
   136         Sum_Term(Term * B, Term * A);
   137         Sum_Term operator+(Term A);
   138         Sum_Term operator-(Term A);
   140     cdef cppclass Contract_Term(Term):
   141         Contract_Term(Term * B, Term * A);
   142         Contract_Term operator*(double scl);
   143         Contract_Term operator*(Term A);
   145     cdef cppclass endomorphism:
   148     cdef cppclass univar_function:
   151     cdef cppclass bivar_function:
   154     cdef cppclass Endomorphism[dtype_A](endomorphism):
   155         Endomorphism(function[void(dtype_A&)] f_);
   157     cdef cppclass Univar_Transform[dtype_A,dtype_B](univar_function):
   158         Univar_Transform(function[void(dtype_A,dtype_B&)] f_);
   160     cdef cppclass Bivar_Transform[dtype_A,dtype_B,dtype_C](bivar_function):
   161         Bivar_Transform(function[void(dtype_A,dtype_B,dtype_C&)] f_);
   163 cdef extern 
from "../ctf_ext.h" namespace 
"CTF_int":
   165     cdef void pow_helper[dtype](ctensor * A, ctensor * B, ctensor * C, char * idx_A, char * idx_B, char * idx_C);
   166     cdef void abs_helper[dtype](ctensor * A, ctensor * B);
   167     cdef void all_helper[dtype](ctensor * A, ctensor * B_bool, char * idx_A, char * idx_B)
   168     cdef void conj_helper[dtype](ctensor * A, ctensor * B);
   169     cdef void any_helper[dtype](ctensor * A, ctensor * B_bool, char * idx_A, char * idx_B)
   170     cdef void get_real[dtype](ctensor * A, ctensor * B)
   171     cdef void get_imag[dtype](ctensor * A, ctensor * B)
   172     cdef void set_real[dtype](ctensor * A, ctensor * B)
   173     cdef void set_imag[dtype](ctensor * A, ctensor * B)
   174     cdef void 
subsample(ctensor * A, double probability)
   175     cdef void 
matrix_svd(ctensor * A, ctensor * U, ctensor * S, ctensor * VT, int rank)
   176     cdef void 
matrix_svd_cmplx(ctensor * A, ctensor * U, ctensor * S, ctensor * VT, int rank)
   177     cdef void 
matrix_qr(ctensor * A, ctensor * Q, ctensor * R)
   179     cdef void 
conv_type(int type_idx1, int type_idx2, ctensor * A, ctensor * B)
   181 cdef extern 
from "ctf.hpp" namespace 
"CTF":
   188     cdef cppclass Idx_Tensor(Term):
   189         Idx_Tensor(ctensor *, char *);
   190         void operator=(Term B);
   191         void operator=(Idx_Tensor B);
   192         void multeq(double scl);
   194     cdef cppclass Typ_Idx_Tensor[dtype](Idx_Tensor):
   195         Typ_Idx_Tensor(ctensor *, char *)
   196         void operator=(Term B)
   197         void operator=(Idx_Tensor B)
   199     cdef cppclass Tensor[dtype](ctensor):
   200         Tensor(int, bint, int *, int *)
   202         void fill_random(dtype, dtype)
   203         void fill_sp_random(dtype, dtype, double)
   204         void read_sparse_from_file(char *, bool)
   205         void write_sparse_to_file(char *, bool)
   206         Typ_Idx_Tensor i(char *)
   207         void read(int64_t, int64_t *, dtype *)
   208         void read(int64_t, dtype, dtype, int64_t *, dtype *)
   209         void read_local(int64_t *, int64_t **, dtype **)
   210         void read_local_nnz(int64_t *, int64_t **, dtype **)
   211         void write(int64_t, int64_t *, dtype *)
   212         void write(int64_t, dtype, dtype, int64_t *, dtype *)
   217     cdef cppclass Vector[dtype](ctensor):
   221     cdef cppclass Matrix[dtype](ctensor):
   226         Matrix(int, int, int, World)
   228     cdef cppclass contraction:
   229         contraction(ctensor *, int *, ctensor *, int *, char *, ctensor *, int *, char *, bivar_function *)
   236     return type(
'Enum', (), enums)
   238 SYM = _enum(NS=0, SY=1, AS=2, SH=3)
   240 def _ord_comp(o1,o2):
   243     if isinstance(o1,int):
   247     if isinstance(o2,int):
   253 def _get_np_div_dtype(typ1, typ2):
   254     return (np.zeros(1,dtype=typ1)/np.ones(1,dtype=typ2)).dtype
   256 def _get_np_dtype(typs):
   257     return np.sum([np.zeros(1,dtype=typ) 
for typ 
in typs]).dtype
   259 cdef char* char_arr_py_to_c(a):
   262     ca = <char*> malloc(dim*sizeof(char))
   265     for i 
in range(0,dim):
   270 cdef int64_t* int64_t_arr_py_to_c(a):
   273     ca = <int64_t*> malloc(dim*sizeof(int64_t))
   276     for i 
in range(0,dim):
   281 cdef int* int_arr_py_to_c(a):
   284     ca = <int*> malloc(dim*sizeof(int))
   287     for i 
in range(0,dim):
   326         if isinstance(scl, (np.int, np.float, np.double, np.number)):
   327             self.tm = (deref(self.tm) * <double>scl).clone()
   329             st = np.ndarray([],dtype=self.dtype).itemsize
   330             beta = <char*>malloc(st)
   331             b = np.asarray([scl],dtype=self.dtype)[0]
   333             for j 
in range(0,st):
   334                 beta[j] = nb.view(dtype=np.int8)[j]
   335             self.tm.mult_scl(beta)
   338         if other.dtype != self.dtype:
   339             other = 
tensor(copy=other,dtype=self.dtype)
   343         if other.dtype != self.dtype:
   344             other = 
tensor(copy=other,dtype=self.dtype)
   350         if (isinstance(first,term)):
   351             if (isinstance(second,term)):
   364         raise ValueError(
"CTF PYTHON ERROR: in abstract conv_type function")
   367         raise ValueError(
"CTF PYTHON ERROR: in abstract __repr__ function")
   370         if isinstance(other, term):
   371             if other.dtype != self.dtype:
   372                 other.conv_type(self.dtype)
   373             deref((<itensor>self).tm) << deref((<term>other).tm)
   375             tsr_copy = 
astensor(other,dtype=self.dtype)
   376             deref((<itensor>self).tm) << deref(
itensor(tsr_copy,
"").it)
   387         self.dtype = _get_np_dtype([a.dtype,b.dtype])
   388         if self.dtype != a.dtype:
   389             self.a.conv_type(self.dtype)
   390         if self.dtype != b.dtype:
   391             self.b.conv_type(self.dtype)
   392         self.tm = new Contract_Term(self.a.tm.clone(), self.b.tm.clone())
   395         self.a.conv_type(dtype)
   396         self.b.conv_type(dtype)
   399         self.tm = new Contract_Term(self.a.tm.clone(), self.b.tm.clone())
   402         return "a is" + self.a.__repr__() + 
"b is" + self.b.__repr__()
   411         self.dtype = _get_np_dtype([a.dtype,b.dtype])
   412         if self.dtype != a.dtype:
   413             self.a.conv_type(self.dtype)
   414         if self.dtype != b.dtype:
   415             self.b.conv_type(self.dtype)
   416         self.tm = new Sum_Term(self.a.tm.clone(), self.b.tm.clone())
   419         self.a.conv_type(dtype)
   420         self.b.conv_type(dtype)
   423         self.tm = new Sum_Term(self.a.tm.clone(), self.b.tm.clone())
   426         return "a is" + self.a.__repr__() + 
"b is" + self.b.__repr__()
   443         self.tsr = 
tensor(copy=self.tsr,dtype=dtype)
   444         self.it = new Idx_Tensor(self.tsr.dt, self.string.encode())
   450         return "tsr is" + self.tsr.__repr__()
   453         self.it = new Idx_Tensor(a.dt, string.encode())
   460         if (isinstance(first,itensor)):
   461             if (isinstance(second,term)):
   467             if (isinstance(first,term)):
   474         self.it.multeq(<double>s)
   484     allstr = 
"abcdefghijklmonpqrstuvwzyx0123456789,./;'][=-`"   490     The class for CTF Python tensor.   495         The number of bytes for the tensor.   498         Total number of elements in the tensor.   501         Number of dimensions.   504         0 indicates the tensor is not sparse tensor, 1 means the tensor is CTF sparse tensor.   507         Tuple of bytes for each dimension to traverse the tensor.   510         Tuple of each dimension.   513         Numpy data-type, indicating the type of tensor.   516         One element in bytes.   519         Bytes memory order for the tensor.   531         Whether all elements give an axis for a tensor is true.   534         Copy the tensor to specified type.   537         Return the self conjugate tensor element-wisely.   540         Copy the tensor to a new tensor.   543         Return the diagonal of the tensor if it is 2D. If the tensor is a higher order square tensor (same shape for every dimension), return diagonal of tensor determined by axis1=0, axis2=1.   546         Return the dot product with tensor other.   549         Fill random elements to the tensor.   552         Fill random elements to a sparse tensor.   555         Convert numpy ndarray to CTF tensor.   558         Return the dims/shape of tensor.   561         Return the dtype of tensor.   564         Core function on summing the ctensor.   567         Return imaginary part of a tensor or set its imaginary part to new value.   570         1-norm of the tensor.   573         2-norm of the tensor.   576         Infinity-norm of the tensor.   582         Function to print the non-zero elements and their indices of a tensor.   585         Return the flattened tensor.   588         Helper function on reading a tensor.   591         Helper function on reading a tensor.   594         Helper function on reading a tensor.   597         Helper function on reading a tensor.   600         Return real part of a tensor or set its real part to new value.   603         Return a new tensor with reshaped shape.   606         Extract a sample of the entries (if sparse of the current nonzeros) by keeping each entry with probability p. Also transforms tensor into sparse format if not already.   609         Set all elements in a tensor to a value.   612         Set all elements in a tensor to 0.   615         Sum of elements in tensor or along specified axis.   618         Take elements from a tensor along axis.   621         Return the tensor dot product of two tensors along axes.   624         Convert the tensor to numpy array.   627         Return the sum over the diagonal of the tensor.   630         Return the transposed tensor with specified order of axes.   633         Helper function on writing a tensor.   636         Helper function on writing a tensor.   652         Attribute strides. Tuple of bytes for each dimension to traverse the tensor.   659         Attribute nbytes. The number of bytes for the tensor.   666         Attribute itemsize. One element in bytes.   673         Attribute size. Total number of elements in the tensor.   680         Attribute ndim. Number of dimensions.   687         Attribute shape. Tuple of each dimension.   694         Attribute dtype. Numpy data-type, indicating the type of tensor.   701         Attribute order. Bytes memory order for the tensor.   704             return chr(self.
order)
   708         Attribute sp. 0 indicates the tensor is not sparse tensor, 1 means the tensor is CTF sparse tensor.   720     def _bool_sum(tensor self):
   724     def _convert_type(tensor self, tensor B):
   725         conv_type(type_index[self.
dtype], type_index[B.dtype], <ctensor*>self.
dt, <ctensor*>B.dt);
   731         Return the dims/shape of tensor.   736             Dims or shape of the tensor.   744         Return the dtype of tensor.   754     def __cinit__(self, lens=None, sp=None, sym=None, dtype=None, order=None, tensor copy=None):
   765             if isinstance(copy,tensor) == 
False:
   777         if isinstance(dtype,np.dtype):
   786         if dtype == np.complex:
   787             dtype = np.complex128
   791             self.
dtype = <cnp.dtype>np.complex128
   793             self.
dtype = <cnp.dtype>np.float64
   795             self.
dtype = <cnp.dtype>dtype
   796         if isinstance(lens,int):
   798         lens = [int(l) 
for l 
in lens]
   801         if isinstance(order,int):
   804             self.
order = ord(order)
   809             self.
sym = np.asarray(sym)
   810         if self.
dtype == np.bool:
   815         for i 
in range(len(self.
shape)):
   818         strides = [1] * len(self.
shape)
   819         for i 
in range(len(self.
shape)-1, -1, -1):
   820             if i == len(self.
shape) -1:
   823                 strides[i] = self.
shape[i+1] * strides[i+1]
   827         if _ord_comp(self.
order, 
'F'):
   828             rlens = _rev_array(lens)
   830                 rsym = _rev_array(self.
sym)
   831                 rsym[0:-1] = rsym[1:]
   834         clens = int_arr_py_to_c(rlens)
   836         csym = int_arr_py_to_c(rsym)
   838             if self.
dtype == np.float64:
   839                 self.
dt = new Tensor[double](self.
ndim, sp, clens, csym)
   840             elif self.
dtype == np.complex64:
   841                 self.
dt = new Tensor[complex64_t](self.
ndim, sp, clens, csym)
   842             elif self.
dtype == np.complex128:
   843                 self.
dt = new Tensor[complex128_t](self.
ndim, sp, clens, csym)
   844             elif self.
dtype == np.bool:
   845                 self.
dt = new Tensor[bool](self.
ndim, sp, clens, csym)
   846             elif self.
dtype == np.int64:
   847                 self.
dt = new Tensor[int64_t](self.
ndim, sp, clens, csym)
   848             elif self.
dtype == np.int32:
   849                 self.
dt = new Tensor[int32_t](self.
ndim, sp, clens, csym)
   850             elif self.
dtype == np.int16:
   851                 self.
dt = new Tensor[int16_t](self.
ndim, sp, clens, csym)
   852             elif self.
dtype == np.int8:
   853                 self.
dt = new Tensor[int8_t](self.
ndim, sp, clens, csym)
   854             elif self.
dtype == np.float32:
   855                 self.
dt = new Tensor[float](self.
ndim, sp, clens, csym)
   857                 raise ValueError(
'CTF PYTHON ERROR: bad dtype')
   859             if isinstance(copy, tensor):
   860                 if dtype 
is None or dtype == copy.dtype:
   861                     if np.all(sym == copy.sym):
   862                         self.
dt = new ctensor(<ctensor*>copy.dt, 
True, 
True)
   864                         self.
dt = new ctensor(<ctensor*>copy.dt, csym)
   867                     copy._convert_type(ccopy)
   868                     self.
dt = new ctensor(<ctensor*>ccopy.dt, 
True, 
True)
   879         Permute the dimensions of the input tensor.   884             Tensor with permuted axes.   893         >>> a = ctf.zeros([3,4,5])   903         tensor.transpose(*axes)   904         Return the transposed tensor with specified order of axes.   909             Tensor with permuted axes.   918         >>> a = ctf.zeros([3,4,5])   921         >>> a.transpose([2,1,0]).shape   925             if isinstance(axes[0], (tuple, list, np.ndarray)):
   932     def _ufunc_interpret(self, tensor other, gen_tsr=True):
   933         if self.
order != other.order:
   934             raise ValueError(
"Universal functions among tensors with different order, i.e. Fortran vs C are not currently supported")
   935         out_order = self.
order   936         out_dtype = _get_np_dtype([self.
dtype, other.dtype])
   937         out_dims = np.zeros(np.maximum(self.
ndim, other.ndim), dtype=np.int)
   938         out_sp = min(self.
sp,other.sp)
   939         out_sym = [SYM.NS]*len(out_dims)
   940         ind_coll = _get_num_str(3*out_dims.size)
   941         idx_C = ind_coll[0:out_dims.size]
   944         red_idx_num = out_dims.size
   945         for i 
in range(out_dims.size):
   946             if i<self.
ndim and i<other.ndim:
   947                 if self.
shape[-i-1] == other.shape[-i-1]:
   948                     idx_A = idx_C[-i-1] + idx_A
   949                     idx_B = idx_C[-i-1] + idx_B
   950                     if i+1<self.
ndim and i+1<other.ndim:
   951                         if self.
sym[-i-2] == other.sym[-i-2]:
   952                             out_sym[-i-2] = self.
sym[-i-2]
   953                 elif self.
shape[-i-1] == 1:
   954                     idx_A = ind_coll[red_idx_num] + idx_A
   956                     idx_B = idx_C[-i-1] + idx_B
   958                         if i+1>=self.
ndim or self.
shape[-i-2] == 1:
   959                             out_sym[-i-2] = other.sym[-i-2]
   960                 elif other.shape[-i-1] == 1:
   961                     idx_A = idx_C[-i-1] + idx_A
   962                     idx_B = ind_coll[red_idx_num] + idx_B
   965                         if i+1>=other.ndim 
or other.shape[-i-2] == 1:
   966                             out_sym[-i-2] = self.
sym[-i-2]
   968                     raise ValueError(
"Invalid use of universal function broadcasting, tensor dimensions are both non-unit and don't match")
   969                 out_dims[-i-1] = np.maximum(self.
shape[-i-1], other.shape[-i-1])
   971                 idx_A = idx_C[-i-1] + idx_A
   972                 out_dims[-i-1] = self.
shape[-i-1]
   974                     out_sym[-i-2] = self.
sym[-i-2]
   976                 idx_B = idx_C[-i-1] + idx_B
   977                 out_dims[-i-1] = other.shape[-i-1]
   979                     out_sym[-i-2] = other.sym[-i-2]
   981             out_tsr = 
tensor(out_dims, out_sp, out_sym, out_dtype, out_order)
   984         return [idx_A, idx_B, idx_C, out_tsr]
   996             raise TypeError(
"CTF PYTHON ERROR: The truth value of a tensor with more than one element is ambiguous. Use ctf.any() or ctf.all()")
  1004             raise TypeError(
"CTF PYTHON ERROR: only length-1 tensors can be converted to Python scalars")
  1009             raise TypeError(
"CTF PYTHON ERROR: only length-1 tensors can be converted to Python scalars")
  1018         [tsr, otsr] = _match_tensor_types(self, neg_one)
  1019         [idx_A, idx_B, idx_C, out_tsr] = tsr._ufunc_interpret(otsr)
  1020         out_tsr.i(idx_C) << tsr.i(idx_A)*otsr.i(idx_B)
  1024         [tsr, otsr] = _match_tensor_types(self,other)
  1026         [idx_A, idx_B, idx_C, out_tsr] = tsr._ufunc_interpret(otsr)
  1028         out_tsr.i(idx_C) << tsr.i(idx_A)
  1029         out_tsr.i(idx_C) << otsr.i(idx_B)
  1034         if np.result_type(self.
dtype, other.dtype) != self.
dtype:
  1035             raise TypeError(
'CTF PYTHON ERROR: refusing to downgrade type within __iadd__ (+=), as done by numpy')
  1037         if len(idx_C) != self.
ndim:
  1038             raise ValueError(
'CTF PYTHON ERROR: invalid call to __iadd__ (+=)')
  1039         if self.
dtype != other.dtype:
  1040             [tsr, otsr] = _match_tensor_types(self,other) 
  1041             self.
i(idx_C) << otsr.i(idx_A)
  1043             self.
i(idx_C) << other.i(idx_A)
  1047         [tsr, otsr] = _match_tensor_types(self,other)
  1049         [idx_A, idx_B, idx_C, out_tsr] = tsr._ufunc_interpret(otsr)
  1051         out_tsr.i(idx_C) << tsr.i(idx_A)*otsr.i(idx_B)
  1056         if np.result_type(self.
dtype, other.dtype) != self.
dtype:
  1057             raise TypeError(
'CTF PYTHON ERROR: refusing to downgrade type within __imul__ (*=), as done by numpy')
  1059         if len(idx_C) != self.
ndim or idx_C != idx_A:
  1060             raise ValueError(
'CTF PYTHON ERROR: invalid call to __imul__ (*=)')
  1061         self_copy = 
tensor(copy=self)
  1063         self.
i(idx_C) << self_copy.i(idx_A)*other.i(idx_B)
  1067         [tsr, otsr] = _match_tensor_types(self,other)
  1069         [idx_A, idx_B, idx_C, out_tsr] = tsr._ufunc_interpret(otsr)
  1070         out_tsr.i(idx_C) << tsr.i(idx_A)
  1071         out_tsr.i(idx_C) << -1*otsr.i(idx_B)
  1076         if np.result_type(self.
dtype, other.dtype) != self.
dtype:
  1077             raise TypeError(
'CTF PYTHON ERROR: refusing to downgrade type within __isub__ (-=), as done by numpy')
  1079         if len(idx_C) != self.
ndim:
  1080             raise ValueError(
'CTF PYTHON ERROR: invalid call to __isub__ (-=)')
  1081         if self.
dtype != other.dtype:
  1082             [tsr, otsr] = _match_tensor_types(self,other) 
  1083             self.
i(idx_C) << -1*otsr.i(idx_A)
  1085             self.
i(idx_C) << -1*other.i(idx_A)
  1089         return _div(self,other)
  1093         if np.result_type(self.
dtype, other.dtype) != self.
dtype:
  1094             raise TypeError(
'CTF PYTHON ERROR: refusing to downgrade type within __itruediv__ (/=), as done by numpy')
  1096         if len(idx_C) != self.
ndim or idx_C != idx_A:
  1097             raise ValueError(
'CTF PYTHON ERROR: invalid call to __itruediv__ (/=)')
  1098         if isinstance(other_in, tensor):
  1099             otsr = 
tensor(copy=other)
  1102         otsr._invert_elements()
  1103         self_copy = 
tensor(copy=self)
  1105         self.
i(idx_C) << self_copy.i(idx_A)*otsr.i(idx_B)
  1109         return _div(self,other)
  1114         if np.result_type(self.
dtype, other.dtype) != self.
dtype:
  1115             raise TypeError(
'CTF PYTHON ERROR: refusing to downgrade type within __idiv__ (/=), as done by numpy')
  1117         if len(idx_C) != self.
ndim or idx_C != idx_A:
  1118             raise ValueError(
'CTF PYTHON ERROR: invalid call to __idiv__ (/=)')
  1119         if isinstance(other_in, tensor):
  1120             otsr = 
tensor(copy=other)
  1123         otsr._invert_elements()
  1124         self_copy = 
tensor(copy=self)
  1126         self.
i(idx_C) << self_copy.i(idx_A)*otsr.i(idx_B)
  1139         if modulus 
is not None:
  1140             raise ValueError(
'CTF PYTHON ERROR: powering function does not accept third parameter (modulus)')
  1141         return power(self,other)
  1152     def _invert_elements(self):
  1153         if self.
dtype == np.float64:
  1154             self.dt.true_divide[double](<ctensor*>self.
dt)
  1155         elif self.
dtype == np.float32:
  1156             self.dt.true_divide[float](<ctensor*>self.
dt)
  1157         elif self.
dtype == np.complex64:
  1158             self.dt.true_divide[complex64_t](<ctensor*>self.
dt)
  1159         elif self.
dtype == np.complex128:
  1160             self.dt.true_divide[complex128_t](<ctensor*>self.
dt)
  1161         elif self.
dtype == np.int64:
  1162             self.dt.true_divide[int64_t](<ctensor*>self.
dt)
  1163         elif self.
dtype == np.int32:
  1164             self.dt.true_divide[int32_t](<ctensor*>self.
dt)
  1165         elif self.
dtype == np.int16:
  1166             self.dt.true_divide[int16_t](<ctensor*>self.
dt)
  1167         elif self.
dtype == np.int8:
  1168             self.dt.true_divide[int8_t](<ctensor*>self.
dt)
  1169         elif self.
dtype == np.bool:
  1170             self.dt.true_divide[bool](<ctensor*>self.
dt)
  1173         if not isinstance(other, tensor):
  1174             raise ValueError(
"input should be tensors")
  1175         return dot(self, other)
  1179         tensor.fill_random(mn=None, mx=None)  1180         Fill random elements to the tensor.  1185             The range of random number from, default 0.  1188             The range of random number to, default 1.  1192         ctf: ctf.tensor.fill_sp_random()  1197         >>> a = ctf.zeros([2, 2])  1201         >>> a.fill_random(3,5)  1203             array([[3.31908598, 4.34013067],  1204                    [4.5355426 , 4.6763659 ]])  1210         if self.
dtype == np.int32:
  1212         elif self.
dtype == np.int64:
  1214         elif self.
dtype == np.float32:
  1216         elif self.
dtype == np.float64:
  1219             raise ValueError(
'CTF PYTHON ERROR: bad dtype')
  1223         tensor.fill_sp_random(mn=None, mx=None, frac_sp=None)  1224         Fill random elements to a sparse tensor.  1229             The range of random number from, default 0.  1232             The range of random number to, default 1.  1235             The percent of non-zero elements.  1239         ctf: ctf.tensor.fill_random()  1244         >>> a = ctf.tensor([3, 3], sp=1)  1245         >>> a.fill_sp_random(frac_sp=0.2)  1247         array([[0.96985989, 0.        , 0.        ],  1248                [0.        , 0.        , 0.10310342],  1257         if self.
dtype == np.int32:
  1259         elif self.
dtype == np.int64:
  1261         elif self.
dtype == np.float32:
  1263         elif self.
dtype == np.float64:
  1266             raise ValueError(
'CTF PYTHON ERROR: bad dtype')
  1274             if self.
dtype == np.int32:
  1275                 (< Tensor[int32_t] * > self.
dt).read_sparse_from_file(path, with_vals)
  1276             elif self.
dtype == np.int64:
  1277                 (< Tensor[int64_t] * > self.
dt).read_sparse_from_file(path, with_vals)
  1278             elif self.
dtype == np.float32:
  1279                 (< Tensor[float] * > self.
dt).read_sparse_from_file(path, with_vals)
  1280             elif self.
dtype == np.float64:
  1281                 (< Tensor[double] * > self.
dt).read_sparse_from_file(path, with_vals)
  1283                 raise ValueError(
'CTF PYTHON ERROR: bad dtype')
  1285             self.dt.read_dense_from_file(path)
  1293             if self.
dtype == np.int32:
  1294                 (< Tensor[int32_t] * > self.
dt).write_sparse_to_file(path, with_vals)
  1295             elif self.
dtype == np.int64:
  1296                 (< Tensor[int64_t] * > self.
dt).write_sparse_to_file(path, with_vals)
  1297             elif self.
dtype == np.float32:
  1298                 (< Tensor[float] * > self.
dt).write_sparse_to_file(path, with_vals)
  1299             elif self.
dtype == np.float64:
  1300                 (< Tensor[double] * > self.
dt).write_sparse_to_file(path, with_vals)
  1302                 raise ValueError(
'CTF PYTHON ERROR: bad dtype')
  1304             self.dt.write_dense_to_file(path)
  1308     def _exp_python(self, tensor A, cast = None, dtype = None):
  1311             if A.dtype == np.int8:
  1312                 self.dt.exp_helper[int8_t, double](<ctensor*>A.dt)
  1313             elif A.dtype == np.int16:
  1314                 self.dt.exp_helper[int16_t, float](<ctensor*>A.dt)
  1315             elif A.dtype == np.int32:
  1316                 self.dt.exp_helper[int32_t, double](<ctensor*>A.dt)
  1317             elif A.dtype == np.int64:
  1318                 self.dt.exp_helper[int64_t, double](<ctensor*>A.dt)
  1319             elif A.dtype == np.float16:
  1320                 self.dt.exp_helper[int64_t, double](<ctensor*>A.dt)
  1321             elif A.dtype == np.float32:
  1322                 self.dt.exp_helper[float, float](<ctensor*>A.dt)
  1323             elif A.dtype == np.float64:
  1324                 self.dt.exp_helper[double, double](<ctensor*>A.dt)
  1325             elif A.dtype == np.float128:
  1326                 self.dt.exp_helper[double, double](<ctensor*>A.dt)
  1328                 raise ValueError(
"exponentiation not supported for these types")
  1335         elif cast == 
'unsafe':
  1337             if A.dtype == np.int64 
and dtype == np.float32:
  1338                 self.dt.exp_helper[int64_t, float](<ctensor*>A.dt)
  1339             elif A.dtype == np.int64 
and dtype == np.float64:
  1340                 self.dt.exp_helper[int64_t, double](<ctensor*>A.dt)
  1342                 raise ValueError(
"exponentiation not supported for these types")
  1344             raise ValueError(
"exponentiation not supported for these types")
  1348     def all(tensor self, axis=None, out=None, keepdims = None):
  1350         all(axis=None, out=None, keepdims = False)  1351         Return whether given an axis elements are True.  1355         axis: None or int, optional  1356             Currently not supported in CTF Python.  1358         out: tensor, optional  1359             Currently not supported in CTF Python.  1361         keepdims : bool, optional  1362             Currently not supported in CTF Python.  1367             Output tensor or scalar.  1376         >>> a = ctf.astensor([[0, 1], [1, 1]])  1380         if keepdims 
is None:
  1384                 if type(out) != np.ndarray:
  1385                     raise ValueError(
'CTF PYTHON ERROR: output must be an array')
  1386                 if out.shape != () 
and keepdims == 
False:
  1387                     raise ValueError(
'CTF PYTHON ERROR: output parameter has too many dimensions')
  1388                 if keepdims == 
True:
  1390                     for i 
in range(len(self.
shape)):
  1392                     dims_keep = tuple(dims_keep)
  1393                     if out.shape != dims_keep:
  1394                         raise ValueError(
'CTF PYTHON ERROR: output must match when keepdims = True')
  1395             B = 
tensor((1,), dtype=np.bool)
  1396             index_A = _get_num_str(self.
ndim)
  1397             if self.
dtype == np.float64:
  1398                 all_helper[double](<ctensor*>self.
dt, <ctensor*>B.dt, index_A.encode(), 
"".encode())
  1399             elif self.
dtype == np.int64:
  1400                 all_helper[int64_t](<ctensor*>self.
dt, <ctensor*>B.dt, index_A.encode(), 
"".encode())
  1401             elif self.
dtype == np.int32:
  1402                 all_helper[int32_t](<ctensor*>self.
dt, <ctensor*>B.dt, index_A.encode(), 
"".encode())
  1403             elif self.
dtype == np.int16:
  1404                 all_helper[int16_t](<ctensor*>self.
dt, <ctensor*>B.dt, index_A.encode(), 
"".encode())
  1405             elif self.
dtype == np.int8:
  1406                 all_helper[int8_t](<ctensor*>self.
dt, <ctensor*>B.dt, index_A.encode(), 
"".encode())
  1407             elif self.
dtype == np.bool:
  1408                 all_helper[bool](<ctensor*>self.
dt, <ctensor*>B.dt, index_A.encode(), 
"".encode())
  1410                 if out.dtype != B.get_type():
  1411                     if keepdims == 
True:
  1412                         dim_keep = np.ones(len(self.
shape),dtype=np.int64)
  1414                     C = 
tensor((1,), dtype=out.dtype)
  1417                     return vals.reshape(out.shape)
  1419                     raise ValueError(
"CTF PYTHON ERROR: invalid output dtype")
  1426             if keepdims == 
True:
  1427                 dim_keep = np.ones(len(self.
shape),dtype=np.int64)
  1435         if isinstance(axis, (int, np.integer)):
  1438             if axis >= len(dim) 
or axis < 0:
  1439                 raise ValueError(
"'axis' entry is out of bounds")
  1440             dim_ret = np.delete(dim, axis)
  1442                 if type(out) != np.ndarray:
  1443                     raise ValueError(
'CTF PYTHON ERROR: output must be an array')
  1444                 if len(dim_ret) != len(out.shape):
  1445                     raise ValueError(
'CTF PYTHON ERROR: output parameter dimensions mismatch')
  1446                 for i 
in range(len(dim_ret)):
  1447                     if dim_ret[i] != out.shape[i]:
  1448                         raise ValueError(
'CTF PYTHON ERROR: output parameter dimensions mismatch')
  1450             if keepdims == 
True:
  1454                     if tuple(dim_keep) != tuple(out.shape):
  1455                         raise ValueError(
'CTF PYTHON ERROR: output must match when keepdims = True')
  1456             index_A = _get_num_str(self.
ndim)
  1457             index_temp = _rev_array(index_A)
  1458             index_B = index_temp[0:axis] + index_temp[axis+1:len(dim)]
  1459             index_B = _rev_array(index_B)
  1460             B = 
tensor(dim_ret, dtype=np.bool)
  1461             if self.
dtype == np.float64:
  1462                 all_helper[double](<ctensor*>self.
dt, <ctensor*>B.dt, index_A.encode(), index_B.encode())
  1463             elif self.
dtype == np.int64:
  1464                 all_helper[int64_t](<ctensor*>self.
dt, <ctensor*>B.dt, index_A.encode(), index_B.encode())
  1465             elif self.
dtype == np.bool:
  1466                 all_helper[bool](<ctensor*>self.
dt, <ctensor*>B.dt, index_A.encode(), index_B.encode())
  1467             elif self.
dtype == np.int32:
  1468                 all_helper[int32_t](<ctensor*>self.
dt, <ctensor*>B.dt, index_A.encode(), index_B.encode())
  1469             elif self.
dtype == np.int16:
  1470                 all_helper[int16_t](<ctensor*>self.
dt, <ctensor*>B.dt, index_A.encode(), index_B.encode())
  1471             elif self.
dtype == np.int8:
  1472                 all_helper[int8_t](<ctensor*>self.
dt, <ctensor*>B.dt, index_A.encode(), index_B.encode())
  1474                 if out.dtype != B.get_type():
  1475                     if keepdims == 
True:
  1476                         C = 
tensor(dim_ret, dtype=out.dtype)
  1480                         C = 
tensor(dim_ret, dtype=out.dtype)
  1483             if keepdims == 
True:
  1486         elif isinstance(axis, (tuple, np.ndarray)):
  1487             axis = np.asarray(axis, dtype=np.int64)
  1489             if keepdims == 
True:
  1491                 for i 
in range(len(axis)):
  1492                     dim_keep[axis[i]] = 1
  1494                     if tuple(dim_keep) 
is not tuple(out.shape):
  1495                         raise ValueError(
'CTF PYTHON ERROR: output must match when keepdims = True')
  1496             for i 
in range(len(axis.shape)):
  1499                 if axis[i] >= len(dim) 
or axis[i] < 0:
  1500                     raise ValueError(
"'axis' entry is out of bounds")
  1501             for i 
in range(len(axis.shape)):
  1502                 if np.count_nonzero(axis==axis[i]) > 1:
  1503                     raise ValueError(
"duplicate value in 'axis'")
  1504             dim_ret = np.delete(dim, axis)
  1506                 if type(out) 
is not np.ndarray:
  1507                     raise ValueError(
'CTF PYTHON ERROR: output must be an array')
  1508                 if len(dim_ret) 
is not len(out.shape):
  1509                     raise ValueError(
'CTF PYTHON ERROR: output parameter dimensions mismatch')
  1510                 for i 
in range(len(dim_ret)):
  1511                     if dim_ret[i] 
is not out.shape[i]:
  1512                         raise ValueError(
'CTF PYTHON ERROR: output parameter dimensions mismatch')
  1513             B = 
tensor(dim_ret, dtype=np.bool)
  1514             index_A = _get_num_str(self.
ndim)
  1515             index_temp = _rev_array(index_A)
  1517             for i 
in range(len(dim)):
  1519                     index_B += index_temp[i]
  1520             index_B = _rev_array(index_B)
  1521             if self.
dtype == np.float64:
  1522                 all_helper[double](<ctensor*>self.
dt, <ctensor*>B.dt, index_A.encode(), index_B.encode())
  1523             elif self.
dtype == np.int64:
  1524                 all_helper[int64_t](<ctensor*>self.
dt, <ctensor*>B.dt, index_A.encode(), index_B.encode())
  1525             elif self.
dtype == np.int32:
  1526                 all_helper[int32_t](<ctensor*>self.
dt, <ctensor*>B.dt, index_A.encode(), index_B.encode())
  1527             elif self.
dtype == np.int16:
  1528                 all_helper[int16_t](<ctensor*>self.
dt, <ctensor*>B.dt, index_A.encode(), index_B.encode())
  1529             elif self.
dtype == np.int8:
  1530                 all_helper[int8_t](<ctensor*>self.
dt, <ctensor*>B.dt, index_A.encode(), index_B.encode())
  1531             elif self.
dtype == np.bool:
  1532                 all_helper[bool](<ctensor*>self.
dt, <ctensor*>B.dt, index_A.encode(), index_B.encode())
  1534                 if out.dtype 
is not B.get_type():
  1535                     if keepdims == 
True:
  1536                         C = 
tensor(dim_ret, dtype=out.dtype)
  1540                         C = 
tensor(dim_ret, dtype=out.dtype)
  1543             if keepdims == 
True:
  1547             raise ValueError(
"an integer is required")
  1549     def i(self, string):
  1552         Core function on summing the ctensor.  1557             Dimensions for summation.  1562             Output tensor or scalar.  1567         >>> a = ctf.astensor([[1,2,3],[4,5,6]])  1568         >>> a.i("ij") << a.i("ij")  1573         if _ord_comp(self.
order, 
'F'):
  1574             return itensor(self, _rev_array(string))
  1581         Function to print the non-zero elements and their indices of a tensor.  1586         >>> a = ctf.astensor([0,1,2,3,0])  1588         Printing tensor ZYTP01  1595     def real(self,tensor value = None):
  1598         Return real part of a tensor or set its real part to new value.  1603             The value tensor set real to the original tensor, current only support value tensor with dtype `np.float64` or `np.complex128`. Default is none.  1612         >>> a = ctf.astensor([1+2j, 3+4j])  1613         >>> b = ctf.astensor([5,6], dtype=np.float64)  1614         >>> a.real(value = b)  1616         array([5.+2.j, 6.+4.j])  1619             if self.
dtype == np.complex64:
  1621                 get_real[float](<ctensor*>self.
dt, <ctensor*>ret.dt)
  1623             elif self.
dtype == np.complex128:
  1625                 get_real[double](<ctensor*>self.
dt, <ctensor*>ret.dt)
  1630             if value.dtype != np.float64 
and value.dtype != np.complex128:
  1631                 raise ValueError(
"CTF PYTHON ERROR: current CTF Python only support value in real function has the dtype np.float64 or np.complex128")
  1632             if self.
dtype == np.complex64:
  1633                 set_real[float](<ctensor*>value.dt, <ctensor*>self.
dt)
  1634             elif self.
dtype == np.complex128:
  1635                 set_real[double](<ctensor*>value.dt, <ctensor*>self.
dt)
  1640     def imag(self,tensor value = None):
  1643         Return imaginary part of a tensor or set its imaginary part to new value.  1648             The value tensor set imaginary to the original tensor, current only support value tensor with dtype `np.float64` or `np.complex128`. Default is none.  1657         >>> a = ctf.astensor([1+2j, 3+4j])  1658         >>> b = ctf.astensor([5,6], dtype=np.float64)  1659         >>> a.imag(value = b)  1661         array([5.+2.j, 6.+4.j])  1664             if self.
dtype == np.complex64:
  1666                 get_imag[float](<ctensor*>self.
dt, <ctensor*>ret.dt)
  1668             elif self.
dtype == np.complex128:
  1670                 get_imag[double](<ctensor*>self.
dt, <ctensor*>ret.dt)
  1672             elif self.
dtype == np.float32:
  1674             elif self.
dtype == np.float64:
  1677                 raise ValueError(
"CTF ERROR: cannot call imag on non-complex/real single/double precision tensor")
  1679             if value.dtype != np.float64 
and value.dtype != np.complex128:
  1680                 raise ValueError(
"CTF PYTHON ERROR: current CTF Python only support value in imaginary function has the dtype np.float64 or np.complex128")
  1681             if self.
dtype == np.complex64:
  1682                 set_imag[float](<ctensor*>value.dt, <ctensor*>self.
dt)
  1683             elif self.
dtype == np.complex128:
  1684                 set_imag[double](<ctensor*>value.dt, <ctensor*>self.
dt)
  1686                 raise ValueError(
"CTF ERROR: cannot call imag with value on non-complex single/double precision tensor")
  1691         Copy the tensor to a new tensor.  1696             Output copied tensor.  1701         >>> a = ctf.astensor([[1,2,3],[4,5,6]])  1706         array([[ True,  True,  True],  1707                [ True,  True,  True]])  1715         Return a new tensor with reshaped shape.  1720             Output reshaped tensor.  1729         >>> a = ctf.astensor([[1,2,3],[4,5,6]])  1741         if not isinstance(integer[0], (int, np.integer)):
  1743                 raise ValueError(
"CTF PYTHON ERROR: invalid shape argument to reshape")
  1745                 integer = integer[0]
  1747         if isinstance(integer, (int, np.integer)):
  1748             newshape.append(integer)
  1749         elif isinstance(newshape, (tuple, list, np.ndarray)):
  1750             for i 
in range(len(integer)):
  1751                 newshape.append(integer[i])
  1753             raise ValueError(
"CTF PYTHON ERROR: invalid shape input to reshape")
  1754         for i 
in range(len(dim)):
  1755             total_size *= dim[i]
  1756         newshape = np.asarray(newshape, dtype=np.int64)
  1759         for i 
in range(len(newshape)):
  1763             for i 
in range(len(newshape)):
  1764                 new_size *= newshape[i]
  1765             if new_size != total_size:
  1766                 raise ValueError(
"CTF PYTHON ERROR: total size of new array must be unchanged")
  1773             for i 
in range(len(newshape)):
  1775                     new_size *= newshape[i]
  1778             nega_size = total_size / new_size
  1780                 raise ValueError(
"can not reshape into this size")
  1781             newshape[pos] = nega_size
  1787             raise ValueError(
'CTF PYTHON ERROR: can only specify one unknown dimension')
  1793         Return the flattened tensor.  1798             Output flattened tensor.  1803         >>> a = ctf.astensor([[1,2,3],[4,5,6]])  1805         array([1, 2, 3, 4, 5, 6])  1807         return ravel(self, order)
  1809     def read(self, init_inds, vals=None, a=None, b=None):
  1811         read(init_inds, vals=None, a=None, b=None)  1812         Helper function on reading a tensor.  1814         inds = np.asarray(init_inds)
  1817             mystrides = np.ones(self.
ndim,dtype=np.int32)
  1818             for i 
in range(1,self.
ndim):
  1820             inds = np.dot(inds, np.asarray(mystrides) )
  1822         if vals 
is not None:
  1823             if vals.dtype != self.
dtype:
  1824                 raise ValueError(
'CTF PYTHON ERROR: bad dtype of vals parameter to read')
  1827             gvals = np.zeros(len(inds),dtype=self.
dtype)
  1832         cdef int64_t * cinds = int64_t_arr_py_to_c(inds)
  1833         cdef char * cvals = char_arr_py_to_c(gvals.view(dtype=np.int8))
  1836         st = np.ndarray([],dtype=self.
dtype).itemsize
  1838             alpha = <char*>self.dt.sr.mulid()
  1840             alpha = <char*>malloc(st)
  1842             for j 
in range(0,st):
  1843                 alpha[j] = na.view(dtype=np.int8)[j]
  1845             beta = <char*>self.dt.sr.addid()
  1847             beta = <char*>malloc(st)
  1849             for j 
in range(0,st):
  1850                 beta[j] = nb.view(dtype=np.int8)[j]
  1851         (<ctensor*>self.
dt).
read(len(inds),<char*>alpha,<char*>beta,cinds,cvals)
  1852         for i 
in range(len(gvals.view(dtype=np.int8))):
  1853             gvals.view(dtype=np.int8)[i]=cvals[i]
  1861     def astype(self, dtype, order='F', casting='unsafe'):
  1863         astype(dtype, order='F', casting='unsafe')  1864         Copy the tensor to specified type.  1872             Bytes order for the tensor.  1874         casting: {‘no’, ‘equiv’, ‘safe’, ‘same_kind’, ‘unsafe’}, optional  1875             Control the casting. Please refer to numpy.ndarray.astype, please refer to numpy.ndarray.astype for more information.  1880             Copied tensor with specified data-type.  1884         numpy: numpy.ndarray.astype  1889         >>> a = ctf.astensor([[1,2,3], [4,5,6], [7,8,9]])  1891         <class 'numpy.int64'>  1892         >>> a.astype(np.float64).dtype  1893         <class 'numpy.float64'>  1896             return self.
astype(np.complex128, order, casting)
  1898             return self.
astype(np.float64, order, casting)
  1899         if dtype == self.
dtype:
  1901         if casting == 
'unsafe':
  1907             if str(dtype) == 
"<class 'bool'>":
  1909             if str(dtype) == 
"<class 'complex'>":
  1910                 dtype = np.complex128
  1914         elif casting == 
'safe':
  1920             if (self.
dtype != np.bool 
and dtype != np.bool) 
and self.
itemsize > dtype.itemsize:
  1921                 raise ValueError(
"Cannot cast array from dtype({0}) to dtype({1}) according to the rule 'safe'".format(self.
dtype,dtype))
  1922             if dtype == np.bool 
and self.
dtype != np.bool:
  1923                 raise ValueError(
"Cannot cast array from dtype({0}) to dtype({1}) according to the rule 'safe'".format(self.
dtype,dtype))
  1924             str_self = str(self.
dtype)
  1925             str_dtype = str(dtype)
  1926             if "float" in str_self 
and "int" in str_dtype:
  1927                 raise ValueError(
"Cannot cast array from dtype({0}) to dtype({1}) according to the rule 'safe'".format(self.
dtype,dtype))
  1928             elif "complex" in str_self 
and (
"int" in str_dtype 
or "float" in str_dtype):
  1929                 raise ValueError(
"Cannot cast array from dtype({0}) to dtype({1}) according to the rule 'safe'".format(self.
dtype,dtype))
  1933         elif casting == 
'equiv':
  1939             if self.
dtype != dtype:
  1940                 raise ValueError(
"Cannot cast array from dtype({0}) to dtype({1}) according to the rule 'safe'".format(self.
dtype,dtype))
  1941         elif casting == 
'no':
  1946             if self.
dtype != dtype:
  1947                 raise ValueError(
"Cannot cast array from dtype({0}) to dtype({1}) according to the rule 'no'".format(self.
dtype,dtype))
  1950         elif casting == 
'same_kind':
  1955             str_self = str(self.
dtype)
  1956             str_dtype = str(dtype)
  1957             if 'float' in str_self 
and 'int' in str_dtype:
  1958                 raise ValueError(
"Cannot cast array from dtype({0}) to dtype({1}) according to the rule 'same_kind'".format(self.
dtype,dtype))
  1959             if 'complex' in str_self 
and (
'int' in str_dtype 
or (
'float' in str_dtype)):
  1960                 raise ValueError(
"Cannot cast array from dtype({0}) to dtype({1}) according to the rule 'same_kind'".format(self.
dtype,dtype))
  1961             if self.
dtype != np.bool 
and dtype == np.bool:
  1962                 raise ValueError(
"Cannot cast array from dtype({0}) to dtype({1}) according to the rule 'same_kind'".format(self.
dtype,dtype))
  1964             raise ValueError(
"casting must be one of 'no', 'equiv', 'safe', 'same_kind', or 'unsafe'")
  1969         Helper function on reading a tensor.  1971         cdef int64_t * cinds
  1974         self.dt.read_local(&n,&cinds,&cdata)
  1975         inds = np.empty(n, dtype=np.int64)
  1976         vals = np.empty(n, dtype=self.
dtype)
  1977         for i 
in range(len(inds)):
  1979         for i 
in range(len(vals.view(dtype=np.int8))):
  1980             vals.view(dtype=np.int8)[i]=cdata[i]
  1989     def dot(self, other, out=None):
  1991         dot(other, out=None)  1992         Return the dot product with tensor other.  1997             The other input tensor.  2000             Currently not supported in CTF Python.  2005             Dot product of two tensors.  2015         >>> a = ctf.astensor([[1,2,3], [4,5,6], [7,8,9]])  2016         >>> b = ctf.astensor([1,1,1])  2020         return dot(self,other,out)
  2024         tensordot(other, axes=2)  2025         Return the tensor dot product of two tensors along axes.  2030             Second input tensor.  2032         axes: int or array_like  2033             Sum over which axes.  2038             Tensor dot product of two tensors.  2042         numpy: numpy.tensordot()  2047         >>> import numpy as np  2048         >>> a = np.arange(60.).reshape(3,4,5)  2049         >>> b = np.arange(24.).reshape(4,3,2)  2050         >>> a = ctf.astensor(a)  2051         >>> b = ctf.astensor(b)  2052         >>> a.tensordot(b, axes=([1,0],[0,1]))  2053         array([[4400., 4730.],  2065         Helper function on reading a tensor.  2067         cdef int64_t * cinds
  2070         self.dt.read_local_nnz(&n,&cinds,&cdata)
  2071         inds = np.empty(n, dtype=np.int64)
  2072         vals = np.empty(n, dtype=self.
dtype)
  2073         for i 
in range(len(inds)):
  2075         for i 
in range(len(vals.view(dtype=np.int8))):
  2076             vals.view(dtype=np.int8)[i]=cdata[i]
  2085     def _tot_size(self, unpack=True):
  2086         return self.dt.get_tot_size(
not unpack)
  2090         read_all(arr=None, unpack=True)  2091         Helper function on reading a tensor.  2095         sz = self.dt.get_tot_size(
not unpack)
  2096         tB = self.dtype.itemsize
  2097         cvals = <char*> malloc(sz*tB)
  2098         self.dt.allread(&sz, cvals, unpack)
  2099         cdef cnp.ndarray buf = np.empty(sz, dtype=self.
dtype)
  2102             sbuf = np.asarray(buf)
  2109         Helper function on writing a tensor.  2113         sz = self.dt.get_tot_size(
False)
  2114         tB = arr.dtype.itemsize
  2115         self.dt.get_raw_data(&cvals, &sz)
  2116         cdef cnp.ndarray buf = np.empty(sz, dtype=self.
dtype)
  2126         Return the self conjugate tensor element-wisely.  2131             The element-wise complex conjugate of the tensor. If the tensor is not complex, just return a copy.  2136         >>> a = ctf.astensor([2+3j, 3-2j])  2138         array([2.+3.j, 3.-2.j])  2140         array([2.-3.j, 3.+2.j])  2144     def permute(self, tensor A, p_A=None, p_B=None, a=None, b=None):
  2146         permute(self, tensor A, p_A=None, p_B=None, a=None, b=None)  2149         if p_A 
is None and p_B 
is None:
  2150             raise ValueError(
"CTF PYTHON ERROR: permute must be called with either p_A or p_B defined")
  2151         if p_A 
is not None and p_B 
is not None:
  2152             raise ValueError(
"CTF PYTHON ERROR: permute cannot be called with both p_A and p_B defined")
  2155         cdef int ** permutation_A = NULL
  2156         cdef int ** permutation_B = NULL
  2159             permutation_A = <int**>malloc(sizeof(int*) * A.ndim)
  2160             for i 
in range(self.
ndim):
  2162                     permutation_A[i] = <int*>malloc(sizeof(int) * A.shape[-i-1])
  2163                     for j 
in range(A.shape[-i-1]):
  2164                         permutation_A[i][j] = p_A[-i-1][j]
  2166                     permutation_A[i] = <int*>malloc(sizeof(int) * A.shape[i])
  2167                     for j 
in range(A.shape[i]):
  2168                         permutation_A[i][j] = p_A[i][j]
  2171             permutation_B = <int**>malloc(sizeof(int*) * self.
ndim)
  2172             for i 
in range(self.
ndim):
  2173                 if self.
order == 
'F':
  2174                     permutation_B[i] = <int*>malloc(sizeof(int) * self.
shape[-i-1])
  2175                     for j 
in range(self.
shape[-i-1]):
  2176                         permutation_B[i][j] = p_B[-i-1][j]
  2178                     permutation_B[i] = <int*>malloc(sizeof(int) * self.
shape[i])
  2179                     for j 
in range(self.
shape[i]):
  2180                         permutation_B[i][j] = p_B[i][j]
  2181         st = np.ndarray([],dtype=self.
dtype).itemsize
  2183             alpha = <char*>self.dt.sr.mulid()
  2185             alpha = <char*>malloc(st)
  2187             for j 
in range(0,st):
  2188                 alpha[j] = na.view(dtype=np.int8)[j]
  2190             beta = <char*>self.dt.sr.addid()
  2192             beta = <char*>malloc(st)
  2194             for j 
in range(0,st):
  2195                 beta[j] = nb.view(dtype=np.int8)[j]
  2196         self.dt.permute(<ctensor*>A.dt, <int**>permutation_A, <char*>alpha, <int**>permutation_B, <char*>beta)
  2202             for i 
in range(0, sizeof(permutation_A), sizeof(int*)):
  2203                 free(permutation_A[i])
  2206             for i 
in range(0, sizeof(permutation_B), sizeof(int*)):
  2207                 free(permutation_B[i])
  2210     def write(self, init_inds, init_vals, a=None, b=None):
  2212         write(init_inds, init_vals, a=None, b=None)  2213         Helper function on writing a tensor.  2215         inds = np.asarray(init_inds)
  2216         vals = np.asarray(init_vals, dtype=self.
dtype)
  2219             mystrides = np.ones(self.
ndim,dtype=np.int32)
  2220             for i 
in range(1,self.
ndim):
  2223             inds = np.dot(inds, np.asarray(mystrides))
  2229         if self.
dtype == np.bool:
  2234             alpha = <char*>self.dt.sr.mulid()
  2236             alpha = <char*>malloc(st)
  2238             for j 
in range(0,st):
  2239                 alpha[j] = na.view(dtype=np.int8)[j]
  2241             beta = <char*>self.dt.sr.addid()
  2243             beta = <char*>malloc(st)
  2245             for j 
in range(0,st):
  2246                 beta[j] = nb.view(dtype=np.int8)[j]
  2247         cdef int64_t * cinds = int64_t_arr_py_to_c(inds)
  2248         cdef char * cvals = char_arr_py_to_c(vals.view(dtype=np.int8))
  2249         self.dt.write(len(inds),alpha,beta,cinds,cvals)
  2256     def _get_slice(self, offsets, ends):
  2259         alpha = <char*>self.dt.sr.mulid()
  2260         beta = <char*>self.dt.sr.addid()
  2262             A = 
tensor(np.asarray(ends)-np.asarray(offsets), dtype=self.
dtype)
  2264             A = 
tensor(np.asarray(ends)-np.asarray(offsets), dtype=self.
dtype, sp=1)
  2268         if _ord_comp(self.
order, 
'F'):
  2269             clens = int_arr_py_to_c(_rev_array(A.shape))
  2270             coffs = int_arr_py_to_c(_rev_array(offsets))
  2271             cends = int_arr_py_to_c(_rev_array(ends))
  2272             czeros = int_arr_py_to_c(np.zeros(len(self.
shape), dtype=np.int32))
  2274             clens = int_arr_py_to_c(A.shape)
  2275             coffs = int_arr_py_to_c(offsets)
  2276             cends = int_arr_py_to_c(ends)
  2277             czeros = int_arr_py_to_c(np.zeros(len(self.
shape), dtype=np.int32))
  2278         A.dt.slice(czeros, clens, beta, self.
dt, coffs, cends, alpha)
  2285     def _write_slice(self, offsets, ends, init_A, A_offsets=None, A_ends=None, a=None, b=None):
  2291             alpha = <char*>self.dt.sr.mulid()
  2293             alpha = <char*>malloc(st)
  2294             na = np.array([a],dtype=self.
dtype)
  2295             for j 
in range(0,st):
  2296                 alpha[j] = na.view(dtype=np.int8)[j]
  2298             beta = <char*>self.dt.sr.addid()
  2300             beta = <char*>malloc(st)
  2302             for j 
in range(0,st):
  2303                 beta[j] = nb.view(dtype=np.int8)[j]
  2309         if _ord_comp(self.
order, 
'F'):
  2310             if A_offsets 
is None:
  2311                 caoffs = int_arr_py_to_c(_rev_array(np.zeros(len(self.
shape), dtype=np.int32)))
  2313                 caoffs = int_arr_py_to_c(_rev_array(A_offsets))
  2315                 caends = int_arr_py_to_c(_rev_array(A.shape))
  2317                 caends = int_arr_py_to_c(_rev_array(A_ends))
  2318             coffs = int_arr_py_to_c(_rev_array(offsets))
  2319             cends = int_arr_py_to_c(_rev_array(ends))
  2321             if A_offsets 
is None:
  2322                 caoffs = int_arr_py_to_c(np.zeros(len(self.
shape), dtype=np.int32))
  2324                 caoffs = int_arr_py_to_c(A_offsets)
  2326                 caends = int_arr_py_to_c(A.shape)
  2328                 caends = int_arr_py_to_c(A_ends)
  2329             coffs = int_arr_py_to_c(offsets)
  2330             cends = int_arr_py_to_c(ends)
  2333         self.dt.slice(coffs, cends, beta, (<tensor>A).dt, caoffs, caends, alpha)
  2349         [key, is_everything, is_single_val, is_contig, inds, corr_shape, one_shape] = _setgetitem_helper(self, key_init)
  2355             vals = self.
read([key])
  2359             offs = [ind[0] 
for ind 
in inds]
  2360             ends = [ind[1] 
for ind 
in inds]
  2365             for i 
in range(self.
ndim):
  2366                 pB.append(np.arange(inds[i][0],inds[i][1],inds[i][2],dtype=int))
  2371             tsr.permute(self, p_B=pB)
  2372             return tsr.reshape(corr_shape)
  2375         mystr = _get_num_str(self.
ndim)
  2376         self.
i(mystr).
scl(0.0)
  2381         Set all elements in a tensor to zero.  2386         >>> a = ctf.astensor([1,2,3])  2391         mystr = _get_num_str(self.
ndim)
  2392         self.
i(mystr).
scl(0.0)
  2397         Set all elements in a tensor to a value.  2402             Value set to a tensor.  2407         >>> a = ctf.astensor([1,2,3])  2412         val = np.asarray([value],dtype=self.
dtype)[0]
  2415         alpha = <char*>malloc(st)
  2416         na = np.array([val],dtype=self.
dtype)
  2417         for j 
in range(0,st):
  2418             alpha[j] = na.view(dtype=np.int8)[j]
  2423         value = deepcopy(value_init)
  2424         [key, is_everything, is_single_val, is_contig, inds, corr_shape, one_shape] = _setgetitem_helper(self, key_init)
  2426             self.
write([key],np.asarray(value,dtype=self.
dtype))
  2428         if isinstance(value, (np.int, np.float, np.complex, np.number)):
  2429             tval = np.asarray([value],dtype=self.
dtype)[0]
  2434             if isinstance(tval,tensor):
  2440             offs = [ind[0] 
for ind 
in inds]
  2441             ends = [ind[1] 
for ind 
in inds]
  2443             if isinstance(tval,tensor):
  2450             for i 
in range(self.
ndim):
  2451                 pA.append(np.arange(inds[i][0],inds[i][1],inds[i][2],dtype=int))
  2453             if isinstance(tval,tensor):
  2457             self.
permute(tsr.reshape(one_shape), pA)
  2459     def trace(self, offset=0, axis1=0, axis2=1, dtype=None, out=None):
  2461         trace(offset=0, axis1=0, axis2=1, dtype=None, out=None)  2462         Return the sum over the diagonal of input tensor.  2466         offset: int, optional  2467             Default is 0 which indicates the main diagonal.  2469         axis1: int, optional  2470             Default is 0 which indicates the first axis of 2-D tensor where diagonal is taken.  2472         axis2: int, optional  2473             Default is 1 which indicates the second axis of 2-D tensor where diagonal is taken.  2475         dtype: data-type, optional  2476             Numpy data-type, currently not supported in CTF Python trace().  2479             Currently not supported in CTF Python trace().  2483         output: tensor or scalar  2484             Sum along diagonal of input tensor.  2493         >>> a = ctf.astensor([[1,2,3], [4,5,6], [7,8,9]])  2497         return trace(self, offset, axis1, axis2, dtype, out)
  2501         diagonal(offset=0, axis1=0, axis2=1)  2502         Return the diagonal of the tensor if it is 2D. If the tensor is a higher order square tensor (same shape for every dimension), return diagonal of tensor determined by axis1=0, axis2=1.  2506         offset: int, optional  2507             Default is 0 which indicates the main diagonal.  2509         axis1: int, optional  2510             Default is 0 which indicates the first axis of 2-D tensor where diagonal is taken.  2512         axis2: int, optional  2513             Default is 1 which indicates the second axis of 2-D tensor where diagonal is taken.  2518             Diagonal of input tensor.  2522         `ctf.diagonal` only supports diagonal of square tensor with order more than 2.  2527         >>> a = ctf.astensor([[1,2,3], [4,5,6], [7,8,9]])  2531         return diagonal(self,offset,axis1,axis2)
  2533     def sum(self, axis = None, dtype = None, out = None, keepdims = None):
  2535         sum(axis = None, dtype = None, out = None, keepdims = None)  2536         Sum of elements in tensor or along specified axis.  2540         axis: None, int or tuple of ints  2541             Axis or axes where the sum of elements is performed.  2543         dtype: data-type, optional  2544             Data-type for the output tensor.  2546         out: tensor, optional  2547             Alternative output tensor.  2549         keepdims: None, bool, optional  2550             If set to true, axes summed over will remain size one.  2564         >>> a = ctf.ones([3,4], dtype=np.int64)  2568         return sum(self, axis, dtype, out, keepdims)
  2573         1-norm of the tensor.  2578             1-norm of the tensor.  2583         >>> a = ctf.ones([3,4], dtype=np.float64)  2587         if self.
dtype == np.float64:
  2588             return (<Tensor[double]*>self.
dt).
norm1()
  2592             raise ValueError(
'CTF PYTHON ERROR: norm not present for this dtype')
  2597         2-norm of the tensor.  2602             2-norm of the tensor.  2607         >>> a = ctf.ones([3,4], dtype=np.float64)  2611         if self.
dtype == np.float64:
  2612             return (<Tensor[double]*>self.
dt).
norm2()
  2613         elif self.
dtype == np.float32:
  2614             return (<Tensor[float]*>self.
dt).
norm2()
  2615         elif self.
dtype == np.int64:
  2616             return (<Tensor[int64_t]*>self.
dt).
norm2()
  2617         elif self.
dtype == np.int32:
  2618             return (<Tensor[int32_t]*>self.
dt).
norm2()
  2619         elif self.
dtype == np.int16:
  2620             return (<Tensor[int16_t]*>self.
dt).
norm2()
  2621         elif self.
dtype == np.int8:
  2622             return (<Tensor[int8_t]*>self.
dt).
norm2()
  2626             raise ValueError(
'CTF PYTHON ERROR: norm not present for this dtype')
  2631         Infinity norm of the tensor.  2636             Infinity norm of the tensor.  2641         >>> a = ctf.ones([3,4], dtype=np.float64)  2645         if self.
dtype == np.float64:
  2650             raise ValueError(
'CTF PYTHON ERROR: norm not present for this dtype')
  2655         Convert tensor to numpy ndarray.  2660             Numpy ndarray of the tensor.  2665         >>> a = ctf.ones([3,4], dtype=np.float64)  2666         >>> a = ctf.ones([3,4])  2668         array([[1., 1., 1., 1.],  2676         return np.reshape(vals, self.
shape)
  2685         Convert numpy ndarray to CTF tensor.  2690             CTF tensor of the numpy ndarray.  2695         >>> import numpy as np  2696         >>> a = np.asarray([1.,2.,3.])  2697         >>> b = ctf.zeros([3, ])  2698         >>> b.from_nparray(a)  2702         if arr.dtype != self.
dtype:
  2703             raise ValueError(
'CTF PYTHON ERROR: bad dtype')
  2704         if self.dt.wrld.np == 1:
  2706         elif self.dt.wrld.rank == 0:
  2709             self.
write(np.arange(0,self.
_tot_size(),dtype=np.int64),arr.ravel())
  2713     def take(self, indices, axis=None, out=None, mode='raise'):
  2715         take(indices, axis=None, out=None, mode='raise')  2716         Take elements from a tensor along axis.  2720         indices: tensor_like  2721             Indices of the values wnat to be extracted.  2724             Select values from which axis, default None.  2727             Currently not supported in CTF Python take().  2729         mode: {‘raise’, ‘wrap’, ‘clip’}, optional  2730             Currently not supported in CTF Python take().  2734         output: tensor or scalar  2735             Elements extracted from the input tensor.  2744         >>> a = ctf.astensor([[1,2,3], [4,5,6], [7,8,9]])  2745         >>> a.take([0, 1, 2])  2748         return take(self,indices,axis,out,mode)
  2751         if isinstance(b,tensor):
  2753         elif isinstance(b,np.ndarray):
  2764         Extract a sample of the entries (if sparse of the current nonzeros) by keeping each entry with probability p. Also transforms tensor into sparse format if not already.  2769             Probability that keep each entry.  2773         output: tensor or scalar  2774             Elements extracted from the input tensor.  2779         >>> a = ctf.astensor([[1,2,3], [4,5,6], [7,8,9]])  2790     def _compare_tensors(tensor self, tensor b, op):
  2793             if self.
dtype == np.float64:
  2795                 c.dt.smaller_than[double](<ctensor*>self.
dt,<ctensor*>b.dt)
  2796             elif self.
dtype == np.bool:
  2798                 c.dt.smaller_than[bool](<ctensor*>self.
dt,<ctensor*>b.dt)
  2800                 raise ValueError(
'CTF PYTHON ERROR: bad dtype')
  2804             if self.
dtype == np.float64:
  2806                 c.dt.smaller_equal_than[double](<ctensor*>self.
dt,<ctensor*>b.dt)
  2807             elif self.
dtype == np.bool:
  2809                 c.dt.smaller_equal_than[bool](<ctensor*>self.
dt,<ctensor*>b.dt)
  2811                 raise ValueError(
'CTF PYTHON ERROR: bad dtype')
  2817             for i 
in range(min(self.
ndim,b.ndim)):
  2818                 new_shape.append(self.
shape[i])
  2819                 if b.shape[i] != new_shape[i]:
  2820                     raise ValueError(
'CTF PYTHON ERROR: bad dtype')
  2821             for i 
in range(min(self.
ndim,b.ndim),max(self.
ndim,b.ndim)):
  2822                 if self.
ndim > b.ndim:
  2823                     new_shape.append(self.
shape[i])
  2825                     new_shape.append(b.shape[i])
  2827             c = 
tensor(new_shape, dtype=np.bool, sp=self.
sp)
  2828             if self.
dtype == np.float64:
  2829                 c.dt.compare_elementwise[double](<ctensor*>self.
dt,<ctensor*>b.dt)
  2830             elif self.
dtype == np.float32:
  2831                 c.dt.compare_elementwise[float](<ctensor*>self.
dt,<ctensor*>b.dt)
  2832             elif self.
dtype == np.complex64:
  2833                 c.dt.compare_elementwise[complex64_t](<ctensor*>self.
dt,<ctensor*>b.dt)
  2834             elif self.
dtype == np.complex128:
  2835                 c.dt.compare_elementwise[complex128_t](<ctensor*>self.
dt,<ctensor*>b.dt)
  2836             elif self.
dtype == np.int64:
  2837                 c.dt.compare_elementwise[int64_t](<ctensor*>self.
dt,<ctensor*>b.dt)
  2838             elif self.
dtype == np.int32:
  2839                 c.dt.compare_elementwise[int32_t](<ctensor*>self.
dt,<ctensor*>b.dt)
  2840             elif self.
dtype == np.int16:
  2841                 c.dt.compare_elementwise[int16_t](<ctensor*>self.
dt,<ctensor*>b.dt)
  2842             elif self.
dtype == np.int8:
  2843                 c.dt.compare_elementwise[int8_t](<ctensor*>self.
dt,<ctensor*>b.dt)
  2844             elif self.
dtype == np.bool:
  2845                 c.dt.compare_elementwise[bool](<ctensor*>self.
dt,<ctensor*>b.dt)
  2847                 raise ValueError(
'CTF PYTHON ERROR: bad dtype')
  2852             if self.
dtype == np.float64:
  2854                 c.dt.not_equals[double](<ctensor*>self.
dt,<ctensor*>b.dt)
  2855             elif self.
dtype == np.bool:
  2857                 c.dt.not_equals[bool](<ctensor*>self.
dt,<ctensor*>b.dt)
  2859                 raise ValueError(
'CTF PYTHON ERROR: bad dtype')
  2864             if self.
dtype == np.float64:
  2866                 print(
"shape is", c.shape)
  2867                 c.dt.larger_than[double](<ctensor*>self.
dt,<ctensor*>b.dt)
  2868             elif self.
dtype == np.bool:
  2870                 print(
"shape is", c.shape)
  2871                 c.dt.larger_than[bool](<ctensor*>self.
dt,<ctensor*>b.dt)
  2873                 raise ValueError(
'CTF PYTHON ERROR: bad dtype')
  2878             if self.
dtype == np.float64:
  2880                 c.dt.larger_equal_than[double](<ctensor*>self.
dt,<ctensor*>b.dt)
  2881             elif self.
dtype == np.bool:
  2883                 c.dt.larger_equal_than[bool](<ctensor*>self.
dt,<ctensor*>b.dt)
  2885                 raise ValueError(
'CTF PYTHON ERROR: bad dtype')
  2905 def _trilSquare(tensor A):
  2906     if not isinstance(A, tensor):
  2907         raise ValueError(
'CTF PYTHON ERROR: A is not a tensor')
  2909         raise ValueError(
'CTF PYTHON ERROR: A is not a matrix')
  2910     if A.shape[0] != A.shape[1]:
  2911         raise ValueError(
'CTF PYTHON ERROR: A is not a square matrix')
  2916     csym = int_arr_py_to_c(np.zeros([2], dtype=np.int32))
  2917     csym2 = int_arr_py_to_c(np.asarray([2,0], dtype=np.int32))
  2920     ct = new ctensor(A.dt, csym2)
  2921     B.dt = new ctensor(ct, csym)
  2928     Return lower triangle of a CTF tensor.  2936         Specify last diagonal not zeroed. Default `k=0` which indicates elements under the main diagonal are zeroed.  2941         Lower triangular 2-d tensor of input tensor.  2946     >>> a = ctf.astensor([[1,2,3],[4,5,6],[7,8,9]])  2947     >>> ctf.tril(a, k=1)  2953     if not isinstance(A, tensor):
  2954         raise ValueError(
'CTF PYTHON ERROR: A is not a tensor')
  2956         raise ValueError(
'CTF PYTHON ERROR: A is not a matrix')
  2960     if A.shape[0] != A.shape[1] 
or k != 0:
  2961         B = A[ max(0, k) : min(k+A.shape[1],A.shape[0]), max(0, -k) : min(A.shape[1], A.shape[0] - k)]
  2963         A[ max(0, k) : min(k+A.shape[1],A.shape[0]), max(0, -k) : min(A.shape[1], A.shape[0] - k)] = C
  2971     Return upper triangle of a CTF tensor.  2979         Specify last diagonal not zeroed. Default `k=0` which indicates elements under the main diagonal are zeroed.  2984         Upper triangular 2-d tensor of input tensor.  2989     >>> a = ctf.astensor([[1,2,3],[4,5,6],[7,8,9]])  2990     >>> ctf.triu(a, k=-1)  3000     Return the real part of the tensor elementwisely.  3010         A tensor with real part of the input tensor.  3014     numpy : numpy.real()  3018     The input should be a CTF tensor.  3023     >>> a = ctf.astensor([1+2j, 3+4j, 5+6j, 7+8j])  3025     array([1.+2.j, 3.+4.j, 5.+6.j, 7.+8.j])  3027     array([1., 3., 5., 7.])  3029     if not isinstance(A, tensor):
  3030         raise ValueError(
'CTF PYTHON ERROR: A is not a tensor')
  3031     if A.get_type() != np.complex64 
and A.get_type() != np.complex128 
and A.get_type() != np.complex256:
  3034         ret = 
tensor(A.shape, dtype = np.float64)
  3035         get_real[double](<ctensor*>A.dt, <ctensor*>ret.dt)
  3041     Return the image part of the tensor elementwisely.  3051         A tensor with real part of the input tensor.  3055     numpy : numpy.imag()  3059     The input should be a CTF tensor.  3064     >>> a = ctf.astensor([1+2j, 3+4j, 5+6j, 7+8j])  3066     array([1.+2.j, 3.+4.j, 5.+6.j, 7.+8.j])  3068     array([2., 4., 6., 8.])  3070     if not isinstance(A, tensor):
  3071         raise ValueError(
'CTF PYTHON ERROR: A is not a tensor')
  3072     if A.get_type() != np.complex64 
and A.get_type() != np.complex128 
and A.get_type() != np.complex256:
  3073         return zeros(A.shape, dtype=A.get_type())
  3075         ret = 
tensor(A.shape, dtype = np.float64)
  3076         get_imag[double](<ctensor*>A.dt, <ctensor*>ret.dt)
  3079 def array(A, dtype=None, copy=True, order='K', subok=False, ndmin=0):
  3081     array(A, dtype=None, copy=True, order='K', subok=False, ndmin=0)  3087         Input tensor like object.  3089     dtype: data-type, optional  3090         The desired data-type for the tensor. If the dtype is not specified, the dtype will be determined as `np.array()`.  3092     copy: bool, optional  3093         If copy is true, the object is copied.  3095     order: {‘K’, ‘A’, ‘C’, ‘F’}, optional  3096         Specify the memory layout for the tensor.  3098     subok: bool, optional  3099         Currently subok is not supported in `ctf.array()`.  3101     ndmin: int, optional  3102         Currently ndmin is not supported in `ctf.array()`.  3107         A tensor object with specified requirements.  3111     ctf : ctf.astensor()  3115     The input of ctf.array() should be tensor or numpy.ndarray  3120     >>> import numpy as np  3121     >>> a = np.array([1, 2, 3.])  3123     >>> b = ctf.array(a)  3127         raise ValueError(
'CTF PYTHON ERROR: ndmin not supported in ctf.array()')
  3130     if _ord_comp(order, 
'K') 
or _ord_comp(order, 
'A'):
  3132             B = 
astensor(A,dtype=dtype,order=
'F')
  3134             B = 
astensor(A,dtype=dtype,order=
'C')
  3136         B = 
astensor(A,dtype=dtype,order=order)
  3143     diag(A, k=0, sp=False)  3144     Return the diagonal tensor of A.  3149         Input tensor with 1-D or 2-D dimensions. If A is 1-D tensor, return a 2-D tensor with A on diagonal.  3152         `k=0` is the diagonal. `k<0`, diagnals below the main diagonal. `k>0`, diagonals above the main diagonal.  3155         If sp is true, the returned tensor is sparse.  3160         Diagonal tensor of A.  3164     When the input tensor is sparse, returned tensor will also be sparse.  3168     ctf : ctf.diagonal()  3177     >>> a = ctf.ones([3,])  3178     >>> ctf.diag(a, k=1)  3179     array([[0., 1., 0., 0.],  3182     >>> b = ctf.zeros([4,4])  3184     array([0., 0., 0., 0.])  3186     if not isinstance(A, tensor):
  3187         raise ValueError(
'CTF PYTHON ERROR: A is not a tensor')
  3191         raise ValueError(
'CTF PYTHON ERROR: diag requires an array of at least 1 dimension')
  3193         B = 
tensor((A.shape[0],A.shape[0]),dtype=A.dtype,sp=sp)
  3194         B.i(
"ii") << A.i(
"i")
  3197             B2 = 
tensor((A.shape[0],A.shape[0]+absk),dtype=A.dtype,sp=sp)
  3201             B2 = 
tensor((A.shape[0]+absk,A.shape[0]),dtype=A.dtype,sp=sp)
  3207     if k < 0 
and dim[0] + k <=0:
  3209     if k > 0 
and dim[1] - k <=0:
  3213             if dim[0] == dim[1]:
  3214                 up_left = np.zeros([2])
  3216                 down_right = np.array([dim[0], dim[1]])
  3219                 up_left = np.zeros([2])
  3220                 m = min(dim[0], dim[1])
  3221                 down_right = np.array([m, m])
  3224                 if down_right[0] > dim[1]:
  3225                     down_right[1] -= (down_right[0] - dim[1])
  3226                     down_right[0] = dim[1]
  3227             return einsum(
"ii->i",A._get_slice(up_left, down_right))
  3229             if dim[0] == dim[1]:
  3230                 up_left = np.zeros([2])
  3232                 down_right = np.array([dim[0], dim[1]])
  3235                 up_left = np.zeros([2])
  3236                 m = min(dim[0], dim[1])
  3237                 down_right = np.array([m, m])
  3240                 if down_right[1] > dim[0]:
  3241                     down_right[0] -= (down_right[1] - dim[0])
  3242                     down_right[1] = dim[0]
  3243             return einsum(
"ii->i",A._get_slice(up_left, down_right))
  3247         for i 
in range(1,len(dim)):
  3248             if dim[0] != dim[i]:
  3252             back = _get_num_str(len(dim)-1)
  3253             front = back[len(back)-1]+back[len(back)-1]+back[0:len(back)-1]
  3254             einsum_input = front + 
"->" + back
  3255             return einsum(einsum_input,A)
  3261     Return the sparse diagonal tensor of A.  3266         Input tensor with 1-D or 2-D dimensions. If A is 1-D tensor, return a 2-D tensor with A on diagonal.  3269         `k=0` is the diagonal. `k<0`, diagnals below the main diagonal. `k>0`, diagonals above the main diagonal.  3274         Sparse diagonal tensor of A.  3278     Same with ctf.diag(A,k,sp=True)  3283     >>> a = ctf.astensor([[1,2,3], [4,5,6], [7,8,9]])  3287     return diag(A,k,sp=
True)
  3291     diagonal(A, offset=0, axis1=0, axis2=1)  3292     Return the diagonal of tensor A if A is 2D. If A is a higher order square tensor (same shape for every dimension), return diagonal of tensor determined by axis1=0, axis2=1.  3299     offset: int, optional  3300         Default is 0 which indicates the main diagonal.  3302     axis1: int, optional  3303         Default is 0 which indicates the first axis of 2-D tensor where diagonal is taken.  3305     axis2: int, optional  3306         Default is 1 which indicates the second axis of 2-D tensor where diagonal is taken.  3311         Diagonal of input tensor.  3315     `ctf.diagonal` only supports diagonal of square tensor with order more than 2.  3320     >>> a = ctf.astensor([[1,2,3], [4,5,6], [7,8,9]])  3326         raise ValueError(
'CTF PYTHON ERROR: axis1 and axis2 cannot be the same')
  3328     if len(dim) == 1 
or len(dim)==0:
  3329         raise ValueError(
'CTF PYTHON ERROR: diagonal requires an array of at least two dimensions')
  3330     if axis1 ==1 
and axis2 == 0:
  3332     if offset < 0 
and dim[0] + offset <=0:
  3334     if offset > 0 
and dim[1] - offset <=0:
  3338             if dim[0] == dim[1]:
  3339                 up_left = np.zeros([2], dtype=np.int)
  3340                 up_left[0] += offset
  3341                 down_right = np.array([dim[0], dim[1]], dtype=np.int)
  3342                 down_right[1] -= offset
  3344                 up_left = np.zeros([2], dtype=np.int)
  3345                 m = min(dim[0], dim[1])
  3346                 down_right = np.array([m, m], dtype=np.int)
  3347                 up_left[0] += offset
  3348                 down_right[0] += offset
  3349                 if down_right[0] > dim[1]:
  3350                     down_right[1] -= (down_right[0] - dim[1])
  3351                     down_right[0] = dim[1]
  3352             return einsum(
"ii->i",A._get_slice(up_left, down_right))
  3354             if dim[0] == dim[1]:
  3355                 up_left = np.zeros([2], dtype=np.int)
  3356                 up_left[1] -= offset
  3357                 down_right = np.array([dim[0], dim[1]], dtype=np.int)
  3358                 down_right[0] += offset
  3360                 up_left = np.zeros([2], dtype=np.int)
  3361                 m = min(dim[0], dim[1])
  3362                 down_right = np.array([m, m], dtype=np.int)
  3363                 up_left[1] -= offset
  3364                 down_right[1] -= offset
  3365                 if down_right[1] > dim[0]:
  3366                     down_right[0] -= (down_right[1] - dim[0])
  3367                     down_right[1] = dim[0]
  3368             return einsum(
"ii->i",A._get_slice(up_left, down_right))
  3372         for i 
in range(1,len(dim)):
  3373             if dim[0] != dim[i]:
  3377             back = _get_num_str(len(dim)-1)
  3378             front = back[len(back)-1]+back[len(back)-1]+back[0:len(back)-1]
  3379             einsum_input = front + 
"->" + back
  3380             return einsum(einsum_input,A)
  3382             raise ValueError(
'CTF PYTHON ERROR: diagonal requires a higher order (>2) tensor to be square')
  3383     raise ValueError(
'CTF PYTHON ERROR: diagonal error')
  3385 def trace(init_A, offset=0, axis1=0, axis2=1, dtype=None, out=None):
  3387     trace(A, offset=0, axis1=0, axis2=1, dtype=None, out=None)  3388     Return the sum over the diagonal of input tensor.  3395     offset: int, optional  3396         Default is 0 which indicates the main diagonal.  3398     axis1: int, optional  3399         Default is 0 which indicates the first axis of 2-D tensor where diagonal is taken.  3401     axis2: int, optional  3402         Default is 1 which indicates the second axis of 2-D tensor where diagonal is taken.  3404     dtype: data-type, optional  3405         Numpy data-type, currently not supported in CTF Python trace().  3408         Currently not supported in CTF Python trace().  3412     output: tensor or scalar  3413         Sum along diagonal of input tensor.  3418     >>> a = ctf.astensor([[1,2,3], [4,5,6], [7,8,9]])  3422     if dtype != 
None or out != 
None:
  3423         raise ValueError(
'CTF PYTHON ERROR: CTF Python trace currently does not support dtype and out')
  3426     if len(dim) == 1 
or len(dim)==0:
  3427         raise ValueError(
'CTF PYTHON ERROR: diag requires an array of at least two dimensions')
  3429         return sum(
diagonal(A, offset=offset, axis1 = axis1, axis2 = axis2))
  3432         return sum(
diagonal(A, offset=offset, axis1 = axis1, axis2 = axis2), axis=len(A.shape)-2)
  3435 def take(init_A, indices, axis=None, out=None, mode='raise'):
  3437     take(A, indices, axis=None, out=None, mode='raise')  3438     Take elements from a tensor along axis.  3445     indices: tensor_like  3446         Indices of the values wnat to be extracted.  3449         Select values from which axis, default None.  3452         Currently not supported in CTF Python take().  3454     mode: {‘raise’, ‘wrap’, ‘clip’}, optional  3455         Currently not supported in CTF Python take().  3459     output: tensor or scalar  3460         Elements extracted from the input tensor.  3469     >>> a = ctf.astensor([[1,2,3], [4,5,6], [7,8,9]])  3470     >>> ctf.take(a, [0, 1, 2])  3474         raise ValueError(
"CTF Python Now ctf does not support to specify 'out' in functions")
  3476     indices = np.asarray(indices)
  3480         if indices.shape == ():
  3481             indices = indices.reshape(1,)
  3483                 indices[0] += A.shape[0]
  3484             if indices[0] > 0 
and indices[0] > A.shape[0]:
  3485                 error = 
"index "+str(indices[0])+
" is out of bounds for size " + str(A.shape[0])
  3486                 error = 
"CTF PYTHON ERROR: " + error
  3487                 raise IndexError(error)
  3489                 error = 
"index "+str(indices[0]-A.shape[0])+
" is out of bounds for size " + str(A.shape[0])
  3490                 error = 
"CTF PYTHON ERROR: " + error
  3491                 raise IndexError(error)
  3492             return A.read(indices)[0]
  3496             for i 
in range(len(A.shape)):
  3497                 total_size *= A.shape[i]
  3498             indices_ravel = np.ravel(indices)
  3499             for i 
in range(len(indices_ravel)):
  3500                 if indices_ravel[i] < 0:
  3501                     indices_ravel[i] += total_size
  3502                     if indices_ravel[i] < 0:
  3503                         error = 
"index "+str(indices_ravel[i]-total_size)+
" is out of bounds for size " + str(total_size)
  3504                         error = 
"CTF PYTHON ERROR: " + error
  3505                         raise IndexError(error)
  3506                 if indices_ravel[i] > 0 
and indices_ravel[0] > total_size:
  3507                     error = 
"index "+str(indices_ravel[i])+
" is out of bounds for size " + str(total_size)
  3508                     error = 
"CTF PYTHON ERROR: " + error
  3509                     raise IndexError(error)
  3510             if len(indices.shape) == 1:
  3511                 B = 
astensor(A.read(indices_ravel))
  3516         if type(axis) != int:
  3517             raise TypeError(
"CTF PYTHON ERROR: the axis should be int type")
  3519             axis += len(A.shape)
  3521                 raise IndexError(
"CTF PYTHON ERROR: axis out of bounds")
  3522         if axis > len(A.shape):
  3523             raise IndexError(
"CTF PYTHON ERROR: axis out of bounds")
  3524         if indices.shape == () 
or indices.shape== (1,):
  3526             for i 
in range(len(A.shape)):
  3528             if indices >= A.shape[axis]:
  3529                 raise IndexError(
"CTF PYTHON ERROR: index out of bounds")
  3530             ret_shape = list(A.shape)
  3531             if indices.shape == ():
  3536             for i 
in range(axis+1, len(A.shape),1):
  3538             next_slot = A.shape[axis] * begin
  3539             start = indices * begin
  3541             for i 
in range(0, axis):
  3542                 arange_times *= A.shape[i]
  3543             a = np.arange(start,start+begin)
  3545             for i 
in range(1,arange_times,1):
  3546                 a = np.concatenate((a, np.arange(start,start+begin)))
  3549             return B.to_nparray()
  3551             if len(indices.shape) > 1:
  3552                 raise ValueError(
"CTF PYTHON ERROR: current ctf does not support when specify axis and the len(indices.shape) > 1")
  3554             for i 
in range(len(A.shape)):
  3556             for i 
in range(len(indices)):
  3557                 if indices[i] >= A.shape[axis]:
  3558                     raise IndexError(
"index out of bounds")
  3559             ret_shape = list(A.shape)
  3561             ret_shape[axis] = len(indices)
  3562             begin = np.ones(indices.shape)
  3563             for i 
in range(axis+1, len(A.shape),1):
  3565             next_slot = A.shape[axis] * begin
  3566             start = indices * begin
  3568             for i 
in range(0, axis):
  3569                 arange_times *= A.shape[i]
  3570             a = np.arange(start[0],start[0]+begin[0])
  3571             start[0] += next_slot[0]
  3572             for i 
in range(1,len(indices),1):
  3573                 a = np.concatenate((a, np.arange(start[i],start[i]+begin[i])))
  3574                 start[i] += next_slot[i]
  3575             for i 
in range(1,arange_times,1):
  3576                 for j 
in range(len(indices)):
  3577                     a = np.concatenate((a, np.arange(start[j],start[j]+begin[j])))
  3578                     start[j] += next_slot[j]
  3581     raise ValueError(
'CTF PYTHON ERROR: CTF error: should not get here')
  3586     Return a copy of tensor A.  3596         A tensor representation of A.  3600     >>> a = ctf.astensor([1,2,3])  3609     B = 
tensor(A.shape, dtype=A.get_type(), copy=A)
  3614     reshape(A, newshape, order='F')  3615     Reshape the input tensor A to new shape.  3622     newshape: tuple of ints or int  3623         New shape where the input tensor is shaped to.  3625     order: {‘C’, ‘F’}, optional  3626         Currently not supported by CTF Python.  3631         Tensor with new shape of A.  3635     ctf: ctf.tensor.reshape()  3640     a = ctf.astensor([1,2,3,4])  3641     >>> ctf.reshape(a, (2, 2))  3645     if A.order != order:
  3646       raise ValueError(
'CTF PYTHON ERROR: CTF does not support reshape with a new element order (Fortran vs C)')
  3647     return A.reshape(newshape)
  3652     astensor(A, dtype = None, order=None)  3653     Convert the input data to tensor.  3660     dtype: data-type, optional  3661         Numpy data-type, if it is not specified, the function will return the tensor with same type as `np.asarray` returned ndarray.  3663     order: {‘C’, ‘F’}, optional  3664         C or Fortran memory order, default is 'F'.  3669         A tensor representation of A.  3673     numpy: numpy.asarray()  3678     >>> a = ctf.astensor([1,2,3])  3682     if isinstance(A,tensor):
  3683         if order 
is not None and order != A.order:
  3684             raise ValueError(
'CTF PYTHON ERROR: CTF does not support this type of order conversion in astensor()')
  3685         if dtype 
is not None and dtype != A.dtype:
  3686             return tensor(copy=A, dtype=dtype)
  3691         narr = np.asarray(A,dtype=dtype,order=order)
  3693         narr = np.asarray(A,order=order)
  3694     t = 
tensor(narr.shape, dtype=narr.dtype)
  3695     t.from_nparray(narr)
  3701     Return the dot product of two tensors A and B.  3709         Second input tensor.  3712         Currently not supported in CTF Python.  3717         Dot product of two tensors.  3726     >>> a = ctf.astensor([[1,2,3], [4,5,6], [7,8,9]])  3727     >>> b = ctf.astensor([1,1,1])  3732         raise ValueError(
"CTF PYTHON ERROR: CTF currently does not support output parameter.")
  3734     if (isinstance(tA, (np.int, np.float, np.complex, np.number)) 
and  3735         isinstance(tB, (np.int, np.float, np.complex, np.number))):
  3745     tensordot(A, B, axes=2)  3746     Return the tensor dot product of two tensors A and B along axes.  3754         Second input tensor.  3756     axes: int or array_like  3757         Sum over which axes.  3762         Tensor dot product of two tensors.  3766     numpy: numpy.tensordot()  3771     >>> import numpy as np  3772     >>> a = np.arange(60.).reshape(3,4,5)  3773     >>> b = np.arange(24.).reshape(4,3,2)  3774     >>> a = ctf.astensor(a)  3775     >>> b = ctf.astensor(b)  3776     >>> c = ctf.tensordot(a,b, axes=([1,0],[0,1]))  3778     array([[4400., 4730.],  3787     if isinstance(axes, (int, np.integer)):
  3788         if axes > len(A.shape) 
or axes > len(B.shape):
  3789             raise ValueError(
"tuple index out of range")
  3790         for i 
in range(axes):
  3791             if A.shape[len(A.shape)-1-i] != B.shape[axes-1-i]:
  3792                 raise ValueError(
"shape-mismatch for sum")
  3793         new_shape = A.shape[0:len(A.shape)-axes] + B.shape[axes:len(B.shape)]
  3796         new_dtype = _get_np_dtype([A.dtype, B.dtype])
  3799             ret_shape = A.shape + B.shape
  3800             C = 
tensor(ret_shape, dtype = new_dtype)
  3805             if A.dtype != new_dtype:
  3806                 A_new = A.astype(dtype = new_dtype)
  3807             if B.dtype != new_dtype:
  3808                 B_new = A.astype(dtype = new_dtype)
  3814             for i 
in range(len(A.shape)):
  3815                 A_str += chr(string_index)
  3817             for i 
in range(len(B.shape)):
  3818                 B_str += chr(string_index)
  3820             C_str = A_str + B_str
  3821             if A_new 
is not None and B_new 
is not None:
  3822                 C.i(C_str) << A_new.i(A_str) * B_new.i(B_str)
  3823             elif A_new 
is not None:
  3824                 C.i(C_str) << A_new.i(A_str) * B.i(B_str)
  3826                 C.i(C_str) << A.i(A_str) * B.i(B_str)
  3834         for i 
in range(axes):
  3835             A_str += chr(string_index)
  3836             B_str += chr(string_index)
  3840         for i 
in range(len(A.shape)-axes):
  3841             A_str = chr(string_index) + A_str
  3842             C_str = chr(string_index) + C_str
  3846         for i 
in range(len(B.shape)-axes):
  3847             B_str += chr(string_index)
  3848             C_str += chr(string_index)
  3851         if A.dtype == new_dtype 
and B.dtype == new_dtype:
  3852             C = 
tensor(new_shape, dtype = new_dtype)
  3853             C.i(C_str) << A.i(A_str) * B.i(B_str)
  3856             C = 
tensor(new_shape, dtype = new_dtype)
  3861             C.i(C_str) << A.i(A_str) * B_new.i(B_str)
  3862             if A.dtype != new_dtype:
  3863                 A_new = A.astype(dtype = new_dtype)
  3864             if B.dtype != new_dtype:
  3865                 B_new = A.astype(dtype = new_dtype)
  3867             if A_new 
is not None and B_new 
is not None:
  3868                 C.i(C_str) << A_new.i(A_str) * B_new.i(B_str)
  3869             elif A_new 
is not None:
  3870                 C.i(C_str) << A_new.i(A_str) * B.i(B_str)
  3872                 C.i(C_str) << A.i(A_str) * B_new.i(B_str)
  3875         axes_arr = np.asarray(axes)
  3876         if len(axes_arr.shape) != 2 
or axes_arr.shape[0] != 2:
  3877             raise ValueError(
"axes should be int or (2,) array like")
  3878         if len(axes_arr[0]) != len(axes_arr[1]):
  3879             raise ValueError(
"two sequences should have same length")
  3880         for i 
in range(len(axes_arr[0])):
  3881             if axes_arr[0][i] < 0:
  3882                 axes_arr[0][i] += len(A.shape)
  3883                 if axes_arr[0][i] < 0:
  3884                     raise ValueError(
"index out of range")
  3885             if axes_arr[1][i] < 0:
  3886                 axes_arr[1][i] += len(B.shape)
  3887                 if axes_arr[1][i] < 0:
  3888                     raise ValueError(
"index out of range")
  3890         for i 
in range(len(axes_arr[0])):
  3891             if axes[0].count(axes_arr[0][i]) > 1:
  3892                 raise ValueError(
"repeated index")
  3893             if axes[1].count(axes_arr[1][i]) > 1:
  3894                 raise ValueError(
"repeated index")
  3895         for i 
in range(len(axes_arr[0])):
  3896             if A.shape[axes_arr[0][i]] != B.shape[axes_arr[1][i]]:
  3897                 raise ValueError(
"shape mismatch")
  3898         new_dtype = _get_np_dtype([A.dtype, B.dtype])
  3907         for i 
in range(len(A.shape)):
  3908             A_str += chr(string_index)
  3911         for i 
in range(len(B.shape)):
  3912             B_str += chr(string_index)
  3915         for i 
in range(len(axes_arr[1])):
  3916             B_str[axes_arr[1][i]] = A_str[axes_arr[0][i]]
  3917         B_str = 
"".join(B_str)
  3918         for i 
in range(len(A_str)):
  3919             if i 
not in axes_arr[0]:
  3921                 new_shape += (A.shape[i],)
  3922         for i 
in range(len(B_str)):
  3923             if i 
not in axes_arr[1]:
  3925                 new_shape += (B.shape[i],)
  3927         if A.dtype == new_dtype 
and B.dtype == new_dtype:
  3928             C = 
tensor(new_shape, dtype = new_dtype)
  3929             C.i(C_str) << A.i(A_str) * B.i(B_str)
  3932             C = 
tensor(new_shape, dtype = new_dtype)
  3937             if A.dtype != new_dtype:
  3938                 A_new = A.astype(dtype = new_dtype)
  3939             if B.dtype != new_dtype:
  3940                 B_new = B.astype(dtype = new_dtype)
  3942             if A_new 
is not None and B_new 
is not None:
  3943                 C.i(C_str) << A_new.i(A_str) * B_new.i(B_str)
  3944             elif A_new 
is not None:
  3945                 C.i(C_str) << A_new.i(A_str) * B.i(B_str)
  3947                 C.i(C_str) << A.i(A_str) * B_new.i(B_str)
  3954 def exp(init_x, out=None, where=True, casting='same_kind', order='F', dtype=None, subok=True):
  3956     exp(A, out=None, where=True, casting='same_kind', order='F', dtype=None, subok=True)  3957     Exponential of all elements in input tensor A.  3962         Input tensor or tensor like array.  3964     out: tensor, optional  3965         Crrently not support by CTF Python.  3967     where: array_like, optional  3968         Crrently not support by CTF Python.  3970     casting: same_kind or unsafe  3974         Crrently not support by CTF Python.  3976     dtype: data-type, optional  3977         Output data-type for the exp result.  3980         Crrently not support by CTF Python.  3985         Output tensor for the exponential.  3994     >>> a = ctf.astensor([1,2,3])  3996     array([ 2.71828183,  7.3890561 , 20.08553692])  4002         raise ValueError(
"CTF PYTHON ERROR: current not support to specify out")
  4004     if out 
is not None and out.shape != x.shape:
  4005         raise ValueError(
"Shape does not match")
  4006     if casting == 
'same_kind' and (out 
is not None or dtype 
is not None):
  4007         if out 
is not None and dtype 
is not None:
  4008             raise TypeError(
"CTF PYTHON ERROR: out and dtype should not be specified together")
  4009         type_list = [np.int8, np.int16, np.int32, np.int64]
  4011             if out 
is not None and out.dtype == type_list[i]:
  4012                 raise TypeError(
"CTF PYTHON ERROR: Can not cast according to the casting rule 'same_kind'")
  4013             if dtype 
is not None and dtype == type_list[i]:
  4014                 raise TypeError(
"CTF PYTHON ERROR: Can not cast according to the casting rule 'same_kind'")
  4017     if casting == 
'unsafe':
  4019         if out 
is not None and dtype 
is not None:
  4020             raise TypeError(
"CTF PYTHON ERROR: out and dtype should not be specified together")
  4022     if dtype 
is not None:
  4024     elif out 
is not None:
  4025         ret_dtype = out.dtype
  4029         if x_dtype == np.int8:
  4030             ret_dtype = np.float16
  4031         elif x_dtype == np.int16:
  4032             ret_dtype = np.float32
  4033         elif x_dtype == np.int32:
  4034             ret_dtype = np.float64
  4035         elif x_dtype == np.int64:
  4036             ret_dtype = np.float64
  4037         elif x_dtype == np.float16 
or x_dtype == np.float32 
or x_dtype == np.float64 
or x_dtype == np.float128:
  4039         elif x_dtype == np.complex64 
or x_dtype == np.complex128 
or x_dtype == np.complex256:
  4041     if casting == 
"unsafe":
  4042         ret = 
tensor(x.shape, dtype = ret_dtype, sp=x.sp)
  4043         ret._exp_python(x, cast = 
'unsafe', dtype = ret_dtype)
  4046         ret = 
tensor(x.shape, dtype = ret_dtype, sp=x.sp)
  4053     Convert the tensor to numpy array.  4058         Input tensor or tensor like array.  4063         Numpy ndarray representation of tensor like input A.  4067     numpy: numpy.asarray()  4072     >>> import numpy as np  4073     >>> a = ctf.zeros([3,4])  4074     >>> b = ctf.to_nparray(a)  4076     array([[0., 0., 0., 0.],  4080     <class 'numpy.ndarray'>  4082     if isinstance(t,tensor):
  4083         return t.to_nparray()
  4085         return np.asarray(t)
  4090     Convert the numpy array to tensor.  4100         Tensor representation of input numpy array.  4109     >>> import numpy as np  4110     >>> a = np.array([1,2,3])  4111     >>> b = ctf.from_nparray(a)  4115     <class 'ctf.core.tensor'>  4121     zeros_like(A, dtype=None, order='F')  4122     Return the tensor of zeros with same shape and dtype of tensor A.  4127         Input tensor where the output tensor shape and dtype defined as.  4129     dtype: data-type, optional  4130         Output data-type for the empty tensor.  4132     order: {‘C’, ‘F’}, optional, default: ‘F’  4133         Currently not support by CTF Python.  4143     >>> import numpy as np  4144     >>> a = ctf.zeros([3,4], dtype=np.int64)  4145     >>> b = ctf.zeros_like(a)  4147     array([[0, 0, 0, 0],  4154         dtype = A.get_type()
  4155     return zeros(shape, dtype, order)
  4157 def zeros(shape, dtype=np.float64, order='F'):
  4159     zeros(shape, dtype=np.float64, order='F')  4160     Return the tensor with specified shape and dtype with all elements filled as zeros.  4164     shape: int or tuple of int  4165         Shape of the empty tensor.  4167     dtype: data-type, optional  4168         Output data-type for the empty tensor.  4170     order: {‘C’, ‘F’}, optional, default: ‘F’  4171         Currently not support by CTF Python.  4181     >>> import numpy as np  4182     >>> a = ctf.zeros([3,4], dtype=np.int64)  4184     array([[0, 0, 0, 0],  4188     A = 
tensor(shape, dtype=dtype)
  4191 def empty(shape, dtype=np.float64, order='F'):
  4193     empty(shape, dtype=np.float64, order='F')  4194     Return the tensor with specified shape and dtype without initialization. Currently not supported by CTF Python, this function same with the ctf.zeros().  4198     shape: int or tuple of int  4199         Shape of the empty tensor.  4201     dtype: data-type, optional  4202         Output data-type for the empty tensor.  4204     order: {‘C’, ‘F’}, optional, default: ‘F’  4205         Currently not support by CTF Python.  4215     >>> import numpy as np  4216     >>> a = ctf.empty([3,4], dtype=np.int64)  4218     array([[0, 0, 0, 0],  4222     return zeros(shape, dtype, order)
  4226     empty_like(A, dtype=None)  4227     Return uninitialized tensor of with same shape and dtype of tensor A. Currently in CTF Python is same with ctf.zero_like.  4232         Input tensor where the output tensor shape and dtype defined as.  4234     dtype: data-type, optional  4235         Output data-type for the empty tensor.  4244     ctf: ctf.zeros_like()  4249     >>> a = ctf.zeros([3,4], dtype=np.int64)  4250     >>> b = ctf.empty_like(a)  4252     array([[0, 0, 0, 0],  4258     return empty(A.shape, dtype=dtype)
  4261 def sum(tensor init_A, axis = None, dtype = None, out = None, keepdims = None):
  4263     sum(A, axis = None, dtype = None, out = None, keepdims = None)  4264     Sum of elements in tensor or along specified axis.  4271     axis: None, int or tuple of ints  4272         Axis or axes where the sum of elements is performed.  4274     dtype: data-type, optional  4275         Data-type for the output tensor.  4277     out: tensor, optional  4278         Alternative output tensor.  4280     keepdims: None, bool, optional  4281         If set to true, axes summed over will remain size one.  4295     >>> a = ctf.ones([3,4], dtype=np.int64)  4300     if not isinstance(out,tensor) 
and out 
is not None:
  4301         raise ValueError(
"CTF PYTHON ERROR: output must be a tensor")
  4305         dtype = A.get_type()
  4308     if keepdims 
is None :
  4312     if isinstance(out,tensor) 
and axis 
is None:
  4313         raise ValueError(
"CTF PYTHON ERROR: output parameter for reduction operation add has too many dimensions")
  4320     if isinstance(axis, (int, np.integer)):
  4321         if axis 
is not None and (axis >= len(dim) 
or axis <= (-len(dim)-1)):
  4322             raise ValueError(
"CTF PYTHON ERROR: 'axis' entry is out of bounds")
  4327         axis = np.asarray(axis, dtype=np.int64)
  4328         if len(axis.shape) > 1:
  4329             raise ValueError(
"CTF PYTHON ERROR: the object cannot be interpreted as integer")
  4330         for i 
in range(len(axis)):
  4331             if axis[i] >= len(dim) 
or axis[i] <= (-len(dim)-1):
  4332                 raise ValueError(
"CTF PYTHON ERROR: 'axis' entry is out of bounds")
  4333         for i 
in range(len(axis)):
  4336             if axis[i] 
in axis_tuple:
  4337                 raise ValueError(
"CTF PYTHON ERROR: duplicate value in 'axis'")
  4338             axis_tuple += (axis[i],)
  4341     if isinstance(out,tensor):
  4342         outputdim = out.shape
  4343         outputdim = np.ndarray.tolist(outputdim)
  4344         outputdim = tuple(outputdim)
  4349         index_A = _get_num_str(len(dim))
  4356         ret = 
tensor([], dtype = A.dtype)
  4357         ret.i(
"") << A.i(index_A)
  4358         if keepdims == 
True:
  4359             return ret.reshape(np.ones(tensor.shape))
  4361             return ret.read_all()[0]
  4364     if isinstance(axis, (int, np.integer)):
  4368         for i 
in range(len(dim)):
  4372                 ret_dim = list(ret_dim)
  4373                 ret_dim.insert(i+1,dim[i])
  4374                 ret_dim = tuple(ret_dim)
  4377         B = 
tensor(ret_dim, dtype = dtype)
  4379         if dtype != A.get_type():
  4380             C = 
tensor(A.shape, dtype = dtype)
  4381         if isinstance(out,tensor):
  4382             if(outputdim != ret_dim):
  4383                 raise ValueError(
"dimension of output mismatch")
  4385                 if keepdims == 
True:
  4386                     raise ValueError(
"Must match the dimension when keepdims = True")
  4388                     B = 
tensor(ret_dim, dtype = out.get_type())
  4389                     C = 
tensor(A.shape, dtype = out.get_type())
  4391         index = _get_num_str(len(dim))
  4392         index_A = index[0:len(dim)]
  4393         index_B = index[0:axis] + index[axis+1:len(dim)]
  4394         if isinstance(C, tensor):
  4396             B.i(index_B) << C.i(index_A)
  4399             B.i(index_B) << A.i(index_A)
  4404     if dtype != A.get_type():
  4405         C = 
tensor(A.shape, dtype = dtype)
  4406     if isinstance(out,tensor):
  4407         if keepdims == 
True:
  4408             raise ValueError(
"Must match the dimension when keepdims = True")
  4410             dtype = out.get_type()
  4411             C = 
tensor(A.shape, dtype = out.get_type())
  4412     if isinstance(C, tensor):
  4417     decrease_dim = list(dim)
  4418     axis_list = list(axis_tuple)
  4420     for i 
in range(len(axis)-1,-1,-1):
  4421         index_removal = axis_list[i]
  4422         temp_dim = list(decrease_dim)
  4423         del temp_dim[index_removal]
  4424         ret_dim = tuple(temp_dim)
  4425         B = 
tensor(ret_dim, dtype = dtype)
  4426         index = _get_num_str(len(decrease_dim))
  4427         index_A = index[0:len(decrease_dim)]
  4428         index_B = index[0:axis_list[i]] + index[axis_list[i]+1:len(decrease_dim)]
  4429         B.i(index_B) << temp.i(index_A)
  4431         del decrease_dim[index_removal]
  4437     Return flattened CTF tensor of input tensor A.  4444     order: {‘C’,’F’, ‘A’, ‘K’}, optional  4445         Currently not support by current CTF Python.  4455     >>> a = ctf.astensor([1,2,3,4,5,6,7,8]).reshape(2,2,2)  4462     array([1, 2, 3, 4, 5, 6, 7, 8])  4466     if _ord_comp(order, A.order):
  4467         return A.reshape(-1)
  4471 def any(tensor init_A, axis=None, out=None, keepdims=None):
  4473     any(A, axis=None, out=None, keepdims = False)  4474     Return whether given an axis any elements are True.  4481     axis: None or int, optional  4482         Axis along which logical OR is applied.  4484     out: tensor_like, optional  4485         Objects which will place the result.  4487     keepdims: bool, optional  4488         If keepdims is set to True, the reduced axis will remain 1 in shape.  4493         Output tensor or scalar.  4498     >>> a = ctf.astensor([[0, 0], [1, 1]])  4501     >>> ctf.any(a, axis=0)  4502     array([ True,  True])  4503     >>> ctf.any(a, axis=1)  4504     array([False,  True])  4508     if keepdims 
is None:
  4512         if out 
is not None and type(out) != np.ndarray:
  4513             raise ValueError(
'CTF PYTHON ERROR: output must be an array')
  4514         if out 
is not None and out.shape != () 
and keepdims == 
False:
  4515             raise ValueError(
'CTF PYTHON ERROR: output parameter has too many dimensions')
  4516         if keepdims == 
True:
  4518             for i 
in range(len(A.shape)):
  4520             dims_keep = tuple(dims_keep)
  4521             if out 
is not None and out.shape != dims_keep:
  4522                 raise ValueError(
'CTF PYTHON ERROR: output must match when keepdims = True')
  4523         B = 
tensor((1,), dtype=np.bool)
  4524         index_A = _get_num_str(len(A.shape))
  4525         if A.get_type() == np.float64:
  4526             any_helper[double](<ctensor*>A.dt, <ctensor*>B.dt, index_A.encode(), 
"".encode())
  4527         elif A.get_type() == np.int64:
  4528             any_helper[int64_t](<ctensor*>A.dt, <ctensor*>B.dt, index_A.encode(), 
"".encode())
  4529         elif A.get_type() == np.int32:
  4530             any_helper[int32_t](<ctensor*>A.dt, <ctensor*>B.dt, index_A.encode(), 
"".encode())
  4531         elif A.get_type() == np.int16:
  4532             any_helper[int16_t](<ctensor*>A.dt, <ctensor*>B.dt, index_A.encode(), 
"".encode())
  4533         elif A.get_type() == np.int8:
  4534             any_helper[int8_t](<ctensor*>A.dt, <ctensor*>B.dt, index_A.encode(), 
"".encode())
  4535         elif A.get_type() == np.bool:
  4536             any_helper[bool](<ctensor*>A.dt, <ctensor*>B.dt, index_A.encode(), 
"".encode())
  4537         if out 
is not None and out.get_type() != np.bool:
  4538             C = 
tensor((1,), dtype=out.dtype)
  4542         elif out 
is not None and keepdims == 
True and out.get_type() != np.bool:
  4543             C = 
tensor(dims_keep, dtype=out.dtype)
  4546         elif out 
is None and keepdims == 
True:
  4549         elif out 
is not None and keepdims == 
True and out.get_type() == np.bool:
  4558     if isinstance(axis, (int, np.integer)):
  4561         if axis >= len(dim) 
or axis < 0:
  4562             raise ValueError(
"'axis' entry is out of bounds")
  4563         dim_ret = np.delete(dim, axis)
  4565             if type(out) != np.ndarray:
  4566                 raise ValueError(
'CTF PYTHON ERROR: output must be an array')
  4567             if len(dim_ret) != len(out.shape):
  4568                 raise ValueError(
'CTF PYTHON ERROR: output parameter dimensions mismatch')
  4569             for i 
in range(len(dim_ret)):
  4570                 if dim_ret[i] != out.shape[i]:
  4571                     raise ValueError(
'CTF PYTHON ERROR: output parameter dimensions mismatch')
  4573         if keepdims == 
True:
  4577                 if tuple(dim_keep) != tuple(out.shape):
  4578                     raise ValueError(
'CTF PYTHON ERROR: output must match when keepdims = True')
  4579         index_A = _get_num_str(len(dim))
  4580         index_temp = _rev_array(index_A)
  4581         index_B = index_temp[0:axis] + index_temp[axis+1:len(dim)]
  4582         index_B = _rev_array(index_B)
  4583         B = 
tensor(dim_ret, dtype=np.bool)
  4584         if A.get_type() == np.float64:
  4585             any_helper[double](<ctensor*>A.dt, <ctensor*>B.dt, index_A.encode(), index_B.encode())
  4586         elif A.get_type() == np.int64:
  4587             any_helper[int64_t](<ctensor*>A.dt, <ctensor*>B.dt, index_A.encode(), index_B.encode())
  4588         elif A.get_type() == np.int32:
  4589             any_helper[int32_t](<ctensor*>A.dt, <ctensor*>B.dt, index_A.encode(), index_B.encode())
  4590         elif A.get_type() == np.int16:
  4591             any_helper[int16_t](<ctensor*>A.dt, <ctensor*>B.dt, index_A.encode(), index_B.encode())
  4592         elif A.get_type() == np.int8:
  4593             any_helper[int8_t](<ctensor*>A.dt, <ctensor*>B.dt, index_A.encode(), index_B.encode())
  4594         elif A.get_type() == np.bool:
  4595             any_helper[bool](<ctensor*>A.dt, <ctensor*>B.dt, index_A.encode(), index_B.encode())
  4597             if out.dtype != B.get_type():
  4598                 if keepdims == 
True:
  4599                     C = 
tensor(dim_ret, dtype=out.dtype)
  4603                     C = 
tensor(dim_ret, dtype=out.dtype)
  4606         if keepdims == 
True:
  4609     elif isinstance(axis, (tuple, np.ndarray)):
  4610         axis = np.asarray(axis, dtype=np.int64)
  4612         if keepdims == 
True:
  4614             for i 
in range(len(axis)):
  4615                 dim_keep[axis[i]] = 1
  4617                 if tuple(dim_keep) != tuple(out.shape):
  4618                     raise ValueError(
'CTF PYTHON ERROR: output must match when keepdims = True')
  4619         for i 
in range(len(axis.shape)):
  4622             if axis[i] >= len(dim) 
or axis[i] < 0:
  4623                 raise ValueError(
"'axis' entry is out of bounds")
  4624         for i 
in range(len(axis.shape)):
  4625             if np.count_nonzero(axis==axis[i]) > 1:
  4626                 raise ValueError(
"duplicate value in 'axis'")
  4627         dim_ret = np.delete(dim, axis)
  4629             if type(out) != np.ndarray:
  4630                 raise ValueError(
'CTF PYTHON ERROR: output must be an array')
  4631             if len(dim_ret) != len(out.shape):
  4632                 raise ValueError(
'CTF PYTHON ERROR: output parameter dimensions mismatch')
  4633             for i 
in range(len(dim_ret)):
  4634                 if dim_ret[i] != out.shape[i]:
  4635                     raise ValueError(
'CTF PYTHON ERROR: output parameter dimensions mismatch')
  4636         B = 
tensor(dim_ret, dtype=np.bool)
  4637         index_A = _get_num_str(len(dim))
  4638         index_temp = _rev_array(index_A)
  4640         for i 
in range(len(dim)):
  4642                 index_B += index_temp[i]
  4643         index_B = _rev_array(index_B)
  4644         if A.get_type() == np.float64:
  4645             any_helper[double](<ctensor*>A.dt, <ctensor*>B.dt, index_A.encode(), index_B.encode())
  4646         elif A.get_type() == np.int64:
  4647             any_helper[int64_t](<ctensor*>A.dt, <ctensor*>B.dt, index_A.encode(), index_B.encode())
  4648         elif A.get_type() == np.int32:
  4649             any_helper[int32_t](<ctensor*>A.dt, <ctensor*>B.dt, index_A.encode(), index_B.encode())
  4650         elif A.get_type() == np.int16:
  4651             any_helper[int16_t](<ctensor*>A.dt, <ctensor*>B.dt, index_A.encode(), index_B.encode())
  4652         elif A.get_type() == np.int8:
  4653             any_helper[int8_t](<ctensor*>A.dt, <ctensor*>B.dt, index_A.encode(), index_B.encode())
  4654         elif A.get_type() == np.bool:
  4655             any_helper[bool](<ctensor*>A.dt, <ctensor*>B.dt, index_A.encode(), index_B.encode())
  4657             if out.dtype != B.get_type():
  4658                 if keepdims == 
True:
  4659                     C = 
tensor(dim_ret, dtype=out.dtype)
  4663                     C = 
tensor(dim_ret, dtype=out.dtype)
  4666         if keepdims == 
True:
  4670         raise ValueError(
"an integer is required")
  4673 def _stackdim(in_tup, dim):
  4674     if type(in_tup) != tuple:
  4675         raise ValueError(
'CTF PYTHON ERROR: The type of input should be tuple')
  4678     for i 
in range(len(in_tup)):
  4680         if ttup[i].ndim == 0:
  4681             ttup[i] = ttup[i].
reshape([1])
  4682         max_dim = max(max_dim,ttup[i].ndim)
  4683     new_dtype = _get_np_dtype([t.dtype 
for t 
in ttup])
  4685     for i 
in range(len(ttup)):
  4686         tup.append(
astensor(ttup[i],dtype=new_dtype))
  4690             for i 
in range(len(ttup)):
  4691                 tup[i] = tup[i].
reshape([1,tup[i].shape[0]])
  4694     out_shape = np.asarray(tup[0].shape)
  4695     out_shape[dim] = np.sum([t.shape[dim] 
for t 
in tup])
  4696     out = 
tensor(out_shape, dtype=new_dtype)
  4698     for i 
in range(len(tup)):
  4700             out[acc_len:acc_len+tup[i].shape[dim],...] = tup[i]
  4702             out[:,acc_len:acc_len+tup[i].shape[dim],...] = tup[i]
  4704             raise ValueError(
'CTF PYTHON ERROR: ctf.stackdim currently only supports dim={0,1}, although this is easily fixed')
  4705         acc_len += tup[i].shape[dim]
  4712     Stack the tensor in column-wise.  4716     in_tup: tuple of tensors  4722         Output horizontally stacked tensor.  4727     >>> a = ctf.astensor([1,2,3])  4728     >>> b = ctf.astensor([4,5,6])  4729     >>> ctf.hstack((a, b))  4730     array([1, 2, 3, 4, 5, 6])  4732     return _stackdim(in_tup, 1)
  4737     Stack the tensor in row-wise.  4741     in_tup: tuple of tensors  4747         Output vertically stacked tensor.  4752     >>> a = ctf.astensor([1,2,3])  4753     >>> b = ctf.astensor([4,5,6])  4754     >>> ctf.vstack((a, b))  4758     return _stackdim(in_tup, 0)
  4763     Return the conjugate tensor A element-wisely.  4773         The element-wise complex conjugate of input tensor A. If tensor A is not complex, just return a copy of A.  4778     >>> a = ctf.astensor([2+3j, 3-2j])  4779     array([2.+3.j, 3.-2.j])  4781     array([2.-3.j, 3.+2.j])  4784     if A.get_type() == np.complex64:
  4785         B = 
tensor(A.shape, dtype=A.get_type())
  4786         conj_helper[float](<ctensor*> A.dt, <ctensor*> B.dt);
  4788     elif A.get_type() == np.complex128:
  4789         B = 
tensor(A.shape, dtype=A.get_type())
  4790         conj_helper[double](<ctensor*> A.dt, <ctensor*> B.dt);
  4795 def all(inA, axis=None, out=None, keepdims = False):
  4797     all(A, axis=None, out=None, keepdims = False)  4798     Return whether given an axis elements are True.  4805     axis: None or int, optional  4806         Currently not supported in CTF Python.  4808     out: tensor, optional  4809         Currently not supported in CTF Python.  4811     keepdims : bool, optional  4812         Currently not supported in CTF Python.  4817         Output tensor or scalar.  4822     >>> a = ctf.astensor([[0, 1], [1, 1]])  4826     if isinstance(inA, tensor):
  4827         return _comp_all(inA, axis, out, keepdims)
  4829         if isinstance(inA, np.ndarray):
  4830             return np.all(inA,axis,out,keepdims)
  4831         if isinstance(inA, np.bool):
  4834             raise ValueError(
'CTF PYTHON ERROR: ctf.all called on invalid operand')
  4837 def _comp_all(tensor A, axis=None, out=None, keepdims=None):
  4838     if keepdims 
is None:
  4840     if axis 
is not None:
  4841         raise ValueError(
"'axis' not supported for all yet")
  4843         raise ValueError(
"'out' not supported for all yet")
  4845         raise ValueError(
"'keepdims' not supported for all yet")
  4848         return x == A._tot_size()
  4852     transpose(A, axes=None)  4853     Permute the dimensions of the input tensor.  4860     axes: list of ints, optional  4861         If axes is None, the dimensions are inversed, otherwise permute the dimensions according to the axes value.  4866         Tensor with permuted axes of A.  4871     >>> a = ctf.zeros([3,4,5])  4874     >>> ctf.transpose(a, axes=[0, 2, 1]).shape  4876     >>> ctf.transpose(a).shape  4884         for i 
in range(len(dim)-1, -1, -1):
  4885             new_dim.append(dim[i])
  4886         new_dim = tuple(new_dim)
  4887         B = 
tensor(new_dim, dtype=A.get_type())
  4888         index = _get_num_str(len(dim))
  4889         rev_index = str(index[::-1])
  4890         B.i(rev_index) << A.i(index)
  4894     if len(axes) != len(dim):
  4895         raise ValueError(
"axes don't match tensor")
  4896     axes = np.asarray(axes,dtype=np.int)
  4897     for i 
in range(A.ndim):
  4899             axes[i] = A.ndim+axes[i]
  4901                 raise ValueError(
"axes too negative for CTF transpose")
  4903     axes_list = list(axes)
  4904     for i 
in range(len(axes)):
  4910         if axes_list[i] < 0:
  4911             axes_list[i] += len(dim)
  4912     for i 
in range(len(axes)):
  4914         if axes_list[i] >= len(dim) 
or axes_list[i] < 0:
  4915             raise ValueError(
"invalid axis for this tensor")
  4917         if axes_list.count(axes_list[i]) > 1:
  4918             raise ValueError(
"repeated axis in transpose")
  4920     index = _get_num_str(len(dim))
  4922     rev_dims = np.asarray(dim)
  4923     for i 
in range(len(dim)):
  4924         rev_index += index[axes_list[i]]
  4925         rev_dims[i] = dim[axes_list[i]]
  4926     B = 
tensor(rev_dims, dtype=A.get_type())
  4927     B.i(rev_index) << A.i(index)
  4930 def ones(shape, dtype = None, order='F'):
  4932     ones(shape, dtype = None, order='F')  4933     Return a tensor filled with ones with specified shape and dtype.  4937     shape: int or sequence of ints  4938         Shape of the returned tensor.  4940     dtype: numpy data-type, optional  4941         The data-type for the tensor.  4943     order: {‘C’, ‘F’}, optional  4944         Not support by current CTF Python.  4949         Tensor with specified shape and dtype.  4954     >>> a = ctf.ones([2, 2])  4959     if isinstance(shape,int):
  4961     shape = np.asarray(shape)
  4962     if dtype 
is not None:
  4963         ret = 
tensor(shape, dtype = dtype)
  4966         for i 
in range(len(shape)):
  4967             string += chr(string_index)
  4969         if dtype == np.float64:
  4970             ret.i(string) << 1.0
  4971         elif dtype == np.complex128:
  4972             ret.i(string) << 1.0
  4973         elif dtype == np.int64:
  4975         elif dtype == np.bool:
  4979         ret = 
tensor(shape, dtype = np.float64)
  4982         for i 
in range(len(shape)):
  4983             string += chr(string_index)
  4985         ret.i(string) << 1.0
  4988 def eye(n, m=None, k=0, dtype=np.float64, sp=False):
  4990     eye(n, m=None, k=0, dtype=np.float64, sp=False)  4991     Return a 2D tensor with ones on the diagonal and zeros elsewhere.  4999         Number of columns, default set to n.  5002         Diagonal index, specify ones on main diagonal, upper diagonal or lower diagonal.  5004     dtype: data-type, optional  5005         Numpy data-type of returned tensor, default `np.float64`.  5008         If `true` the returned tensor will be sparse, default `sp=False`.  5018     >>> e = ctf.eye(3,m=4,k=-1)  5020     array([[0., 0., 0., 0.],  5033     A = 
tensor([l, l], dtype=dtype, sp=sp)
  5034     if dtype == np.float64 
or dtype == np.complex128 
or dtype == np.complex64 
or dtype == np.float32:
  5036     elif dtype == np.bool 
or dtype == np.int64 
or dtype == np.int32 
or dtype == np.int16 
or dtype == np.int8:
  5039         raise ValueError(
'CTF PYTHON ERROR: bad dtype')
  5043         B = 
tensor([n, m], dtype=dtype, sp=sp)
  5045             B._write_slice([0, k], [l, l+k], A)
  5047             B._write_slice([-k, 0], [l-k, l], A)
  5052     identity(n, dtype=np.float64)  5053     Return a squared 2-D tensor where the main diagonal contains ones and elsewhere zeros.  5060     dtype: data-type, optional  5061         Numpy data-type of returned tensor, default `np.float64`.  5074     >>> a = ctf.identity(3)  5076     array([[1., 0., 0.],  5080     return eye(n, dtype=dtype)
  5082 def speye(n, m=None, k=0, dtype=np.float64):
  5084     speye(n, m=None, k=0, dtype=np.float64)  5085     Return a sparse 2D tensor with ones on the diagonal and zeros elsewhere.  5093         Number of columns, default set to n.  5096         Diagonal index, specify ones on main diagonal, upper diagonal or lower diagonal.  5098     dtype: data-type, optional  5099         Numpy data-type of returned tensor, default `np.float64`.  5112     >>> e = ctf.speye(3,m=4,k=-1)  5114     array([[0., 0., 0., 0.],  5119     return eye(n, m, k, dtype, sp=
True)
  5121 def einsum(subscripts, *operands, out=None, dtype=None, order='K', casting='safe'):
  5123     einsum(subscripts, *operands, out=None, dtype=None, order='K', casting='safe')  5124     Einstein summation on operands.  5129         Subscripts for summation.  5131     operands: list of tensor  5135         If the out is not None, calculated result will stored into out tensor.  5137     dtype: data-type, optional  5138         Numpy data-type of returned tensor, dtype of returned tensor will be specified by operand tensors.  5140     order: {‘C’, ‘F’, ‘A’, ‘K’}, optional  5141         Currently not supported by CTF Python.  5143     casting: {‘no’, ‘equiv’, ‘safe’, ‘same_kind’, ‘unsafe’}, optional  5144         Currently not supported by CTF Python.  5152     numpy : numpy.einsum()  5157     >>> a = ctf.astensor([[1,2,3], [4,5,6], [7,8,9]])  5158     >>> ctf.einsum("ii->i", a)  5161     if order != 
'K' or casting != 
'safe':
  5162         raise ValueError(
'CTF PYTHON ERROR: CTF Python einsum currently does not support order and casting')
  5163     numop = len(operands)
  5169     for i 
in range(numop):
  5171         while j < len(subscripts) 
and subscripts[j] != 
',' and subscripts[j] != 
' ' and subscripts[j] != 
'-':
  5172             if dind_lens.has_key(subscripts[j]):
  5173                 uniq_subs.discard(subscripts[j])
  5175                 uniq_subs.add(subscripts[j])
  5176             if operands[i].ndim <= len(inds[i]):
  5177                 raise ValueError(
"CTF PYTHON ERROR: einsum subscripts string contains too many subscripts for operand {0}".format(i))
  5178             dind_lens[subscripts[j]] = operands[i].shape[len(inds[i])]
  5179             inds[i] += subscripts[j]
  5180             all_inds.append(subscripts[j])
  5183         while j < len(subscripts) 
and subscripts[j] == 
' ':
  5188     if j < len(subscripts) 
and subscripts[j] == 
'-':
  5190     if j < len(subscripts) 
and subscripts[j] == 
'>':
  5194     while j < len(subscripts) 
and subscripts[j] == 
' ':
  5196     while j < len(subscripts) 
and subscripts[j] != 
' ':
  5197         out_inds += subscripts[j]
  5198         out_lens.append(dind_lens[subscripts[j]])
  5201         for ind 
in all_inds:
  5202             if ind 
in uniq_subs:
  5204                 out_lens.append(dind_lens[ind])
  5205                 uniq_subs.remove(ind)
  5207         out_dtype = _get_np_dtype([x.dtype 
for x 
in operands])
  5208         output = 
tensor(out_lens, dtype=out_dtype)
  5212         output.i(out_inds) << operands[0].i(inds[0])
  5214         output.i(out_inds) << operands[0].i(inds[0])*operands[1].i(inds[1])
  5216         output.i(out_inds) << operands[0].i(inds[0])*operands[1].i(inds[1])*operands[2].i(inds[2])
  5218         output.i(out_inds) << operands[0].i(inds[0])*operands[1].i(inds[1])*operands[2].i(inds[2])*operands[3].i(inds[3])
  5220         output.i(out_inds) << operands[0].i(inds[0])*operands[1].i(inds[1])*operands[2].i(inds[2])*operands[3].i(inds[3])*operands[4].i(inds[4])
  5222         output.i(out_inds) << operands[0].i(inds[0])*operands[1].i(inds[1])*operands[2].i(inds[2])*operands[3].i(inds[3])*operands[4].i(inds[4])*operands[5].i(inds[5])
  5224         output.i(out_inds) << operands[0].i(inds[0])*operands[1].i(inds[1])*operands[2].i(inds[2])*operands[3].i(inds[3])*operands[4].i(inds[4])*operands[5].i(inds[5])*operands[6].i(inds[6])
  5226         output.i(out_inds) << operands[0].i(inds[0])*operands[1].i(inds[1])*operands[2].i(inds[2])*operands[3].i(inds[3])*operands[4].i(inds[4])*operands[5].i(inds[5])*operands[6].i(inds[6])*operands[7].i(inds[7])
  5228         output.i(out_inds) << operands[0].i(inds[0])*operands[1].i(inds[1])*operands[2].i(inds[2])*operands[3].i(inds[3])*operands[4].i(inds[4])*operands[5].i(inds[5])*operands[6].i(inds[6])*operands[7].i(inds[7])*operands[8].i(inds[8])
  5230         output.i(out_inds) << operands[0].i(inds[0])*operands[1].i(inds[1])*operands[2].i(inds[2])*operands[3].i(inds[3])*operands[4].i(inds[4])*operands[5].i(inds[5])*operands[6].i(inds[6])*operands[7].i(inds[7])*operands[8].i(inds[8])*operands[9].i(inds[9])
  5232         raise ValueError(
'CTF PYTHON ERROR: CTF einsum currently allows no more than 10 operands')
  5238     Compute Single Value Decomposition of tensor A.  5243         Input tensor 2-D dimensions.  5245     rank: int or None, optional  5246         Target rank for SVD, default `k=0`.  5251         A unitary CTF tensor with 2-D dimensions.  5254         A 1-D tensor with singular values.  5257         A unitary CTF tensor with 2-D dimensions.  5259     if not isinstance(A,tensor) 
or A.ndim != 2:
  5260         raise ValueError(
'CTF PYTHON ERROR: SVD called on invalid tensor, must be CTF double matrix')
  5263         k = min(A.shape[0],A.shape[1])
  5266     S = 
tensor(k,dtype=A.dtype)
  5267     U = 
tensor([A.shape[0],k],dtype=A.dtype)
  5268     VT = 
tensor([k,A.shape[1]],dtype=A.dtype)
  5269     if A.dtype == np.float64 
or A.dtype == np.float32:
  5271     elif A.dtype == np.complex128 
or A.dtype == np.complex64:
  5278     Compute QR factorization of tensor A.  5283         Input tensor 2-D dimensions.  5288         A CTF tensor with 2-D dimensions and orthonormal columns.  5291         An upper triangular 2-D CTF tensor.  5293     if not isinstance(A,tensor) 
or A.ndim != 2:
  5294         raise ValueError(
'CTF PYTHON ERROR: QR called on invalid tensor, must be CTF double matrix')
  5296     Q = 
tensor(B.shape,dtype=B.dtype)
  5297     R = 
tensor([B.shape[0],B.shape[0]],dtype=B.dtype)
  5298     if A.dtype == np.float64 
or A.dtype == np.float32:
  5300     elif A.dtype == np.complex128 
or A.dtype == np.complex64:
  5302     return [Q.T(), R.T()]
  5307     Return norm of tensor A.  5312         Input tensor with 1-D or 2-D dimensions. If A is 1-D tensor, return a 2-D tensor with A on diagonal.  5314     ord: {int 1, 2, inf}, optional  5325     >>> import ctf.linalg as la  5326     >>> a = ctf.astensor([3,4.])  5335         return A.norm_infty()
  5337         raise ValueError(
'CTF PYTHON ERROR: CTF only supports 1/2/inf vector norms')
  5339 def _match_tensor_types(first, other):
  5340     if isinstance(first, tensor):
  5344     if isinstance(other, tensor):
  5348     out_dtype = _get_np_dtype([tsr.dtype, otsr.dtype])
  5349     if tsr.dtype != out_dtype:
  5350         tsr = 
tensor(copy=tsr, dtype = out_dtype)
  5351     if otsr.dtype != out_dtype:
  5352         otsr = 
tensor(copy=otsr, dtype = out_dtype)
  5355 def _div(first, other):
  5356     if isinstance(first, tensor):
  5360     if isinstance(other, tensor):
  5364     out_dtype = _get_np_div_dtype(tsr.dtype, otsr.dtype)
  5365     if tsr.dtype != out_dtype:
  5366         tsr = 
tensor(copy=tsr, dtype = out_dtype)
  5367     if otsr.dtype != out_dtype:
  5368         otsr = 
tensor(copy=otsr, dtype = out_dtype)
  5370     [idx_A, idx_B, idx_C, out_tsr] = tsr._ufunc_interpret(otsr)
  5373         otsr = 
tensor(copy=other)
  5375     otsr._invert_elements()
  5377     out_tsr.i(idx_C) << tsr.i(idx_A)*otsr.i(idx_B)
  5380 def _tensor_pow_helper(tensor tsr, tensor otsr, tensor out_tsr, idx_A, idx_B, idx_C):
  5381     if _ord_comp(tsr.order, 
'F'):
  5382         idx_A = _rev_array(idx_A)
  5383     if _ord_comp(otsr.order, 
'F'):
  5384         idx_B = _rev_array(idx_B)
  5385     if _ord_comp(out_tsr.order, 
'F'):
  5386         idx_C = _rev_array(idx_C)
  5387     if out_tsr.dtype == np.float64:
  5388         pow_helper[double](<ctensor*>tsr.dt, <ctensor*>otsr.dt, <ctensor*>out_tsr.dt, idx_A.encode(), idx_B.encode(), idx_C.encode())
  5389     elif out_tsr.dtype == np.float32:
  5390         pow_helper[float](<ctensor*>tsr.dt, <ctensor*>otsr.dt, <ctensor*>out_tsr.dt, idx_A.encode(), idx_B.encode(), idx_C.encode())
  5391     elif out_tsr.dtype == np.complex64:
  5392         pow_helper[complex64_t](<ctensor*>tsr.dt, <ctensor*>otsr.dt, <ctensor*>out_tsr.dt, idx_A.encode(), idx_B.encode(), idx_C.encode())
  5393     elif out_tsr.dtype == np.complex128:
  5394         pow_helper[complex128_t](<ctensor*>tsr.dt, <ctensor*>otsr.dt, <ctensor*>out_tsr.dt, idx_A.encode(), idx_B.encode(), idx_C.encode())
  5395     elif out_tsr.dtype == np.int64:
  5396         pow_helper[int64_t](<ctensor*>tsr.dt, <ctensor*>otsr.dt, <ctensor*>out_tsr.dt, idx_A.encode(), idx_B.encode(), idx_C.encode())
  5397     elif out_tsr.dtype == np.int32:
  5398         pow_helper[int32_t](<ctensor*>tsr.dt, <ctensor*>otsr.dt, <ctensor*>out_tsr.dt, idx_A.encode(), idx_B.encode(), idx_C.encode())
  5399     elif out_tsr.dtype == np.int16:
  5400         pow_helper[int16_t](<ctensor*>tsr.dt, <ctensor*>otsr.dt, <ctensor*>out_tsr.dt, idx_A.encode(), idx_B.encode(), idx_C.encode())
  5401     elif out_tsr.dtype == np.int8:
  5402         pow_helper[int8_t](<ctensor*>tsr.dt, <ctensor*>otsr.dt, <ctensor*>out_tsr.dt, idx_A.encode(), idx_B.encode(), idx_C.encode())
  5407     Elementwisely raise tensor A to powers from the tensor B.  5420         The output tensor containing elementwise bases A raise to exponents of B.  5425     >>> a = ctf.astensor([2., 3])  5427     >>> b = ctf.astensor([2., 3])  5432     [tsr, otsr] = _match_tensor_types(first,second)
  5434     [idx_A, idx_B, idx_C, out_tsr] = tsr._ufunc_interpret(otsr)
  5436     _tensor_pow_helper(tsr, otsr, out_tsr, idx_A, idx_B, idx_C)
  5443     Calculate the elementwise absolute value of a tensor.  5453         A tensor containing the absolute value of each element in input tensor. For complex number :math:`a + bi`, the absolute value is calculated as :math:`\sqrt{a^2 + b^2}`  5461     >>> a = ctf.astensor([-2, 3])  5468     cdef tensor oA = 
tensor(copy=A)
  5469     if A.dtype == np.float64:
  5470         abs_helper[double](<ctensor*>A.dt, <ctensor*>oA.dt)
  5471     elif A.dtype == np.float32:
  5472         abs_helper[float](<ctensor*>A.dt, <ctensor*>oA.dt)
  5473     elif A.dtype == np.complex64:
  5474         abs_helper[complex64_t](<ctensor*>A.dt, <ctensor*>oA.dt)
  5475     elif A.dtype == np.complex128:
  5476         abs_helper[complex128_t](<ctensor*>A.dt, <ctensor*>oA.dt)
  5477     elif A.dtype == np.int64:
  5478         abs_helper[int64_t](<ctensor*>A.dt, <ctensor*>oA.dt)
  5479     elif A.dtype == np.int32:
  5480         abs_helper[int32_t](<ctensor*>A.dt, <ctensor*>oA.dt)
  5481     elif A.dtype == np.int16:
  5482         abs_helper[int16_t](<ctensor*>A.dt, <ctensor*>oA.dt)
  5483     elif A.dtype == np.int8:
  5484         abs_helper[int8_t](<ctensor*>A.dt, <ctensor*>oA.dt)
  5487 def _setgetitem_helper(obj, key_init):
  5492     key = deepcopy(key_init)
  5495     if isinstance(key,int):
  5497     elif isinstance(key,slice):
  5499     elif key 
is Ellipsis:
  5502         if not isinstance(key, tuple):
  5503             raise ValueError(
"CTF PYTHON ERROR: fancy indexing with non-slice/int/ellipsis-type indices is unsupported and can instead be done via take or read/write")
  5504         for i 
in range(len(key)):
  5505             if not isinstance(key[i], slice) 
and not isinstance(key[i],int) 
and key[i] 
is not Ellipsis:
  5506                 raise ValueError(
"CTF PYTHON ERROR: invalid __setitem__/__getitem__ tuple passed, type of elements not recognized")
  5512         if isinstance(s,int):
  5513             if obj.shape[i] != 1:
  5515             inds.append((s,s+1,1))
  5520                 raise ValueError(
'CTF PYTHON ERROR: Only one Ellipsis, ..., supported in __setitem__ and __getitem__')
  5521             for j 
in range(lensl-1,obj.ndim):
  5522                 inds.append((0,obj.shape[i],1))
  5523                 corr_shape.append(obj.shape[i])
  5524                 one_shape.append(obj.shape[i])
  5531             ind = s.indices(obj.shape[i])
  5535             if ind[1] != obj.shape[i]:
  5541             corr_shape.append(int((np.abs(ind[1]-ind[0])+np.abs(ind[2])-1)/np.abs(ind[2])))
  5542             one_shape.append(int((np.abs(ind[1]-ind[0])+np.abs(ind[2])-1)/np.abs(ind[2])))
  5543     if lensl != obj.ndim:
  5545     for i 
in range(lensl,obj.ndim):
  5546         inds.append((0,obj.shape[i],1))
  5547         corr_shape.append(obj.shape[i])
  5548         one_shape.append(obj.shape[i])
  5549     return [key, is_everything, is_single_val, is_contig, inds, corr_shape, one_shape]
 void permute(int order, int const *perm, int *arr)
permute an array 
def astype(self, dtype, order='F', casting='unsafe')
def permute(self, tensor, A, p_A=None, p_B=None, a=None, b=None)
def ravel(init_A, order="F")
def diagonal(self, offset=0, axis1=0, axis2=1)
def read(self, init_inds, vals=None, a=None, b=None)
def sum(tensor, init_A, axis=None, dtype=None, out=None, keepdims=None)
def reshape(A, newshape, order='F')
def _compare_tensors(tensor, self, tensor, b, op)
def _write_slice(self, offsets, ends, init_A, A_offsets=None, A_ends=None, a=None, b=None)
def sample(tensor, self, p)
def from_nparray(self, arr)
def empty_like(A, dtype=None)
def _ufunc_interpret(self, tensor, other, gen_tsr=True)
def transpose(self, axes)
def __pow__(self, other, modulus)
int64_t sum_bool_tsr(tensor *A)
sum all 1 values in boolean tensor 
def read_all(self, arr=None, unpack=True)
void matrix_svd(tensor *A, tensor *U, tensor *S, tensor *VT, int rank)
def array(A, dtype=None, copy=True, order='K', subok=False, ndmin=0)
def read_from_file(self, path, with_vals=True)
def _convert_type(tensor, self, tensor, B)
int bivar_function(int n, World &dw)
def conv_type(self, dtype)
def trace(self, offset=0, axis1=0, axis2=1, dtype=None, out=None)
def all(inA, axis=None, out=None, keepdims=False)
int endomorphism(int n, World &dw)
void matrix_qr_cmplx(tensor *A, tensor *Q, tensor *R)
def any(tensor, init_A, axis=None, out=None, keepdims=None)
def __idiv__(self, other_in)
def tensordot(self, other, axes)
def dot(tA, tB, out=None)
def diag(A, k=0, sp=False)
void subsample(tensor *A, double probability)
extract a sample of the entries (if sparse of the current nonzeros) 
def identity(n, dtype=np.float64)
def __cinit__(self, lens=None, sp=None, sym=None, dtype=None, order=None, tensor, copy=None)
def trace(init_A, offset=0, axis1=0, axis2=1, dtype=None, out=None)
def __isub__(self, other_in)
def __matmul__(self, other)
def zeros_like(init_A, dtype=None, order='F')
def ravel(self, order="F")
def svd(tensor, A, rank=None)
def eye(n, m=None, k=0, dtype=np.float64, sp=False)
def empty(shape, dtype=np.float64, order='F')
def dot(self, other, out=None)
def __imul__(self, other_in)
def __getitem__(self, key_init)
def all(tensor, self, axis=None, out=None, keepdims=None)
def fill_random(self, mn=None, mx=None)
def __deepcopy__(self, memo)
def einsum(subscripts, operands, out=None, dtype=None, order='K', casting='safe')
def real(self, tensor, value=None)
def __iadd__(self, other_in)
def exp(init_x, out=None, where=True, casting='same_kind', order='F', dtype=None, subok=True)
def __setitem__(self, key_init, value_init)
def astensor(A, dtype=None, order=None)
def _tot_size(self, unpack=True)
def __mul__(first, second)
def zeros(shape, dtype=np.float64, order='F')
def sum(self, axis=None, dtype=None, out=None, keepdims=None)
def __lshift__(self, other)
def fill_sp_random(self, mn=None, mx=None, frac_sp=None)
def transpose(init_A, axes=None)
def imag(self, tensor, value=None)
def write(self, init_inds, init_vals, a=None, b=None)
def __richcmp__(self, b, op)
void matrix_qr(tensor *A, tensor *Q, tensor *R)
int univar_function(int n, World &dw)
def speye(n, m=None, k=0, dtype=np.float64)
def reshape(self, integer)
def diagonal(init_A, offset=0, axis1=0, axis2=1)
def take(self, indices, axis=None, out=None, mode='raise')
def __itruediv__(self, other_in)
def write_to_file(self, path, with_vals=True)
def _get_slice(self, offsets, ends)
def take(init_A, indices, axis=None, out=None, mode='raise')
def tensordot(tA, tB, axes=2)
def __truediv__(self, other)
void matrix_svd_cmplx(tensor *A, tensor *U, tensor *S, tensor *VT, int rank)
def ones(shape, dtype=None, order='F')