Tensor

class Tensor(*args)

Tensor.

Available constructors:

Tensor(n_rows, n_columns, n_sheets)

Tensor(n_rows, n_columns, n_sheets, values)

Tensor(sequence)

Parameters
n_rowsint, n_r > 0

Number of rows.

n_columnsint, n_c > 0

Number of columns.

n_sheetsint, n_s > 0

Number of sheets.

valuessequence of float with size n_r \times n_c \times n_s, optional

Values. OpenTURNS uses column-major ordering (like Fortran) for reshaping the flat list of values. If not mentioned, a zero tensor is created.

sequencesequence of float

Values.

Examples

>>> import openturns as ot
>>> print(ot.Tensor(2, 2, 2, [1]))
sheet #0
[[ 1 0 ]
 [ 0 0 ]]
sheet #1
[[ 0 0 ]
 [ 0 0 ]]
>>> T = ot.Tensor(2, 2, 3, range(2*2*3))
>>> print(T)
sheet #0
[[  0  2 ]
 [  1  3 ]]
sheet #1
[[  4  6 ]
 [  5  7 ]]
sheet #2
[[  8 10 ]
 [  9 11 ]]

Get or set terms:

>>> print(T[0, 0, 0])
0.0
>>> T[0, 0, 0] = 1.
>>> print(T[0, 0, 0])
1.0

Create an openturns tensor from a sequence:

>>> T = ot.Tensor([[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], [[7.0, 8.0, 9.0], [10.0, 11.0, 12.0]]])
>>> print(T)
sheet #0
[[  1  4 ]
 [  7 10 ]]
sheet #1
[[  2  5 ]
 [  8 11 ]]
sheet #2
[[  3  6 ]
 [  9 12 ]]

Methods

clean(self, threshold)

Set elements smaller than a threshold to zero.

getClassName(self)

Accessor to the object’s name.

getId(self)

Accessor to the object’s id.

getImplementation(self)

Accessor to the underlying implementation.

getName(self)

Accessor to the object’s name.

getNbColumns(self)

Accessor to the number of columns.

getNbRows(self)

Accessor to the number of rows.

getNbSheets(self)

Accessor to the number of sheets.

getSheet(self, k)

Get a sheet of the tensor.

isEmpty(self)

Tell if the tensor is empty.

setName(self, name)

Accessor to the object’s name.

setSheet(self, k, m)

Set a matrix as a sheet of the complex tensor.

__init__(self, \*args)

Initialize self. See help(type(self)) for accurate signature.

clean(self, threshold)

Set elements smaller than a threshold to zero.

Parameters
thresholdfloat

Threshold for zeroing elements.

Returns
cleaned_tensorTensor

Input tensor with elements smaller than the threshold set to zero.

getClassName(self)

Accessor to the object’s name.

Returns
class_namestr

The object class name (object.__class__.__name__).

getId(self)

Accessor to the object’s id.

Returns
idint

Internal unique identifier.

getImplementation(self)

Accessor to the underlying implementation.

Returns
implImplementation

The implementation class.

getName(self)

Accessor to the object’s name.

Returns
namestr

The name of the object.

getNbColumns(self)

Accessor to the number of columns.

Returns
n_columnsint
getNbRows(self)

Accessor to the number of rows.

Returns
n_rowsint
getNbSheets(self)

Accessor to the number of sheets.

Returns
n_sheetsint

Examples

>>> import openturns as ot
>>> T = ot.Tensor(2, 2, 3, range(2*2*3))
>>> print(T.getNbSheets())
3
getSheet(self, k)

Get a sheet of the tensor.

Parameters
sheetint

Index of sheet element.

Returns
MMatrix

The sheet element.

Examples

>>> import openturns as ot
>>> T = ot.Tensor(2, 2, 3, range(2*2*3))
>>> print(T.getSheet(1))
[[ 4 6 ]
 [ 5 7 ]]
isEmpty(self)

Tell if the tensor is empty.

Returns
is_emptybool

True if the tensor contains no element.

Examples

>>> import openturns as ot
>>> T = ot.Tensor()
>>> T.isEmpty()
True
setName(self, name)

Accessor to the object’s name.

Parameters
namestr

The name of the object.

setSheet(self, k, m)

Set a matrix as a sheet of the complex tensor.

Parameters
sheetint

Index of sheet element.

MMatrix

The matrix.

Examples

>>> import openturns as ot
>>> T = ot.Tensor(2, 2, 3, range(2*2*3))
>>> print(T)
sheet #0
[[  0  2 ]
 [  1  3 ]]
sheet #1
[[  4  6 ]
 [  5  7 ]]
sheet #2
[[  8 10 ]
 [  9 11 ]]
>>> M = ot.Matrix([[1, 2],[3, 4]])
>>> T.setSheet(0, M)
>>> print(T)
sheet #0
[[  1  2 ]
 [  3  4 ]]
sheet #1
[[  4  6 ]
 [  5  7 ]]
sheet #2
[[  8 10 ]
 [  9 11 ]]