symmetr.tensors module

Defines a symbolic tensor class.

All the tensors that are symmetrized use this class.

class symmetr.tensors.GenericTensor(dim1, dim2, ind_types=None)[source]

Bases: object

This class cannot be directly used by itself. Classed that inherit from this class must define __getitem__, __setitem__ and copy0.

__add__(other)[source]
__dict__ = mappingproxy({'__module__': 'symmetr.tensors', '__firstlineno__': 234, '__doc__': '\nThis class cannot be directly used by itself.\nClassed that inherit from this class must define __getitem__, __setitem__\nand copy0.\n', '__init__': <function GenericTensor.__init__>, '__len__': <function GenericTensor.__len__>, '__add__': <function GenericTensor.__add__>, '__sub__': <function GenericTensor.__sub__>, '__mul__': <function GenericTensor.__mul__>, '__div__': <function GenericTensor.__div__>, '__radd__': <function GenericTensor.__radd__>, '__rsub__': <function GenericTensor.__rsub__>, '__rmul__': <function GenericTensor.__rmul__>, '__neg__': <function GenericTensor.__neg__>, '__iter__': <function GenericTensor.__iter__>, 'reduce': <function GenericTensor.reduce>, 'convert': <function GenericTensor.convert>, 'def_trans': <function GenericTensor.def_trans>, 'use_numpy_mode': <function GenericTensor.use_numpy_mode>, 'transform': <function GenericTensor.transform>, 'is_even': <function GenericTensor.is_even>, 'def_metric': <function GenericTensor.def_metric>, 'reverse_index': <function GenericTensor.reverse_index>, 'raise_index': <function GenericTensor.raise_index>, 'lower_index': <function GenericTensor.lower_index>, 'is_contravar': <function GenericTensor.is_contravar>, 'is_covar': <function GenericTensor.is_covar>, '__static_attributes__': ('G', 'Gi', 'P_trans', 'T_comp', 'T_trans', 'dim1', 'dim2', 'ind_trans', 'ind_types', 'inds', 'permute_inds', 'trans_type'), '__dict__': <attribute '__dict__' of 'GenericTensor' objects>, '__weakref__': <attribute '__weakref__' of 'GenericTensor' objects>, '__annotations__': {}})
__div__(num)[source]
__firstlineno__ = 234
__init__(dim1, dim2, ind_types=None)[source]

Creates a symbolic tensor.

Parameters:
  • dim1 (int) – How many values can each index have.

  • dim2 (int) – How many indeces are there in the tensor.

  • ind_types (optinal[tuple]) – Specifies whether individual indeces are covariant or contravariant. 1 specifies contravariant and -1 covariant. If not specified all the indeces are assumed contravariant.

__iter__()[source]
__len__()[source]
__module__ = 'symmetr.tensors'
__mul__(num)[source]
__neg__()[source]
__radd__(other)[source]
__rmul__(num)[source]
__rsub__(other)[source]
__static_attributes__ = ('G', 'Gi', 'P_trans', 'T_comp', 'T_trans', 'dim1', 'dim2', 'ind_trans', 'ind_types', 'inds', 'permute_inds', 'trans_type')
__sub__(other)[source]
convert(T, in_place=True)[source]

Return the tensor transormed by coordinate transformation matrix T.

Parameters:

T (matrix) – Coordinate transformation matrix. T transforms from A to B, ie Tx_A = x_B.

Returns:

the transformed tensor

Return type:

ten_T (tensor)

def_metric(G)[source]

Defines the metric for the coordinate system in which the tensor is defined

G should be the covariant metric.

def_trans(ind_trans=None, T_comp=None, P_trans=None, T_trans=None, permute_inds=None)[source]
is_contravar(i)[source]

Checkes if the index i is contravariant.

is_covar(i)[source]

Checkes if the index i is covariant.

is_even(sym=None)[source]
lower_index(i)[source]
raise_index(i)[source]
reduce(comp, value)[source]
reverse_index(i, in_place=True)[source]
transform(sym)[source]
use_numpy_mode()[source]
class symmetr.tensors.NumTensor(kind, dim1, dim2, name='x', ind_types=None)[source]

Bases: GenericTensor

__annotations__ = {}
__eq__(other)[source]

Return self==value.

__firstlineno__ = 1013
__getitem__(key)[source]
__hash__ = None
__init__(kind, dim1, dim2, name='x', ind_types=None)[source]

Creates a symbolic tensor represented by a matrix.

This is only possible for linear tensors, that is tensors such that every element is a linear combination of symbolic variables

Parameters:
  • kind – Sets what kind of tensor. ‘s’ for symbolic, 0 for zero tensor

  • dim1 (int) – How many values can each index have.

  • dim2 (int) – How many indeces are there in the tensor.

  • name (optional[str]) – Name of the symbolic variables. Defaults to ‘x’.

  • ind_types (optinal[tuple]) – Specifies whether individual indeces are covariant or contravariant. 1 specifies contravariant and -1 covariant. If not specified all the indeces are assumed contravariant.

__module__ = 'symmetr.tensors'
__mul__(num)[source]
__ne__(other)[source]

Return self!=value.

