
Boost Users : 
Subject: Re: [Boostusers] Multiprecison: converting a complex class to a number<> backend
From: John Maddock (jz.maddock_at_[hidden])
Date: 20160629 14:28:19
On 28/06/2016 20:49, Daniel Brake wrote:
> Hi Boost,
>
> I have implemented my own complex class wrapping around
> Boost.Multiprecision number<>, particularly mpfr_float. Now, I'd like
> to take the class I've written, and extend/convert it into a backend
> suitable for use in number<>. While I've read the documentation
> describing the requirements for a backend
> <http://www.boost.org/doc/libs/1_61_0/libs/multiprecision/doc/html/boost_multiprecision/ref/backendconc.html>,
> and think I understand what I need to do, I have found 0 examples in
> the wild.
>
> My current setup has a pair of files mpfr_complex.hpp,
> mpfr_complex.cpp describing something like (just what you'd expect,
> really)
>
> 
> class complex{
> mpfr_float real_, imag_;
>
> public:
> // the *= family of operators, getters, setters,
> // some factory functions, etc
> };
>
> //the free operators defined down here, along with pow, exp, etc.
> 
>
> I think what I'd like to write is a new complex_backend.hpp file which
> implements the required functions for the complex wrapper around
> mpfr_float. In this file, I believe I implement the family of
> functions such as
> eval_multiply(b, a)
> in terms of the operators for my complex class. Is this right?
Yes.
>
> In a related vein, with et_on for mpfr_float, I've started adding
> templated overloads for the complex operators and math function
> accepting expressions of reals, and I think this is going to get out
> of hand, so using number<> would conceivably save me a ton of time
> writing my own functions. It will also save me a ton of errors for
> handwriting the expression templates for complexes, which appear to
> be ready to go from Boost.Multiprecision once I get a backend implemented.
>
> Again, my goal is expression templates for complexes with real and
> imaginary fields consisting of mpfr_floats, leveraging the number<>
> template, with et_on, and my own backend. Do you have any advice?
> Any references to any other custom backends?
>
I suggest you take a look at rational_adaptor which does more or less
exactly this but for rational types  if you basically just gut the
function implementations from that file, search and replace the class
names, and then fill in the blanks you should be 95% there.
This is actually something that's been on our TODO list for far too
long, ideally this should be part of the library  fancy helping out 
or at least being a testing guinea pig?
I quickly mocked up an example (attached) of how I think this should
look if we were to add it to the library, basically:
template<class Backend>
struct complex_adaptor{ /* details */ };
Then complex number typedefs that mirror each floating point typedef:
typedef number<complex_adaptor<cpp_bin_float_quad::backend_type> >
complex_cpp_bin_float_quad; // etc
As noted in the attached example, we really need another number category
for complex types, and while mixed arithmetic with (real) scalars will
just work, if you wanted an imaginary type as well, that would involve a
whole lot more work, as class number does not currently support return a
type different from either argument type in the binary operators (for
real + imaginary for example).
And finally... there is this:
https://code.google.com/archive/p/cppimaginarynumbers/ which takes a
different view of things (but loses multiprecision's expression templates).
HTH, John.
PS, one final note: ideally this would be handled by std::complex, but
sadly that class can not be instantiated on any type other than
float/double/long double with invoking undefined behaviour.
Boostusers list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net