5.5. Constant

class Constant(TensorLike)

This class represents a tensor constant.

The tensor may be dense or sparse, it is n-dimensional.

For example:

listdata = [[0, 1], [2, 3]]
ndarray = numpy.array(listdata)
coo_mat = scipy.sparse.coo_matrix(ndarray)

# From scalar
scalar = Constant(0)
# From iterable
dense1 = Constant(listdata)
# From numpy ndarray
dense2 = Constant(ndarray)
# From shape, coordinates, values
sparse1 = Constant((2, 2), [(0, 0), (1, 1)], [1, 1])
# From shape, flat indices, values
sparse2 = Constant((2, 2), [0, 3], [1, 1])
# From scipy sparse matrix
sparse3 = Constant(coo_mat)

Usually, users do not need to use this class, a list of number, a numpy ndarray, even a scipy sparse matrix may be preferred.

A scenario for this class is, a sparse tensor with more than 2 dimensions can be and only can be created by using this class.

For example:

>>> A = [[1, 2], [3, 4]]
>>> X = Var(2, 2)

>>> A @ X
Expr((2, 2), MATMUL('ndarray', 'Var'))
>>> numpy.array(A) @ X
Expr((2, 2), MATMUL('ndarray', 'Var'))
>>> Constant(A) @ X
Expr((2, 2), MATMUL('Constant', 'Var'))
>>> scipy.sparse.coo_matrix(A) @ X
Expr((2, 2), MATMUL('coo_matrix', 'Var'))
>>> # When A is a sparse cube
>>> Constant((2, 2, 2), range(8), range(8)) @ X
Expr((2, 2, 2), MATMUL('Constant', 'Var'))

This class is numpy compatible:

>>> c = Constant([[1, 2], [3, 4]])
>>> numpy.abs(norm(c, ord=2) - numpy.linalg.norm(c, ord=2)) < 1e-9
True

Properties

T

Transpose of the constant.

data

The content of this constant.

Methods

__init__()

Construct a constant.

asDense()

If constant is sparse, make a dense copy for it.

asScalar()

Convert this tensor to a scalar value.

asSparse()

If constant is dense, make a sparse copy for it.

dispose()

Instantly free memory for constant object, or constant may be freed automatically by garbage collector.

hasAttr()

Check if constant has a specified attribute.

isDense()

Check if this constant is a dense tensor.

isSparse()

Check if this constant is a sparse tensor.

reshape()

Reshape this constant, number of elements should not be changed between new shape and original shape.

__getitem__()

Get sub-tensor from this constant.

__neg__()

Returns -constant.

__add__()

Constant + object. Operands may be broadcasted.

__radd__()

object + constant. Operands may be broadcasted.

__sub__()

Constant - object. Operands may be broadcasted.

__rsub__()

object - constant. Operands may be broadcasted.

__mul__()

Constant * object. Operands may be broadcasted.

__rmul__()

object * constant. Operands may be broadcasted.

__matmul__()

Constant @ object. Operands may be broadcasted.

__rmatmul__()

object @ constant. Operands may be broadcasted.

__truediv__()

Constant / object. Operands may be broadcasted.

__rtruediv__()

object / constant. Operands may be broadcasted.

__pow__()

Constant ** object. Operands may be broadcasted.

__rpow__()

object ** constant. Operands may be broadcasted.

property T: Constant

Transpose of the constant.

property data: np.ndarray | Tuple[np.ndarray, np.ndarray]

The content of this constant.

It is a numpy ndarray for dense. In case of sparse, this is a tuple contains 2 ndarray, the indices and values.

__init__(arr: Constant | ArrayLike, ind: ArrayLike | None = None, val: ArrayLike | None = None)

Construct a constant.

param arr:

Type: Union[Constant, ArrayLike]

The value for this constant.

Can be another constant, a scipy sparse matrix, an array-like object like a list, or a numpy ndarray.

param ind:

Type: Optional[ArrayLike] = None

If ind is not None, it should be an array-like object to provide indices for sparse tensor. In this case arr should be a shape.

param val:

Type: Optional[ArrayLike] = None

If val is not None, it should be an array-like object to provide values for sparse tensor. In this case arr should be a shape.

asDense()

If constant is sparse, make a dense copy for it.

return:

Type: Constant

The dense copy for sparse constant. No copy will be made if constant is dense.

asScalar()

Convert this tensor to a scalar value.

return:

Type: float

The value of only element contained by tensor. if tensor has more elements, this method raise exception.

asSparse()

If constant is dense, make a sparse copy for it.

return:

Type: Constant

The sparse copy for dense constant. No copy will be made if constant is sparse.

dispose()

