-
Notifications
You must be signed in to change notification settings - Fork 374
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
Add More Functions #15
Changes from all commits
bb06d49
d34b0c9
ffc70f9
d4a9cc5
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 |
---|---|---|
|
@@ -41,7 +41,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) | ||
|
@@ -82,7 +82,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] | ||
|
@@ -103,18 +103,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 = { | ||
|
@@ -164,9 +165,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): | ||
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 function needs the same strategy to 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. for "visit_BoolOp"
for 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. As far as the papers I read, most authors tend to use comma to connect statements have AND relationship, because OR relationship is quite rarely seen. ORs are likely to be clearly indicated when needed. 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 prefer single-line form with 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 agree with the keeping minimality about extra conventions. I will try to implement the brackets for parenthesis in single-line form in the next commit. |
||
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} ' | ||
|
||
|
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.
I'm not sure simply printing
is
is good or not: could you removeis
andis not
for now? We need some discussion to make consensus.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.
for "is"&"is not":
I tested several cases-the layout, the view, the font, of "is" and "is not", and found that what I really need may actually be "\in" and "\notin". Because normal papers would almost never have statements like "$if x is int$", but "$if x \in Z$".
An approach is that the user manually define a variable like
Z=type('Z',(int,),{})
and then useif x is Z
to have the output "$if x is Z$". So maybe change "$is$" and "$is not$" to "$\in$" and "$\notin$" is better.I cannot decide this alone, so it's great to make some discussion for this.
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.
How is this going?
For the use-cases like
x \in Z
, the equivalent Python functionality isisinstance(x, int)
. The Python'sis
is not abelongs-to (\in)
operator: it checks if the two expressions represent the same instance, e.g.,1 is 1
isTrue
. Also the Python'sin
is provided basically for collection types. If we want to implement the\in
operator, I think we need to take two implementations: the Python'sin
and the pattern match replacement forisinstance
. As the latter one may involve some garbages, it may be good to restrict acceptable types (e.g., number types only).If we provide some representations of
is
, I think the nearest one is maybe isomorphic (\equiv
) though there's some differences between them.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.
Oh I thought you mean not to change them at present.
OK, I will change the implementation of the
ast.Is
clause into\equiv
and delete theast.IsNot
clause. As forisinstance
, that couldn't be processed invisit_Compare
so maybe that might be implemented in the future.