Skip to content

Commit

Permalink
Merge pull request #738 from Mathics3/apply-reduction3
Browse files Browse the repository at this point in the history
apply->eval and minor tweaks
  • Loading branch information
rocky authored Jan 10, 2023
2 parents c850b39 + 1a37d4c commit 3e1420d
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 53 deletions.
26 changes: 14 additions & 12 deletions mathics/builtin/forms/output.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# FIXME: split these forms up further.
# MathML and TeXForm feel more closely related since they go with specific kinds of interpreters:
# LaTeX and MathML
# MathML and TeXForm feel more closely related since they go with
# specific kinds of interpreters: LaTeX and MathML

# SympyForm and PythonForm feel related since are our own hacky thing (and mostly broken for now)
# SympyForm and PythonForm feel related since are our own hacky thing
# (and mostly broken for now)

# NumberForm, TableForm, and MatrixForm seem closely related since they seem to be relevant
# for particular kinds of structures rather than applicable to all kinds of expressions.
# NumberForm, TableForm, and MatrixForm seem closely related since
# they seem to be relevant for particular kinds of structures rather
# than applicable to all kinds of expressions.

"""
Forms which appear in '$OutputForms'.
Expand Down Expand Up @@ -50,7 +52,7 @@
SymbolSubscriptBox,
SymbolSuperscriptBox,
)
from mathics.eval.makeboxes import format_element
from mathics.eval.makeboxes import StringLParen, StringRParen, format_element

MULTI_NEWLINE_RE = re.compile(r"\n{2,}")

Expand Down Expand Up @@ -1058,12 +1060,12 @@ class MatrixForm(TableForm):
in_printforms = False
summary_text = "format as a matrix"

def eval_makeboxes_matrix(self, table, f, evaluation, options):
def eval_makeboxes_matrix(self, table, form, evaluation, options):
"""MakeBoxes[%(name)s[table_, OptionsPattern[%(name)s]],
f:StandardForm|TraditionalForm]"""
form:StandardForm|TraditionalForm]"""

result = super(MatrixForm, self).eval_makeboxes(table, f, evaluation, options)
result = super(MatrixForm, self).eval_makeboxes(
table, form, evaluation, options
)
if result.get_head_name() == "System`GridBox":
return RowBox(String("("), result, String(")"))

return result
return RowBox(StringLParen, result, StringRParen)
7 changes: 5 additions & 2 deletions mathics/builtin/matrices/partmatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@


from mathics.builtin.base import Builtin
from mathics.core.evaluation import Evaluation
from mathics.core.list import ListExpression


Expand Down Expand Up @@ -47,7 +48,7 @@ class Diagonal(Builtin):

summary_text = "gives a list with the diagonal elements of a given matrix"

def apply(self, expr, diag, evaluation):
def eval(self, expr, diag, evaluation: Evaluation):
"Diagonal[expr_List, diag_Integer]"

result = []
Expand All @@ -63,7 +64,9 @@ def apply(self, expr, diag, evaluation):

