tensor v1.1.0 Tensor

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

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

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 tensor as a nested list of values

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

Types

tensor()

Functions

abs(tensor)

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

add(a, b)

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.
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)

add_tensor(tensor1, tensor2)
add_tensor(tensor, tensor) :: tensor

Elementwise addition of the tensor_a and tensor_b.

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},

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

Returns the dimensions of the tensor.

div(a, b)

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.
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.

div_tensor(tensor1, tensor2)
div_tensor(tensor, tensor) :: tensor

Elementwise division of tensor_a by tensor_b.

do_dense_map_with_coordinates(tensor, list, fun, coordinates)
do_map(tensor_contents, list, fun, new_identity)
do_sparse_map_with_coordinates(tensor_contents, list, fun, coordinates, new_identity)
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 bare 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.

from_slices(list_of_values)
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.

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

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

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.

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.

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

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.

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

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

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

merge_with_index(arg1, arg2, 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.

minus(tensor)

Returns the Tensor where all elements have been negated.

mul(a, b)

Elementwise multiplication.

  • If both a and b are Tensors, the same as calling mul_tensor/2.
  • If one of a or b is any kind of number, the same as calling mul_number/2.
mul_number(a, b)
mul_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.

mul_tensor(tensor1, tensor2)
mul_tensor(tensor, tensor) :: tensor

Elementwise multiplication of the tensor_a with tensor_b.

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.

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.

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│
└                 ┘
>
"
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.

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.

sub(a, b)

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.
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)

sub_tensor(tensor1, tensor2)
sub_tensor(tensor, tensor) :: tensor

Elementwise substraction of the tensor_b from tensor_a.

to_list(tensor)
to_list(tensor) :: list

Converts the tensor as a nested list of values.

For a Vector, returns a list of values For a Matrix, returns a list of lists of values For an order-3 Tensor, returns a list of lists of lists of values. Etc.

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

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

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.

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

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

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}.

with_coordinates(tensor, coordinates)