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...),
  • in-place modifications (prefixed veci...),
  • dicts as sparse vectors (prefixed vecs...),
  • tensor functions (prefixed ten...) for multilinear operations,
  • sparse tensor functions (prefixed tens...) for multilinear sparse operations &
  • improved numpy-routines (prefixed vecnp...) for parallelised operations.

Functional, sparse and multilinear sparse additionally contain wrapper classes.

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 Dense Lazy In-place Sparse Multilinear Multilinear sparse Parallelised
Creation
Zero constant veczero veclzero vecizero vecszero tenzero tenszero vecnpzero
Basis vecbasis veclbasis vecibasis vecsbasis tenbasis tensbasis vecnpbasis
Bases vecbases veclbases vecibases vecsbases
Random uniform vecrand veclrand vecirand vecsrand tenrand tensrand vecnprand
Random normal vecrandn veclrandn vecirandn vecsrandn tenrandn tensrandn vecnprandn
Conversion
to special type vecdtos
from special type vecstod
Utility
Dimensionality veclen tendim tensdim vecnpdim
Rank vecslen tenrank tensrank
Comparison veceq vecleq vecseq tenseq vecnpeq
Trimming vectrim vecltrim vecstrim tentrim tenstrim vecnptrim
in-place vecitrim vecsitrim tensitrim
Right shift vecrshift veclrshift vecsrshift tenrshift tensrshift
in-place vecirshift vecsirshift
Left shift veclshift vecllshift vecslshift tenlshift tenslshift
in-place vecilshift vecsilshift
Hilbert space
Conjugation vecconj veclconj vecsconj tenconj tensconj
in-place veciconj vecsiconj tensiconj
Norm vecabs vecsabs vecnpabs
Norm squared vecabsq vecsabsq vecnpabsq
Inner product vecdot vecsdot vecnpdot
Vector space
Positive vecpos veclpos vecspos tenpos tenspos vecnppos
in-place vecipos vecsipos tensipos
Negative vecneg veclneg vecsneg tenneg tensneg vecnpneg
in-place vecineg vecsineg tensineg
Addition vecadd vecladd vecsadd tenadd tensadd vecnpadd
in-place veciadd vecsiadd tensiadd
Basis addition vecaddc vecladdc vecsaddc tenaddc tensaddc
in-place veciaddc vecsiaddc tensiaddc
Subtraction vecsub veclsub vecssub tensub tenssub vecnpsub
in-place vecisub vecsisub tensisub
Basis subtraction vecsubc veclsubc vecssubc tensubc tenssubc
in-place vecisubc vecsisubc tensisubc
Multiplication vecrmul veclrmul vecsrmul tenrmul tensrmul
from left vecmul veclmul vecsmul tenmul tensmul vecnpmul
in-place vecimul vecsimul tensimul
True division vectruediv vecltruediv vecstruediv tentruediv tenstruediv vecnptruediv
in-place vecitruediv vecsitruediv tensitruediv
Floor division vecfloordiv veclfloordiv vecsfloordiv tenfloordiv tensfloordiv vecnpfloordiv
in-place vecifloordiv vecsifloordiv tensifloordiv
Mod vecmod veclmod vecstruediv tenmod tensmod vecnpmod
in-place vecimod vecsitruediv tensimod
Divmod vecdivmod vecldivmod vecsdivmod tendivmod tensdivmod
Elementwise
Multiplication vechadamard veclhadamard vecihadamard vecshadamard tenhadamard tenshadamard
True division vechadamardtruediv veclhadamardtruediv vecihadamardtruediv vecshadamardtruediv tenhadamardtruediv tenshadamardtruediv
Floor division vechadamardfloordiv veclhadamardfloordiv vecihadamardfloordiv vecshadamardfloordiv tenhadamardfloordiv tenshadamardfloordiv
Mod vechadamardmod veclhadamardmod vecihadamardmod vecshadamardmod tenhadamardmod tenshadamardmod
Divmod vechadamarddivmod veclhadamarddivmod vecshadamarddivmod tenhadamarddivmod tenshadamarddivmod
Min vechadamardmin veclhadamardmin vecshadamardmin tenhadamardmin tenshadamardmin
Max vechadamardmax veclhadamardmax vecshadamardmax tenhadamardmax tenshadamardmax

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
  • multilinear vectors: tensors?
  • Absolute type safety.
  • Complexity analysis. Perfect complexity
  • dimensionality signature (e.g. vecadd: \(\mathbb{K}^m\times\mathbb{K}^n\to\mathbb{K}^{\max{m, n}}\))
  • sparse vectors (dicts)
  • in-place
  • veceq to veccmp?
  • argument checks
  • lp-norms & metrics
  • vechadamardminmax
  • never use numpy.int64, they don't detect overflows
  • 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.