Vectors
A vector is an ordered list of numbers. In panchi, Vector is the fundamental object representing a point or direction in n-dimensional space.
Construction
import panchi as pan
v = pan.Vector([1, 2, 3])
print(v) # [1, 2, 3]
print(v.dims) # 3
Vectors can only hold numbers (int or float). Passing anything else raises a TypeError with an explanation.
Arithmetic
Vectors support standard arithmetic with natural syntax:
a = pan.Vector([1, 2, 3])
b = pan.Vector([4, 5, 6])
print(a + b) # [5, 7, 9]
print(a - b) # [-3, -3, -3]
print(2 * a) # [2, 4, 6]
print(a / 2) # [0.5, 1.0, 1.5]
print(-a) # [-1, -2, -3]
All operations return a new Vector and leave the original unchanged.
Magnitude and normalization
The magnitude of a vector is its Euclidean length ā the square root of the sum of squared components.
v = pan.Vector([3, 4])
print(v.magnitude) # 5.0
print(v.normalize()) # [0.6, 0.8]
normalize() returns the unit vector pointing in the same direction. It raises ZeroDivisionError if the vector has zero magnitude.
Dot product
The dot product of two vectors measures how much they point in the same direction. It is defined as the sum of the products of corresponding components.
u = pan.Vector([1, 2, 3])
v = pan.Vector([4, 5, 6])
print(pan.dot(u, v)) # 32
If the dot product is zero, the vectors are orthogonal.
Cross product
The cross product is defined only for 3D vectors. It produces a new vector perpendicular to both inputs, with magnitude equal to the area of the parallelogram they span.
u = pan.Vector([1, 0, 0])
v = pan.Vector([0, 1, 0])
print(pan.cross(u, v)) # [0, 0, 1]
Factory functions
panchi provides several convenience functions for constructing common vectors:
pan.zero_vector(3) # [0, 0, 0]
pan.one_vector(3) # [1, 1, 1]
pan.unit_vector(3, 1) # [0, 1, 0] ā standard basis vector eā
pan.random_vector(3) # random entries
Indexing and iteration
Vectors are indexable and iterable:
v = pan.Vector([10, 20, 30])
print(v[0]) # 10
v[1] = 99 # mutation is supported
print(list(v)) # [10, 99, 30]
Conversion
v = pan.Vector([1, 2, 3])
v.to_list() # [1, 2, 3] ā returns an independent copy
v.to_tuple() # (1, 2, 3)