Skip to content

Commit 4d03255

Browse files
committed
add scientific notation
1 parent 3a79ee7 commit 4d03255

File tree

2 files changed

+47
-16
lines changed

2 files changed

+47
-16
lines changed

src/arraytex/api.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Main package API."""
2+
23
import re
34
from typing import Any
45
from typing import Optional
@@ -12,40 +13,51 @@ def to_matrix(
1213
arr: NDArray[Any],
1314
style: str = "b",
1415
num_format: Optional[str] = None,
16+
scientific_notation: bool = False,
1517
) -> str:
1618
"""Convert a numpy.NDArray to LaTeX matrix.
1719
1820
Args:
1921
arr: the array to be converted
2022
style: a style formatter string, either "b" for "bmatrix" or "p" for "pmatrix"
2123
num_format: a number formatter string, e.g. ".2f"
24+
scientific_notation: a flag to determine whether 1 x 10^3 should be used,
25+
otherwise e-notation is used (1e3)
2226
2327
Returns:
2428
the string representation of the array
2529
"""
26-
27-
def num_formatter(x: Union[np.int64, np.float64, np.float32]) -> str:
28-
return f"%{num_format}" % x
29-
3030
environment = f"{style}matrix"
3131

3232
formatter = {}
3333
if num_format:
34+
35+
def num_formatter(x: Union[np.int64, np.float64, np.float32]) -> str:
36+
return f"%{num_format}" % x
37+
3438
formatter.update({"float_kind": num_formatter, "int_kind": num_formatter})
3539

3640
lines = (
37-
np.array2string(arr, max_line_width=np.inf, formatter=formatter) # type: ignore # noqa
41+
np.array2string(
42+
arr,
43+
max_line_width=np.inf, # type: ignore # noqa
44+
formatter=formatter, # type: ignore # noqa
45+
separator=" & ",
46+
)
3847
.replace("[", "")
3948
.replace("]", "")
49+
.replace(" &\n", "\n")
4050
)
4151

4252
if num_format and "e" in num_format:
4353
pattern = r"e(-?\d+)"
4454
replace = r"\\mathrm{e}{\g<1>}"
55+
if scientific_notation:
56+
replace = r" \\times 10^{\g<1>}"
4557
lines = re.sub(pattern, replace, lines)
4658

4759
rv = [f"\\begin{{{environment}}}"]
48-
rv += [" " + " & ".join(line.split()) + r" \\" for line in lines.splitlines()]
60+
rv += [line.strip() + r" \\" for line in lines.splitlines()]
4961
rv += [f"\\end{{{environment}}}"]
5062

5163
return "\n".join(rv)

tests/test_api.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ def test_default(self) -> None:
2121
assert (
2222
out
2323
== r"""\begin{bmatrix}
24-
1 & 2 & 3 \\
25-
4 & 5 & 6 \\
24+
1 & 2 & 3 \\
25+
4 & 5 & 6 \\
2626
\end{bmatrix}"""
2727
)
2828

@@ -40,8 +40,8 @@ def test_style(self) -> None:
4040
assert (
4141
out
4242
== r"""\begin{pmatrix}
43-
1 & 2 & 3 \\
44-
4 & 5 & 6 \\
43+
1 & 2 & 3 \\
44+
4 & 5 & 6 \\
4545
\end{pmatrix}"""
4646
)
4747

@@ -59,13 +59,13 @@ def test_decimal_format(self) -> None:
5959
assert (
6060
out
6161
== r"""\begin{bmatrix}
62-
1.00 & 2.00 & 3.00 \\
63-
4.00 & 5.00 & 6.00 \\
62+
1.00 & 2.00 & 3.00 \\
63+
4.00 & 5.00 & 6.00 \\
6464
\end{bmatrix}"""
6565
)
6666

67-
def test_scientific_notation(self) -> None:
68-
"""Scientific notation can be selected."""
67+
def test_e_notation(self) -> None:
68+
"""Scientific E notation can be selected."""
6969
mat = np.array(
7070
[
7171
[0.001, 0.002, 0.003],
@@ -78,7 +78,26 @@ def test_scientific_notation(self) -> None:
7878
assert (
7979
out
8080
== 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} \\
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} \\
83+
\end{bmatrix}"""
84+
)
85+
86+
def test_scientific_notation(self) -> None:
87+
"""Scientific can be selected."""
88+
mat = np.array(
89+
[
90+
[0.001, 0.002, 0.003],
91+
[0.004, 0.005, 0.006],
92+
]
93+
)
94+
95+
out = to_matrix(mat, num_format=".2e", scientific_notation=True)
96+
97+
assert (
98+
out
99+
== r"""\begin{bmatrix}
100+
1.00 \times 10^{-03} & 2.00 \times 10^{-03} & 3.00 \times 10^{-03} \\
101+
4.00 \times 10^{-03} & 5.00 \times 10^{-03} & 6.00 \times 10^{-03} \\
83102
\end{bmatrix}"""
84103
)

0 commit comments

Comments
 (0)