vector v0.2.0 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
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 A⨯B
Returns the norm (magnitude) of the cross product of two vectors A⨯B
Divide a vector by scalar value s
Returns the dot product of two vectors A⨯B
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
Functions
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}
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
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
Returns the cross product of two vectors A⨯B
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}
Returns the norm (magnitude) of the cross product of two vectors A⨯B
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
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}
Returns the dot product of two vectors A⨯B
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
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
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}
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
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
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
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
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}