View Source MatrixReloaded.Vector (matrix_reloaded v2.3.0)

Provides a set of functions to work with vectors.

Mostly functions is written for a row vectors. So if you'll need a similar functionality even for a column vectors you can use transpose function on row vector.

Link to this section Summary

Functions

Addition of two a row vectors. These two vectors must have a same size. Otherwise you get an error message.

Create row vector of alternating sequence of numbers.

Create a column vector of the specified size. Default values of vector is set to 0. This value can be changed.

Scalar product of two a row vectors. These two vectors must have a same size. Otherwise you get an error message.

Inner product of two a row vectors. It produces a row vector where each element i, j is the product of elements i, j of the original two row vectors. These two vectors must have a same size. Otherwise you get an error message.

Multiply a vector by number.

Outer product of two a row vectors. It produces a matrix of dimension {m, n} where m and n are length (size) of row vectors. If input vectors aren't a row type you get an error message.

Create a row vector of the specified size. Default values of vector is set to 0. This value can be changed.

The size of the vector.

Subtraction of two a row vectors. These two vectors must have a same size. Otherwise you get an error message.

Convert (transpose) a row vector to column and vice versa.

Update row vector by given a row subvector (list) of numbers or just by one number. The vector elements you want to change are given by the index. The index is a non-negative integer and determines the position of the element in the vector.

Update row vector by given a row subvector (list) of numbers or by one number. The elements you want to change are given by the vector of indices. These indices must be a non-negative integers and determine the positions of the element in the vector.

Link to this section Types

@type column() :: [[number()]]
@type subvector() :: number() | t()
@type t() :: [number()]

Link to this section Functions

@spec add(t(), t()) :: Result.t(String.t(), t())

Addition of two a row vectors. These two vectors must have a same size. Otherwise you get an error message.

Returns result, it means either tuple of {:ok, vector} or {:error, "msg"}.

examples

Examples

iex> MatrixReloaded.Vector.add([1, 2, 3], [4, 5, 6])
{:ok, [5, 7, 9]}
Link to this function

alternate_seq(vec, val, step \\ 2)

View Source
@spec alternate_seq(t(), number(), pos_integer()) :: t()

Create row vector of alternating sequence of numbers.

examples

Examples

iex> MatrixReloaded.Vector.row(5) |> MatrixReloaded.Vector.alternate_seq(1)
[1, 0, 1, 0, 1]

iex> MatrixReloaded.Vector.row(7) |> MatrixReloaded.Vector.alternate_seq(1, 3)
[1, 0, 0, 1, 0, 0, 1]
@spec col(pos_integer(), number()) :: column()

Create a column vector of the specified size. Default values of vector is set to 0. This value can be changed.

Returns list of list number.

examples

Examples

iex> MatrixReloaded.Vector.col(3)
[[0], [0], [0]]

iex> MatrixReloaded.Vector.col(3, 4)
[[4], [4], [4]]
@spec dot(t(), t()) :: Result.t(String.t(), number())

Scalar product of two a row vectors. These two vectors must have a same size. Otherwise you get an error message.

Returns result, it means either tuple of {:ok, number} or {:error, "msg"}.

examples

Examples

iex> MatrixReloaded.Vector.dot([1, 2, 3], [4, 5, 6])
{:ok, 32}
Link to this function

inner_product(vec1, vec2)

View Source
@spec inner_product(t(), t()) :: Result.t(String.t(), t())

Inner product of two a row vectors. It produces a row vector where each element i, j is the product of elements i, j of the original two row vectors. These two vectors must have a same size. Otherwise you get an error message.

Returns result, it means either tuple of {:ok, vector} or {:error, "msg"}.

examples

Examples

iex> MatrixReloaded.Vector.inner_product([1, 2, 3], [4, 5, 6])
{:ok, [4, 10, 18]}
@spec mult_by_num(t() | column(), number()) :: t() | column()

Multiply a vector by number.

examples

Examples

iex> MatrixReloaded.Vector.row(3, 2) |> MatrixReloaded.Vector.mult_by_num(3)
[6, 6, 6]

