Einsums Python Runtime Tensors#
These tensors are how the Python and C++ sides of Einsums are able to interact. There
are four tensors, RuntimeTensorF
, RuntimeTensorD
, RuntimeTensorC
, and
RuntimeTensorZ
, corresponding to single-precision real values, double-precision real values,
single-precision complex values, and double-precision complex values respecively. In order to increase
brevity, these will all be referred to as RuntimeTensorX
, where X should be replaced by the
respective letter. There is also the base class, RuntimeTensor
, though this does not have
much code to itself.
- class einsums.core.RuntimeTensor#
Very basic class. Should not be instantiated. It is the superclass for all
RuntimeTensorX
andRuntimeTensorViewX
types.
- class einsums.core.RuntimeTensorX#
These are the tensor classes that allow the Einsums Python code to interface with the C++ library. It has a runtime-computed rank, since compile-time computed ranks are not available in Python. This wraps the
einsums::RuntimeTensor
class with a trampoline class provided byeinsums::python::PyTensor
.- __init__()#
Construct a new empty tensor.
- __init__(name: str, dims: list[int])
Construct a new tensor with a name and dimensions.
- Parameters:
name – The name of the tensor.
dims – The dimensions of the tensor. The rank is determined from the length of this argument.
- __init__(dims: list[int])
Construct a new tensor with the given dimensions. Its name will be initialized to a default value.
- Parameters:
dims – The dimensions of the tensor. The rank is determined from the length of this argument.
- __init__(buffer_object)
Construct a new tensor from the given buffer object. Its dimensions will be determined from this object, and its data will be copied from this object.
- Parameters:
buffer_object – The object to copy from. Can only be an object implementing the Python buffer protocol.
- zero() None #
Zero out the data in the tensor. Wraps
einsums::RuntimeTensor::zero()
.
- set_all(value) None #
Set all values in the tensor to the value passed to the function. Wraps
einsums::RuntimeTensor::set_all()
- Parameters:
value – The value to fill the tensor with.
- __getitem__(index)#
Get the value at an index using Python’s bracket syntax.
- Parameters:
index – The index to pass. Can be a single value, a tuple, a slice, or pretty much anything that normally works.
- Returns:
Return value depends on the index passed. It may be a single value or it may be a
einsums.core.RuntimeTensorView
object.
- __setitem__(key, value)#
Similar to
__getitem__()
, it can take pretty much anything that will normally work for the key. For the value, a single value is always accepted. If the key creates a view, this will fill the view with the single value. If the key is a single value, it will only set that value. Otherwise, if the value is a buffer object, including a tensor or tensor view, the key must refer to a view with the same dimensions as that buffer object. It will then copy that object into the view.- Parameters:
key – Which item or items to set.
value – The value or buffer of values to set that key to.
- __imul__(other)#
- __itruediv__(other)#
- __iadd__(other)#
- __isub__(other)#
In-place arithmetic operations. These can accept either a single value or a buffer object. If
other
is a single value, it will operate every single element with that value. If it is a buffer, then it must have the same dimensions as this tensor, and it will then perform the element-wise operation between the elements of the tensor and the buffer.- Parameters:
other – The object to operate with.
- assign(buffer)#
Copy the buffer into this tensor. The tensor will resize and reshape to fit the buffer.
- Parameters:
buffer – The buffer object to assign from.
- dim(axis: int) int #
Get the dimension along the given axis.
- Parameters:
axis – The axis whose dimension should be found.
- dims() list[int] #
Get the dimensions of the tensor.
- stride(axis: int) int #
Get the stride in elements along the given axis.
- Parameters:
axis – The axis whos stride should be found.
- strides() list[int] #
Get the strides of the tensor, in elements.
- to_rank_1_view() einsums.core.RuntimeTensorViewX #
Return a view of the tensor where all the elements are in a list. Here is an example.
>>> A = einsums.utils.create_random_tensor("A", [3, 3]) >>> print(A) Name: A Type: In Core Runtime Tensor Data Type: double Dims{3 3 } Strides{3 1 } (0, 0-2): 0.03651354 0.25669908 0.11172557 (1, 0-2): 0.56452605 0.26229278 0.13112895 (2, 0-2): 0.45176621 0.25069921 0.54104020 >>> print(A.to_rank_1_view()) Name: (unnamed view) Type: In Core Runtime Tensor View Data Type: double Dims{9 } Strides{1 } (0): 0.03651354 (1): 0.25669908 (2): 0.11172557 (3): 0.56452605 (4): 0.26229278 (5): 0.13112895 (6): 0.45176621 (7): 0.25069921 (8): 0.54104020
- get_name() str #
Get the name of the tensor.
- set_name(name: str)#
Set the name of the tensor.
- Parameters:
name – The new name of the tensor.
- property name#
Python property wrapping
get_name()
andset_name()
.
- size() int #
- __len__() int #
Get the number of elements in the tensor.
size
and__len__
are synonyms of each other.- Returns:
The number of elements in the tensor.
- __iter__() einsums.core.PyTensorIteratorX #
Get an iterator that iterates over the elements in the tensor.
- Returns:
An iterator that will iterate over the elements.
- __reversed__() einsums.core.PyTensorIteratorX #
Get an iterator that iterates over the elements in the tensor in reverse.
- Returns:
An iterator that will iterate over the elements in reverse.
- rank() int #
Get the rank of the tensor, or the number of dimensions.
- Returns:
The rank of the tensor.
- __copy__()#
- __deepcopy__()#
- copy()#
- deepcopy()#
Create a copy of the tensor. These are all synonyms of each other.
- Returns:
A copy of the tensor.
- __str__() str #
Return a string representation of the tensor.