Skip to content

Commit

Permalink
moving private doctests to pytests. Fix the compatibility issue 0. I-…
Browse files Browse the repository at this point in the history
…> 0.
  • Loading branch information
mmatera committed Aug 1, 2023
1 parent 2aa00f0 commit d33be8b
Show file tree
Hide file tree
Showing 14 changed files with 407 additions and 167 deletions.
132 changes: 1 addition & 131 deletions mathics/builtin/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,45 +279,6 @@ class Complex_(Builtin):
= 1 + 2 I / 3
>> Abs[Complex[3, 4]]
= 5
#> OutputForm[Complex[2.0 ^ 40, 3]]
= 1.09951×10^12 + 3. I
#> InputForm[Complex[2.0 ^ 40, 3]]
= 1.099511627776*^12 + 3.*I
#> -2 / 3 - I
= -2 / 3 - I
#> Complex[10, 0]
= 10
#> 0. + I
= 0. + 1. I
#> 1 + 0 I
= 1
#> Head[%]
= Integer
#> Complex[0.0, 0.0]
= 0. + 0. I
#> 0. I
= 0.
#> 0. + 0. I
= 0.
#> 1. + 0. I
= 1.
#> 0. + 1. I
= 0. + 1. I
## Check Nesting Complex
#> Complex[1, Complex[0, 1]]
= 0
#> Complex[1, Complex[1, 0]]
= 1 + I
#> Complex[1, Complex[1, 1]]
= I
"""

summary_text = "head for complex numbers"
Expand Down Expand Up @@ -442,10 +403,6 @@ class Conjugate(MPMathFunction):
>> Conjugate[{{1, 2 + I 4, a + I b}, {I}}]
= {{1, 2 - 4 I, Conjugate[a] - I Conjugate[b]}, {-I}}
## Issue #272
#> {Conjugate[Pi], Conjugate[E]}
= {Pi, E}
>> Conjugate[1.5 + 2.5 I]
= 1.5 - 2.5 I
"""
Expand Down Expand Up @@ -485,11 +442,6 @@ class DirectedInfinity(SympyFunction):
>> DirectedInfinity[0]
= ComplexInfinity
#> DirectedInfinity[1+I]+DirectedInfinity[2+I]
= (2 / 5 + I / 5) Sqrt[5] Infinity + (1 / 2 + I / 2) Sqrt[2] Infinity
#> DirectedInfinity[Sqrt[3]]
= Infinity
"""

summary_text = "infinite quantity with a defined direction in the complex plane"
Expand Down Expand Up @@ -703,11 +655,6 @@ class Im(SympyFunction):
>> Plot[{Sin[a], Im[E^(I a)]}, {a, 0, 2 Pi}]
= -Graphics-
#> Re[0.5 + 2.3 I]
= 0.5
#> % // Precision
= MachinePrecision
"""

summary_text = "imaginary part"
Expand Down Expand Up @@ -740,10 +687,6 @@ class Integer_(Builtin):
>> Head[5]
= Integer
## Test large Integer comparison bug
#> {a, b} = {2^10000, 2^10000 + 1}; {a == b, a < b, a <= b}
= {False, True, True}
"""

summary_text = "head for integer numbers"
Expand Down Expand Up @@ -789,10 +732,6 @@ class Product(IterationFunction, SympyFunction):
>> primorial[12]
= 7420738134810
## Used to be a bug in sympy, but now it is solved exactly!
## Again a bug in sympy - regressions between 0.7.3 and 0.7.6 (and 0.7.7?)
## #> Product[1 + 1 / i ^ 2, {i, Infinity}]
## = 1 / ((-I)! I!)
"""

summary_text = "discrete product"
Expand Down Expand Up @@ -847,9 +786,6 @@ class Rational_(Builtin):
>> Rational[1, 2]
= 1 / 2
#> -2/3
= -2 / 3
"""

summary_text = "head for rational numbers"
Expand Down Expand Up @@ -878,11 +814,6 @@ class Re(SympyFunction):
>> Plot[{Cos[a], Re[E^(I a)]}, {a, 0, 2 Pi}]
= -Graphics-
#> Im[0.5 + 2.3 I]
= 2.3
#> % // Precision
= MachinePrecision
"""

