Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
odashi committed Aug 12, 2020
2 parents 4e0ec7f + 05ead80 commit 6ee9727
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 4 deletions.
29 changes: 29 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# How to Contribute

We'd love to accept your patches and contributions to this project. There are
just a few small guidelines you need to follow.

## Contributor License Agreement

Contributions to this project must be accompanied by a Contributor License
Agreement (CLA). You (or your employer) retain the copyright to your
contribution; this simply gives us permission to use and redistribute your
contributions as part of the project. Head over to
<https://cla.developers.google.com/> to see your current agreements on file or
to sign a new one.

You generally only need to submit a CLA once, so if you've already submitted one
(even if it was for a different project), you probably don't need to do it
again.

## Code reviews

All submissions, including submissions by project members, require review. We
use GitHub pull requests for this purpose. Consult
[GitHub Help](https://help.github.com/articles/about-pull-requests/) for more
information on using pull requests.

## Community Guidelines

This project follows
[Google's Open Source Community Guidelines](https://opensource.google/conduct/).
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ Generates LaTeX math description from Python functions.
## Example

[Google Colaboratory](https://colab.research.google.com/drive/1MuiawKpVIZ12MWwyYuzZHmbKThdM5wNJ?usp=sharing)

## Disclaimer

This is not an officially supported Google product.
14 changes: 14 additions & 0 deletions latexify/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from latexify import core

get_latex = core.get_latex
Expand Down
43 changes: 39 additions & 4 deletions latexify/core.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This is very scratchy and supports only limited portion of Python functions.

import ast
Expand Down Expand Up @@ -41,7 +55,7 @@ def visit_FunctionDef(self, node):
name_str = r'\operatorname{' + 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
return name_str + '(' + ', '.join(arg_strs) + r')\triangleq ' + body_str

def visit_Return(self, node):
return self.visit(node.value)
Expand Down Expand Up @@ -82,7 +96,7 @@ def visit_Call(self, node):
else:
if callee_str.startswith('math.'):
callee_str = callee_str[5:]
lstr = r'\operatorname{' + callee_str + '}\left('
lstr = r'\operatorname{' + callee_str + r'}\left('
rstr = r'\right)'

arg_strs = [self.visit(arg) for arg in node.args]
Expand All @@ -103,18 +117,19 @@ def visit_UnaryOp(self, node):
def _wrap(child):
latex = self.visit(child)
if isinstance(child, ast.BinOp) and isinstance(child.op, (ast.Add, ast.Sub)):
return '(' + latex + ')'
return r'\left(' + latex + r'\right)'
return latex

reprs = {
ast.UAdd: (lambda: _wrap(node.operand)),
ast.USub: (lambda: '-' + _wrap(node.operand)),
ast.Not: (lambda: r'\lnot\left(' + _wrap(node.operand)+r'\right)')
}

if type(node.op) in reprs:
return reprs[type(node.op)]()
else:
return r'\operatorname{unknown\_uniop}(' + vstr + ')'
return r'\operatorname{unknown\_uniop}(' + self.visit(node.operand) + ')'

def visit_BinOp(self, node):
priority = {
Expand Down Expand Up @@ -164,9 +179,29 @@ def visit_Compare(self, node):

if isinstance(node.ops[0], ast.Eq):
return lstr + '=' + rstr
elif isinstance(node.ops[0], ast.Gt):
return lstr + '>' + rstr
elif isinstance(node.ops[0], ast.Lt):
return lstr + '<' + rstr
elif isinstance(node.ops[0], ast.GtE):
return lstr + r'\ge ' + rstr
elif isinstance(node.ops[0], ast.LtE):
return lstr + r'\le ' + rstr
elif isinstance(node.ops[0], ast.NotEq):
return lstr + r'\ne ' + rstr
elif isinstance(node.ops[0], ast.Is):
return lstr + r'\equiv' + rstr

else:
return r'\operatorname{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} '
# 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)'

def visit_If(self, node):
latex = r'\left\{ \begin{array}{ll} '

Expand Down
14 changes: 14 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import setuptools


Expand Down
14 changes: 14 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import math
import pytest

Expand Down

0 comments on commit 6ee9727

Please sign in to comment.