class MatrixQ(Builtin):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/MatrixQ.html</url>
<url>
:WMA link:
https://reference.wolfram.com/language/ref/MatrixQ.html</url>
<dl>
<dt>'MatrixQ[$m$]'
Expand Down
34 changes: 20 additions & 14 deletions mathics/builtin/messages.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""
Message related functions.
Message-related functions.
"""


Expand All @@ -10,7 +9,7 @@
from mathics.builtin.base import BinaryOperator, Builtin
from mathics.core.atoms import String
from mathics.core.attributes import A_HOLD_ALL, A_HOLD_FIRST, A_PROTECTED
from mathics.core.evaluation import Message as EvaluationMessage
from mathics.core.evaluation import Evaluation, Message as EvaluationMessage
from mathics.core.expression import Expression
from mathics.core.list import ListExpression
from mathics.core.symbols import Symbol, SymbolNull
Expand All @@ -19,7 +18,9 @@

class Message(Builtin):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/Message.html</url>
<url>
:WMA link:
https://reference.wolfram.com/language/ref/Message.html</url>
<dl>
<dt>'Message[$symbol$::$msg$, $expr1$, $expr2$, ...]'
Expand All @@ -43,7 +44,7 @@ class Message(Builtin):
}
summary_text = "display a message"

def apply(self, symbol, tag, params, evaluation):
def eval(self, symbol: Symbol, tag: String, params, evaluation: Evaluation):
"Message[MessageName[symbol_Symbol, tag_String], params___]"

params = params.get_sequence()
Expand All @@ -62,11 +63,14 @@ def check_message(expr) -> bool:

class Check(Builtin):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/Check.html</url>
<url>
:WMA link:
https://reference.wolfram.com/language/ref/Check.html</url>
<dl>
<dt>'Check[$expr$, $failexpr$]'
<dd>evaluates $expr$, and returns the result, unless messages were generated, in which case it evaluates and $failexpr$ will be returned.
<dd>evaluates $expr$, and returns the result, unless messages were \
generated, in which case it evaluates and $failexpr$ will be returned.
<dt>'Check[$expr$, $failexpr$, {s1::t1,s2::t2,...}]'
<dd>checks only for the specified messages.
</dl>
Expand Down Expand Up @@ -141,11 +145,11 @@ class Check(Builtin):
}
summary_text = "discard the result if the evaluation produced messages"

def apply_1_argument(self, expr, evaluation):
def eval(self, expr, evaluation: Evaluation):
"Check[expr_]"
return evaluation.message("Check", "argmu")

def apply(self, expr, failexpr, params, evaluation):
def eval_with_fail(self, expr, failexpr, params, evaluation: Evaluation):
"Check[expr_, failexpr_, params___]"

# Todo: To implement the third form of this function , we need to implement the function $MessageGroups first
Expand Down Expand Up @@ -257,7 +261,7 @@ class Quiet(Builtin):
}
summary_text = "evaluate without showing messages"

def apply(self, expr, moff, mon, evaluation):
def eval(self, expr, moff, mon, evaluation: Evaluation):
"Quiet[expr_, moff_, mon_]"

def get_msg_list(expr):
Expand Down Expand Up @@ -351,7 +355,7 @@ class Off(Builtin):
attributes = A_HOLD_ALL | A_PROTECTED
summary_text = "turn off a message for printing"

def apply(self, expr, evaluation):
def eval(self, expr, evaluation: Evaluation):
"Off[expr___]"

seq = expr.get_sequence()
Expand Down Expand Up @@ -399,7 +403,7 @@ class On(Builtin):
attributes = A_HOLD_ALL | A_PROTECTED
summary_text = "turn on a message for printing"

def apply(self, expr, evaluation):
def eval(self, expr, evaluation: Evaluation):
"On[expr___]"

seq = expr.get_sequence()
Expand Down Expand Up @@ -458,7 +462,7 @@ class MessageName(BinaryOperator):
),
}

def apply(self, symbol, tag, evaluation):
def eval(self, symbol: Symbol, tag: String, evaluation: Evaluation):
"MessageName[symbol_Symbol, tag_String]"

pattern = Expression(SymbolMessageName, symbol, tag)
Expand Down Expand Up @@ -575,7 +579,9 @@ class Syntax(Builtin):

class General(Builtin):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/General.html</url>
<url>
:WMA link:
https://reference.wolfram.com/language/ref/General.html</url>
<dl>
<dt>'General'
Expand Down
15 changes: 8 additions & 7 deletions mathics/builtin/numeric.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# cython: language_level=3
# -*- coding: utf-8 -*-

# Note: docstring is flowed in documentation. Line breaks in the docstring will appear in the
# printed output, so be careful not to add them mid-sentence. Line breaks like \
# this work though.
# Note: docstring is flowed in documentation. Line breaks in the
# docstring will appear in the printed output, so be careful not to
# add them mid-sentence. Line breaks like \ this work though.

"""
Numerical Functions
Expand Down Expand Up @@ -89,7 +89,8 @@ def eval(self, expr, delta, evaluation: Evaluation):

class N(Builtin):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/N.html</url>
<url>:WMA link:
https://reference.wolfram.com/language/ref/N.html</url>
<dl>
<dt>'N[$expr$, $prec$]'
Expand Down Expand Up @@ -213,7 +214,7 @@ class N(Builtin):

summary_text = "numerical evaluation to specified precision and accuracy"

def apply_with_prec(self, expr, prec, evaluation, options=None):
def eval_with_prec(self, expr, prec, evaluation, options=None):
"N[expr_, prec_, OptionsPattern[%(name)s]]"

# If options are passed, set the preference in evaluation, and call again
Expand All @@ -236,15 +237,15 @@ def apply_with_prec(self, expr, prec, evaluation, options=None):
if preference:
preference_queue.append(preference)
try:
result = self.apply_with_prec(expr, prec, evaluation)
result = self.eval_with_prec(expr, prec, evaluation)
except Exception:
result = None
preference_queue.pop()
return result

return eval_nvalues(expr, prec, evaluation)

def apply_N(self, expr, evaluation):
def eval_N(self, expr, evaluation: Evaluation):
"""N[expr_]"""
# TODO: Specialize for atoms
return eval_nvalues(expr, SymbolMachinePrecision, evaluation)
Expand Down
36 changes: 24 additions & 12 deletions mathics/builtin/optimization.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# -*- coding: utf-8 -*-
"""Mathematical Optimization
Mathematical optimization is the selection of a best element, with regard to some criterion, from some set of available alternatives.
Mathematical optimization is the selection of a best element, with regard to \
some criterion, from some set of available alternatives.
Optimization problems of sorts arise in all quantitative disciplines from computer science and engineering to operations research and economics, and the development of solution methods has been of interest in mathematics for centuries.
Optimization problems of sorts arise in all quantitative disciplines from \
computer science and engineering to operations research and economics, \
and the development of solution methods has been of interest in mathematics \
for centuries.
We intend to provide local and global optimization techniques, both numeric and symbolic.
We intend to provide local and global optimization techniques, both numeric \
and symbolic.
"""

