matrix_reloaded v2.0.0 MatrixReloaded.Vector

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

Link to this section Types

Link to this type

column()
column() :: [[number()]]

Link to this section Functions

Link to this function

add(vec1, vec2)
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)
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]
Link to this function

col(size, val \\ 0)
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

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

iex> MatrixReloaded.Vector.col(3, 4)
[[4], [4], [4]]
Link to this function

dot(vec1, vec2)
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

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

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