NumPy for humans: a very good vector-geometry and linear-algebra toolbelt
Project description
vg
NumPy for humans: a very good vector-geometry and linear-algebra toolbelt.
vg makes code more readable
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)
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)
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)
Features
See the complete API reference: https://vgpy.readthedocs.io/en/latest/
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.
normalizenormalizes a vector.sprojcomputes the scalar projection of one vector onto another.projcomputes the vector projection of one vector onto another.rejectcomputes the vector rejection of one vector from another.reject_axiszeros or squashes one component of a vector.magnitudecomputes the magnitude of a vector.anglecomputes the unsigned angle between two vectors.signed_anglecomputes the signed angle between two vectors.almost_zerotests if a vector is almost the zero vector.almost_collineartests if two vectors are almost collinear.pad_with_onesadds a column of ones.unpadstrips off a column (e.g. of ones).apply_homogeneousapplies a transformation matrix using homogeneous coordinates.principal_componentscomputes principal components of a set of coordinates.major_axisreturns the first one.
Installation
pip install numpy vg
Usage
import numpy as np
import vg
projected = vg.sproj(np.array([5.0, -3.0, 1.0]), onto=vg.basis.neg_y)
Motivation
Linear algebra is useful but 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:
-
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.
-
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.
-
These implementations are more robust. They automatically inspect
ndimon 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.
Acknowledgements
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.
License
The project is licensed under the two-clause BSD license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file vg-0.5.1.tar.gz.
File metadata
- Download URL: vg-0.5.1.tar.gz
- Upload date:
- Size: 15.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.9.1 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bf147c2c4cc2f3d7563f16026cb6cf750c88fdbd675e90532ffbb64d135531e6
|
|
| MD5 |
7f82157f274080b3d0920d1ba318fc4f
|
|
| BLAKE2b-256 |
5a5186048e6af2bb9e44be049913156864c7875f13c54f0c8e76b1d814995b00
|