Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Unary & binary ops

Part 2: Safe, Labeled Arithmetic (Unary & Binary Ops)

This notebook demonstrates how coordax provides safe, dimension-aware arithmetic operations that automatically handle broadcasting and alignment.

2.1 Unit Conversion (Unary Operations)

Converting temperature from Kelvin to Celsius while preserving coordinate metadata.

Temperature (K): ('latitude', 'longitude'), shape: (16, 32)
Temperature (C): ('latitude', 'longitude')
Coordinates preserved: ['latitude', 'longitude']
Coordinates preserved: True

More complex unary operations

All operations preserve dims: True

Custom unary transformation with cmap

Normalized range: [0.00, 1.00]
Shape: (16, 32), Dims: ('latitude', 'longitude')

2.2 Applying a Time-Dependent Correction (Binary Operations)

Binary operations automatically broadcast over matching named dimensions.

Data: ('time', 'sensor'), shape: (50, 5)

Binary operation with automatic broadcasting

Corrected: ('time', 'sensor'), shape: (50, 5)
Calibrated: ('time', 'sensor'), shape: (50, 5)
Fully corrected: ('time', 'sensor'), shape: (50, 5)

Automatic alignment with different dimension orders

Coordax automatically aligns dimensions when operands have different axis orderings.

Result dims: ('time', 'sensor'), shape: (50, 5)

2.3 Subtracting the Zonal Mean (Advanced Binary Operations)

A common operation in climate science: removing the longitudinal mean to study anomalies.

Temperature field: ('time', 'latitude', 'longitude'), shape: (6, 16, 32)

Compute and subtract the zonal mean

Zonal mean: ('time', 'latitude'), shape: (6, 16)
Anomaly: ('time', 'latitude', 'longitude'), shape: (6, 16, 32)
Anomaly zonal mean (should be ~0): max=7.15e-06

Temporal mean and global spatial mean

Temporal mean: ('latitude', 'longitude'), shape: (16, 32)
Temporal anomaly: ('time', 'latitude', 'longitude'), shape: (6, 16, 32)
Global mean: ('time',), shape: (6,)

Key Patterns Summary

  1. Unary operations (field * 2, field - 273.15, cx.cmap(jnp.abs)(field)) — preserve dims/coords
  2. Binary operations automatically broadcast over matching named dimensions
  3. Automatic alignment — different dimension orderings are handled without manual transpose
  4. Reductions — field.untag('dim') then cx.cmap(lambda x: jnp.mean(x, axis=...))(untagged)