Instantly free memory for constant object, or constant may be freed automatically by garbage collector.

hasAttr(**kwargs: bool)

Check if constant has a specified attribute.

c = Constant([[1, 0], [0, 1]])
c.hasAttr(symmetric=True)
# Expression is True
c.hasAttr(neg=True)
# Expression is False
param **kwargs:

Type: bool

The attribute to check. Attributes include:

  • nonneg

  • nonpos

  • symmetric

  • diag

  • PSD

  • NSD

  • pos

  • neg

Value of this argument should be True. A False-valued argument will be ignored.

return:

Type: bool

True if constant has specified attribute.

isDense()

Check if this constant is a dense tensor.

return:

Type: bool

True if constant is dense, False otherwise

isSparse()

Check if this constant is a sparse tensor.

return:

Type: bool

True if constant is sparse, False otherwise

reshape(*shape: int)

Reshape this constant, number of elements should not be changed between new shape and original shape.

param *shape:

Type: int

The dimensions for new shape.

return:

Type: Constant

The reshaped constant.

__getitem__(slc: slice)

Get sub-tensor from this constant.

param slc:

Type: slice

The index range for each dimension for sub-tensor.

return:

Type: Constant

The sub-tensor.

__neg__()

Returns -constant.

return:

Type: Constant

-constant.

__add__(r: TensorLike | ArrayLike)

Constant + object. Operands may be broadcasted.

param r:

Type: Union[TensorLike, ArrayLike]

The right-hand-side object

return:

Type: Union[Constant, Expr]

The result of add operation.

__radd__(l: TensorLike | ArrayLike)

object + constant. Operands may be broadcasted.

param l:

Type: Union[TensorLike, ArrayLike]

The left-hand-side object.

return:

Type: Union[Constant, Expr]

The result of add operation.

__sub__(r: TensorLike | ArrayLike)

Constant - object. Operands may be broadcasted.

param r:

Type: Union[TensorLike, ArrayLike]

The right-hand-side object

return:

Type: Union[Constant, Expr]

The result of subtraction.

__rsub__(l: TensorLike | ArrayLike)

object - constant. Operands may be broadcasted.

param l:

Type: Union[TensorLike, ArrayLike]

The left-hand-side object.

return:

Type: Union[Constant, Expr]

The result of subtraction.

__mul__(r: TensorLike | ArrayLike)

Constant * object. Operands may be broadcasted.

param r:

Type: Union[TensorLike, ArrayLike]

The right-hand-side object

return:

Type: Union[Constant, Expr]

The result of multiplication.

Note

This operation is element-wise multiplication.

__rmul__(l: TensorLike | ArrayLike)

object * constant. Operands may be broadcasted.

param l:

Type: Union[TensorLike, ArrayLike]

The left-hand-side object.

return:

Type: Union[Constant, Expr]

The result of multiplication.

Note

This operation is element-wise multiplication.

__matmul__(r: TensorLike | ArrayLike)

Constant @ object. Operands may be broadcasted.

param r:

Type: Union[TensorLike, ArrayLike]

The right-hand-side object

return:

Type: Union[Constant, Expr]

The result of multiplication operation.

Note

This operation is matrix multiplication.

__rmatmul__(l: TensorLike | ArrayLike)

object @ constant. Operands may be broadcasted.

param l:

Type: Union[TensorLike, ArrayLike]

The left-hand-side object.

return:

Type: Union[Constant, Expr]

The result of multiplication.

Note

This operation is matrix multiplication.

__truediv__(r: TensorLike | ArrayLike)

Constant / object. Operands may be broadcasted.

param r:

Type: Union[TensorLike, ArrayLike]

The right-hand-side object

return:

Type: Union[Constant, Expr]

The result of division.

Note

This operation is element-wise division.

__rtruediv__(l: TensorLike | ArrayLike)

object / constant. Operands may be broadcasted.

param l:

Type: Union[TensorLike, ArrayLike]

The left-hand-side object.

return:

Type: Union[Constant, Expr]

The result of division.

Note

This operation is element-wise division.

__pow__(r: TensorLike | ArrayLike)

Constant ** object. Operands may be broadcasted.

param r:

Type: Union[TensorLike, ArrayLike]

The right-hand-side object

return:

Type: Union[Constant, Expr]

The result of exponentiation.

Note

This operation is element-wise exponentiation.

__rpow__(l: TensorLike | ArrayLike)

object ** constant. Operands may be broadcasted.

param l:

Type: Union[TensorLike, ArrayLike]

The left-hand-side object.

return:

Type: Union[Constant, Expr]

The result of exponentiation.

Note

This operation is element-wise exponentiation.