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.
Callback implementation for FunLand.Reducable.reduce/3
.
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
tensor()
(opaque)
tensor()
tensor()
Link to this section Functions
abs(tensor)
Returns the Tensor where all elements are converted to their absolute values.
add(a, b)
Elementwise addition.
- If both
a
andb
are Tensors, the same as callingadd_tensor/2
. - If one of
a
orb
is any kind of number, the same as callingadd_number/2
.
add_number(a, b)
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(tensor_a, tensor_b)
Elementwise addition of the tensor_a
and tensor_b
.
dense_map_with_coordinates(tensor, fun)
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()]
dimensions(tensor()) :: [non_neg_integer()]
Returns the dimensions of the tensor.
div(a, b)
Elementwise multiplication.
- If both
a
andb
are Tensors, the same as callingdiv_tensor/2
. - If one of
a
orb
is any kind of number, the same as callingdiv_number/2
.
div_number(a, b)
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(tensor_a, tensor_b)
Elementwise division of tensor_a
by tensor_b
.
do_map(tensor_contents, list, fun, new_identity)
fetch(tensor, index)
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.
from_slices(list_of_slices)
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.
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
get(tensor, index, default)
Returns the element at index
from tensor
. If index
is out of bounds, returns default
.
get_and_update(tensor, key, fun)
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)
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)
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)
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)
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.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()
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 usingtensor_b
's identity, with two arguments:tensor_a_val, tensor_b_identity
- When a certain location is occupied in
tensor_b
,fun
is called usingtensor_a
's identity, with two arguments:tensor_a_identity, tensor_b_val
- When a certain location is occupied in both
tensor_a
andtensor_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(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 usingtensor_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 usingtensor_a
's identity, with three arguments:coords_list, tensor_a_identity, tensor_b_val
- When a certain location is occupied in both
tensor_a
andtensor_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.
mult(a, b)
Elementwise multiplication.
- If both
a
andb
are Tensors, the same as callingmult_tensor/2
. - If one of
a
orb
is any kind of number, the same as callingmult_number/2
.
mult_number(a, b)
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
.
mult_tensor(tensor_a, tensor_b)
Elementwise multiplication of the tensor_a
with tensor_b
.
new(nested_list_of_values, dimensions \\ nil, identity \\ 0)
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()
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)
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│
└ ┘
>
"
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.
reduce(tensor, acc, fun)
Callback implementation for FunLand.Reducable.reduce/3
.
slices(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)
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
andb
are Tensors, the same as callingsub_tensor/2
. - If one of
a
orb
is any kind of number, the same as callingsub_number/2
.
sub_number(a, b)
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(tensor_a, tensor_b)
Elementwise substraction of the tensor_b
from tensor_a
.
to_list(tensor)
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.
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}
transpose(tensor, dimension_b_index)
transpose(tensor(), non_neg_integer()) :: tensor()
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()
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)
Returs true if the tensor is a 1-order Tensor, which is also known as a Vector.
with_coordinates(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}
.