Skip to content

Commit d222664

Browse files
committed
add number formatting options
1 parent 90144f6 commit d222664

File tree

2 files changed

+87
-7
lines changed

2 files changed

+87
-7
lines changed

src/arraytex/api.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,51 @@
11
"""Main package API."""
2-
2+
import re
33
from typing import Any
4+
from typing import Optional
5+
from typing import Union
46

57
import numpy as np
68
from numpy.typing import NDArray
79

810

9-
def to_matrix(arr: NDArray[Any]) -> str:
11+
def to_matrix(
12+
arr: NDArray[Any],
13+
style: str = "b",
14+
num_format: Optional[str] = None,
15+
) -> str:
1016
"""Convert a numpy.NDArray to LaTeX matrix.
1117
1218
Args:
1319
arr: the array to be converted
20+
style: a style formatter string, either "b" for "bmatrid" or "p" for "pmatrix"
21+
num_format: a number formatter string, e.g. ".2f"
1422
1523
Returns:
1624
the string representation of the array
1725
"""
18-
environment = "bmatrix"
26+
27+
def num_formatter(x: Union[int, float]) -> str:
28+
return f"%{num_format}" % x
29+
30+
environment = f"{style}matrix"
31+
32+
formatter = {}
33+
if num_format:
34+
formatter.update({"float_kind": num_formatter, "int_kind": num_formatter})
35+
1936
lines = (
20-
np.array2string(arr, max_line_width=np.inf) # type: ignore # noqa
37+
np.array2string(arr, max_line_width=np.inf, formatter=formatter) # type: ignore # noqa
2138
.replace("[", "")
2239
.replace("]", "")
23-
.splitlines()
2440
)
41+
42+
if num_format and "e" in num_format:
43+
pattern = r"e(-?\d+)"
44+
replace = r"\\mathrm{e}{\g<1>}"
45+
lines = re.sub(pattern, replace, lines)
46+
2547
rv = [f"\\begin{{{environment}}}"]
26-
rv += [" " + " & ".join(line.split()) + r" \\" for line in lines]
48+
rv += [" " + " & ".join(line.split()) + r" \\" for line in lines.splitlines()]
2749
rv += [f"\\end{{{environment}}}"]
50+
2851
return "\n".join(rv)

tests/test_api.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class TestToMatrix:
88
"""Tests for the `to_matrix` function."""
99

10-
def test_success(self) -> None:
10+
def test_default(self) -> None:
1111
"""A formatted matrix is returned."""
1212
mat = np.array(
1313
[
@@ -23,5 +23,62 @@ def test_success(self) -> None:
2323
== r"""\begin{bmatrix}
2424
1 & 2 & 3 \\
2525
4 & 5 & 6 \\
26+
\end{bmatrix}"""
27+
)
28+
29+
def test_style(self) -> None:
30+
"""An arg can be supplied to determine the style of the returned matrix."""
31+
mat = np.array(
32+
[
33+
[1, 2, 3],
34+
[4, 5, 6],
35+
]
36+
)
37+
38+
out = to_matrix(mat, style="p")
39+
40+
assert (
41+
out
42+
== r"""\begin{pmatrix}
43+
1 & 2 & 3 \\
44+
4 & 5 & 6 \\
45+
\end{pmatrix}"""
46+
)
47+
48+
def test_decimal_format(self) -> None:
49+
"""Decimal style of number formatting can be selected."""
50+
mat = np.array(
51+
[
52+
[1, 2, 3],
53+
[4, 5, 6],
54+
]
55+
)
56+
57+
out = to_matrix(mat, num_format=".2f")
58+
59+
assert (
60+
out
61+
== r"""\begin{bmatrix}
62+
1.00 & 2.00 & 3.00 \\
63+
4.00 & 5.00 & 6.00 \\
64+
\end{bmatrix}"""
65+
)
66+
67+
def test_scientific_notation(self) -> None:
68+
"""Scientific notation can be selected."""
69+
mat = np.array(
70+
[
71+
[0.001, 0.002, 0.003],
72+
[0.004, 0.005, 0.006],
73+
]
74+
)
75+
76+
out = to_matrix(mat, num_format=".2e")
77+
78+
assert (
79+
out
80+
== r"""\begin{bmatrix}
81+
1.00\mathrm{e}{-03} & 2.00\mathrm{e}{-03} & 3.00\mathrm{e}{-03} \\
82+
4.00\mathrm{e}{-03} & 5.00\mathrm{e}{-03} & 6.00\mathrm{e}{-03} \\
2683
\end{bmatrix}"""
2784
)

0 commit comments

Comments
 (0)