matrix_reloaded v2.2.1 MatrixReloaded.Vector View Source

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

Link to this section Functions

Link to this function

add(vec1, vec2) View Source
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

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
alternate_seq(t(), number(), pos_integer()) :: t()

Create row vector of alternating sequence of numbers.

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]

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

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

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

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

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

inner_product(vec1, vec2) View Source
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

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

mult_by_num(vec, val) View Source
mult_by_num(t() | column(), number()) :: t() | column()

Multiply a vector by number.

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
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

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]
    ]
  }
Link to this function

row(size, val \\ 0) View Source
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

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

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

The size of the vector.

Returns a positive integer.

Example:

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

iex> MatrixReloaded.Vector.col(4, -1) |> MatrixReloaded.Vector.size()
4
Link to this function

sub(vec1, vec2) View Source
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

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

transpose(vec) View Source
transpose(t() | column()) :: column() | t()

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

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
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:

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
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:

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]}