Skip to main content

Linear algebra for humans: a very good vector-geometry and linear-algebra toolbelt

Project description


version python version license build docs build code style

A very good vector-geometry and linear-algebra toolbelt. Linear algebra for humans. Simple NumPy operations made readable, built to scale from prototyping to production.

See the complete API reference:


Normalize a stack of vectors:

# 😮
vs_norm = vs / np.linalg.norm(vs, axis=1)[:, np.newaxis]

# 😀
vs_norm = vg.normalize(vs)

Check for the zero vector:

# 😣
is_almost_zero = np.allclose(v, np.array([0.0, 0.0, 0.0]), rtol=0, atol=1e-05)

# 🤓
is_almost_zero = vg.almost_zero(v, atol=1e-05)

Find the major axis of variation (first principal component):

# 😩
mean = np.mean(coords, axis=0)
_, _, pcs = np.linalg.svd(coords - mean)
first_pc = pcs[0]

# 😍
first_pc = vg.major_axis(coords)

Compute pairwise angles between two stacks of vectors:

# 😭
dot_products = np.einsum("ij,ij->i", v1s.reshape(-1, 3), v2s.reshape(-1, 3))
cosines = dot_products / np.linalg.norm(v1s, axis=1) / np.linalg.norm(v1s, axis=1)
angles = np.arccos(np.clip(cosines, -1.0, 1.0))

# 🤯
angles = vg.angle(v1s, v2s)


All functions are optionally vectorized, meaning they accept single inputs and stacks of inputs interchangeably. They return The Right Thing – a single result or a stack of results – without the need to reshape inputs or outputs. With the power of NumPy, the vectorized functions are fast.


pip install numpy vg


import numpy as np
import vg

projected = vg.scalar_projection(
  np.array([5.0, -3.0, 1.0]),

Design principles

Linear algebra is useful and it doesn't have to be dificult to use. With the power of abstractions, simple operations can be made simple, without poring through lecture slides, textbooks, inscrutable Stack Overflow answers, or dense NumPy docs. Code that uses linear algebra and geometric transformation should be readable like English, without compromising efficiency.

These common operations should be abstracted for a few reasons:

  1. If a developer is not programming linalg every day, they might forget the underlying formula. These forms are easier to remember and more easily referenced.

  2. These forms tend to be self-documenting in a way that the NumPy forms are not. If a developer is not programming linalg every day, this will again come in handy.

  3. These implementations are more robust. They automatically inspect ndim on their arguments, so they work equally well if the argument is a vector or a stack of vectors. They are more careful about checking edge cases like a zero norm or zero cross product and returning a correct result or raising an appropriate error.


This library adheres to Semantic Versioning.


This collection was developed at Body Labs by Paul Melnikow and extracted from the Body Labs codebase and open-sourced as part of blmath by Alex Weiss. blmath was subsequently forked by Paul Melnikow and later the vx namespace was broken out into its own package. The project was renamed to vg to resolve a name conflict.


The project is licensed under the two-clause BSD license.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for vg, version 1.10.0
Filename, size File type Python version Upload date Hashes
Filename, size vg-1.10.0-py3-none-any.whl (29.9 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size vg-1.10.0.tar.gz (15.7 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page