Arrays, dtypes and axes¶
"""dtype rules, axis order, shape validation and the read-only contract.
Every assertion here restates a convention that the rest of the site relies
on, so a change to any of them breaks this file rather than only the prose.
"""
import numpy as np
from gragra import (
PointMass,
WeightedPointSource,
point_acceleration,
point_gravity_gradient,
)
# Coordinates are float64 tables of shape (N, 3): one row per source point,
# the component axis last.
positions = np.array([[0.0, 0.0, 0.0], [1.0, 0.0, 0.0]])
real_source = WeightedPointSource(positions_m=positions, weights_kg=[1.0, -1.0])
assert real_source.positions_m.dtype == np.float64
assert real_source.positions_m.shape == (2, 3)
# Weights keep the kind of number they were given: real stays float64, complex
# is promoted to complex128. Coordinates are float64 either way.
assert real_source.weights_kg.dtype == np.float64
complex_source = WeightedPointSource(
positions_m=positions, weights_kg=[1.0 + 0.5j, -1.0]
)
assert complex_source.weights_kg.dtype == np.complex128
assert complex_source.positions_m.dtype == np.float64
# Low-level numeric observables consume bare source-frame SI target arrays.
# The output dtype follows the weights, never the coordinates.
targets_xyz_m = [[0.0, 2.0, 0.0], [0.0, 0.0, 3.0]]
assert point_acceleration(real_source, targets_xyz_m).dtype == np.float64
assert point_acceleration(complex_source, targets_xyz_m).dtype == np.complex128
# A vector observable ends in one component axis of length 3, a tensor
# observable in two. The observation-point axis stays in front of them.
assert point_acceleration(real_source, targets_xyz_m).shape == (2, 3)
assert point_gravity_gradient(real_source, targets_xyz_m).shape == (2, 3, 3)
# Stored arrays are defensive copies, so a later edit of the caller's array
# cannot reach inside the frozen source ...
mutable_input = np.array([[4.0, 0.0, 0.0]])
frozen_source = WeightedPointSource(positions_m=mutable_input, weights_kg=[2.0])
mutable_input[0, 0] = 99.0
assert frozen_source.positions_m[0, 0] == 4.0
assert frozen_source.positions_m.flags.writeable is False
assert real_source.weights_kg.flags.writeable is False
# ... and the arrays an observable hands back are read-only too.
assert point_acceleration(real_source, targets_xyz_m).flags.writeable is False
# A table of points has shape (N, 3). A bare (3,) array means "one point", and
# low-level observable functions accept that shorthand. Source dataclasses
# remain strict so a vector cannot be mistaken for a one-column table.
single = point_acceleration(PointMass(1.0, [0.0, 0.0, 0.0]), [5.0, 0.0, 0.0])
assert single.shape == (1, 3)
try:
WeightedPointSource(positions_m=[5.0, 0.0, 0.0], weights_kg=[1.0])
except ValueError as error:
assert "(N, 3)" in str(error)
else:
raise AssertionError("a (3,) array must not be accepted as source table")
dtypes¶
Coordinates and other geometric quantities are always float64. Amplitudes
and weights keep the kind of number they were given: a real-valued
weights_kg stays float64, a complex-valued one is promoted to
complex128. An observable's output dtype follows its weights, never the
coordinates — a real source and a complex source passed through the same
function produce float64 and complex128 results respectively.
Axis order¶
Component and tensor axes¶
The component axis (for a vector observable) or the two tensor axes (for
point_gravity_gradient) are always trailing: a vector observable
returns shape (M, 3), a tensor observable (M, 3, 3), where M is the
number of observation points.
Frequency and direction axes¶
When a result carries a frequency or direction axis in addition to the
component axis, that axis is inserted before the component axis:
(M, Nf, 3) for a frequency axis, (M, Ndir, 3) for a direction axis. When
both are present, direction comes before frequency: (M, Ndir, Nf, 3). See
Coherent responses and cross statistics and
Waves for the functions that produce these shapes.
Read-only arrays¶
Two independent contracts, both enforced with setflags(write=False):
- Stored arrays. The
positions_mandweights_kgarrays held by a numerical source orTargetPoints.positions_mheld by the high-level target container are read-only after construction. Input arrays are copied, so later caller mutations do not reach the stored data. - Returned arrays. Every public observable function
(
point_potential,point_acceleration,point_gravity_gradient, and their coherent counterparts) returns a read-only array. If you need to modify a result, copy it first.
Shape validation¶
Numerical source dataclasses and high-level TargetPoints require a strict
(N, 3) position table. High-level targets also require a length Quantity,
unique IDs and a Frame; pass them to a model's .at(targets) method. The
low-level numerical observables accept bare source-frame SI coordinate arrays,
including a single (3,) point promoted to (1, 3) for convenience.
On-disk layout is not the in-memory shape¶
The axis-order rules above describe in-memory API shapes — the arrays
stored in dataclasses and returned by public functions. On-disk storage
(HDF5 field files, for example) follows a separate convention chosen for
I/O efficiency: a leading batch axis, e.g. displacement_m shaped
(P, Nv, 3) for P snapshots. Batch-conversion helpers (see
External field data and HDF5) accept this
leading-batch-axis form as an input convenience, but their return values
are always converted back to the in-memory convention above — component
axis trailing, parametric axes before it. Do not assume a leading batch
axis anywhere a return value from gragra's public API is documented.