Tensorex.Operator (tensorex v0.6.0) View Source

Functions for basic arithmetic operations with tensors.

Link to this section Summary

Functions

Adds two tensors.

Performs a self contraction on the given tensor.

Returns the determinant of the given tensor.

Divides all elements of the tensor by the scalar.

Makes a product of tensors.

Makes a dot product of tensors.

Negates a tensor.

Subtracts a tensor from another.

Returns a transposed tensor.

Link to this section Functions

Specs

Adds two tensors.

iex> Tensorex.Operator.add(
...>   Tensorex.from_list([[0,  1  ,  2  ],
...>                       [3, -4  , -5.5]]),
...>   Tensorex.from_list([[3, -2  , -2  ],
...>                       [6, -8.1, 12  ]]))
%Tensorex{data: %{[0, 0] => 3, [0, 1] =>  -1,
                  [1, 0] => 9, [1, 1] => -12.1, [1, 2] => 6.5}, shape: [2, 3]}

iex> Tensorex.Operator.add(
...>   Tensorex.from_list([[0  ,  1  ,  2  ],
...>                       [3  , -4  , -5.5]]),
...>   Tensorex.from_list([[0.0, -1  , -2  ],
...>                       [6  , -8.1, 12  ]]))
%Tensorex{data: %{[1, 0] => 9, [1, 1] => -12.1, [1, 2] => 6.5}, shape: [2, 3]}

iex> Tensorex.Operator.add(
...>   Tensorex.from_list([[ 0,  6],
...>                       [-3,  0]]),
...>   Tensorex.from_list([[ 8,  0],
...>                       [ 0,  9]]))
%Tensorex{data: %{[0, 0] =>  8, [0, 1] => 6,
                  [1, 0] => -3, [1, 1] => 9}, shape: [2, 2]}
Link to this function

contract(tensorex, axes)

View Source

Specs

contract(Tensorex.t(), [non_neg_integer()]) :: Tensorex.t() | number()

Performs a self contraction on the given tensor.

It is known as the trace for 2nd rank tensors.

iex> Tensorex.Operator.contract(
...>   Tensorex.from_list([[1, 2, 3],
...>                       [4, 5, 6],
...>                       [7, 8, 9]]), [0, 1])
15

iex> Tensorex.Operator.contract(
...>   Tensorex.from_list([[[1, 2, 3],
...>                        [4, 5, 6],
...>                        [7, 8, 9]],
...>                       [[2, 3, 4],
...>                        [5, 6, 7],
...>                        [8, 9, 1]],
...>                       [[3, 4, 5],
...>                        [6, 7, 8],
...>                        [9, 1, 2]]]), [0, 2])
%Tensorex{data: %{[0] => 9, [1] => 18, [2] => 18}, shape: [3]}

Specs

determinant(Tensorex.t()) :: number()

Returns the determinant of the given tensor.

iex> Tensorex.Operator.determinant(
...>   Tensorex.from_list([[13,  1,  2,  3],
...>                       [ 4, 14,  5,  6],
...>                       [ 7,  8, 15,  9],
...>                       [10, 11, 12, 16]])
...> )
14416

iex> Tensorex.Operator.determinant(
...>   Tensorex.from_list([[0, 0],
...>                       [0, 0]])
...> )
0

iex> Tensorex.Operator.determinant(
...>   Tensorex.from_list([[2.5, 0  , 0],
...>                       [0  , 1.8, 0],
...>                       [0  , 0  , 3]])
...> )
13.5

iex> Tensorex.Operator.determinant(
...>   Tensorex.from_list([[[13,  1,  2,  3],
...>                        [ 4, 14,  5,  6],
...>                        [ 7,  8, 15,  9],
...>                        [10, 11, 12, 16]],
...>                       [[33, 21, 22, 23],
...>                        [24, 34, 25, 26],
...>                        [27, 28, 35, 29],
...>                        [30, 31, 32, 36]],
...>                       [[53, 41, 42, 43],
...>                        [44, 54, 45, 46],
...>                        [47, 48, 55, 49],
...>                        [50, 51, 52, 56]],
...>                       [[73, 61, 62, 63],
...>                        [64, 74, 65, 66],
...>                        [67, 68, 75, 69],
...>                        [70, 71, 72, 76]]])
...> )
1567104

Specs

divide(Tensorex.t(), number()) :: Tensorex.t()

Divides all elements of the tensor by the scalar.

iex> Tensorex.Operator.divide(
...>   Tensorex.from_list([[2  , 3.5, -1.6,   8.2],
...>                       [1.1, 3.0,  0  , -12.1]]), 4)
%Tensorex{data: %{[0, 0] => 0.5  , [0, 1] => 0.875, [0, 2] => -0.4, [0, 3] =>  2.05 ,
                  [1, 0] => 0.275, [1, 1] => 0.75 ,                 [1, 3] => -3.025}, shape: [2, 4]}
Link to this function

multiply(scalar, tensor)

View Source

Specs

multiply(Tensorex.t() | number(), Tensorex.t() | number()) :: Tensorex.t()

Makes a product of tensors.

If both arguments are tensors, it returns a tensor product of them. When one of arguments is a number/0, then all elements of the tensor will be amplified by the scalar.

iex> Tensorex.Operator.multiply(
...>   Tensorex.from_list([2, 5.2, -4  , 0  ]),
...>   Tensorex.from_list([2, 3.5, -1.6, 8.2]))
%Tensorex{data: %{[0, 0] => 4   , [0, 1] =>   7.0, [0, 2] => -3.2 , [0, 3] => 16.4 ,
                  [1, 0] => 10.4, [1, 1] =>  18.2, [1, 2] => -8.32, [1, 3] => 42.64,
                  [2, 0] => -8  , [2, 1] => -14.0, [2, 2] =>  6.4 , [2, 3] => -32.8}, shape: [4, 4]}

