tensor v2.1.2 Tensor.Tensor

Link to this section Summary

Functions

Returns the Tensor where all elements are converted to their absolute values.

Elementwise addition.

If the Tensor is the first argument a, adds the number b to all elements in Tensor a.

Elementwise addition of the tensor_a and tensor_b.

Maps a function over all values in the tensor, including all values that are equal to the tensor identity. This is useful to map a function with side effects over the Tensor.

Returns the dimensions of the tensor.

Elementwise multiplication.

If the Tensor is the first argument a, divides all elements of Tensor a by the number b.

Elementwise division of tensor_a by tensor_b.

Returns a Tensor of one order less, containing all fields for which the highest-order accessor location matches index.

Builds up a tensor from a list of slices in a lower dimension. A list of values will build a Vector. A list of same-length vectors will create a Matrix. A list of same-size matrices will create an order-3 Tensor.

Converts a sparse map where each key is a coordinate list, and each value is anything to a Tensor with the given dimensions and contents.

Returns the element at index from tensor. If index is out of bounds, returns default.

Gets the value inside tensor at key key, and calls the passed function fun on it, which might update it, or return :pop if it ought to be removed.

Returns the identity, the default value a tensor inserts at a position when no other value is set.

lifts a Tensor up one order, by adding a dimension of size 1 to the start.

Maps fun over all values in the Tensor.

Returs true if the tensor is a 2-order Tensor, which is also known as a Matrix.

Merges tensor_a with tensor_b by calling fun for each element that exists in at least one of them

Merges tensor_a with tensor_b by calling fun for each element that exists in at least one of them

Returns the Tensor where all elements have been negated.

Elementwise multiplication.

If the Tensor is the first argument a, multiplies all elements of Tensor a with the number b.

Elementwise multiplication of the tensor_a with tensor_b.

Creates a new Tensor from a list of lists (of lists of lists of ...). The second argument should be the dimensions the tensor should become. The optional third argument is an identity value for the tensor, that all non-set values will default to.

Returns the order of the Tensor.

Removes the element associated with index from the tensor. Returns a tuple, the first element being the removed element (or nil if nothing was removed), the second the updated Tensor with the element removed.

A variant of reduce that accepts anything that is Combinable as second argument. This Combinable will determine what the empty value and the combining operation will be.

Returns a list containing all lower-dimension Tensors in the Tensor.

Maps a function over the values in the tensor.

Elementwise subtraction.

If the Tensor is the first argument a, subtracts the number b from all elements in Tensor a.

Elementwise substraction of the tensor_b from tensor_a.

Converts the reducable into a list, by building up a list from all elements, and in the end reversing it.

Returns a map where the keys are coordinate lists, and the values are the values stored in the matrix.

Transposes the Tensor, by swapping the outermost dimension for the b-th dimension.

Transposes the Tensor, by swapping the a-th dimension for the b-th dimension.

Returs true if the tensor is a 1-order Tensor, which is also known as a Vector.

Returns a new tensor, where all values are {list_of_coordinates, value} tuples.

Link to this section Types

Link to this opaque

tensor() (opaque)
tensor()

Link to this section Functions

Returns the Tensor where all elements are converted to their absolute values.

Link to this function

add(a, b)
add(tensor(), tensor()) :: tensor()
add(Numeric.t(), tensor()) :: tensor()
add(tensor(), Numeric.t()) :: tensor()

Elementwise addition.

  • If both a and b are Tensors, the same as calling add_tensor/2.
  • If one of a or b is any kind of number, the same as calling add_number/2.
Link to this function

add_number(a, b)
add_number(tensor(), Numeric.t()) :: tensor()

If the Tensor is the first argument a, adds the number b to all elements in Tensor a.

If the Tensor is the second argument b, adds all numbers in b to a.

(There only is a difference in the outcomes of these two cases if on the underlying numeric type, addition is not commutative)

Link to this function

add_tensor(tensor_a, tensor_b)
add_tensor(tensor(), tensor()) :: tensor()

Elementwise addition of the tensor_a and tensor_b.

Link to this function

dense_map_with_coordinates(tensor, fun)
dense_map_with_coordinates(tensor(), ({list() | :identity, any()} -> any())) ::
  tensor()

Maps a function over all values in the tensor, including all values that are equal to the tensor identity. This is useful to map a function with side effects over the Tensor.

