Vector (vector v1.1.0)

A library of two- and three-dimensional vector operations. All vectors are represented as tuples with either two or three elements.

examples

Examples

iex> # Vector Tripple Product Identity
...> a = {2, 3, 1}
...> b = {1, 4, -2}
...> c = {-1, 2, 1}
...> Vector.equal?(
...>   Vector.cross(Vector.cross(a, b), c),
...>   Vector.subtract(Vector.multiply(b, Vector.dot(a, c)), Vector.multiply(a, Vector.dot(b, c))))
true

Link to this section Summary

Functions

Adds two vectors

Returns the basis vector for the given axis

Returns the scalar component for the axis given

Returns the cross product of two vectors AB

Returns the norm (magnitude) of the cross product of two vectors AB

Divide a vector by scalar value s

Returns the dot product of two vectors AB

Compares two vectors for equality, with an optional tolerance

Multiply a vector by scalar value s

Returns the norm (magnitude) of a vector

Returns the square of the norm norm (magnitude) of a vector

Returns a new coordinate by projecting a given length distance from coordinate start along vector

Reverses a vector

Subtract vector B from vector A. Equivalent to Vector.add(A, Vector.revers(B))

Returns the unit vector parallel ot the given vector. This will raise an ArithmeticError if a zero-magnitude vector is given. Use unit_safe if there is a chance that a zero-magnitude vector will be sent.

Returns the unit vector parallel ot the given vector, but will handle the vectors {0, 0} and {0, 0, 0} by returning the same vector

Link to this section Types

@type location() :: {number(), number()} | {number(), number(), number()}
@type vector() :: {number(), number()} | {number(), number(), number()}

Link to this section Functions

Link to this function

add(arg1, arg2)

@spec add(vector(), vector()) :: vector()

Adds two vectors

examples

Examples

iex> Vector.add({3, -4}, {2, 1})
{5,-3}
iex> Vector.add({-2, 0, 5}, {0, 0, 0})
{-2, 0, 5}
iex> Vector.add({2, 1, -2}, Vector.reverse({2, 1, -2}))
{0, 0, 0}
@spec basis(atom()) :: vector()

Returns the basis vector for the given axis

examples

Examples

iex> Vector.basis(:x)
{1, 0, 0}
iex> Vector.basis(:y)
{0, 1, 0}
iex> Vector.component(Vector.basis(:y), :y)
1
Link to this function

component(arg, atom)

@spec component(vector(), atom()) :: number()

Returns the scalar component for the axis given

examples

Examples

iex> Vector.component({3, -4}, :y)
-4
iex> Vector.component({-6, 0, 8}, :z)
8
iex> Vector.component({1, -2}, :z)
0
iex> Vector.component(Vector.basis(:x), :z)
0
Link to this function

cross(arg1, arg2)

@spec cross(vector(), vector()) :: vector()

Returns the cross product of two vectors AB

examples

Examples

iex> Vector.cross({2, 3}, {1, 4})
{0, 0, 5}
iex> Vector.cross({2, 2, -1}, {1, 4, 2})
{8, -5, 6}
iex> Vector.cross({3, -3, 1}, {4, 9, 2})
{-15, -2, 39}
Link to this function

cross_norm(arg1, arg2)

@spec cross_norm(vector(), vector()) :: number()

Returns the norm (magnitude) of the cross product of two vectors AB

examples

Examples

iex> Vector.cross_norm({2, 3}, {1, 4})
5
iex> Vector.cross_norm({1, 4}, {2, 2})
6
iex> Vector.cross_norm({2, 0, -1}, {0, 3, 3})
9.0
iex> Float.floor(:math.pow(Vector.cross_norm({2, 2, -1}, {1, 4, 2}), 2))
125.0
@spec divide(vector(), number()) :: vector()

Divide a vector by scalar value s

examples

Examples

iex> Vector.divide({3, -4}, 2.5)
{1.2, -1.6}
iex> Vector.divide({-2, 0, 5}, -2)
{1.0, 0.0, -2.5}
Link to this function

dot(arg1, arg2)

