File tree Expand file tree Collapse file tree 4 files changed +49
-0
lines changed
Expand file tree Collapse file tree 4 files changed +49
-0
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 88import numpy as np
99from numpy .typing import NDArray
1010
11+ from .errors import TooManyDimensionsError
12+
1113
1214def 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 = {}
Original file line number Diff line number Diff line change 1+ """Module to hold custom errors."""
2+
3+
4+ class TooManyDimensionsError (Exception ):
5+ """Raised when an array of more than 2 dimensions is supplied."""
Original file line number Diff line number Diff line change 11"""Tests for the main API."""
22import numpy as np
3+ import pytest
34
45from arraytex .api import to_matrix
6+ from arraytex .errors import TooManyDimensionsError
57
68
79class TestToMatrix :
@@ -101,3 +103,36 @@ def test_scientific_notation(self) -> None:
1011034.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 )
You can’t perform that action at this time.
0 commit comments