Ticket #7557(closed defect: fixed)

Opened 3 years ago

conversion of complex numbers in symbolic expressions to maxima broken

Reported by: Owned by: burcin burcin major sage-5.7 symbolics interfaces kcrisman N/A Volker Braun, Karl-Dieter Crisman Burcin Erocal sage-5.7.beta0

Reported on sage-support:

```var('y', domain='real')
assume(y, 'real')

abs(exp(y*I)).simplify()
1

abs(exp(1.1*y*I)).simplify()
e^(1.1*I*y)

The last result is incorrect. It seems simplify() doesn't like
floating point?
```

More evidence:

```sage: t = abs(exp(y*I)); t
abs(e^(I*y))
sage: t._maxima_init_()
'abs(exp((y)*(0+%i*1)))'

sage: u = abs(exp(1.1*y*I)); u
abs(e^(1.10000000000000*I*y))
sage: u._maxima_init_()
'abs(exp((y)*(1.1000000000000001*I)))'
```

This might be the reason:

```sage: t.operands()[0].operands()[0].operands()[1].pyobject()
I
sage: type(t.operands()[0].operands()[0].operands()[1].pyobject())

sage: u.operands()[0].operands()[0].operands()[1].pyobject()
1.10000000000000*I
sage: type(u.operands()[0].operands()[0].operands()[1].pyobject())
<type 'sage.rings.complex_number.ComplexNumber'>
```

Change History

comment:1 Changed 2 years ago by kcrisman

The thread  here suggests perhaps this is fixed in Maxima 5.23.

comment:2 Changed 15 months ago by kcrisman

Robert Dodier of Maxima suggests in  this sage-support thread that this should now be fixed.

comment:3 Changed 15 months ago by kcrisman

Oops, those were the same thread!

comment:4 Changed 6 months ago by burcin

This sage-support thread is also about the same problem. Patch coming up.

comment:5 Changed 6 months ago by burcin

• Status changed from new to needs_review
• Authors set to Burcin Erocal

comment:6 Changed 6 months ago by burcin

I updated the patch with a minor doctest correction in expression.pyx.

comment:7 Changed 6 months ago by vbraun

• Status changed from needs_review to positive_review
• Reviewers set to Volker Braun

Looks good to me!

comment:8 Changed 6 months ago by pipedream

Am I testing this correctly? I don't see any change:

```0 jan@osprey:~/sage/sage-5.4/devel\$hg qpush
applying trac_7557-maxima_complex_number_conversion.patch
now at: trac_7557-maxima_complex_number_conversion.patch
0 jan@osprey:~/sage/sage-5.4/devel\$hg qapplied
trac_7557-maxima_complex_number_conversion.patch
0 jan@osprey:~/sage/sage-5.4/devel\$mysage -b complexbug

----------------------------------------------------------
sage: Building and installing modified Sage library files.

Installing c_lib
scons: `install' is up to date.
Updating Cython code....
Executing 0 commands (using 1 thread)
Time to execute 0 commands: 0.0179660320282 seconds
Finished compiling Cython code (time = 0.490197181702 seconds)
running install
running build
running build_py
running build_ext
Executing 0 commands (using 1 thread)
Time to execute 0 commands: 0.00303983688354 seconds
Total time spent compiling C/C++ extensions:  0.0404081344604 seconds.
running install_lib
running install_egg_info
Removing /home/jan/sage/sage-5.4/local/lib/python2.7/site-packages/sage-0.0.0-py2.7.egg-info
Writing /home/jan/sage/sage-5.4/local/lib/python2.7/site-packages/sage-0.0.0-py2.7.egg-info

real	0m1.531s
user	0m1.112s
sys	0m0.240s
0 jan@osprey:~/sage/sage-5.4/devel\$mysage
----------------------------------------------------------------------
| Sage Version 5.4, Release Date: 2012-11-09                         |
| Type "notebook()" for the browser-based notebook interface.        |
| Type "help()" for help.                                            |
----------------------------------------------------------------------
sage: u=1.2; m=0.5; jacobi('sn',u,m)
0.887715488619
sage: jacobi('sn',u+2*I*elliptic_kc(1-m),m)
jacobi_sn(1.2 + 3.7081493546*I, 0.500000000000000)
sage: n(jacobi('sn',u+2*I*elliptic_kc(1-m),m))