The function will be called once to calculate the new identity. This call will be of shape {:identity, value}. After the dense map, all values that are the same as the newly calculated identity are again removed, to make the Tensor sparse again.

The function will receive a tuple of the form {list_of_coordinates, value},

Link to this function

dimensions(tensor)
dimensions(tensor()) :: [non_neg_integer()]

Returns the dimensions of the tensor.

Link to this function

div(a, b)
div(tensor(), tensor()) :: tensor()
div(Numeric.t(), tensor()) :: tensor()
div(tensor(), Numeric.t()) :: tensor()

Elementwise multiplication.

  • If both a and b are Tensors, the same as calling div_tensor/2.
  • If one of a or b is any kind of number, the same as calling div_number/2.
Link to this function

div_number(a, b)
div_number(tensor(), number()) :: tensor()

If the Tensor is the first argument a, divides all elements of Tensor a by the number b.

If the Tensor is the second argument b, the result is a Tensor filled with a divided by all numbers in Tensor b.

Link to this function

div_tensor(tensor_a, tensor_b)
div_tensor(tensor(), tensor()) :: tensor()

Elementwise division of tensor_a by tensor_b.

Link to this function

do_map(tensor_contents, list, fun, new_identity)

Link to this function

fetch(tensor, index)
fetch(tensor(), integer()) :: {:ok, any()} | :error

Returns a Tensor of one order less, containing all fields for which the highest-order accessor location matches index.

In the case of a Vector, returns the simple value at the given index location. In the case of a Matrix, returns a Vector containing the row at the given column indicated by index.

index has to be an integer, smaller than the size of the highest dimension of the tensor. When index is negative, we will look from the right side of the Tensor.

If index falls outside of the range of the Tensor's highest dimension, :error is returned. See also get/3.

This is part of the Access Behaviour implementation for Tensor.

Link to this function

from_slices(list_of_slices)
from_slices([] | tensor()) :: tensor()

Builds up a tensor from a list of slices in a lower dimension. A list of values will build a Vector. A list of same-length vectors will create a Matrix. A list of same-size matrices will create an order-3 Tensor.

Link to this function

from_sparse_map(list, dimensions, identity \\ 0)

Converts a sparse map where each key is a coordinate list, and each value is anything to a Tensor with the given dimensions and contents.

See to_sparse_map/1 for the inverse operation.

iex> mat_map = %{[0, 0] => 1, [0, 1] => 2, [0, 2] => 3, [1, 0] => 4, [1, 1] => 5, [1, 2] => 6, [2, 0] => 7, [2, 1] => 8, [2, 2] => 9} iex> mat = Matrix.from_sparse_map(mat_map, 3, 3) iex> mat == Matrix.new([[1,2,3],[4,5,6],[7,8,9]], 3, 3) true

Link to this function

get(tensor, index, default)
get(tensor(), integer(), any()) :: any()

Returns the element at index from tensor. If index is out of bounds, returns default.

Link to this function

get_and_update(tensor, key, fun)
get_and_update(tensor(), integer(), (any() -> {get, any()})) ::
  {get, tensor()}
when get: var

Gets the value inside tensor at key key, and calls the passed function fun on it, which might update it, or return :pop if it ought to be removed.

key has to be an integer, smaller than the size of the highest dimension of the tensor. When key is negative, we will look from the right side of the Tensor.

Link to this function

identity(tensor)
identity(tensor()) :: any()

Returns the identity, the default value a tensor inserts at a position when no other value is set.

This is mostly used internally, and is used to allow Tensors to take a lot less space because only values that are not empty have to be stored.

Link to this function

lift(tensor)
lift(tensor()) :: tensor()

lifts a Tensor up one order, by adding a dimension of size 1 to the start.

This transforms a length-n Vector to a 1×n Matrix, a n×m matrix to a 1×n×m 3-order Tensor, etc.

See also Tensor.slices/1

Link to this function

map(tensor, fun)
map(tensor(), (any() -> any())) :: tensor()

Maps fun over all values in the Tensor.

This is a true mapping operation, as the result will be a new Tensor.

fun gets the current value as input, and should return the new value to use.

It is important that fun is a pure function, as internally it will only be mapped over all values that are non-empty, and once over the identity of the tensor.

Link to this function

matrix?(tensor)
matrix?(tensor()) :: boolean()