summary_text = "real part"
Expand Down Expand Up @@ -919,61 +850,6 @@ class Real_(Builtin):
>> Head[x]
= Real
## Formatting tests
#> 1. * 10^6
= 1.×10^6
#> 1. * 10^5
= 100000.
#> -1. * 10^6
= -1.×10^6
#> -1. * 10^5
= -100000.
#> 1. * 10^-6
= 1.×10^-6
#> 1. * 10^-5
= 0.00001
#> -1. * 10^-6
= -1.×10^-6
#> -1. * 10^-5
= -0.00001
## Mathematica treats zero strangely
#> 0.0000000000000
= 0.
#> 0.0000000000000000000000000000
= 0.×10^-28
## Parse *^ Notation
#> 1.5×10^24
= 1.5×10^24
#> 1.5*^+24
= 1.5×10^24
#> 1.5*^-24
= 1.5×10^-24
## Don't accept *^ with spaces
#> 1.5 *^10
: "1.5 *" cannot be followed by "^10" (line 1 of "<test>").
#> 1.5*^ 10
: "1.5*" cannot be followed by "^ 10" (line 1 of "<test>").
## Issue654
#> 1^^2
: Requested base 1 in 1^^2 should be between 2 and 36.
: Expression cannot begin with "1^^2" (line 1 of "<test>").
#> 2^^0101
= 5
#> 2^^01210
: Digit at position 3 in 01210 is too large to be used in base 2.
: Expression cannot begin with "2^^01210" (line 1 of "<test>").
#> 16^^5g
: Digit at position 2 in 5g is too large to be used in base 16.
: Expression cannot begin with "16^^5g" (line 1 of "<test>").
#> 36^^0123456789abcDEFxyzXYZ
= 14142263610074677021975869033659
#> 37^^3
: Requested base 37 in 37^^3 should be between 2 and 36.
: Expression cannot begin with "37^^3" (line 1 of "<test>").
"""

summary_text = "head for real numbers"
Expand All @@ -999,7 +875,7 @@ class RealNumberQ(Test):
>> RealNumberQ[0 * I]
= True
>> RealNumberQ[0.0 * I]
= True
= False
"""

attributes = A_NO_ATTRIBUTES
Expand Down Expand Up @@ -1076,12 +952,6 @@ class Sum(IterationFunction, SympyFunction):
>> Sum[x ^ 2, {x, 1, y}] - y * (y + 1) * (2 * y + 1) / 6
= 0
## >> (-1 + a^n) Sum[a^(k n), {k, 0, m-1}] // Simplify
## = -1 + (a ^ n) ^ m # this is what I am getting
## = Piecewise[{{m (-1 + a ^ n), a ^ n == 1}, {-1 + (a ^ n) ^ m, True}}]
#> a=Sum[x^k*Sum[y^l,{l,0,4}],{k,0,4}]]
: "a=Sum[x^k*Sum[y^l,{l,0,4}],{k,0,4}]" cannot be followed by "]" (line 1 of "<test>").
## Issue #302
## The sum should not converge since the first term is 1/0.
Expand Down
1 change: 0 additions & 1 deletion mathics/builtin/numbers/calculus.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,6 @@ def diff(evaluation):
def eval_with_x_tuple(self, f, xtuple, evaluation: Evaluation, options: dict):
"%(name)s[f_, xtuple_, OptionsPattern[]]"
f_val = f.evaluate(evaluation)

if f_val.has_form("Equal", 2):
f = Expression(SymbolPlus, f_val.elements[0], f_val.elements[1])