```

(Hangs, I interrupted it after 30 seconds. Running this calculation directly in maxima takes one second)

comment:9 Changed 6 months ago by vbraun

You need the latest Sage version. On Sage-5.5.rc0 + the patch from this ticket I get the following:

```sage: u=1.2; m=0.5; jacobi('sn',u,m)
0.887715488619
sage: jacobi('sn',u+2*I*elliptic_kc(1-m),m)
0.887715488619 - 1.79195288805e-15*I
sage: n(jacobi('sn',u+2*I*elliptic_kc(1-m),m))
0.887715488619280 - 1.79195288804672e-15*I
```

so it works as it should.

comment:10 Changed 6 months ago by kcrisman

Great work, Burcin!

comment:11 Changed 5 months ago by jdemeyer

• Status changed from positive_review to needs_work

The documentation isn't properly formatted:

```docstring of sage.symbolic.expression.Expression.abs:24: WARNING: Literal block expected; none found.
```

Use this patch

comment:12 Changed 5 months ago by kcrisman

• Status changed from needs_work to positive_review
• Reviewers changed from Volker Braun to Volker Braun, Karl-Dieter Crisman
• Description modified (diff)

I think this one should work?

Patchbot, apply trac_7557-maxima_complex_number_conversion.2.patch

comment:13 Changed 5 months ago by jdemeyer

• Status changed from positive_review to needs_work

On arando (32-bit Linux i686) and possibly all 32-bit systems:

```sage -t  --long -force_lib devel/sage/sage/functions/special.py
**********************************************************************
File "/var/lib/buildbot/build/sage/arando-1/arando_full/build/sage-5.6.beta1/devel/sage-main/sage/functions/special.py", line 481:
sage: t._maxima_init_(maxima)
Expected:
'0.88771548861927996 - 1.7919528880467190e-15*%i'
Got:
'0.88771548861927740 - 4.3254035228713778e-16*%i'
**********************************************************************
File "/var/lib/buildbot/build/sage/arando-1/arando_full/build/sage-5.6.beta1/devel/sage-main/sage/functions/special.py", line 483:
sage: t.n()
Expected:
0.887715488619280 - 1.79195288804672e-15*I
Got:
0.887715488619277 - 4.32540352287138e-16*I
**********************************************************************
```

comment:14 Changed 4 months ago by jdemeyer

* ping *

Using # abs tol should fix these.

comment:15 Changed 4 months ago by burcin

• Status changed from needs_work to needs_review
• Description modified (diff)

Thanks for the reminder Jeroen.

comment:16 follow-up: ↓ 18 Changed 4 months ago by kcrisman

I get

```sage -t  "devel/sage-main/sage/functions/special.py"
... ''', res, 1e-13, 'ab
^
SyntaxError: invalid syntax

[0.2 s]

----------------------------------------------------------------------
The following tests failed:

sage -t  "devel/sage-main/sage/functions/special.py"
Total time for all tests: 0.2 seconds
```

Not sure what happened here. This is on 64-bit, apparently - I get the old answer.

comment:17 Changed 4 months ago by kcrisman

Here's the relevant part from the tmp file.

```        TESTS:

Check if complex numbers in the arguments are converted to maxima
correctly :trac:`7557`::

>>> t = jacobi('sn',RealNumber('1.2')+Integer(2)*I*elliptic_kc(Integer(1)-RealNumber('.5')),RealNumber('.5'))###line 480:_sage_    >>> t = jacobi(\$
>>> t._maxima_init_(maxima)###line 481:_sage_    >>> t._maxima_init_(maxima)
'0.88771548861927...*%i'
>>> res = Exception
>>> res =  t.n() # abs tol 1e-13###line 483:_sage_    >>> t.n() # abs tol 1e-13
>>> check_with_tolerance('''
...             0.887715488619280 - 1.79195288804672e-15*I
...         """
... ''', res, 1e-13, 'ab
>>> sig_on_count()
0
"""
```

So this might be something that only works with a working abs tol, which I may not yet have - is this based on beta3 or beta2? I haven't compiled beta3 yet since I can't upgrade it ;-)

comment:18 in reply to: ↑ 16 Changed 4 months ago by burcin

```sage -t  "devel/sage-main/sage/functions/special.py"
... ''', res, 1e-13, 'ab
^
SyntaxError: invalid syntax
```

This seems to be a problem with the sage-doctest script. If the triple-quotes immediately follow the doctest result, it generates a temporary file with a syntax error. I cleaned up the patch before submitting, but didn't realize deleting empty lines could cause doctest failures.

I uploaded a new patch with the same name. It should work now.

I don't have time to open a ticket for the sage-doctest bug right now.

comment:19 Changed 4 months ago by kcrisman

• Status changed from needs_review to positive_review

Seems fine now, thanks. I don't have a 32-bit system to test this on, though.

Patchbot, apply trac_7557-maxima_complex_number_conversion.2.patch and trac_7557-fix_doctest_precision.patch

comment:20 Changed 4 months ago by jdemeyer

• Milestone changed from sage-5.6 to sage-5.7

comment:21 Changed 4 months ago by jdemeyer

• Status changed from positive_review to closed
• Resolution set to fixed
• Merged in set to sage-5.7.beta0
Note: See TracTickets for help on using tickets.