iex> MatrixReloaded.Vector.col(3, 2) |> MatrixReloaded.Vector.mult_by_num(3)
[[6], [6], [6]]
Link to this function

outer_product(vec1, vec2)

View Source
@spec outer_product(t(), t()) :: Result.t(String.t(), MatrixReloaded.Matrix.t())

Outer product of two a row vectors. It produces a matrix of dimension {m, n} where m and n are length (size) of row vectors. If input vectors aren't a row type you get an error message.

Returns result, it means either tuple of {:ok, matrix} or {:error, "msg"}.

examples

Examples

iex> MatrixReloaded.Vector.outer_product([1, 2, 3, 4], [1, 2, 3])
{:ok,
    [
      [1, 2, 3],
      [2, 4, 6],
      [3, 6, 9],
      [4, 8, 12]
    ]
  }
@spec row(pos_integer(), number()) :: t()

Create a row vector of the specified size. Default values of vector is set to 0. This value can be changed.

Returns list of numbers.

examples

Examples

iex> MatrixReloaded.Vector.row(4)
[0, 0, 0, 0]

iex> MatrixReloaded.Vector.row(4, 3.9)
[3.9, 3.9, 3.9, 3.9]
@spec size(t()) :: non_neg_integer()

The size of the vector.

Returns a positive integer.

example

Example:

iex> MatrixReloaded.Vector.row(3) |> MatrixReloaded.Vector.size()
3

iex> MatrixReloaded.Vector.col(4, -1) |> MatrixReloaded.Vector.size()
4
@spec sub(t(), t()) :: Result.t(String.t(), t())

Subtraction of two a row vectors. These two vectors must have a same size. Otherwise you get an error message.

Returns result, it means either tuple of {:ok, vector} or {:error, "msg"}.

examples

Examples

iex> MatrixReloaded.Vector.sub([1, 2, 3], [4, 5, 6])
{:ok, [-3, -3, -3]}
@spec transpose(t() | column()) :: column() | t()

Convert (transpose) a row vector to column and vice versa.

examples

Examples

iex> MatrixReloaded.Vector.transpose([1, 2, 3])
[[1], [2], [3]]

iex(23)> MatrixReloaded.Vector.transpose([[1], [2], [3]])
[1, 2, 3]
Link to this function

update(vec, subvec, index)

View Source
@spec update(t(), number() | t(), non_neg_integer()) :: Result.t(String.t(), t())

Update row vector by given a row subvector (list) of numbers or just by one number. The vector elements you want to change are given by the index. The index is a non-negative integer and determines the position of the element in the vector.

Returns result, it means either tuple of {:ok, vector} or {:error, "msg"}.

example

Example:

iex> vec = 0..10 |> Enum.to_list()
iex> MatrixReloaded.Vector.update(vec, [0, 0, 0], 5)
{:ok, [0, 1, 2, 3, 4, 0, 0, 0, 8, 9, 10]}

iex> vec = 0..10 |> Enum.to_list()
iex> MatrixReloaded.Vector.update(vec, 0, 5)
{:ok, [0, 1, 2, 3, 4, 0, 6, 7, 8, 9, 10]}
Link to this function

update_map(vec, subvec, position_indices)

View Source
@spec update_map(t(), number() | t(), [non_neg_integer()]) ::
  Result.t(String.t(), t())

Update row vector by given a row subvector (list) of numbers or by one number. The elements you want to change are given by the vector of indices. These indices must be a non-negative integers and determine the positions of the element in the vector.

Returns result, it means either tuple of {:ok, vector} or {:error, "msg"}.

example

Example:

iex> vec = 0..10 |> Enum.to_list()
iex> MatrixReloaded.Vector.update_map(vec, [0, 0], [2, 7])
{:ok, [0, 1, 0, 0, 4, 5, 6, 0, 0, 9, 10]}

iex> vec = 0..10 |> Enum.to_list()
iex> MatrixReloaded.Vector.update_map(vec, 0, [2, 7])
{:ok, [0, 1, 0, 3, 4, 5, 6, 0, 8, 9, 10]}