Skip to content

vector

An infinite-dimensional vector Python package.

>>> from vector import vecadd
>>> vecadd((1, 2), (4, 5, 6))
(5, 7, 6)
>>> 
>>> from vector import Vector
>>> v = Vector((1, 2))
>>> w = Vector((4, 5, 6))
>>> v + w
Vector(5, 7, 6, ...)
>>> 
>>> from vector import vecnpadd
>>> vecnpadd((1, 2), ((3, 4, 5),
...                   (6, 7, 8)))
array([[4, 6, 5],
       [7, 9, 8]])

Installation

pip install git+https://github.com/goessl/vector.git

Usage

This package includes

  • general-purpose functions (prefixed vec...) in pure Python with perfect complexity,
  • lazy generators (prefixed vecl...),
  • a clean class (Vector) with easy to use syntax,
  • improved numpy-routines (prefixed vecnp...) for parallelised operations &
  • tensor functions (prefixed ten...) for multiaxis operations

to handle type-independent, infinite-dimensional vectors. It operates on vectors of different lengths, treating them as infinite-dimensional by assuming that all components after the given ones are zero.

All vectors are zero-indexed.

Operation Functional Lazy Object-oriented Parallelised Multiaxis
Creation
Zero constant veczero veclzero Vector.ZERO vecnpzero tenzero
Basis vecbasis veclbasis Vector vecnpbasis tenbasis
Random uniform vecrand veclrand Vector.rand vecnprand tenrand
Random normal vecrandn veclrandn Vector.randn vecnprandn tenrandn
Utility
Dimensionality veclen len vecnpdim tendim
Rank tenrank
Comparison veceq vecleq == vecnpeq
Trimming vectrim vecltrim .trim vecnptrim tentrim
Rounding vecround veclround .round vecnpround tenround
Right shift vecrshift veclrshift >>
Left shift veclshift vecllshift <<
Hilbert space
Conjugation vecconj veclconj
Norm vecabs abs vecnpabs
Norm squared vecabsq .absq vecnpabsq
Inner product vecdot @ vecnpdot
Parallelism vecparallel .is_parallel vecnpparallel
Vector space
Positive vecpos veclpos + vecnppos tenpos
Negative vecneg veclneg - vecnpneg tenneg
Addition vecadd vecladd + vecnpadd tenadd
Basis addition vecaddc vecladdc .addc tenaddc
Subtraction vecsub veclsub - vecnpsub tensub
Basis subtraction vecsubc veclsubc
Multiplication vecmul veclmul * vecnpmul tenmul
True division vectruediv vecltruediv / vecnptruediv tentruediv
Floor division vecfloordiv veclfloordiv // vecnpfloordiv tenfloordiv
Mod vecmod veclmod % vecnpmod tenmod
Divmod vecdivmod vecldivmod divmod tendivmod
Elementwise
Multiplication vechadamard veclhadamard .hadamard tenhadamard
True division vechadamardtruediv veclhadamardtruediv .hadamardtruediv tenhadamardtruediv
Floor division vechadamardfloordiv veclhadamardfloordiv .hadamardfloordiv tenhadamardfloordiv
Mod vechadamardmod veclhadamardmod .hadamardmod tenhadamardmod
Divmod vechadamarddivmod veclhadamarddivmod
Min vechadamardmin veclhadamardmin .hadamardmin
Max vechadamardmax veclhadamardmax .hadamardmax

Design choices

  1. Integers are the best. As many functions as possible should work with pure integer arithmetic.
  2. Floats are necessary. (Also let's don't forget about complex numbers.) When possible, extended precision intermediates are used (sum, sumprod, ...)
  3. Python allows operator overloading. Exclusive type arithmetic should be possible (zero, one & inf arguments; ...)

Prefix Design

Could use no prefix to be more mathematically pure, like add instead of vecadd, but then you would always have to use from vec import add as vecadd if used with other libraries (like operator).

Also avoids keyword collisions (abs is reserved, vecabs isn't).

Do it like numpy.polynomial.polynomial. ....

Roadmap

  • zip version between zip & zip_longest. Yields different sized tuples. Done: goessl/zipvar
  • vecdivmod
  • docstrings
  • numpy routines
  • multiaxis vectors: tensors?
  • Absolute type safety.
  • Complexity analysis. Perfect complexity
  • argument checks
  • dimensionality signature (e.g. vecadd: \(\mathbb{K}^m\times\mathbb{K}^n\to\mathbb{K}^{\max{m, n}}\))
  • vechadamardminmax
  • never use numpy.int64, they don't detect overflows
  • sparse vectors (dicts)
  • C++ & Java version
  • Ballin
  • Fields medal

License (MIT)

Copyright (c) 2022-2025 Sebastian Gössl

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.