Returs true if the tensor is a 2-order Tensor, which is also known as a Matrix.

Link to this function

merge(tensor_a, tensor_b, fun)
merge(
  %Tensor.Tensor{contents: term(), dimensions: term(), identity: term()},
  %Tensor.Tensor{contents: term(), dimensions: term(), identity: term()},
  ([integer()] | :identity, a, a -> any())
) :: %Tensor.Tensor{contents: term(), dimensions: term(), identity: term()}
when a: any()
merge(
  %Tensor.Tensor{contents: term(), dimensions: term(), identity: term()},
  %Tensor.Tensor{contents: term(), dimensions: term(), identity: term()},
  (a, a -> any())
) :: %Tensor.Tensor{contents: term(), dimensions: term(), identity: term()}
when a: any()

Merges tensor_a with tensor_b by calling fun for each element that exists in at least one of them:

  • When a certain location is occupied in tensor_a, fun is called using tensor_b's identity, with two arguments: tensor_a_val, tensor_b_identity
  • When a certain location is occupied in tensor_b, fun is called using tensor_a's identity, with two arguments: tensor_a_identity, tensor_b_val
  • When a certain location is occupied in both tensor_a and tensor_b, fun is called with two arguments: tensor_a_val, tensor_b_val

Finally, fun is invoked one last time, with tensor_a_identity, tensor_b_identity.

An error will be raised unless tensor_a and tensor_b have the same dimensions.

Link to this function

merge_with_index(tensor_a, tensor_b, fun)

Merges tensor_a with tensor_b by calling fun for each element that exists in at least one of them:

  • When a certain location is occupied in tensor_a, fun is called using tensor_b's identity, with three arguments: coords_list, tensor_a_val, tensor_b_identity
  • When a certain location is occupied in tensor_b, fun is called using tensor_a's identity, with three arguments: coords_list, tensor_a_identity, tensor_b_val
  • When a certain location is occupied in both tensor_a and tensor_b, fun is called with three arguments: coords_list, tensor_a_val, tensor_b_val

Finally, fun is invoked one last time, with :identity, tensor_a_identity, tensor_b_identity.

An error will be raised unless tensor_a and tensor_b have the same dimensions.

Returns the Tensor where all elements have been negated.

Link to this function

mult(a, b)
mult(tensor(), tensor()) :: tensor()
mult(Numeric.t(), tensor()) :: tensor()
mult(tensor(), Numeric.t()) :: tensor()

Elementwise multiplication.

  • If both a and b are Tensors, the same as calling mult_tensor/2.
  • If one of a or b is any kind of number, the same as calling mult_number/2.
Link to this function

mult_number(a, b)
mult_number(tensor(), number()) :: tensor()

If the Tensor is the first argument a, multiplies all elements of Tensor a with the number b.

If the Tensor is the second argument b, multiplies a with all elements of Tensor b.

Link to this function

mult_tensor(tensor_a, tensor_b)
mult_tensor(tensor(), tensor()) :: tensor()

Elementwise multiplication of the tensor_a with tensor_b.

Link to this function

new(nested_list_of_values, dimensions \\ nil, identity \\ 0)
new([], [integer()], any()) :: tensor()

Creates a new Tensor from a list of lists (of lists of lists of ...). The second argument should be the dimensions the tensor should become. The optional third argument is an identity value for the tensor, that all non-set values will default to.

TODO: Solve this, maybe find a nicer way to create tensors.

Link to this function

order(tensor)
order(tensor()) :: non_neg_integer()

Returns the order of the Tensor.

This is 1 for Vectors, 2 for Matrices, etc. It is the amount of dimensions the tensor has.

Link to this function

pop(tensor, index, default \\ nil)
pop(tensor(), integer(), any()) :: {tensor() | any(), tensor()}

Removes the element associated with index from the tensor. Returns a tuple, the first element being the removed element (or nil if nothing was removed), the second the updated Tensor with the element removed.

index has to be an integer, smaller than the size of the highest dimension of the tensor. When index is negative, we will look from the right side of the Tensor.

Notice that because of how Tensors are structured, the structure of the tensor will not change. Elements that are popped are reset to the 'identity' value.

This is part of the Access Behaviour implementation for Tensor.

Examples