iex> Tensorex.Operator.multiply(3.5,
...>   Tensorex.from_list([[2   ,  3.5, -1.5, 8.0],
...>                       [4.12, -2  ,  1  , 0  ]]))
%Tensorex{data: %{[0, 0] =>  7.0 , [0, 1] => 12.25, [0, 2] => -5.25, [0, 3] => 28.0,
                  [1, 0] => 14.42, [1, 1] => -7.0 , [1, 2] =>  3.5                 }, shape: [2, 4]}
Link to this function

multiply(tensorex1, tensorex2, axes)

View Source

Specs

Makes a dot product of tensors.

Components specified by the axes will be sumed up (or contracted).

iex> Tensorex.Operator.multiply(
...>   Tensorex.from_list([0, 0.0,  0.0, 0  ]),
...>   Tensorex.from_list([2, 3.5, -1.6, 8.2]), [{0, 0}])
0.0

iex> Tensorex.Operator.multiply(
...>   Tensorex.from_list([[2  , 3.5, -1.6,   8.2],
...>                       [1.1, 3.0,  8  , -12.1]]),
...>   Tensorex.from_list([[0  , 0.0],
...>                       [0.0, 0  ],
...>                       [0.0, 0  ],
...>                       [0  , 0  ]]), [{0, 1}, {1, 0}])
0.0

iex> Tensorex.Operator.multiply(
...>   Tensorex.from_list([2, 5.2, -4  , 0  ]),
...>   Tensorex.from_list([2, 3.5, -1.6, 8.2]), [{0, 0}])
28.6

iex> Tensorex.Operator.multiply(
...>   Tensorex.from_list([[ 2   ,  5.5, -4  , 0  ],
...>                       [ 4.12, -2  ,  1  , 0  ]]),
...>   Tensorex.from_list([[ 2   ,  3.5],
...>                       [-1.6 ,  8.2],
...>                       [ 2   , -3.5],
...>                       [-1.5 ,  8.0]]), [{0, 1}])
%Tensorex{data: %{[0, 0] => 18.42, [0, 1] =>  30.584, [0, 2] => -10.42, [0, 3] =>  29.96,
                  [1, 0] =>  4.0 , [1, 1] => -25.2  , [1, 2] =>  18.0 , [1, 3] => -24.25,
                  [2, 0] => -4.5 , [2, 1] =>  14.6  , [2, 2] => -11.5 , [2, 3] =>  14.0 }, shape: [4, 4]}

Specs

negate(Tensorex.t()) :: Tensorex.t()

Negates a tensor.

iex> Tensorex.Operator.negate(
...>   Tensorex.from_list([[ 2  , 3.5, -4  , 0  ],
...>                       [-2.2, 6  ,  0.0, 5.5]]))
%Tensorex{data: %{[0, 0] => -2  , [0, 1] => -3.5, [0, 2] => 4,
                  [1, 0] =>  2.2, [1, 1] => -6  ,              [1, 3] => -5.5}, shape: [2, 4]}
Link to this function

subtract(tensor, tensorex)

View Source

Specs

subtract(Tensorex.t(), Tensorex.t()) :: Tensorex.t()

Subtracts a tensor from another.

iex> Tensorex.Operator.subtract(
...>   Tensorex.from_list([[0,  1,  2], [3, -4,   -5.5]]),
...>   Tensorex.from_list([[3, -2, -2], [6, -8.1, 12  ]]))
%Tensorex{data: %{[0, 0] => -3, [0, 1] => 3  , [0, 2] =>   4  ,
                  [1, 0] => -3, [1, 1] => 4.1, [1, 2] => -17.5}, shape: [2, 3]}

iex> Tensorex.Operator.subtract(
...>   Tensorex.from_list([[0,   1, 2], [3, -4,   -5.5]]),
...>   Tensorex.from_list([[0.0, 1, 2], [6, -8.1, 12  ]]))
%Tensorex{data: %{[1, 0] => -3, [1, 1] => 4.1, [1, 2] => -17.5}, shape: [2, 3]}
Link to this function

transpose(tensorex, axes)

View Source

Specs

transpose(Tensorex.t(), [{non_neg_integer(), non_neg_integer()}, ...]) ::
  Tensorex.t()

Returns a transposed tensor.

iex> Tensorex.Operator.transpose(
...>   Tensorex.from_list([[[ 2   ,  5.5, -4, 0  ],
...>                        [ 4.12, -2  ,  1, 0  ]],
...>                       [[ 3   ,  1.2,  5, 8.9],
...>                        [ 1   ,  6  ,  7, 1.3]]]), [{0, 2}])
%Tensorex{data: %{[0, 0, 0] =>  2   , [0, 0, 1] => 3  ,
                  [0, 1, 0] =>  4.12, [0, 1, 1] => 1  ,
                  [1, 0, 0] =>  5.5 , [1, 0, 1] => 1.2,
                  [1, 1, 0] => -2   , [1, 1, 1] => 6  ,
                  [2, 0, 0] => -4   , [2, 0, 1] => 5  ,
                  [2, 1, 0] =>  1   , [2, 1, 1] => 7  ,
                                      [3, 0, 1] => 8.9,
                                      [3, 1, 1] => 1.3}, shape: [4, 2, 2]}