__setitem__(key, value)[source]
__static_attributes__ = ('G', 'Gi', 'name', 't')
__str__()[source]

Return str(self).

convert(T, in_place=True)[source]

Return the tensor transormed by coordinate transformation matrix T.

Parameters:

T (matrix) – Coordinate transformation matrix. T transforms from A to B, ie Tx_A = x_B.

Returns:

the transformed tensor

Return type:

ten_T (tensor)

convert2tensor(num_prec=None)[source]
copy()[source]
copy0()[source]

returns a zero tensor with the same shape and transformation rules

ind2i(ind)[source]
is_even(sym=None, num_prec=1e-14)[source]
pprint()[source]
round(decimals)[source]
subs(i, sub)[source]
transform(sym)[source]
class symmetr.tensors.Tensor(kind, dim1, dim2, name='x', ind_types=None)[source]

Bases: GenericTensor

Creates a symbolic tensor.

Usage:
Use by t=tensor(‘s’,dim1,dim2), where ‘s’ means the tensor should be symbolic, dim2 is the number of indeces

of the tensor, dim1 is the number of values each index can have.

By default variables will be called x### for a different variable name, Run with tensor(‘s’,dim1,dim2,’name’). For nonsymbolic zero tensor, run tensor(0,dim1,dim2). Print by print t Write out element of the tensor by t[1,2,3] or t[(1,2,3)] for example Assign a value to element: t[1,2,3] = 0. Sum or substract two tensors: t1+t2. Multiply by a number or symbolic variable: t*2. Loop over all elements, following will print all elements for example. Note that the loop is over indeces, not elements!

for i in t:

print t[i]

If you need to use the symbolic variables use t.x[index], for example for x011, use t.x[0,1,1] (or t.x[(0,1,1)]).

You can do for example t[0,0,0] = t.x[1,1,1], then t[0,0,0] will be equal to x111. You can also access x011 by t[‘x011’].

__annotations__ = {}
__eq__(other)[source]

Return self==value.

__firstlineno__ = 540
__getitem__(key)[source]
__hash__ = None
__init__(kind, dim1, dim2, name='x', ind_types=None)[source]

Creates a symbolic tensor.

Parameters:
  • kind – Sets what kind of tensor. ‘s’ for symbolic, 0 for zero tensor

  • dim1 (int) – How many values can each index have.

  • dim2 (int) – How many indeces are there in the tensor.

  • name (optional[str]) – Name of the symbolic variables. Defaults to ‘x’.

  • ind_types (optinal[tuple]) – Specifies whether individual indeces are covariant or contravariant. 1 specifies contravariant and -1 covariant. If not specified all the indeces are assumed contravariant.

__module__ = 'symmetr.tensors'
__ne__(other)[source]

Return self!=value.

__setitem__(key, value)[source]
__static_attributes__ = ('name', 't', 'v', 'x')
__str__()[source]

Return str(self).

__type__()[source]
check_calc_symmetry(Xc, scale, verbosity=0)[source]
convert2numpy(acc=None)[source]
convert2numtensor(td=False)[source]
copy()[source]

returns a copy of itself

copy0()[source]

returns a zero tensor with the same shape and transformation rules

evalf(acc=15)[source]
static load(inp)[source]

Loads the Tensor from either a dictionary or a file. :param inp: can be either the filename or the dictionary

mat(numpy=False)[source]
pprint(print_format=None, latex=False, no_newline=False, ind_types=False, remove_zeros=False, ret=False)[source]
remove_zeros(num_digits=14)[source]
rename_vars(name=None, xyz=False)[source]
round(prec)[source]
save(fname=None)[source]

Exports the tensor to a json-serializable dictionary and optionaly saves it to file. If fname is None, it returns the dictionary, otherwise it saves it to a file fname.

simplify()[source]
subs(old, new='notset', inplace=True)[source]
subs_tensor(Y)[source]
symmetr.tensors.makeinds(dim1, dim2)[source]
class symmetr.tensors.matrix(kind, dim1, name='x')[source]

Bases: Tensor

T()[source]
__add__(other)[source]
__annotations__ = {}
__div__(num)[source]
__firstlineno__ = 1252
__init__(kind, dim1, name='x')[source]

Creates a symbolic tensor.

Parameters:
  • kind – Sets what kind of tensor. ‘s’ for symbolic, 0 for zero tensor

  • dim1 (int) – How many values can each index have.

  • dim2 (int) – How many indeces are there in the tensor.

  • name (optional[str]) – Name of the symbolic variables. Defaults to ‘x’.

  • ind_types (optinal[tuple]) – Specifies whether individual indeces are covariant or contravariant. 1 specifies contravariant and -1 covariant. If not specified all the indeces are assumed contravariant.

__module__ = 'symmetr.tensors'
__mul__(other)[source]
__radd__(other)[source]
__rmul__(other)[source]
__rsub__(other)[source]
__static_attributes__ = ()
__sub__(other)[source]
symmetr.tensors.tensor2Y(X, Y_format='sympy', algo=3, reverse=False, td=False)[source]

Converts the tensor into a numerical matrix form. Only possible for linear tensors!

symmetr.tensors.var_name(name, num, dim)[source]
symmetr.tensors.var_name_xyz(name, num)[source]