Opened 2 years ago

Closed 23 months ago

#28907 closed defect (fixed)

remove the method MathematicaElement.N()

Reported by: gh-mwageringel Owned by:
Priority: minor Milestone: sage-9.1
Component: interfaces Keywords: mathematica
Cc: dimpase, mkoeppe Merged in:
Authors: Markus Wageringel Reviewers: Dima Pasechnik
Report Upstream: N/A Work issues:
Branch: 52f8c9d (Commits, GitHub, GitLab) Commit: 52f8c9d3ff7fdc84be10151c2aa5a152698d26fa
Dependencies: Stopgaps:

Status badges


Currently, the method MathematicaElement.N() returns a string (2), which is not useful for further computations. It also contradicts the convention of interface elements that mathematica(1/3).N() is a more pythonic syntax for mathematica.N(mathematica(1/3)), as is the case for all the dynamically added methods in interfaces. So for consistencey, the method N() should return a MathematicaElement instead of a string.

This ticket removes the method altogether, for simplicity, as it is not needed anymore (1). Now a call to mathematica(1/3).N() dynamically redirects to Mathematica's N[] function and returns a MathematicaElement as expected.

This change also reveaved an issue with serialization of Mathematica elements, which is also fixed by this ticket (see _reduce()). The corresponding doctest was actually just serializing a string. This is now fixed:

sage: a = mathematica('N[Pi/2,50]')
sage: loads(dumps(a)) == a            # should be True
TypeError: Error executing code in Mathematica
Mathematica ERROR:

More details:

(1) In the past, Sage's object hierarchy used to define a method N(), so it had to be overwritten in the interface, but that is no longer the case.

(2) The return type was changed to string in #18888 as a workaround to an apparent bug in Mathematica itself. This bug does not seem to exist anymore in Mathematica 12, so the workaround is not needed anymore. Indeed, in the following computation, the decimal representation gets correctly truncated after 10 digits

Mathematica 12.0.0 Kernel for Linux x86 (64-bit)
Copyright 1988-2019 Wolfram Research, Inc.

In[1]:= x2=N[Pi,10]
Out[1]= 3.141592654
In[2]:= InputForm[x2,NumberMarks->False]
Out[2]//InputForm= 3.1415926536

whereas, in 18888#comment:20 with Mathematica 10.0, the same computation did not truncate the output

In[4]:= InputForm[x2,NumberMarks->False]
Out[4]//InputForm= 3.14159265358979323846264338327950419993

Change History (7)

comment:1 Changed 2 years ago by gh-mwageringel

  • Authors set to Markus Wageringel
  • Branch set to u/gh-mwageringel/28907
  • Cc dimpase added
  • Commit set to 52f8c9d3ff7fdc84be10151c2aa5a152698d26fa
  • Status changed from new to needs_review

New commits:

52f8c9d28907: fix return type/remove method MathematicaElement.N()

comment:2 Changed 2 years ago by dimpase

Shouldn't there be a deprecation period set up, before removal?

comment:3 Changed 2 years ago by gh-mwageringel

It is difficult to deprecate this, as the method does not change name, but effectively just changes the return type. However, the string that is currently returned by this method cannot be used to compute anything else in a meaningful way, so it is unlikely that any existing code depends on the return type being a string.

comment:4 Changed 2 years ago by dimpase

  • Cc mkoeppe added

seems that I don't have access to Mathematica any more, so I can't test :-(

comment:5 Changed 2 years ago by dimpase

  • Reviewers set to Dima Pasechnik
  • Status changed from needs_review to positive_review

OK, got mathematica reinstalled on my desktop, all is good with this branch, thanks!

comment:6 Changed 2 years ago by gh-mwageringel

Thank you.

comment:7 Changed 23 months ago by vbraun

  • Branch changed from u/gh-mwageringel/28907 to 52f8c9d3ff7fdc84be10151c2aa5a152698d26fa
  • Resolution set to fixed
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.