Skip to content

Commit ac45be5

Browse files
committed
support for form parameter in ToString
merge series
1 parent 33129a4 commit ac45be5

File tree

2 files changed

+14
-25
lines changed

2 files changed

+14
-25
lines changed

mathics/builtin/calculus.py

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -238,26 +238,6 @@ def apply(self, f, x, evaluation):
238238
)
239239
else: # many leaves
240240

241-
if len(terms) == 0:
242-
return IntegerZero
243-
elif len(terms) == 1:
244-
return terms[0]
245-
else:
246-
return Expression(SymbolPlus, *terms)
247-
elif len(f.leaves) == 1:
248-
if f.leaves[0] == x:
249-
return Expression(
250-
Expression(Expression("Derivative", Integer(1)), f.head), x
251-
)
252-
else:
253-
g = f.leaves[0]
254-
return Expression(
255-
SymbolTimes,
256-
Expression("D", Expression(f.head, g), g),
257-
Expression("D", g, x),
258-
)
259-
else: # many leaves
260-
261241
def summand(leaf, index):
262242
result = Expression(
263243
Expression(
@@ -412,7 +392,6 @@ def __init__(self, *args, **kwargs):
412392
super(Derivative, self).__init__(*args, **kwargs)
413393

414394
def to_sympy(self, expr, **kwargs):
415-
print("calling to_sympy")
416395
inner = expr
417396
exprs = [inner]
418397
try:
@@ -766,7 +745,7 @@ class Solve(Builtin):
766745
>> sol = Solve[eqs, {x, y}] // Simplify
767746
= {{x -> 0, y -> 0}, {x -> 1, y -> 1}, {x -> -1 / 2 + I / 2 Sqrt[3], y -> -1 / 2 - I / 2 Sqrt[3]}, {x -> (1 - I Sqrt[3]) ^ 2 / 4, y -> -1 / 2 + I / 2 Sqrt[3]}}
768747
>> eqs /. sol // Simplify
769-
= {{True, True}, {True, True}, {True, True}, {True, True}}
748+
= {{True, True}, {True, True}, {False, False}, {True, True}}
770749
771750
An underdetermined system:
772751
>> Solve[x^2 == 1 && z^2 == -1, {x, y, z}]

mathics/builtin/strings.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,6 +1627,9 @@ class ToString(Builtin):
16271627
<dl>
16281628
<dt>'ToString[$expr$]'
16291629
<dd>returns a string representation of $expr$.
1630+
<dt>'ToString[$expr$, $form$]'
1631+
<dd>returns a string representation of $expr$ in the form
1632+
$form$.
16301633
</dl>
16311634
16321635
>> ToString[2]
@@ -1640,6 +1643,9 @@ class ToString(Builtin):
16401643
= U <> 2
16411644
>> "U" <> ToString[2]
16421645
= U2
1646+
>> ToString[Integrate[f[x],x], TeXForm]
1647+
= \\int f\\left[x\\right] \\, dx
1648+
16431649
"""
16441650

16451651
options = {
@@ -1652,10 +1658,14 @@ class ToString(Builtin):
16521658
"TotalWidth": "Infinity",
16531659
}
16541660

1655-
def apply(self, value, evaluation, **options):
1661+
def apply_default(self, value, evaluation, options):
16561662
"ToString[value_, OptionsPattern[ToString]]"
1657-
encoding = options["options"]["System`CharacterEncoding"]
1658-
text = value.format(evaluation, "System`OutputForm", encoding=encoding)
1663+
return self.apply_form(value, Symbol("System`OutputForm"), evaluation, options)
1664+
1665+
def apply_form(self, value, form, evaluation, options):
1666+
"ToString[value_, form_, OptionsPattern[ToString]]"
1667+
encoding = options["System`CharacterEncoding"]
1668+
text = value.format(evaluation, form.get_name(), encoding=encoding)
16591669
text = text.boxes_to_text(evaluation=evaluation)
16601670
return String(text)
16611671

0 commit comments

Comments
 (0)