iex> mat = Matrix.new([[1,2],[3,4]], 2,2)
iex> {vector, mat2} = Tensor.pop(mat, 0)
iex> vector
#Vector<(2)[1, 2]>
iex> inspect(mat2)
"#Matrix<(2×2)
┌                 ┐
│       0,       0│
│       3,       4│
└                 ┘
>
"
Link to this function

reduce(a, combinable)

A variant of reduce that accepts anything that is Combinable as second argument. This Combinable will determine what the empty value and the combining operation will be.

Pass in the combinable module name to start with empty as accumulator, or the combinable as struct to use that as starting accumulator.

Link to this function

reduce(tensor, acc, fun)

Callback implementation for FunLand.Reducable.reduce/3.

Link to this function

slices(tensor)
slices(tensor()) :: tensor() | []

Returns a list containing all lower-dimension Tensors in the Tensor.

For a Vector, this will just be a list of values. For a Matrix, this will be a list of rows. For a order-3 Tensor, this will be a list of matrices, etc.

Link to this function

sparse_map_with_coordinates(tensor, fun)
sparse_map_with_coordinates(tensor(), ({list() | :identity, any()} -> any())) ::
  tensor()

Maps a function over the values in the tensor.

The function will receive a tuple of the form {list_of_coordinates, value}.

Note that only the values that are not the same as the identity will call the function. The function will be called once to calculate the new identity. This call will be of shape {:identity, value}.

Because of this sparse/lazy invocation, it is important that fun is a pure function, as this is the only way to guarantee that the results will be the same, regardless of at what place the identity is used.

Link to this function

sub(a, b)
sub(tensor(), tensor()) :: tensor()
sub(Numeric.t(), tensor()) :: tensor()
sub(tensor(), Numeric.t()) :: tensor()

Elementwise subtraction.

  • If both a and b are Tensors, the same as calling sub_tensor/2.
  • If one of a or b is any kind of number, the same as calling sub_number/2.
Link to this function

sub_number(a, b)
sub_number(tensor(), Numeric.t()) :: tensor()

If the Tensor is the first argument a, subtracts the number b from all elements in Tensor a.

If the Tensor is the second argument b, the result is a Tensor filled with a subtracted by all numbers in b. (There only is a difference in the outcomes of these two cases if on the underlying numeric type, multiplication is not commutative)

Link to this function

sub_tensor(tensor_a, tensor_b)
sub_tensor(tensor(), tensor()) :: tensor()

Elementwise substraction of the tensor_b from tensor_a.

Link to this function

to_list(tensor)
to_list(tensor()) :: list()

Converts the reducable into a list, by building up a list from all elements, and in the end reversing it.

This is an automatic function implementation, made possible because Tensor.Tensor implements the FunLand.Reducable behaviour.

Link to this function

to_sparse_map(tensor)

Returns a map where the keys are coordinate lists, and the values are the values stored in the matrix.

This is a flattened representation of the values that are actually stored inside the sparse tensor.

This representation could be used to do advanced manipulation of the sparsly-stored tensor elements.

See from_sparse_map/2 for the inverse operation.

iex> mat = Matrix.new([[1,2,3],[4,5,6],[7,8,9]], 3,3) iex> Matrix.to_sparse_map(mat) %{[0, 0] => 1, [0, 1] => 2, [0, 2] => 3, [1, 0] => 4, [1, 1] => 5, [1, 2] => 6, [2, 0] => 7, [2, 1] => 8, [2, 2] => 9}

Link to this function

transpose(tensor, dimension_b_index)
transpose(tensor(), non_neg_integer()) :: tensor()

Transposes the Tensor, by swapping the outermost dimension for the b-th dimension.

Link to this function

transpose(tensor, dimension_a_index, dimension_b_index)
transpose(tensor(), non_neg_integer(), non_neg_integer()) :: tensor()

Transposes the Tensor, by swapping the a-th dimension for the b-th dimension.

This is done in three steps (outside <-> a, outside <-> b, outside <-> a), so it is not extremely fast.

Link to this function

vector?(tensor)
vector?(tensor()) :: boolean()

Returs true if the tensor is a 1-order Tensor, which is also known as a Vector.

Link to this function

with_coordinates(tensor)
with_coordinates(tensor()) :: tensor()

Returns a new tensor, where all values are {list_of_coordinates, value} tuples.

Note that this new tuple is always dense, as the coordinates of all values are different. The identity is changed to {:identity, original_identity}.

Link to this function

with_coordinates(tensor, coordinates)