ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 18 Jul 2020 02:22:40 +0200Difference between ComplexField and MPComplexField?https://ask.sagemath.org/question/52478/difference-between-complexfield-and-mpcomplexfield/Hi everyone. I'm a long-time SAGE user, a little over a decade off and on. I have a variety of interests, but most of my coding time is spent working with matrix LU decompositions, finding Taylor series of "elementary" complex functions (logarithm or algebraic roots, e.g., log(x-c) or (x-c)^(2\*pi\*I/a), where c and a are complex numbers). And for years, I've been using the RealField and ComplexField classes, with their corresponding RealNumber and ComplexNumber elements, as the backbone of my code.
Just today, I discovered the MPComplexField class. I've been writing my own cython modules, borrowing code from the sage/rings/real_mpfr.pyx file. I noticed somewhere in my boilerplate code, that I was importing the MPC library. The MPC library appears to just be a C wrapper around the MPFR library, to perform complex arithmetic using pairs of MPFR numbers.
This struck me as a bit odd, because I've casually read through the source code in sage/rings/complex_number.pyx, and it looks to me like all the complex math is coded manually. E.g.,
a+b is calculated as (a.re + b.re) + I\*(a.im + b.im)
a*b is calculated as (a.re\*b.re - a.im\*b.im) + I\*(a.re\*b.im + a.im\*b.re)
sin(a) is a horrendous calculation involving sinh, square root, sine and cosine, as well as basic operations like multiplication, squaring, etc.
(Yes, I paraphrased the mpfr_xxx calls to make them readable.)
In other words, the ComplexNumber class implements the complex calculations manually, whereas the MPComplexNumber class lets the library handle those calculations. Calculating sin(a) is just a call to mpc_sin().
Which leads to my question. Why the two different classes? Which one is preferred as the more SAGE-ish approach? Is there a long-term plan to switch to MPC as the default in tutorials and code examples?
I ran some simple tests, and it appears that the rounding is not the same, at least not with default settings. After a few simple arithmetic calculations, I get diverging results, which I can only assume is due to a difference in rounding, and possibly due to differing internal algorithms. I've been using the ComplexField for over a decade, so I'm hesitant to switch. But I'm wondering if the MPComplexNumber has either better performance or better rounding, at least for most common applications.
Sorry for the long post, but this MPComplexNumber class appears to have been in SAGE for quite some time (since 2008). It seems like it must be the red-headed stepchild, because I don't seem to see it get mentioned anywhere. But if the performance is better, I'd gladly make the switch in all my coding projects. Any insight or advice?
Edit: My asterisk got turned into *italics*, which messed up my math sections. Escaped the asterisks, hope that fixes it.Wed, 15 Jul 2020 20:27:57 +0200https://ask.sagemath.org/question/52478/difference-between-complexfield-and-mpcomplexfield/Comment by Emmanuel Charpentier for <p>Hi everyone. I'm a long-time SAGE user, a little over a decade off and on. I have a variety of interests, but most of my coding time is spent working with matrix LU decompositions, finding Taylor series of "elementary" complex functions (logarithm or algebraic roots, e.g., log(x-c) or (x-c)^(2*pi*I/a), where c and a are complex numbers). And for years, I've been using the RealField and ComplexField classes, with their corresponding RealNumber and ComplexNumber elements, as the backbone of my code.</p>
<p>Just today, I discovered the MPComplexField class. I've been writing my own cython modules, borrowing code from the sage/rings/real_mpfr.pyx file. I noticed somewhere in my boilerplate code, that I was importing the MPC library. The MPC library appears to just be a C wrapper around the MPFR library, to perform complex arithmetic using pairs of MPFR numbers.</p>
<p>This struck me as a bit odd, because I've casually read through the source code in sage/rings/complex_number.pyx, and it looks to me like all the complex math is coded manually. E.g.,</p>
<p>a+b is calculated as (a.re + b.re) + I*(a.im + b.im)
a*b is calculated as (a.re*b.re - a.im*b.im) + I*(a.re*b.im + a.im*b.re)
sin(a) is a horrendous calculation involving sinh, square root, sine and cosine, as well as basic operations like multiplication, squaring, etc.
(Yes, I paraphrased the mpfr_xxx calls to make them readable.)</p>
<p>In other words, the ComplexNumber class implements the complex calculations manually, whereas the MPComplexNumber class lets the library handle those calculations. Calculating sin(a) is just a call to mpc_sin().</p>
<p>Which leads to my question. Why the two different classes? Which one is preferred as the more SAGE-ish approach? Is there a long-term plan to switch to MPC as the default in tutorials and code examples?</p>
<p>I ran some simple tests, and it appears that the rounding is not the same, at least not with default settings. After a few simple arithmetic calculations, I get diverging results, which I can only assume is due to a difference in rounding, and possibly due to differing internal algorithms. I've been using the ComplexField for over a decade, so I'm hesitant to switch. But I'm wondering if the MPComplexNumber has either better performance or better rounding, at least for most common applications.</p>
<p>Sorry for the long post, but this MPComplexNumber class appears to have been in SAGE for quite some time (since 2008). It seems like it must be the red-headed stepchild, because I don't seem to see it get mentioned anywhere. But if the performance is better, I'd gladly make the switch in all my coding projects. Any insight or advice?</p>
<p>Edit: My asterisk got turned into <em>italics</em>, which messed up my math sections. Escaped the asterisks, hope that fixes it.</p>
https://ask.sagemath.org/question/52478/difference-between-complexfield-and-mpcomplexfield/?comment=52551#post-id-52551This question may have a better chance of answer if asked on [sage-devel](https://groups.google.com/forum/#!forum/sage-devel), I think...Sat, 18 Jul 2020 00:38:25 +0200https://ask.sagemath.org/question/52478/difference-between-complexfield-and-mpcomplexfield/?comment=52551#post-id-52551Comment by jaydfox for <p>Hi everyone. I'm a long-time SAGE user, a little over a decade off and on. I have a variety of interests, but most of my coding time is spent working with matrix LU decompositions, finding Taylor series of "elementary" complex functions (logarithm or algebraic roots, e.g., log(x-c) or (x-c)^(2*pi*I/a), where c and a are complex numbers). And for years, I've been using the RealField and ComplexField classes, with their corresponding RealNumber and ComplexNumber elements, as the backbone of my code.</p>
<p>Just today, I discovered the MPComplexField class. I've been writing my own cython modules, borrowing code from the sage/rings/real_mpfr.pyx file. I noticed somewhere in my boilerplate code, that I was importing the MPC library. The MPC library appears to just be a C wrapper around the MPFR library, to perform complex arithmetic using pairs of MPFR numbers.</p>
<p>This struck me as a bit odd, because I've casually read through the source code in sage/rings/complex_number.pyx, and it looks to me like all the complex math is coded manually. E.g.,</p>
<p>a+b is calculated as (a.re + b.re) + I*(a.im + b.im)
a*b is calculated as (a.re*b.re - a.im*b.im) + I*(a.re*b.im + a.im*b.re)
sin(a) is a horrendous calculation involving sinh, square root, sine and cosine, as well as basic operations like multiplication, squaring, etc.
(Yes, I paraphrased the mpfr_xxx calls to make them readable.)</p>
<p>In other words, the ComplexNumber class implements the complex calculations manually, whereas the MPComplexNumber class lets the library handle those calculations. Calculating sin(a) is just a call to mpc_sin().</p>
<p>Which leads to my question. Why the two different classes? Which one is preferred as the more SAGE-ish approach? Is there a long-term plan to switch to MPC as the default in tutorials and code examples?</p>
<p>I ran some simple tests, and it appears that the rounding is not the same, at least not with default settings. After a few simple arithmetic calculations, I get diverging results, which I can only assume is due to a difference in rounding, and possibly due to differing internal algorithms. I've been using the ComplexField for over a decade, so I'm hesitant to switch. But I'm wondering if the MPComplexNumber has either better performance or better rounding, at least for most common applications.</p>
<p>Sorry for the long post, but this MPComplexNumber class appears to have been in SAGE for quite some time (since 2008). It seems like it must be the red-headed stepchild, because I don't seem to see it get mentioned anywhere. But if the performance is better, I'd gladly make the switch in all my coding projects. Any insight or advice?</p>
<p>Edit: My asterisk got turned into <em>italics</em>, which messed up my math sections. Escaped the asterisks, hope that fixes it.</p>
https://ask.sagemath.org/question/52478/difference-between-complexfield-and-mpcomplexfield/?comment=52554#post-id-52554Hey, thanks for the link! I'm developing my own cython modules for SAGE, so I'll check that group out.Sat, 18 Jul 2020 02:22:40 +0200https://ask.sagemath.org/question/52478/difference-between-complexfield-and-mpcomplexfield/?comment=52554#post-id-52554Answer by jaydfox for <p>Hi everyone. I'm a long-time SAGE user, a little over a decade off and on. I have a variety of interests, but most of my coding time is spent working with matrix LU decompositions, finding Taylor series of "elementary" complex functions (logarithm or algebraic roots, e.g., log(x-c) or (x-c)^(2*pi*I/a), where c and a are complex numbers). And for years, I've been using the RealField and ComplexField classes, with their corresponding RealNumber and ComplexNumber elements, as the backbone of my code.</p>
<p>Just today, I discovered the MPComplexField class. I've been writing my own cython modules, borrowing code from the sage/rings/real_mpfr.pyx file. I noticed somewhere in my boilerplate code, that I was importing the MPC library. The MPC library appears to just be a C wrapper around the MPFR library, to perform complex arithmetic using pairs of MPFR numbers.</p>
<p>This struck me as a bit odd, because I've casually read through the source code in sage/rings/complex_number.pyx, and it looks to me like all the complex math is coded manually. E.g.,</p>
<p>a+b is calculated as (a.re + b.re) + I*(a.im + b.im)
a*b is calculated as (a.re*b.re - a.im*b.im) + I*(a.re*b.im + a.im*b.re)
sin(a) is a horrendous calculation involving sinh, square root, sine and cosine, as well as basic operations like multiplication, squaring, etc.
(Yes, I paraphrased the mpfr_xxx calls to make them readable.)</p>
<p>In other words, the ComplexNumber class implements the complex calculations manually, whereas the MPComplexNumber class lets the library handle those calculations. Calculating sin(a) is just a call to mpc_sin().</p>
<p>Which leads to my question. Why the two different classes? Which one is preferred as the more SAGE-ish approach? Is there a long-term plan to switch to MPC as the default in tutorials and code examples?</p>
<p>I ran some simple tests, and it appears that the rounding is not the same, at least not with default settings. After a few simple arithmetic calculations, I get diverging results, which I can only assume is due to a difference in rounding, and possibly due to differing internal algorithms. I've been using the ComplexField for over a decade, so I'm hesitant to switch. But I'm wondering if the MPComplexNumber has either better performance or better rounding, at least for most common applications.</p>
<p>Sorry for the long post, but this MPComplexNumber class appears to have been in SAGE for quite some time (since 2008). It seems like it must be the red-headed stepchild, because I don't seem to see it get mentioned anywhere. But if the performance is better, I'd gladly make the switch in all my coding projects. Any insight or advice?</p>
<p>Edit: My asterisk got turned into <em>italics</em>, which messed up my math sections. Escaped the asterisks, hope that fixes it.</p>
https://ask.sagemath.org/question/52478/difference-between-complexfield-and-mpcomplexfield/?answer=52548#post-id-52548Hmm, I didn't get any hits on this. In the meantime, I think I may have answered one of my own questions. It's such a simple test that I'm kicking myself for not thinking of it sooner:
sage: a = -2.0 + 0
sage: sqrt(a)
1.41421356237310*I
sage: type(a)
<class 'sage.rings.real_mpfr.RealNumber'>
sage: type(sqrt(a))
<class 'sage.rings.complex_number.ComplexNumber'>
The `+ 0` part of `a = -2.0 + 0` is to coerce -2.0 from `RealLiteral` to `RealNumber`. After that, it's pretty clear that `ComplexNumber` is the standard SAGE type for complex numbers. It automatically used that type, given a `RealNumber`. So I will avoid the `MPComplexField` class, unless I need specialized functionality from the MPC library. I can't think of what that is right now, but I guess I'm answering this for anyone else that might have the same question in the future.
If anyone else has deeper insights into the MPComplexField class, including any advantages or exclusive features of that class, I would still love to hear it.Fri, 17 Jul 2020 23:41:22 +0200https://ask.sagemath.org/question/52478/difference-between-complexfield-and-mpcomplexfield/?answer=52548#post-id-52548