This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH, libstdc++, complex] complex multiplication algorithm improved


Hi,
 ÂComplex number multiplication done with 4 multiplications, but
this one only need 3:

      (a+ib)(c+id) = ac - bd + i( bc + ad ) -- 4 multiplies
               Â= ac - bd + i[ ( a + b )( c + d ) - ac
- bd ] -- 3 multiplies





diff -ru gcc/libstdc++-v3/include/std/complex
gcc.working.copy/libstdc++-v3//include/std/complex
--- gcc/libstdc++-v3/include/std/complex    Â2010-08-13
16:48:07.000000000 +0800
+++ gcc.working.copy/libstdc++-v3//include/std/complex Â2010-09-11
13:21:00.000000000 +0800
@@ -291,9 +291,13 @@
  complex<_Tp>&
  complex<_Tp>::operator*=(const complex<_Up>& __z)
  {
- Â Â Âconst _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
- Â Â Â_M_imag = _M_real * __z.imag() + _M_imag * __z.real();
- Â Â Â_M_real = __r;
+ Â Â Â//(a+ib)(c+id) = ac - bd + i( bc + ad ) -- 4 multiplies
+ Â Â Â// Â Â Â Â Â Â = ac - bd + i[ ( a + b )( c + d ) - ac - bd ] --
3 multiplies
+ Â Â Âconst _Tp ac = _M_real * __z.real();
+ Â Â Âconst _Tp bd = _M_imag * __z.imag();
+ Â Â Âconst _Tp ab_cd = (_M_real+_M_imag) * (__z.real()+__z.imag());
+ Â Â Â_M_real = ac - bd;
+ Â Â Â_M_imag = ab_cd - ac - bd;
   return *this;
  }

@@ -304,10 +308,11 @@
  complex<_Tp>&
  complex<_Tp>::operator/=(const complex<_Up>& __z)
  {
- Â Â Âconst _Tp __r = Â_M_real * __z.real() + _M_imag * __z.imag();
- Â Â Âconst _Tp __n = std::norm(__z);
- Â Â Â_M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
- Â Â Â_M_real = __r / __n;
+   Â// a + ib  Â( a + ib ) ( c - id )
+ Â Â Â//-------- = --------------------- Â-- 6 multiplies
+   Â// c + id     Âc^2 + d^2
+ Â Â Â*this *= conj(__z);
+ Â Â Â*this /= norm(__z);
   return *this;
  }

Attachment: complex.patch
Description: Binary data


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]