Skip to content

Commit

Permalink
tools: leave 1d tp spaces unchanged
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfikl committed Oct 11, 2024
1 parent 3a9eb2e commit 514d1ba
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion modepy/test/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ def test_tensor_product_shapes():

# {{{ tensor product reshape

@pytest.mark.parametrize("dim", [2, 3])
@pytest.mark.parametrize("dim", [1, 2, 3])
def test_tensor_product_reshape(dim):
interval = mp.Simplex(1)
shape = mp.TensorProductShape((interval,) * dim)
Expand Down
18 changes: 13 additions & 5 deletions modepy/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ def reshape(


def reshape_array_for_tensor_product_space(
space: TensorProductSpace, ary: ReshapeableT, axis=-1) -> ReshapeableT:
space: TensorProductSpace, ary: ReshapeableT, axis: int = -1) -> ReshapeableT:
"""Return a reshaped view of *ary* that exposes the tensor product nature
of the space. Axis number *axis* of *ary* must index coefficients
corresponding to a tensor-product-structured basis (e.g. modal or nodal
Expand All @@ -489,13 +489,21 @@ def reshape_array_for_tensor_product_space(
function along a given dimension will be represented by variation
of array entries along the corresponding array axis.
"""

ndim = len(ary.shape)
if axis < 0:
axis += len(ary.shape)
if not (0 <= axis < len(ary.shape)):
raise ValueError("invalid axis specified")
axis += ndim

if not (0 <= axis < ndim):
raise ValueError(f"Invalid axis specified: {axis} not in 0..{ndim}")

if ary.shape[axis] != space.space_dim:
raise ValueError(f"array's axis {axis} must have length "
raise ValueError(f"The input array's axis {axis} must have length "
f"{space.space_dim}, found {ary.shape[axis]} instead")

if space.spatial_dim == 1:
return ary

return ary.reshape(
(ary.shape[:axis]
+ tuple(s.space_dim for s in space.bases)
Expand Down

0 comments on commit 514d1ba

Please sign in to comment.