vector v1.0.1 Vector

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

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

Link to this type

location()
location() :: {number(), number()} | {number(), number(), number()}

Link to this type

vector()
vector() :: {number(), number()} | {number(), number(), number()}

Link to this section Functions

Link to this function

add(arg1, arg2)
add(vector(), vector()) :: vector()

Adds two vectors

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

basis(atom)
basis(atom()) :: vector()

Returns the basis vector for the given axis

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)
component(vector(), atom()) :: number()

Returns the scalar component for the axis given

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)
cross(vector(), vector()) :: vector()

Returns the cross product of two vectors AB

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)
cross_norm(vector(), vector()) :: number()

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

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

divide(arg, s)
divide(vector(), number()) :: vector()

Divide a vector by scalar value s

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)
dot(vector(), vector()) :: number()

Returns the dot product of two vectors AB

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)
equal?(vector(), vector(), number()) :: boolean()

Compares two vectors for euqality, with an optional tolerance

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)
multiply(vector(), number()) :: vector()

Multiply a vector by scalar value s

Examples

iex> Vector.multiply({3, -4}, 2.5)
{7.5, -10.0}
iex> Vector.multiply({-2, 0, 5}, -2)
{4, 0, -10}
Link to this function

norm(arg)
norm(vector()) :: number()

Returns the norm (magnitude) of a vector

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)
norm_squared(vector()) :: number()

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

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)
project(vector(), location(), number()) :: location()

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

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

reverse(arg)
reverse(vector()) :: vector()

Reverses a vector

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

subtract(a, b)
subtract(vector(), vector()) :: vector()

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

Examples

iex> Vector.subtract({3, -4}, {2, 1})
{1,-5}
iex> Vector.subtract({-2, 0, 5}, {-3, 1, 2})
{1, -1, 3}

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

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

unit_safe(v)
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

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}