exalgebra v0.0.4 ExAlgebra.Vector
Functions that perform computations on vectors.
Summary
Functions
Computes the addition of two vectors. This is a new vector with entries equal to the sum of the pair of vector’s corresponding entries.
Examples
iex> ExAlgebra.Vector.add([1, 2, 3], [2, 3, 4])
[3, 5, 7]
Computes the angle between two vectors.
Examples
iex> ExAlgebra.Vector.angle([3, 4], [-8, 6])
1.5707963267948966
Computes an orthogonal basis from a set of linearly independent vectors.
Examples
iex> ExAlgebra.Vector.create_orthogonal_basis([[1, 2, 4, 0], [0, 1, 1, 0], [0, 3, 1, 4]])
[[1, 2, 4, 0],
[-0.2857142857142857, 0.4285714285714286, -0.1428571428571428, 0.0],
[0.666666666666667, 0.33333333333333315, -0.3333333333333328, 4.0]]
Creates an orthogonal vector from an input vector that is linearly independent to each vector in a set of linearly independent vectors.
Examples
iex> ExAlgebra.Vector.create_orthogonal_vector([0, 1, 1, 0], [[1, 2, 4, 0]])
[-0.2857142857142857, 0.4285714285714286, -0.1428571428571428, 0.0]
Computes an orthonormal basis from a set of linearly independent vectors. This uses the modified version of the Gram–Schmidt process.
Examples
iex> ExAlgebra.Vector.create_orthonormal_basis([[1, 1, 1], [2, 1, 0], [5, 1, 3]])
[[0.5773502691896258, 0.5773502691896258, 0.5773502691896258], [0.7071067811865475, 0.0, -0.7071067811865475], [0.4082482904638631, -0.8164965809277261, 0.4082482904638631]]
Computes the length of the line segment connecting a pair of vectors.
Examples
iex> ExAlgebra.Vector.distance([1, 2, 3], [4, 5, 6])
5.196152422706632
Computes the dot product of a pair of vectors. This is the sum of the products of the pair of vector’s corresponding entries.
Examples
iex> ExAlgebra.Vector.dot([1, 2, 3], [2, 3, 4])
20
Returns true if and only if a set of vectors are linearly independent.
Examples
iex> ExAlgebra.Vector.is_linearly_independent?([[1, 1, 1], [2, 1, 0], [5, 1, 3]])
true
Returns true if and only if a pair of vectors are orthogonal. This is equivalent to a pair of vectors being perpendicular in Euclidian space.
Examples
iex> ExAlgebra.Vector.is_orthogonal?([1, 1, 1], [-2, 1, 1])
true
Computes the magnitude of a vector. This is also known as the length of a vector.
Examples
iex> ExAlgebra.Vector.magnitude([1, 2, 3, 4])
5.477225575051661
Computes the normalization of a vector. This is a vector pointing in the same
direction, but with magnitude 1
.
Examples
iex> ExAlgebra.Vector.normalize([1, 2, 3, 4])
[0.18257418583505536, 0.3651483716701107, 0.5477225575051661, 0.7302967433402214]
Computes the multiple of a vector by a scalar value.
Examples
iex> ExAlgebra.Vector.scalar_multiply([1, 2, 3], 2.5)
[2.5, 5.0, 7.5]
Computes the scalar projection of u
onto v
. This is the length of the
orthogonal projection of u
onto v
.
Examples
iex> ExAlgebra.Vector.scalar_projection([4, 1], [2, 3])
3.05085107923876
Computes the square of the magnitude of a vector. This avoids the expensive square root operation.
Examples
iex> ExAlgebra.Vector.sqr_magnitude([1, 2, 3, 4])
30
Computes the subtraction of two vectors. This is a new vector with entries equal to the difference of the pair of vector’s corresponding entries.
Examples
iex> ExAlgebra.Vector.subtract([1, 2, 3], [2, 3, 4])
[-1, -1, -1]
Computes the vector projection of a pair of vectors u, v
. This is the
orthogonal projection of u
onto v
.
Examples
iex> ExAlgebra.Vector.vector_projection([0, 1, 1, 0], [1, 2, 4, 0])
[0.2857142857142857, 0.5714285714285714, 1.1428571428571428, 0.0]
Functions
Specs
add([number], [number]) :: [number]
Computes the addition of two vectors. This is a new vector with entries equal to the sum of the pair of vector’s corresponding entries.
Examples
iex> ExAlgebra.Vector.add([1, 2, 3], [2, 3, 4])
[3, 5, 7]
Specs
angle([number], [number]) :: number
Computes the angle between two vectors.
Examples
iex> ExAlgebra.Vector.angle([3, 4], [-8, 6])
1.5707963267948966
iex> ExAlgebra.Vector.angle([9, 2, 7], [4, 8, 10])
0.6672196386878
Specs
create_orthogonal_basis([[number]]) :: [[number]]
Computes an orthogonal basis from a set of linearly independent vectors.
Examples
iex> ExAlgebra.Vector.create_orthogonal_basis([[1, 2, 4, 0], [0, 1, 1, 0], [0, 3, 1, 4]])
[[1, 2, 4, 0],
[-0.2857142857142857, 0.4285714285714286, -0.1428571428571428, 0.0],
[0.666666666666667, 0.33333333333333315, -0.3333333333333328, 4.0]]
Specs
create_orthogonal_vector([number], [[number]]) :: [number]
Creates an orthogonal vector from an input vector that is linearly independent to each vector in a set of linearly independent vectors.
Examples
iex> ExAlgebra.Vector.create_orthogonal_vector([0, 1, 1, 0], [[1, 2, 4, 0]])
[-0.2857142857142857, 0.4285714285714286, -0.1428571428571428, 0.0]
Specs
create_orthonormal_basis([[number]]) :: [[number]]
Computes an orthonormal basis from a set of linearly independent vectors. This uses the modified version of the Gram–Schmidt process.
Examples
iex> ExAlgebra.Vector.create_orthonormal_basis([[1, 1, 1], [2, 1, 0], [5, 1, 3]])
[[0.5773502691896258, 0.5773502691896258, 0.5773502691896258], [0.7071067811865475, 0.0, -0.7071067811865475], [0.4082482904638631, -0.8164965809277261, 0.4082482904638631]]
Specs
distance([number], [number]) :: number
Computes the length of the line segment connecting a pair of vectors.
Examples
iex> ExAlgebra.Vector.distance([1, 2, 3], [4, 5, 6])
5.196152422706632
Specs
dot([number], [number]) :: number
Computes the dot product of a pair of vectors. This is the sum of the products of the pair of vector’s corresponding entries.
Examples
iex> ExAlgebra.Vector.dot([1, 2, 3], [2, 3, 4])
20
Specs
is_linearly_independent?([[number]]) :: boolean
Returns true if and only if a set of vectors are linearly independent.
Examples
iex> ExAlgebra.Vector.is_linearly_independent?([[1, 1, 1], [2, 1, 0], [5, 1, 3]])
true
iex> ExAlgebra.Vector.is_linearly_independent?([[2, 3, 5], [-1, -4, -10], [1, -2, -8]])
false
Specs
is_orthogonal?([number], [number]) :: boolean
Returns true if and only if a pair of vectors are orthogonal. This is equivalent to a pair of vectors being perpendicular in Euclidian space.
Examples
iex> ExAlgebra.Vector.is_orthogonal?([1, 1, 1], [-2, 1, 1])
true
iex> ExAlgebra.Vector.is_orthogonal?([1, 1, 1], [1, 1, 1])
false
Specs
magnitude([number]) :: number
Computes the magnitude of a vector. This is also known as the length of a vector.
Examples
iex> ExAlgebra.Vector.magnitude([1, 2, 3, 4])
5.477225575051661
Specs
normalize([number]) :: [number]
Computes the normalization of a vector. This is a vector pointing in the same
direction, but with magnitude 1
.
Examples
iex> ExAlgebra.Vector.normalize([1, 2, 3, 4])
[0.18257418583505536, 0.3651483716701107, 0.5477225575051661, 0.7302967433402214]
Specs
scalar_multiply([number], number) :: [number]
Computes the multiple of a vector by a scalar value.
Examples
iex> ExAlgebra.Vector.scalar_multiply([1, 2, 3], 2.5)
[2.5, 5.0, 7.5]
Specs
scalar_projection([number], [number]) :: number
Computes the scalar projection of u
onto v
. This is the length of the
orthogonal projection of u
onto v
.
Examples
iex> ExAlgebra.Vector.scalar_projection([4, 1], [2, 3])
3.05085107923876
Specs
sqr_magnitude([number]) :: number
Computes the square of the magnitude of a vector. This avoids the expensive square root operation.
Examples
iex> ExAlgebra.Vector.sqr_magnitude([1, 2, 3, 4])
30
Specs
subtract([number], [number]) :: [number]
Computes the subtraction of two vectors. This is a new vector with entries equal to the difference of the pair of vector’s corresponding entries.
Examples
iex> ExAlgebra.Vector.subtract([1, 2, 3], [2, 3, 4])
[-1, -1, -1]
Specs
vector_projection([number], [number]) :: [number]
Computes the vector projection of a pair of vectors u, v
. This is the
orthogonal projection of u
onto v
.
Examples
iex> ExAlgebra.Vector.vector_projection([0, 1, 1, 0], [1, 2, 4, 0])
[0.2857142857142857, 0.5714285714285714, 1.1428571428571428, 0.0]