tensor v0.7.2 Tensor
Summary
Functions
Adds number or tensor b
to tensor a
.
If you know beforehand that b
will always be a number, use add_number/2
instead.
If you know beforehand that b
will always be a tensor, use add_tensor/2
instead
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
Divides tensor a
by number or tensor b
.
If you know beforehand that b
will always be a number, use div_number/2
instead.
If you know beforehand that b
will always be a tensor, use div_tensor/2
instead
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
Multiplies number or tensor b
with tensor a
.
If you know beforehand that b
will always be a number, use mul_number/2
instead.
If you know beforehand that b
will always be a tensor, use mul_tensor/2
instead
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
Subtracts number or tensor b
to tensor a
.
If you know beforehand that b
will always be a number, use sub_number/2
instead.
If you know beforehand that b
will always be a tensor, use sub_tensor/2
instead
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
Adds number or tensor b
to tensor a
.
If you know beforehand that b
will always be a number, use add_number/2
instead.
If you know beforehand that b
will always be a tensor, use add_tensor/2
instead.
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.
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},
Specs
dimensions(tensor) :: [non_neg_integer]
Returns the dimensions of the tensor.
Divides tensor a
by number or tensor b
.
If you know beforehand that b
will always be a number, use div_number/2
instead.
If you know beforehand that b
will always be a tensor, use div_tensor/2
instead.
Divides all elements of Tensor a
by the number b
.
Elementwise division of tensor_a
by tensor_b
.
Specs
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.
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.
Specs
get(tensor, integer, any) :: any
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.
Specs
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.
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.
Specs
matrix?(tensor) :: boolean
Returs true if the tensor is a 2-order Tensor, which is also known as a Matrix.
Specs
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.
Multiplies number or tensor b
with tensor a
.
If you know beforehand that b
will always be a number, use mul_number/2
instead.
If you know beforehand that b
will always be a tensor, use mul_tensor/2
instead.
Multiplies all elements of Tensor a
with the number b
.
Elementwise multiplication of the tensor_a
with tensor_b
.
Specs
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.
Specs
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.
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.
Subtracts number or tensor b
to tensor a
.
If you know beforehand that b
will always be a number, use sub_number/2
instead.
If you know beforehand that b
will always be a tensor, use sub_tensor/2
instead.
Subtracts the number b
from all elements in Tensor a
.
Elementwise substraction of the tensor_b
from tensor_a
.
Specs
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.
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.
Specs
vector?(tensor) :: boolean
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}
.