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
48 changes: 44 additions & 4 deletions mathics/builtin/arithfns/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
IntegerM1,
Number,
RationalOneHalf,
String,
)
from mathics.core.attributes import (
A_FLAT,
Expand All @@ -34,11 +35,21 @@
)
from mathics.core.evaluation import Evaluation
from mathics.core.expression import Expression
from mathics.core.symbols import Symbol, SymbolNull, SymbolPower, SymbolTimes
from mathics.core.list import ListExpression
from mathics.core.symbols import (
Symbol,
SymbolHoldForm,
SymbolNull,
SymbolPower,
SymbolTimes,
SymbolTrue,
)
from mathics.core.systemsymbols import (
SymbolBlank,
SymbolComplexInfinity,
SymbolIndeterminate,
SymbolInfix,
SymbolLeft,
SymbolPattern,
SymbolSequence,
)
Expand Down Expand Up @@ -144,7 +155,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 @@ -160,6 +171,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("/"),
Integer(400),
SymbolLeft,
)
return None


class Minus(PrefixOperator):
"""
Expand Down Expand Up @@ -350,10 +379,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 Down
18 changes: 16 additions & 2 deletions mathics/builtin/forms/print.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from mathics.core.symbols import SymbolFalse, SymbolTrue
from mathics.core.systemsymbols import SymbolInputForm, SymbolOutputForm
from mathics.format.box.makeboxes import is_print_form_callback
from mathics.format.form import render_input_form, render_output_form
from mathics.format.form import render_input_form

sort_order = "mathics.builtin.forms.general-purpose-forms"

Expand Down Expand Up @@ -258,7 +258,21 @@ def eval_makeboxes_outputform(expr: BaseElement, evaluation: Evaluation, **kwarg
Build a 2D representation of the expression using only keyboard characters.
"""

text_outputform = str(render_output_form(expr, evaluation, **kwargs))
from mathics.builtin.box.layout import InterpretationBox
from mathics.format.form.outputform import render_output_form
from mathics.format.render.prettyprint import render_2d_text

if (
evaluation.definitions.get_ownvalues("System`$Use2DOutputForm")[0].replace
is SymbolTrue
):
text_outputform = str(render_2d_text(expr, evaluation, **{"2d": True}))

if "\n" in text_outputform:
text_outputform = "\n" + text_outputform
else: # 1D
text_outputform = str(render_output_form(expr, evaluation, **kwargs))

pane = PaneBox(String('"' + text_outputform + '"'))
return InterpretationBox(
pane, Expression(SymbolOutputForm, expr), **{"System`Editable": SymbolFalse}
Expand Down
39 changes: 38 additions & 1 deletion mathics/builtin/forms/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@

"""

from mathics.core.attributes import A_LOCKED, A_PROTECTED, A_READ_PROTECTED
from mathics.core.attributes import (
A_LOCKED,
A_NO_ATTRIBUTES,
A_PROTECTED,
A_READ_PROTECTED,
)
from mathics.core.builtin import Builtin, Predefined
from mathics.core.list import ListExpression

Expand Down Expand Up @@ -179,3 +184,35 @@ class PrintForms_(Predefined):

def evaluate(self, evaluation):
return ListExpression(*evaluation.definitions.printforms)


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

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"
1 change: 0 additions & 1 deletion mathics/doc/doc_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import logging
import re
from abc import ABC
from os import getenv
from typing import TYPE_CHECKING, Callable, Dict, List, Optional, Sequence, Tuple

from mathics.core.evaluation import Message, Print, _Out
Expand Down
Loading
Loading