Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions mathics/builtin/arithfns/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Integer1,
Integer3,
Integer310,
Integer400,
IntegerM1,
Number,
Rational,
Expand Down Expand Up @@ -49,6 +50,7 @@
SymbolNull,
SymbolPower,
SymbolTimes,
SymbolTrue,
)
from mathics.core.systemsymbols import (
SymbolBlank,
Expand Down Expand Up @@ -161,7 +163,7 @@ class Divide(InfixOperator):
expected_args = 2

formats = {
(("InputForm", "OutputForm"), "Divide[x_, y_]"): (
("InputForm", "Divide[x_, y_]"): (
'Infix[{HoldForm[x], HoldForm[y]}, "/", 400, Left]'
),
}
Expand All @@ -177,6 +179,24 @@ class Divide(InfixOperator):

summary_text = "divide a number"

def format_outputform(self, x, y, evaluation):
"(OutputForm,): Divide[x_, y_]"
use_2d = (
evaluation.definitions.get_ownvalues("System`$Use2DOutputForm")[0].replace
is SymbolTrue
)
if not use_2d:
return Expression(
SymbolInfix,
ListExpression(
Expression(SymbolHoldForm, x), Expression(SymbolHoldForm, y)
),
String("/"),
Integer400,
SymbolLeft,
)
return None


class Minus(PrefixOperator):
"""
Expand Down Expand Up @@ -414,10 +434,21 @@ class Power(InfixOperator, MPMathFunction):
Expression(SymbolPattern, Symbol("x"), Expression(SymbolBlank)),
RationalOneHalf,
): "HoldForm[Sqrt[x]]",
(("InputForm", "OutputForm"), "x_ ^ y_"): (
(("InputForm",), "x_ ^ y_"): (
'Infix[{HoldForm[x], HoldForm[y]}, "^", 590, Right]'
),
("", "x_ ^ y_"): (
(("OutputForm",), "x_ ^ y_"): (
"If[$Use2DOutputForm, "
"Superscript[HoldForm[x], HoldForm[y]], "
'Infix[{HoldForm[x], HoldForm[y]}, "^", 590, Right]]'
),
(
(
"StandardForm",
"TraditionalForm",
),
"x_ ^ y_",
): (
"PrecedenceForm[Superscript[PrecedenceForm[HoldForm[x], 590],"
" HoldForm[y]], 590]"
),
Expand All @@ -442,6 +473,7 @@ class Power(InfixOperator, MPMathFunction):
rules = {
"Power[]": "1",
"Power[x_]": "x",
"MakeBoxes[x_^y_, fmt_]": "SuperscriptBox[MakeBoxes[x, fmt],MakeBoxes[y, fmt]]",
}

summary_text = "exponentiate a number"
Expand Down
6 changes: 6 additions & 0 deletions mathics/builtin/box/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ def __new__(cls, *elements, **kwargs):
instance._elements = tuple(elements)
return instance

def __repr__(self):
result = str(type(self))
for elem in self._elements:
result += " * " + repr(elem)
return result

def do_format(self, evaluation, format):
return self

Expand Down
27 changes: 27 additions & 0 deletions mathics/builtin/box/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,33 @@ def eval_display(boxexpr, evaluation):
return boxexpr.elements[0]


class PaneBox(BoxExpression):
"""
<url>
:WMA link:
https://reference.wolfram.com/language/ref/InterpretationBox.html</url>

<dl>
<dt>'PaneBox[expr]'
<dd> is a low-level box construct, used in OutputForm.
</dl>

"""

attributes = A_HOLD_ALL_COMPLETE | A_PROTECTED | A_READ_PROTECTED
summary_text = "box associated to panel"

def apply_display(boxexpr, evaluation, expression):
"""ToExpression[boxexpr_PaneBox, form_]"""
return Expression(expression.head, boxexpr.elements[0], form).evaluate(
evaluation
)

def apply_display(boxexpr, evaluation):
"""DisplayForm[boxexpr_PaneBox]"""
return boxexpr.elements[0]


class RowBox(BoxExpression):
"""
<url>
Expand Down
24 changes: 23 additions & 1 deletion mathics/builtin/forms/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
"""
from typing import Optional

from mathics.builtin.box.layout import RowBox
from mathics.builtin.box.layout import (
GridBox,
InterpretationBox,
PaneBox,
RowBox,
to_boxes,
)
from mathics.builtin.forms.base import FormBaseClass
from mathics.core.atoms import Integer, Real, String, StringFromPython
from mathics.core.builtin import Builtin
Expand All @@ -28,8 +34,11 @@
SymbolInfinity,
SymbolMakeBoxes,
SymbolNumberForm,
SymbolOutputForm,
SymbolRowBox,
SymbolRuleDelayed,
SymbolStandardForm,
SymbolSubscriptBox,
SymbolSuperscriptBox,
)
from mathics.eval.makeboxes import (
Expand All @@ -40,7 +49,10 @@
eval_mathmlform,
eval_tableform,
eval_texform,
format_element,
)
from mathics.eval.testing_expressions import expr_min
from mathics.format.prettyprint import expression_to_2d_text


class BaseForm(FormBaseClass):
Expand Down Expand Up @@ -490,8 +502,18 @@ class OutputForm(FormBaseClass):
= -Graphics-
"""

formats = {"OutputForm[s_String]": "s"}
summary_text = "plain-text output format"

def eval_makeboxes(self, expr, form, evaluation):
"""MakeBoxes[OutputForm[expr_], form_]"""
print(" eval Makeboxes outputform")
text2d = expression_to_2d_text(expr, evaluation, form).text
elem1 = PaneBox(String(text2d))
elem2 = Expression(SymbolOutputForm, expr)
result = InterpretationBox(elem1, elem2)
return result


class PythonForm(FormBaseClass):
"""
Expand Down
38 changes: 37 additions & 1 deletion mathics/builtin/forms/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,47 @@

"""

from mathics.core.attributes import A_LOCKED, A_PROTECTED
from mathics.core.attributes import A_LOCKED, A_NO_ATTRIBUTES, A_PROTECTED
from mathics.core.builtin import Predefined
from mathics.core.list import ListExpression


class Use2DOutputForm_(Predefined):
r"""
<dl>
<dt>'$Use2DOutputForm'
<dd>internal variable that controls if 'OutputForm[expr]' is shown \
in one line (standard Mathics behavior) or \
or in a prettyform-like multiline output (the standard way in WMA).
The default value is 'False', keeping the standard Mathics behavior.
</dl>

>> $Use2DOutputForm
= False
>> OutputForm[a^b]
= a ^ b
>> $Use2DOutputForm = True; OutputForm[a ^ b]
=
. b
. a

Notice that without the 'OutputForm' wrapper, we fall back to the normal
behavior:
>> a ^ b
= Superscript[a, b]
Setting the variable back to False go back to the normal behavior:
>> $Use2DOutputForm = False; OutputForm[a ^ b]
= a ^ b
"""

attributes = A_NO_ATTRIBUTES
name = "$Use2DOutputForm"
rules = {
"$Use2DOutputForm": "False",
}
summary_text = "use the 2D OutputForm"


class PrintForms_(Predefined):
r"""
<dl>
Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/makeboxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class MakeBoxes(Builtin):
def eval_general(self, expr, f, evaluation):
"""MakeBoxes[expr_,
f:TraditionalForm|StandardForm|OutputForm|InputForm|FullForm]"""
return eval_generic_makeboxes(self, expr, f, evaluation)
return eval_generic_makeboxes(expr, f, evaluation)

def eval_outerprecedenceform(self, expr, precedence, form, evaluation):
"""MakeBoxes[PrecedenceForm[expr_, precedence_],
Expand Down
1 change: 1 addition & 0 deletions mathics/core/atoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ def user_hash(self, update):
Integer3 = Integer(3)
Integer4 = Integer(4)
Integer310 = Integer(310)
Integer400 = Integer(400)
Integer10 = Integer(10)
IntegerM1 = Integer(-1)

Expand Down
6 changes: 5 additions & 1 deletion mathics/eval/makeboxes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
eval_tableform,
eval_texform,
)
from mathics.eval.makeboxes.precedence import builtins_precedence, parenthesize
from mathics.eval.makeboxes.precedence import (
builtins_precedence,
compare_precedence,
parenthesize,
)

__all__ = [
"NumberForm_to_String",
Expand Down
Loading
Loading