Skip to content

Commit

Permalink
Merge pull request #22 from google/develop
Browse files Browse the repository at this point in the history
v0.0.7
  • Loading branch information
odashi authored Aug 21, 2020
2 parents aa2871b + 5c0a2a8 commit 28064fa
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 12 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Temporary files
.swp
temp
tmp

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down Expand Up @@ -135,4 +140,4 @@ dmypy.json
.pytype/

# Cython debug symbols
cython_debug/
cython_debug/
172 changes: 172 additions & 0 deletions examples/equation.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"import latexify"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-1.0\n",
"\\mathrm{solve}(a, b, c)\\triangleq \\frac{-b + \\sqrt{b^{2} - 4ac}}{2a}\n",
"\n"
]
},
{
"data": {
"text/latex": [
"$$ \\displaystyle \\mathrm{solve}(a, b, c)\\triangleq \\frac{-b + \\sqrt{b^{2} - 4ac}}{2a} $$"
],
"text/plain": [
"<latexify.core.with_latex.<locals>._LatexifiedFunction at 0x10c803100>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"@latexify.with_latex\n",
"def solve(a, b, c):\n",
" return (-b + math.sqrt(b**2 - 4*a*c)) / (2*a)\n",
"\n",
"print(solve(1, 4, 3))\n",
"print(solve)\n",
"print()\n",
"solve"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$$ \\displaystyle \\mathrm{sinc}(x)\\triangleq \\left\\{ \\begin{array}{ll} 1, & \\mathrm{if} \\ x=0 \\\\ \\frac{\\sin{\\left({x}\\right)}}{x}, & \\mathrm{otherwise} \\end{array} \\right. $$"
],
"text/plain": [
"<latexify.core.with_latex.<locals>._LatexifiedFunction at 0x10c84eaf0>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"@latexify.with_latex\n",
"def sinc(x):\n",
" if x == 0:\n",
" return 1\n",
" else:\n",
" return math.sin(x) / x\n",
"\n",
"sinc"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$$ \\displaystyle \\mathrm{fib}(x)\\triangleq \\left\\{ \\begin{array}{ll} 1, & \\mathrm{if} \\ x=0 \\\\ 1, & \\mathrm{if} \\ x=1 \\\\ \\mathrm{fib}\\left(x - 1\\right) + \\mathrm{fib}\\left(x - 2\\right), & \\mathrm{otherwise} \\end{array} \\right. $$"
],
"text/plain": [
"<latexify.core.with_latex.<locals>._LatexifiedFunction at 0x10c85ab50>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Elif or nested else-if are unrolled.\n",
"@latexify.with_latex\n",
"def fib(x):\n",
" if x == 0:\n",
" return 1\n",
" elif x == 1:\n",
" return 1\n",
" else:\n",
" return fib(x-1) + fib(x-2)\n",
"\n",
"fib"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$$ \\displaystyle \\mathrm{greek}({\\alpha}, {\\beta}, {\\gamma}, {\\Omega})\\triangleq {\\alpha}{\\beta} + \\Gamma\\left({{\\gamma}}\\right) + {\\Omega} $$"
],
"text/plain": [
"<latexify.core.with_latex.<locals>._LatexifiedFunction at 0x10c85ae20>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Some math symbols are converted automatically.\n",
"@latexify.with_latex\n",
"def greek(alpha, beta, gamma, Omega):\n",
" return alpha * beta + math.gamma(gamma) + Omega\n",
"\n",
"greek"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
20 changes: 10 additions & 10 deletions latexify/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def visit_Module(self, node):
return self.visit(node.body[0])

def visit_FunctionDef(self, node):
name_str = r'\operatorname{' + str(node.name) + '}'
name_str = r'\mathrm{' + str(node.name) + '}'
arg_strs = [self._parse_math_symbols(str(arg.arg)) for arg in node.args.args]
body_str = self.visit(node.body[0])
return name_str + '(' + ', '.join(arg_strs) + r')\triangleq ' + body_str
Expand All @@ -73,11 +73,11 @@ def visit_Call(self, node):
builtin_callees = {
'abs': (r'\left|{', r'}\right|'),
'math.acos': (r'\arccos{\left({', r'}\right)}'),
'math.acosh': (r'\operatorname{arccosh}{\left({', r'}\right)}'),
'math.acosh': (r'\mathrm{arccosh}{\left({', r'}\right)}'),
'math.asin': (r'\arcsin{\left({', r'}\right)}'),
'math.asinh': (r'\operatorname{arcsinh}{\left({', r'}\right)}'),
'math.asinh': (r'\mathrm{arcsinh}{\left({', r'}\right)}'),
'math.atan': (r'\arctan{\left({', r'}\right)}'),
'math.atanh': (r'\operatorname{arctanh}{\left({', r'}\right)}'),
'math.atanh': (r'\mathrm{arctanh}{\left({', r'}\right)}'),
'math.ceil': (r'\left\lceil{', r'}\right\rceil'),
'math.cos': (r'\cos{\left({', r'}\right)}'),
'math.cosh': (r'\cosh{\left({', r'}\right)}'),
Expand Down Expand Up @@ -105,7 +105,7 @@ def visit_Call(self, node):
else:
if callee_str.startswith('math.'):
callee_str = callee_str[5:]
lstr = r'\operatorname{' + callee_str + r'}\left('
lstr = r'\mathrm{' + callee_str + r'}\left('
rstr = r'\right)'

arg_strs = [self.visit(arg) for arg in node.args]
Expand Down Expand Up @@ -138,7 +138,7 @@ def _wrap(child):
if type(node.op) in reprs:
return reprs[type(node.op)]()
else:
return r'\operatorname{unknown\_uniop}(' + self.visit(node.operand) + ')'
return r'\mathrm{unknown\_uniop}(' + self.visit(node.operand) + ')'

def visit_BinOp(self, node):
priority = {
Expand Down Expand Up @@ -180,7 +180,7 @@ def _wrap(child):
if type(node.op) in reprs:
return reprs[type(node.op)]()
else:
return r'\operatorname{unknown\_binop}(' + _unwrap(l) + ', ' + _unwrap(r) + ')'
return r'\mathrm{unknown\_binop}(' + _unwrap(l) + ', ' + _unwrap(r) + ')'

def visit_Compare(self, node):
lstr = self.visit(node.left)
Expand All @@ -202,12 +202,12 @@ def visit_Compare(self, node):
return lstr + r'\equiv' + rstr

else:
return r'\operatorname{unknown\_comparator}(' + lstr + ', ' + rstr + ')'
return r'\mathrm{unknown\_comparator}(' + lstr + ', ' + rstr + ')'

def visit_BoolOp(self, node):
logic_operator = r'\lor ' if isinstance(node.op, ast.Or) \
else r'\land ' if isinstance(node.op, ast.And) \
else r' \operatorname{unknown\_operator} '
else r' \mathrm{unknown\_operator} '
# visit all the elements in the ast.If node recursively
return r'\left('+self.visit(node.values[0])+r'\right)'+logic_operator+r'\left('+self.visit(node.values[1])+r'\right)'

Expand Down Expand Up @@ -266,7 +266,7 @@ def _repr_latex_(self):
"""
Hooks into Jupyter notebook's display system.
"""
return self._str
return r'$$ \displaystyle ' + self._str + ' $$'

if len(args) == 1 and callable(args[0]):
return _LatexifiedFunction(args[0])
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def main():

setuptools.setup(
name='latexify-py',
version='0.0.6',
version='0.0.7',
description='Generates LaTeX source from Python functions.',
long_description=readme,
long_description_type='text/markdown',
Expand Down

0 comments on commit 28064fa

Please sign in to comment.