Vectors is a simple library toolkit dealing with common vector logic in the 3-dimensional space.
pip install vectors
- Initialization
- To points
- Magnitude
- Addition
- Subtract
- Multiplication
- Dot Product
- Cross/Scalar Product
- Unit Vector
- Angle Theta
- Rotate
- Parallel, Perpendicular, Non-Parallel
There are multiple ways to create our vector instances using the vectors module.
from vectors import Vector
v1 = Vector(1, 2, 3) #=> Vector(1, 2, 3)
from vectors import Vector
components = [1.2, 2.4, 3.8]
v = Vector.from_list(components) #=> Vector(1.2, 2.4, 3.8)
Vectors package contains also Point
class that allow to create vector from to points.
from vectors import Point, Vector
p1 = Point(1, 2, 6) #=> Point(1, 2, 6)
p2 = Point(2, 0, 2) #=> Point(2, 0, 2)
v = Vector.from_points(p1, p2) #=> Vector(1, -2, -4)
Using spherical coordinates in the r, theta, phi space.
from vectors import Vector
r, theta, phi = 1, 0, 0
v = Vector.spherical(r, theta, phi) #=> Vector(0.0, 0.0, 1.0)
Using cylindrical coordinates in the r, theta, z space.
from vectors import Vector
r, theta, z = 1, 0, 0
v = Vector.cylindrical(r, theta, z) #=> Vector(1.0, 0.0, 0)
We can also get access to the vector array to use it with other libraries.
from vectors import Vector
v = Vector(1,2,3)
v.to_points() #=> [1, 2, 3]
We can get the magnitude of the vector easily.
from vectors import Vector
v = Vector(2,0,0)
v.magnitude() #==> 2.0
v1 = Vector(1,2,3)
v1.magnitude() #==> 3.7416573867739413
v2 = Vector(2,4,6)
v2.magnitude() #==> 7.483314773547883
We can add a real number to a vector or compute the vector sum of two vectors as follows.
from vectors import Vector
v1 = Vector(1,2,3)
v2 = Vector(2,4,6)
v1.add(2) #=> Vector(3.0, 4.0, 5.0)
v1 + 2 #=> Vector(3.0, 4.0, 5.0)
v1.sum(v2) #=> Vector(3.0, 6.0, 9.0)
v1 + v2 #=> Vector(3.0, 6.0, 9.0)
Both methods return a Vector instance.
We can subtract a real number from a vector or compute the vector subtraction of two vectors as follows.
from vectors import Vector
v1 = Vector(1,2,3)
v2 = Vector(2,4,6)
v1.subtract(2) #=> Vector(-1, 0 ,-1)
v1 - 2 #=> Vector(-1, 0, -1)
v1.subtract(v2) #=> Vector(-1, -2, -3)
v1 - v2 #=> Vector(-1, -2, -3)
Both methods return a Vector instance.
We can multiply a vector by a real number.
from vectors import Vector
v1 = Vector(1,2,3)
v2 = Vector(2,4,6)
v1.multiply(4) #=> Vector(4.0, 8.0, 12.0)
# TODO: add * operator for real numbers
The above returns a Vector instance.
We can find the dot product of two vectors.
from vectors import Vector
v1 = Vector(1,2,3)
v2 = Vector(2,4,6)
v1.dot(v2) #=> 28
We can also use angle theta on the dot function.
from vectors import Vector
v1 = Vector(1,2,3)
v2 = Vector(2,4,6)
# TODO: This does not work
v1.dot(v2, 180)
Dot product returns a real number.
We can find the cross product of two vectors.
from vectors import Vector
v1 = Vector(1,2,3)
v2 = Vector(2,4,6)
v1.cross(v2) #=> Vector(0, 0, 0)
Cross product returns a Vector instance, which is always perpendicular to the other two vectors.
We can find the unit vector of a given vector.
from vectors import Vector
v1 = Vector(1,2,3)
v1.unit() #=> Vector(0.267261241912, 0.534522483825, 0.801783725737)
Unit vector function returns a Vector instance that has a magnitude of 1.
We can also find the angle theta between two vectors.
from vectors import Vector
v1 = Vector(1,2,3)
v2 = Vector(2,4,6)
v1.angle(v2) #=> 0.0
Angle is a measured in degrees.
We can also rotate vector by given angle.
from vectors import Vector
import math
v = Vector(1,0,0)
v1.angle(math.pi/2) #=> Vector(6.123233995736766e-17, 1.0, 0)
It does not correct problem of floating numbers.
Angle is provided in radians.
We can check if two vectors are parallel, perpendicular or non-parallel to each other.
from vectors import Vector
v1 = Vector(1,2,3)
v2 = Vector(2,4,6)
v1.parallel(v2) #=> True
v1.perpendicular(v2) #=> False
v1.non_parallel(v2) #=> False
All of the above return either True or False.
#TODO
- Change to named tuple?
- Implement sequence protocol
- Floating point fix
- Fix dot with theta value
- Tests
- Generalize methods so it is easier to inherit
- Vector2D? Vector4D?
- Allow for * operator in multiplication with real numbers
- Allow for initialization with key arguments
- x, y, z unit vectors
- visualization / demo ?
- Check for optimization without loosing simplicity
- Standardize for radians/degrees
- Logo?
- Force to update package in pip
I'm looking for collaborators, so if you have something interesting, feel free to collaborate.