Skip to content

Commit d6d0f2f

Browse files
committed
check for too many dimensions
1 parent 6091771 commit d6d0f2f

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Inspired by @josephcslater's [array_to_latex](https://github.com/josephcslater/a
4040

4141
- Support for different environment delimiters (`bmatrix` or `pmatrix`) and different
4242
number formatters (`:.2f`, `:.3e`, etc.).
43+
- Fully tested and typed.
4344

4445
## Requirements
4546

src/arraytex/api.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import numpy as np
99
from numpy.typing import NDArray
1010

11+
from .errors import TooManyDimensionsError
12+
1113

1214
def to_matrix(
1315
arr: NDArray[Any],
@@ -26,7 +28,13 @@ def to_matrix(
2628
2729
Returns:
2830
the string representation of the array
31+
32+
Raises:
33+
TooManyDimensionsError: when the supplied array has more than 2 dimensions
2934
"""
35+
if len(arr.shape) > 2:
36+
raise TooManyDimensionsError
37+
3038
environment = f"{style}matrix"
3139

3240
formatter = {}

src/arraytex/errors.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""Module to hold custom errors."""
2+
3+
4+
class TooManyDimensionsError(Exception):
5+
"""Raised when an array of more than 2 dimensions is supplied."""

tests/test_api.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""Tests for the main API."""
22
import numpy as np
3+
import pytest
34

45
from arraytex.api import to_matrix
6+
from arraytex.errors import TooManyDimensionsError
57

68

79
class TestToMatrix:
@@ -101,3 +103,36 @@ def test_scientific_notation(self) -> None:
101103
4.00 \times 10^{-03} & 5.00 \times 10^{-03} & 6.00 \times 10^{-03} \\
102104
\end{bmatrix}"""
103105
)
106+
107+
def test_one_d(self) -> None:
108+
"""One dimensional vectors are handled correctly."""
109+
mat = np.array([1, 2, 3])
110+
111+
out = to_matrix(mat)
112+
113+
assert (
114+
out
115+
== r"""\begin{bmatrix}
116+
1 & 2 & 3 \\
117+
\end{bmatrix}"""
118+
)
119+
120+
def test_0_d(self) -> None:
121+
"""0 dimensional vectors are handled correctly."""
122+
mat = np.array(1)
123+
124+
out = to_matrix(mat)
125+
126+
assert (
127+
out
128+
== r"""\begin{bmatrix}
129+
1 \\
130+
\end{bmatrix}"""
131+
)
132+
133+
def test_3_d(self) -> None:
134+
""">2 dimensional arrays are handled correctly."""
135+
mat = np.arange(8).reshape(2, 2, 2)
136+
137+
with pytest.raises(TooManyDimensionsError):
138+
to_matrix(mat)

0 commit comments

Comments
 (0)