Expand Down
30 changes: 0 additions & 30 deletions mathics/builtin/numbers/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,6 @@ class ComplexInfinity(_SympyConstant):
= ComplexInfinity
>> FullForm[ComplexInfinity]
= DirectedInfinity[]
## Issue689
#> ComplexInfinity + ComplexInfinity
: Indeterminate expression ComplexInfinity + ComplexInfinity encountered.
= Indeterminate
#> ComplexInfinity + Infinity
: Indeterminate expression ComplexInfinity + Infinity encountered.
= Indeterminate
"""

summary_text = "infinite complex quantity of undetermined direction"
Expand Down Expand Up @@ -302,15 +294,6 @@ class Degree(_MPMathConstant, _NumpyConstant, _SympyConstant):
>> N[\\[Degree]] == N[Degree]
= True
#> Cos[Degree[x]]
= Cos[Degree[x]]
#> N[Degree]
= 0.0174533
#> N[Degree, 30]
= 0.0174532925199432957692369076849
"""

summary_text = "conversion factor from radians to degrees"
Expand Down Expand Up @@ -367,9 +350,6 @@ class E(_MPMathConstant, _NumpyConstant, _SympyConstant):
= 2.71828
>> N[E, 50]
= 2.7182818284590452353602874713526624977572470937000
#> 5. E
= 13.5914
"""

summary_text = "exponential constant E ≃ 2.7182"
Expand Down Expand Up @@ -512,16 +492,6 @@ class Infinity(_SympyConstant):
Use 'Infinity' in sum and limit calculations:
>> Sum[1/x^2, {x, 1, Infinity}]
= Pi ^ 2 / 6
#> FullForm[Infinity]
= DirectedInfinity[1]
#> (2 + 3.5*I) / Infinity
= 0.
#> Infinity + Infinity
= Infinity
#> Infinity / Infinity
: Indeterminate expression 0 Infinity encountered.
= Indeterminate
"""

sympy_name = "oo"
Expand Down
20 changes: 17 additions & 3 deletions mathics/core/convert/mpmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,21 @@
from mathics.core.systemsymbols import SymbolIndeterminate


@lru_cache(maxsize=1024)
# Another issue with the lru_cache: for mpmath, mpmath.mpc(0.,0.) and
# mpmath.mpf(0.) produces the same hash. As a result, depending on
# what is called first, the output of this function is different.
#
# note mmatera: When I start to pass the private doctests
# to pytests, I realize that WMA evaluates `0. I` to `Complex[0.,0.]`
# instead of `0.`. To fix this incompatibility, I removed from
# `from_sympy` the lines that convert `mpc(0.,0.)` to `mpf(0.0)`,
# and then this issue becomes evident.
#
# As we decide by now that performance comes after ensuring the compatibility
# and clarity of the code, I propose here to conserve the right tests,
# and commented out the cache and the lines that convert mpc(0,0) to MachineReal(0).
#
# @lru_cache(maxsize=1024)
def from_mpmath(
value: Union[mpmath.mpf, mpmath.mpc],
precision: Optional[int] = None,
Expand All @@ -42,8 +56,8 @@ def from_mpmath(
# HACK: use str here to prevent loss of precision
return PrecisionReal(sympy.Float(str(value), precision=precision - 1))
elif isinstance(value, mpmath.mpc):
if value.imag == 0.0:
return from_mpmath(value.real, precision=precision)
# if value.imag == 0.0:
# return from_mpmath(value.real, precision=precision)
val_re, val_im = value.real, value.imag
if mpmath.isinf(val_re):
if mpmath.isinf(val_im):
Expand Down
2 changes: 1 addition & 1 deletion mathics/format/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def superscriptbox(self, **options):
base = self.tex_block(tex1, True)
superidx_to_tex = lookup_conversion_method(self.superindex, "latex")
superindx = self.tex_block(superidx_to_tex(self.superindex, **options), True)
if isinstance(self.superindex, (String, StyleBox)):
if len(superindx) == 1 and isinstance(self.superindex, (String, StyleBox)):
return "%s^%s" % (
base,
superindx,
Expand Down
Loading

0 comments on commit d33be8b

Please sign in to comment.