# This tells documentation how to sort this module
Expand All @@ -18,6 +23,7 @@
from mathics.core.attributes import A_CONSTANT, A_PROTECTED, A_READ_PROTECTED
from mathics.core.convert.python import from_python
from mathics.core.convert.sympy import from_sympy
from mathics.core.evaluation import Evaluation
from mathics.core.expression import Expression
from mathics.core.list import ListExpression
from mathics.core.symbols import Atom, Symbol
Expand All @@ -28,11 +34,14 @@

class Maximize(Builtin):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/Maximize.html</url>
<url>
:WMA link:
https://reference.wolfram.com/language/ref/Maximize.html</url>
<dl>
<dt>'Maximize[$f$, $x$]'
<dd>compute the maximum of $f$ respect $x$ that change between $a$ and $b$
<dd>compute the maximum of $f$ respect $x$ that change between \
$a$ and $b$.
</dl>
>> Maximize[-2 x^2 - 3 x + 5, x]
Expand All @@ -48,7 +57,7 @@ class Maximize(Builtin):
attributes = A_PROTECTED | A_READ_PROTECTED
summary_text = "compute the maximum of a function"

def apply(self, f, vars, evaluation):
def eval(self, f, vars, evaluation: Evaluation):
"Maximize[f_?NotListQ, vars_]"

dual_f = f.to_sympy() * (-1)
Expand All @@ -66,7 +75,7 @@ def apply(self, f, vars, evaluation):

return from_python(solutions)

def apply_constraints(self, f, vars, evaluation):
def eval_constraints(self, f, vars, evaluation: Evaluation):
"Maximize[f_List, vars_]"

constraints = [function for function in f.elements]
Expand All @@ -86,11 +95,14 @@ def apply_constraints(self, f, vars, evaluation):

class Minimize(Builtin):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/Minimize.html</url>
<url>
:WMA link:
https://reference.wolfram.com/language/ref/Minimize.html</url>
<dl>
<dt>'Minimize[$f$, $x$]'
<dd>compute the minimum of $f$ respect $x$ that change between $a$ and $b$
<dd>compute the minimum of $f$ respect $x$ that change between \
$a$ and $b$.
</dl>
>> Minimize[2 x^2 - 3 x + 5, x]
Expand All @@ -106,7 +118,7 @@ class Minimize(Builtin):
attributes = A_PROTECTED | A_READ_PROTECTED
summary_text = "compute the minimum of a function"

def apply_onevariable(self, f, x, evaluation):
def eval_onevariable(self, f, x, evaluation: Evaluation):
"Minimize[f_?NotListQ, x_?NotListQ]"

sympy_x = x.to_sympy()
Expand Down Expand Up @@ -137,7 +149,7 @@ def apply_onevariable(self, f, x, evaluation):
)
)

def apply_multiplevariable(self, f, vars, evaluation):
def eval_multiplevariable(self, f, vars, evaluation: Evaluation):
"Minimize[f_?NotListQ, vars_List]"

head_name = vars.get_head_name()
Expand Down Expand Up @@ -215,7 +227,7 @@ def apply_multiplevariable(self, f, vars, evaluation):
)
)

def apply_constraints(self, f, vars, evaluation):
def eval_constraints(self, f, vars, evaluation: Evaluation):
"Minimize[f_List, vars_List]"
head_name = vars.get_head_name()
vars_or = vars
Expand Down
10 changes: 7 additions & 3 deletions mathics/builtin/recurrence.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"""

# This tells documentation how to sort this module
# Here we are also hiding "moments" since this erroneously appears at the top level.
# Here we are also hiding "moments" since this erroneously appears at the
# top level.
sort_order = "mathics.builtin.solving-recurrence-equations"


Expand All @@ -15,6 +16,7 @@
from mathics.core.atoms import IntegerM1
from mathics.core.attributes import A_CONSTANT
from mathics.core.convert.sympy import from_sympy, sympy_symbol_prefix
from mathics.core.evaluation import Evaluation
from mathics.core.expression import Expression
from mathics.core.list import ListExpression
from mathics.core.symbols import Atom, Symbol, SymbolPlus, SymbolTimes
Expand All @@ -23,7 +25,9 @@

class RSolve(Builtin):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/RSolve.html</url>
<url>
:WMA link:
https://reference.wolfram.com/language/ref/RSolve.html</url>
<dl>
<dt>'RSolve[$eqn$, $a$[$n$], $n$]'
Expand Down Expand Up @@ -63,7 +67,7 @@ class RSolve(Builtin):
}
summary_text = "recurrence equations solver"

def eval(self, eqns, a, n, evaluation):
def eval(self, eqns, a, n, evaluation: Evaluation):
"RSolve[eqns_, a_, n_]"

# TODO: Do this with rules?
Expand Down
Loading

0 comments on commit 3e1420d

Please sign in to comment.