einops style multi dimensional meshgrids
Project description
einops-style multi dimensional meshgrids
Installation
Simple installation from uv:
uv add einmesh
or pip:
pip install einmesh
Features
- einops-style Meshgrid Generation: The core function
einmeshallows creating multi-dimensional meshgrids (liketorch.meshgrid) using a concise string pattern similar toeinops. - Flexible Space Definitions: Users define the dimensions using various "space" objects:
LinSpace: Linearly spaced points.LogSpace: Logarithmically spaced points.UniformDistribution: Points sampled from a uniform distribution.NormalDistribution: Points sampled from a normal distribution.
- Pattern Features:
- Named Dimensions: Pattern elements correspond to keyword arguments (e.g.,
einmesh("x y", x=LinSpace(...), y=LogSpace(...))). - Dimension Ordering: The order in the pattern determines the order/shape of the output tensors (using
ijindexing convention, like NumPy). - Stacking (
*): A*in the pattern stacks the individual coordinate tensors along a new dimension, returning a single tensor. - Grouping (
()): Parentheses group axes for rearrangement usingeinops.rearrange. - Duplicate Names: Handles patterns like
"x x y", which repeats an axis and re-samples it.
- Named Dimensions: Pattern elements correspond to keyword arguments (e.g.,
- Output:
- Returns a tuple of coordinate tensors if no
*is present. - Returns a single stacked tensor if
*is present.
- Returns a tuple of coordinate tensors if no
- Backend: Numpy, Torch and JAX are all supported! Just import the einmesh function from the backend like
from einmesh.numpy import einmesh # Creates numpy arrays
from einmesh.jax import einmesh # Creates JAX arrays
from einmesh.torch import einmesh # Creates Torch Tensors
Examples
Here are a few examples demonstrating how to use einmesh:
1. Basic 2D Linear Grid
Create a simple 2D grid with linearly spaced points along x and y.
from einmesh import LinSpace
from einmesh.numpy import einmesh
# Define the spaces
x_space = LinSpace(0, 1, 10) # 10 points from 0 to 1
y_space = LinSpace(-1, 1, 20) # 20 points from -1 to 1
# Create the meshgrid
# Output: tuple of two tensors, each with shape (10, 20) following 'ij' indexing
x_coords, y_coords = einmesh("x y", x=x_space, y=y_space)
print(f"{x_coords.shape=}")
print(f"{y_coords.shape=}")
# Output:
# x_coords.shape=(10, 20)
# y_coords.shape=(10, 20)
2. Stacked Coordinates
Create a 3D grid and stack the coordinate tensors into a single tensor.
x_space = LinSpace(0, 1, 5)
y_space = LinSpace(0, 1, 6)
z_space = LogSpace(1, 2, 7)
# Use '*' to stack the coordinates along the last dimension
# Output: single tensor with shape (5, 6, 7, 3)
coords = einmesh("x y z *", x=x_space, y=y_space, z=z_space)
print(coords.shape)
# Output: (5, 6, 7, 3)
# coords[..., 0] contains x coordinates
# coords[..., 1] contains y coordinates
# coords[..., 2] contains z coordinates
3. Using Distributions
Generate grid points by sampling from distributions.
from einmesh import UniformDistribution, NormalDistribution
# Sample 10 points uniformly between -5 and 5 for x
x_dist = UniformDistribution(-5, 5, 10)
# Sample 15 points from a normal distribution (mean=0, std=1) for y
y_dist = NormalDistribution(0, 1, 15)
# Create the meshgrid
# Output: tuple of two tensors, each with shape (10, 15)
x_samples, y_samples = einmesh("x y", x=x_dist, y=y_dist)
print(x_samples.shape, y_samples.shape)
# Output: (10, 15) (10, 15)
# Note: The points along each axis will not be sorted.
4. Duplicate Dimension Names
Use the same space definition for multiple axes.
space = LinSpace(0, 1, 5)
# 'x' space is used for both the first and second dimensions.
# Output shapes: (5, 5, 10) for each tensor
x0_coords, x1_coords, y_coords = einmesh("x x y", x=space, y=LinSpace(-1, 1, 10))
print(x0_coords.shape, x1_coords.shape, y_coords.shape)
# Output: (5, 5, 10) (5, 5, 10) (5, 5, 10)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file einmesh-0.1.2.tar.gz.
File metadata
- Download URL: einmesh-0.1.2.tar.gz
- Upload date:
- Size: 157.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0942e1ef6c25278efc79aca5affb8ce9b7426d9353dba7441ac838d230779112
|
|
| MD5 |
808be72d4368b7ed138146783178cea1
|
|
| BLAKE2b-256 |
3d7ee2a7b2715a0902ad4dc9a9a185b800adaf844c9944ed6a45f0b67bd6eeed
|
File details
Details for the file einmesh-0.1.2-py3-none-any.whl.
File metadata
- Download URL: einmesh-0.1.2-py3-none-any.whl
- Upload date:
- Size: 17.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b7233b15bb87b5878adc9563d50e963b0145ccd9943ec3c29f58129f12af9901
|
|
| MD5 |
a77357d738287c312fabf65f26ee8bf0
|
|
| BLAKE2b-256 |
05c7b25757b2a06349603b87bbb3d38ee209352190fd1dc0b375f611a0fc9584
|