Tensorex.get_and_update

You're seeing just the function get_and_update, go back to Tensorex module for more information.
Link to this function

get_and_update(tensor, indices, fun)

View Source

Specs

get_and_update(
  t(),
  [integer() | Range.t(), ...],
  (t() -> :pop | {any(), t()}) | (number() -> :pop | {any(), number()})
) :: {any(), t()}

Returns a tensor or a number stored at the index and update it at the same time.

iex> get_and_update_in(
...>   Tensorex.from_list([[[ 1   ,   -3.1,   2  ],
...>                        [ 4   ,    5  ,  -6.1],
...>                        [ 0.9 ,  -91.2,  11  ]],
...>                       [[10   ,  -30.1,  20  ],
...>                        [40   ,   50  , -60.1],
...>                        [ 0.09, -910.2, 110  ]]])[[0, 1, 0]], &{&1, &1 * 3.5})
{4, %Tensorex{data: %{[0, 0, 0] =>  1   , [0, 0, 1] =>   -3.1, [0, 0, 2] =>   2  ,
                      [0, 1, 0] => 14.0 , [0, 1, 1] =>    5  , [0, 1, 2] =>  -6.1,
                      [0, 2, 0] =>  0.9 , [0, 2, 1] =>  -91.2, [0, 2, 2] =>  11  ,
                      [1, 0, 0] => 10   , [1, 0, 1] =>  -30.1, [1, 0, 2] =>  20  ,
                      [1, 1, 0] => 40   , [1, 1, 1] =>   50  , [1, 1, 2] => -60.1,
                      [1, 2, 0] =>  0.09, [1, 2, 1] => -910.2, [1, 2, 2] => 110  }, shape: [2, 3, 3]}}

iex> get_and_update_in(
...>   Tensorex.from_list([[ 1,  2,  3],
...>                       [ 4,  5,  6],
...>                       [ 7,  8,  9],
...>                       [10, 11, 12]])[[1..2, 1..2]],
...>   &{&1, Tensorex.from_list([[13, 14],
...>                             [15, 16]])})
{%Tensorex{data: %{[0, 0] => 5, [0, 1] => 6,
                   [1, 0] => 8, [1, 1] => 9}, shape: [2, 2]},
 %Tensorex{data: %{[0, 0] =>  1, [0, 1] =>  2, [0, 2] =>  3,
                   [1, 0] =>  4, [1, 1] => 13, [1, 2] => 14,
                   [2, 0] =>  7, [2, 1] => 15, [2, 2] => 16,
                   [3, 0] => 10, [3, 1] => 11, [3, 2] => 12}, shape: [4, 3]}}

iex> get_and_update_in(
...>   Tensorex.from_list([[ 1,  2,  3],
...>                       [ 4,  5,  6],
...>                       [ 7,  8,  9],
...>                       [10, 11, 12]])[[2]],
...>   &{&1, Tensorex.from_list([0, 0, 16])})
{%Tensorex{data: %{[0] => 7, [1] => 8, [2] => 9}, shape: [3]},
 %Tensorex{data: %{[0, 0] =>  1, [0, 1] =>  2, [0, 2] =>  3,
                   [1, 0] =>  4, [1, 1] =>  5, [1, 2] =>  6,
                                               [2, 2] => 16,
                   [3, 0] => 10, [3, 1] => 11, [3, 2] => 12}, shape: [4, 3]}}

iex> get_and_update_in(
...>   Tensorex.from_list([[ 1,  2],
...>                       [ 3,  4]])[[0..-1, 0..-1]],
...>   &{&1, Tensorex.from_list([[-2,  0],
...>                             [ 0, -3]])})
{%Tensorex{data: %{[0, 0] =>  1, [0, 1] =>  2,
                   [1, 0] =>  3, [1, 1] =>  4}, shape: [2, 2]},
 %Tensorex{data: %{[0, 0] => -2,
                                 [1, 1] => -3}, shape: [2, 2]}}

iex> get_and_update_in(
...>   Tensorex.zero([3, 2])[[1..-1, 1..-1]],
...>   &{&1, Tensorex.from_list([[ 1],
...>                             [-1]])})
{%Tensorex{data: %{}, shape: [2, 1]},
 %Tensorex{data: %{[1, 1] =>  1,
                   [2, 1] => -1}, shape: [3, 2]}}