@spec dot(vector(), vector()) :: number()

Returns the dot product of two vectors AB

examples

Examples

iex> Vector.dot({2, 3}, {1, 4})
14
iex> Vector.dot({1, 4}, {2, 2})
10
iex> Vector.dot({2, 0, -1}, {0, 3, 3})
-3
Link to this function

equal?(a, b, tolerance \\ 0.0)

@spec equal?(vector(), vector(), number()) :: boolean()

Compares two vectors for equality, with an optional tolerance

examples

Examples

iex> Vector.equal?({3, -4}, {3, -4})
true
iex> Vector.equal?({3, -4}, {3.0001, -3.9999})
false
iex> Vector.equal?({3, -4}, {3.0001, -3.9999}, 0.001)
true
iex> Vector.equal?({3, -4, 1}, {3.0001, -3.9999, 1.0}, 0.001)
true
Link to this function

multiply(arg, s)

@spec multiply(vector(), number()) :: vector()

Multiply a vector by scalar value s

examples

Examples

iex> Vector.multiply({3, -4}, 2.5)
{7.5, -10.0}
iex> Vector.multiply({-2, 0, 5}, -2)
{4, 0, -10}
@spec norm(vector()) :: number()

Returns the norm (magnitude) of a vector

examples

Examples

iex> Vector.norm({3, 4})
5.0
iex> Vector.norm({-1, 0})
1
iex> Vector.norm({0, -2, 0})
2
Link to this function

norm_squared(arg)

@spec norm_squared(vector()) :: number()

Returns the square of the norm norm (magnitude) of a vector

examples

Examples

iex> Vector.norm_squared({3, 4})
25
iex> Vector.norm_squared({1, 0})
1
iex> Vector.norm_squared({2, 0, -1})
5
iex> Vector.norm_squared({-2, 3, 1})
14
Link to this function

project(vector, start, distance)

@spec project(vector(), location(), number()) :: location()

Returns a new coordinate by projecting a given length distance from coordinate start along vector

examples

Examples

iex> Vector.project({3, -4}, {-1, 1}, 4)
{1.4, -2.2}
iex> Vector.project({-6, 0, 8}, {1, -2, 0.4}, 2.5)
{-0.5, -2.0, 2.4}
iex> Vector.project({-2, 1, 3}, {0, 0, 0}, 2.5) |> Vector.norm()
2.5
@spec reverse(vector()) :: vector()

Reverses a vector

examples

Examples

iex> Vector.reverse({3, -4})
{-3, 4}
iex> Vector.reverse({-2, 0, 5})
{2, 0, -5}
iex> Vector.cross_norm({-2, 3, 5}, Vector.reverse({-2, 3, 5}))
0
@spec subtract(vector(), vector()) :: vector()

Subtract vector B from vector A. Equivalent to Vector.add(A, Vector.revers(B))

examples

Examples

iex> Vector.subtract({3, -4}, {2, 1})
{1,-5}
iex> Vector.subtract({-2, 0, 5}, {-3, 1, 2})
{1, -1, 3}
@spec unit(vector()) :: vector()

Returns the unit vector parallel ot the given vector. This will raise an ArithmeticError if a zero-magnitude vector is given. Use unit_safe if there is a chance that a zero-magnitude vector will be sent.

examples

Examples

iex> Vector.unit({3, 4})
{0.6, 0.8}
iex> Vector.unit({8, 0, 6})
{0.8, 0.0, 0.6}
iex> Vector.unit({-2, 0, 0})
{-1.0, 0.0, 0.0}
iex> Vector.unit({0, 0, 0})
** (ArithmeticError) bad argument in arithmetic expression
@spec unit_safe(vector()) :: vector()

Returns the unit vector parallel ot the given vector, but will handle the vectors {0, 0} and {0, 0, 0} by returning the same vector

examples

Examples

iex> Vector.unit_safe({3, 4})
{0.6, 0.8}
iex> Vector.unit_safe({0, 0})
{0, 0}
iex> Vector.unit_safe({0, 0, 0})
{0, 0, 0}