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
Functions
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
.
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)
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.
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},
Returns the dimensions of the tensor.
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
.
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
.
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
.
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.
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.
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.
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.
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
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.
Returs true if the tensor is a 2-order Tensor, which is also known as a Matrix.
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 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.
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.
Elementwise multiplication.
- If both
a
andb
are Tensors, the same as callingmul_tensor/2
. - If one of
a
orb
is any kind of number, the same as callingmul_number/2
.
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
.
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.
TODO: Solve this, maybe find a nicer way to create tensors.
Returns the order of the Tensor.
This is 1 for Vectors, 2 for Matrices, etc. It is the amount of dimensions the tensor has.
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│
└ ┘
>
"
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.
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.
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
.
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)
Elementwise substraction of the tensor_b
from tensor_a
.
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.
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.
This is done in three steps (outside <-> a, outside <-> b, outside <-> a), so it is not extremely fast.
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.
Note that this new tuple is always dense, as the coordinates of all values are different.
The identity is changed to {:identity, original_identity}
.