Ticket #4446 (new enhancement)

Opened 2 months ago

Last modified 3 weeks ago

[with patches, needs review] New module complex_mpc using lib mpc for complex multiprecision arithmetic

Reported by: thevenyp Assigned to: mabshoff
Priority: major Milestone: sage-3.4
Component: optional packages Keywords:
Cc: robertwb

Description

Herewith and there (http://www.loria.fr/~thevenyp/complex_mpc.patch 38K) is a patch with new classes using the MPC library for complex multi-precision arithmetic (see ticket #4308 for the associated spackage).

This is an adaptation of the module real_mpfr and of ComplexField? and ComplexNumber? classes. It adds a class MPComplexField with precision (common to both part) and rounding modes (specific to each part) and a class MPComplexNumber.

This first attempt implements only the complex arithmetic.

The test suite does fail due to coercion problems I can't solve.

Attachments

complex_mpc.patch (37.4 kB) - added by thevenyp on 11/05/2008 10:05:16 AM.
trac4446-complex_mpc.patch (37.2 kB) - added by AlexGhitza on 11/22/2008 12:28:10 AM.
replace the previous patch (rebase against 3.2)
complex_mpc.p0.patch (48.5 kB) - added by thevenyp on 12/04/2008 10:04:08 AM.
coercion problems resolved, more functions
trac4446_fix.patch (0.6 kB) - added by AlexGhitza on 12/04/2008 11:50:30 PM.
apply after complex_mpc.p0.patch
trac4446_doctests.patch (31.0 kB) - added by AlexGhitza on 12/10/2008 09:04:40 PM.
apply after complex_mpc.p0.patch and trac4446_fix.patch
trac4446_norm.patch (4.6 kB) - added by thevenyp on 12/18/2008 04:47:06 AM.

Change History

11/05/2008 10:05:16 AM changed by thevenyp

  • attachment complex_mpc.patch added.

11/07/2008 09:38:56 PM changed by mabshoff

  • milestone set to sage-3.2.1.

11/22/2008 12:28:10 AM changed by AlexGhitza

  • attachment trac4446-complex_mpc.patch added.

replace the previous patch (rebase against 3.2)

11/22/2008 12:30:15 AM changed by AlexGhitza

There seems to have been some bitrot due to recent changes in Sage. I have uploaded a very slightly modified version of Philippe's patch, which applies cleanly against 3.2. A number of doctests fail for various reasons. I will try to look into this soon.

12/04/2008 10:04:08 AM changed by thevenyp

  • attachment complex_mpc.p0.patch added.

coercion problems resolved, more functions

12/04/2008 10:04:58 AM changed by thevenyp

  • summary changed from [with patch, needs work] New module complex_mpc using lib mpc for complex multiprecision arithmetic to [with patch, needs review] New module complex_mpc using lib mpc for complex multiprecision arithmetic.

The coercion problems have been solved, all doctests now succeed in new version. The whole set of mpc functions involving complex numbers is now in the interface.

12/04/2008 11:50:30 PM changed by AlexGhitza

  • attachment trac4446_fix.patch added.

apply after complex_mpc.p0.patch

12/04/2008 11:50:45 PM changed by AlexGhitza

Philippe,

Great work! I'll do my best to review this for 3.2.2. Right now I notice that the patch doesn't work in 3.2.1 because of #4580. I'm attaching a tiny patch that fixes that (should be applied after complex_mpc.p0.patch.

12/06/2008 12:58:45 AM changed by AlexGhitza

  • summary changed from [with patch, needs review] New module complex_mpc using lib mpc for complex multiprecision arithmetic to [with patch, needs more doctests] New module complex_mpc using lib mpc for complex multiprecision arithmetic.

There are a few small issues that I ran into, but since this is going to be (for now) an optional part of Sage and nothing depends on it, I think we can fix these later (I'm keeping a list of them so they don't get lost).

However, there is a policy of 100% doctest for all new code. The coverage is now:

[ghitza@artin sage]$ sage -coverage rings/complex_mpc.pyx
----------------------------------------------------------------------
rings/complex_mpc.pyx
SCORE rings/complex_mpc.pyx: 68% (46 of 67)

Missing documentation:
	 * _repr_ (self):
	 * _latex_(self):
	 * _an_element_(self):
	 * random_element(self, bound):
	 * __hash__(self):
	 * prec(self):
	 * _set(self, z, y=None, base=10):
	 * _repr_(self):
	 * _latex_(self):
	 * ModuleElement _add_(self, ModuleElement right):
	 * ModuleElement _sub_(self, ModuleElement right):
	 * RingElement _mul_(self, RingElement right):
	 * RingElement _div_(self, RingElement right):
	 * ModuleElement _neg_(self):
	 * __create__MPComplexField_version0 (prec, rnd):
	 * __create_MPComplexNumber_version0 (parent, s, base=10):


Missing doctests:
	 * _rnd(self):
	 * _rnd_re(self):
	 * _rnd_im(self):
	 * _real_field(self):
	 * _imag_field(self):


Possibly wrong (function name doesn't occur in doctests):
	 * _element_constructor_(self, z):
	 * _coerce_map_from_(self, S):
	 * bint is_exact(self) except -2: return False def is_finite(self):
	 * Element _call_(self, z):
	 * Element _call_(self, x):

----------------------------------------------------------------------

So the missing docstrings and doctests will have to be added before this patch can be merged. I would happily do this but the earliest I can get to it is Thursday or so (and that's being optimistic).

To end on a positive note: this looks good. It will take a bit more work, but if we can show that (a) the performance is improved, or doesn't suffer too much, and (b) the switch can be made seamlessly, then it will be easy to convince people that we should switch the core of our complex numbers functionality over to MPC.

12/07/2008 02:10:37 AM changed by robertwb

  • cc set to robertwb.

12/10/2008 09:04:40 PM changed by AlexGhitza

  • attachment trac4446_doctests.patch added.

apply after complex_mpc.p0.patch and trac4446_fix.patch

12/10/2008 09:13:09 PM changed by AlexGhitza

  • summary changed from [with patch, needs more doctests] New module complex_mpc using lib mpc for complex multiprecision arithmetic to [with patches, needs review] New module complex_mpc using lib mpc for complex multiprecision arithmetic.

I added a patch trac4446_doctests.patch, which does a number of things:

  • adds doctests for all functions except three internal use only functions
  • changes _repr_ of complex numbers so that it agrees with the way complex numbers are currently printed in Sage
  • makes MPComplexField inherit from ParentWithGens?, being generated over its real field by the square root of -1 (just as it is now); this required adding a few functions. So now one can do
sage: from sage.rings.complex_mpc import MPComplexField
sage: MPC.<j> = MPComplexField()
sage: j^2
-1.00000000000000 + 0.000000000000000*I

Note that only the last three patches should be applied, in order: complex_mpc.p0.patch, trac4446_fix.patch, and trac4446_doctests.patch

12/18/2008 04:47:06 AM changed by thevenyp

  • attachment trac4446_norm.patch added.

12/18/2008 04:57:44 AM changed by thevenyp

One more patch trac4446_norm.patch with:

* Alex Ghitza listed in authors list and copyright notice

* complex_mpc uses a copy of mpfr rounding mode list instead of its private one

* the abs and norm documentation has been improved.