-
Notifications
You must be signed in to change notification settings - Fork 393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for numpy NDarray method. Added corresponding tests #135
Changes from 3 commits
e37a2bf
9b0d0c4
274cf22
2af25d4
f79c1a8
12fe732
1e9daba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,13 @@ | |
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import math\n", | ||
"import latexify" | ||
"import latexify\n", | ||
"import numpy as np" | ||
] | ||
}, | ||
{ | ||
|
@@ -165,11 +166,57 @@ | |
"\n", | ||
"solve" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/latex": [ | ||
"$$ \\displaystyle \\mathrm{numpy}(a) = \\begin{bmatrix} {1} & {2} & {3} & {4} \\\\ {5} & {6} & {7} & {8} \\\\ {9} & {10} & {11} & {12} \\end{bmatrix} $$" | ||
], | ||
"text/plain": [ | ||
"<latexify.frontend.LatexifiedFunction at 0x7f76c00f9990>" | ||
] | ||
}, | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"@latexify.function\n", | ||
"def numpy(a):\n", | ||
" return np.ndarray([1,2,3,4],[5,6,7,8],[9,10,11,12])\n", | ||
"\n", | ||
"numpy" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unnecessary metadata. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed kernelspec from metadata. |
||
"display_name": "Python 3.10.8 64-bit", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"name": "python" | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.8" | ||
}, | ||
"vscode": { | ||
"interpreter": { | ||
"hash": "8a94588eda9d64d9e9a351ab8144e55b1fabf5113b54e67dd26a8c27df0381b3" | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
from __future__ import annotations | ||
|
||
import math | ||
import numpy as np | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This import requires additional dev dependency in pyproject.toml. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi Odashi, I added numpy version in pyproject.toml, let me know if the version is suitable for the project. |
||
from collections.abc import Callable | ||
from typing import Any | ||
|
||
|
@@ -330,3 +331,12 @@ def solve(x): | |
|
||
latex = r"\mathrm{solve}(x) = x" | ||
_check_function(solve, latex) | ||
|
||
|
||
def test_generate_ndarray() -> None: | ||
def solve(a): | ||
"""A 2x3 numpy matrix""" | ||
return np.ndarray([1, 2, 3], [4, 5, 6]) | ||
|
||
latex = "\\mathrm{solve}(a) = \\begin{bmatrix} {1} & {2} & {3} \\\\ {4} & {5} & {6} \\end{bmatrix}" | ||
_check_function(solve, latex) |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -4,7 +4,7 @@ | |||
|
||||
import ast | ||||
import textwrap | ||||
|
||||
import numpy as np | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed numpy import from function_codegen_test.py |
||||
import pytest | ||||
|
||||
from latexify import exceptions, test_utils | ||||
|
@@ -744,3 +744,18 @@ def test_use_set_symbols_compare(code: str, latex: str) -> None: | |||
tree = ast.parse(code).body[0].value | ||||
assert isinstance(tree, ast.Compare) | ||||
assert function_codegen.FunctionCodegen(use_set_symbols=True).visit(tree) == latex | ||||
|
||||
|
||||
def test_generate_ndarrays(): | ||||
tree = ast.parse( | ||||
textwrap.dedent( | ||||
""" | ||||
def numpy(a): | ||||
return np.ndarray([1,2,3],[4,5,6]) | ||||
""" | ||||
) | ||||
).body[0] | ||||
|
||||
latex = "\\mathrm{numpy}(a) = \\begin{bmatrix} {1} & {2} & {3} \\\\ {4} & {5} & {6} \\end{bmatrix}" | ||||
assert isinstance(tree, ast.FunctionDef) | ||||
assert FunctionCodegen().visit(tree) == latex |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function name "numpy" is weird as it may overrides the package name (in some cases). Something else (e.g.,
myarray
) is preferred.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@odashi I renamed function into myNdarray.