Exemple #1
0
complex<typename add_typeof_helper<X,Y>::type>
operator+(const complex<X>& x,const complex<Y>& y)
{
    typedef typename boost::units::add_typeof_helper<X,Y>::type type;
    
    return complex<type>(x.real()+y.real(),x.imag()+y.imag());
}
	/* https://github.com/AE9RB/fftbench/blob/master/cxlr.hpp
	 * Nice trick from AE9RB (David Turnbull) to get compiler to produce simpler
	 * fma (fused multiply-accumulate) instead of worrying about NaN handling
	 */
	inline complex<float>
	operator*(const complex<float>& v1, const complex<float>& v2) {
		return complex<float> {
			v1.real() * v2.real() - v1.imag() * v2.imag(),
			v1.real() * v2.imag() + v1.imag() * v2.real()
		};
	}
Exemple #3
0
int cmp1(complex<double> a,complex<double> b)
{
	if(a.real()==b.real())
		return a.imag()<=b.imag();
	else
		return a.real()<b.real();
}
Exemple #4
0
complex<typename boost::units::subtract_typeof_helper<X,Y>::type>
operator-(const complex<X>& x,const complex<Y>& y)
{
    typedef typename boost::units::subtract_typeof_helper<X,Y>::type    type;
    
    return complex<type>(x.real()-y.real(),x.imag()-y.imag());
}
Exemple #5
0
void EMLTracker::integrate(complex<double> in)
{
    localReplica.tick();

    // First bring the signal level of the input to the same as the
    // local replicas
    in *= baseGain;

    complex<double> carrier = localReplica.getCarrier();
    complex<double> code = localReplica.getCode() ? plusOne : minusOne;

    // mix in the carrier local replica
    complex<double> m0 = in * conj(carrier);

    // and sum them up.. (yea, the conj of the codes should be a NoOp)
    code = conj(code);
    early.process(m0, code);
    prompt.process(m0, code);
    late.process(m0, code);

    // Update our sums for normalizing things...
    complex<double> lr = conj(carrier) * code;
    inSumSq += in.real()*in.real() + in.imag()*in.imag();
    lrSumSq += lr.real()*lr.real() + lr.imag()*lr.imag();
}
Exemple #6
0
bool cmp(const complex <int>  &a, const complex <int>  &b)
{
	if(a.imag()==b.imag())
		return a.real()<b.real();
	else
		return a.imag()<b.imag();
}
Exemple #7
0
 static type value(const complex<quantity<Unit,Y> >& x)  
 { 
     const complex<value_type>   tmp =
         root<static_rational<N,D> >(complex<Y>(x.real().value(),
                                                x.imag().value()));
     
     return type(quantity_type::from_value(tmp.real()),
                quantity_type::from_value(tmp.imag()));
 }
 bool operator()(const complex<double>& lhs, const complex<double>& rhs) {
   if (norm(lhs) != norm(rhs)) {
     return norm(lhs) < norm(rhs);
   } else if (lhs.real() != rhs.real()) {
     return lhs.real() < rhs.real();
   } else {
     return lhs.imag() < rhs.imag();
   }
 }
complex<double> complexAdd(complex<double> x1, complex<double> x2)
{
    int x1_real = int(x1.real());
    int x2_real = int(x2.real());
    int x1_imag = int(x1.imag());
    int x2_imag = int(x2.imag());
    int r1 = int(bitset<16>(x1_real+x2_real).to_ulong());
    int r2 = int(bitset<16>(x1_imag+x2_imag).to_ulong());
    return complex<double>(double(r1), double(r2));
}
Exemple #10
0
std::string fmtvalue(std::true_type, const complex<T>& x)
{
    std::string restr = as_string(fmt<'g', number_width, number_precision>(x.real()));
    if (restr.size() > number_width)
        restr = as_string(fmt<'g', number_width, number_precision_short>(x.real()));

    std::string imstr = as_string(fmt<'g', -1, number_precision>(std::abs(x.imag())));
    if (imstr.size() > number_width)
        imstr = as_string(fmt<'g', -1, number_precision_short>(std::abs(x.imag())));

    return restr + (x.imag() < T(0) ? "-" : "+") + padleft(number_width, imstr + "j");
}
double recursiveColor(complex<double> complexNum, complex<double> constant, int iterations)
{
	if(iterations == maxIterations)
		return double(iterations);
	if(sqrt(complexNum.real() * complexNum.real() + complexNum.imag() * complexNum.imag()) > 2)
		return double(iterations);
	else
	{
		complexNum = complex<double>(complexNum.real() * complexNum.real() - (complexNum.imag() * complexNum.imag()) + constant.real(), 2 * complexNum.real() * complexNum.imag() + constant.imag());
		return recursiveColor(complexNum, constant, ++iterations);
	}
}
Exemple #12
0
/* Complex root forward deflation.
 */
static int complexdeflation( const register int n, double a[], const complex<double> z )
   {
   register int i;
   double r, u;

   r = -2.0 * z.real();
   u = z.real() * z.real() + z.imag() * z.imag();
   a[ 1 ] -= r * a[ 0 ];
   for( i = 2; i < n - 1; i++ )
      a[ i ] = a[ i ] - r * a[ i - 1 ] - u * a[ i - 2 ];

   return n - 2;
}
Exemple #13
0
void Biquad::setZeroPolePairs(const complex<double> &zero, const complex<double> &pole)
{
    double b0 = 1;
    double b1 = -2 * zero.real();

    double zeroMag = abs(zero);
    double b2 = zeroMag * zeroMag;

    double a1 = -2 * pole.real();

    double poleMag = abs(pole);
    double a2 = poleMag * poleMag;
    setNormalizedCoefficients(b0, b1, b2, 1, a1, a2);
}
complex<double> complexMultiply(complex<double> x1, complex<double> x2)
{
    int x1_real = int(x1.real());
    int x2_real = int(x2.real());
    int x1_imag = int(x1.imag());
    int x2_imag = int(x2.imag());
    int r1 = int(bitset<16>(x1_real*x2_real).to_ulong());
    int r2 = int(bitset<16>(x1_imag*x2_imag).to_ulong());
    int r3 = int(bitset<16>(x1_real*x2_imag).to_ulong());
    int r4 = int(bitset<16>(x1_imag*x2_real).to_ulong());
    int m_real = int(bitset<16>(r1-r2).to_ulong());
    int m_imag = int(bitset<16>(r3+r4).to_ulong());
    return complex<double>(double(m_real), double(m_imag));
}
Exemple #15
0
complex<typename boost::units::divide_typeof_helper<X,Y>::type>
operator/(const complex<X>& x,const complex<Y>& y)
{
    // naive implementation of complex division
    typedef typename boost::units::divide_typeof_helper<X,Y>::type type;

    return complex<type>((x.real()*y.real()+x.imag()*y.imag())/
                            (y.real()*y.real()+y.imag()*y.imag()),
                         (x.imag()*y.real()-x.real()*y.imag())/
                            (y.real()*y.real()+y.imag()*y.imag()));
                         
//  fully correct implementation has more complex return type
//
//  typedef typename boost::units::multiply_typeof_helper<X,Y>::type xy_type;
//  typedef typename boost::units::multiply_typeof_helper<Y,Y>::type yy_type;
//
//  typedef typename boost::units::add_typeof_helper<xy_type, xy_type>::type
//      xy_plus_xy_type;
//  typedef typename boost::units::subtract_typeof_helper<
//      xy_type,xy_type>::type xy_minus_xy_type;
//
//  typedef typename boost::units::divide_typeof_helper<
//      xy_plus_xy_type,yy_type>::type      xy_plus_xy_over_yy_type;
//  typedef typename boost::units::divide_typeof_helper<
//      xy_minus_xy_type,yy_type>::type     xy_minus_xy_over_yy_type;
//
//  BOOST_STATIC_ASSERT((boost::is_same<xy_plus_xy_over_yy_type,
//                                  xy_minus_xy_over_yy_type>::value == true));
//
//  return complex<xy_plus_xy_over_yy_type>(
//      (x.real()*y.real()+x.imag()*y.imag())/
//          (y.real()*y.real()+y.imag()*y.imag()),
//      (x.imag()*y.real()-x.real()*y.imag())/
//          (y.real()*y.real()+y.imag()*y.imag()));
}
Exemple #16
0
	string num_to_string(const complex<T>& num, int pad=0){
		string ret;
		// both components non-zero
		if(num.real()!=0 && num.imag()!=0){
			// don't add "+" in the middle if imag is negative and will start with "-"
			string ret=num_to_string(num.real(),/*pad*/0)+(num.imag()>0?"+":"")+num_to_string(num.imag(),/*pad*/0)+"j";
			if(pad==0 || (int)ret.size()>=pad) return ret;
			return string(pad-ret.size(),' ')+ret; // left-pad with spaces
		}
		// only imaginary is non-zero: skip the real part, and decrease padding to accomoadate the trailing "j"
		if(num.imag()!=0){
			return num_to_string(num.imag(),/*pad*/pad>0?pad-1:0)+"j";
		}
		// non-complex (zero or not)
		return num_to_string(num.real(),pad);
	}
Exemple #17
0
complex<Float> Comms::GlobalSum(complex<Float> z)
{

  if (!initialized) { Initialize(); }

#ifdef USE_MPI
  Float sendbuf[2];
  Float recvbuf[2];

  sendbuf[0] = z.real();
  sendbuf[1] = z.imag();

  MPI_Barrier(MPI_COMM_WORLD);

#ifdef USE_SINGLE
  MPI_Reduce( sendbuf, recvbuf, 2, MPI_FLOAT, MPI_SUM, 0, MPI_COMM_WORLD );
#endif
#ifdef USE_DOUBLE
  MPI_Reduce( sendbuf, recvbuf, 2, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD );
#endif
#ifdef USE_LONG_DOUBLE
  MPI_Reduce( sendbuf, recvbuf, 2, MPI_LONG_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD );
#endif

  return complex<Float>(recvbuf[0], recvbuf[1]);
#else
  return z;
#endif

}
/** Implement Exponential Integral function, E1(z), based on formulaes in
 *Abramowitz and Stegun (A&S)
 *  In fact this implementation returns exp(z)*E1(z) where z is a complex number
 *
 *  @param z :: input
 *  @return exp(z)*E1(z)
 */
complex<double> exponentialIntegral(const complex<double> &z) {
  double z_abs = abs(z);

  if (z_abs == 0.0) {
    // Basically function is infinite in along the real axis for this case
    return complex<double>(std::numeric_limits<double>::max(), 0.0);
  } else if (z_abs < 10.0) // 10.0 is a guess based on formula 5.1.55 (no idea
                           // how good it really is)
  {
    // use formula 5.1.11 in A&S. rewrite last term in 5.1.11 as
    // x*sum_n=0 (-1)^n x^n / (n+1)*(n+1)! and then calculate the
    // terms in the sum recursively
    complex<double> z1(1.0, 0.0);
    complex<double> z2(1.0, 0.0);
    for (int i = 1; i <= 100; i++) // is max of 100 here best?
    {
      z2 = -static_cast<double>(i) * (z2 * z) / ((i + 1.0) * (i + 1.0));
      z1 += z2;
      if (abs(z2) < 1.0E-10 * abs(z1))
        break; // i.e. if break loop if little change to term added
    }
    return exp(z) * (-log(z) - M_EULER + z * z1);
  } else {
    // use formula 5.1.22 in A&S. See discussion page 231
    complex<double> z1(0.0, 0.0);
    for (int i = 20; i >= 1; i--) // not sure if 20 is always sensible?
      z1 = static_cast<double>(i) / (1.0 + static_cast<double>(i) / (z + z1));
    complex<double> retVal = 1.0 / (z + z1);
    if (z.real() <= 0.0 && z.imag() == 0.0)
      retVal -= exp(z) * complex<double>(0.0, 1.0) * M_PI; // see formula 5.1.7
    return retVal;
  }
}
Exemple #19
0
complex<typename unary_plus_typeof_helper<X>::type>
operator+(const complex<X>& x)
{
    typedef typename unary_plus_typeof_helper<X>::type  type;
    
    return complex<type>(x.real(),x.imag());
}
Exemple #20
0
complex<typename unary_minus_typeof_helper<X>::type>
operator-(const complex<X>& x)
{
    typedef typename unary_minus_typeof_helper<X>::type type;
    
    return complex<type>(-x.real(),-x.imag());
}
Exemple #21
0
void write_mandel(std::string name, int x, int y)
{
  GIF_image gif(x, y);

  for(int i=0; i<256;i++)
  {
	  gif.setColor(i, i, i, (char)255);
  }

  for (int i = 0; i < y; i++) 
  { 
    for (int j = 0; j < x; j++) 
    {
      complex<double> c(c1.real()+j*(c2-c1).real()/X, c1.imag()+i*(c2-c1).imag()/Y);
      complex<double> z = 0;
      int iter = 0;
      
      while (abs(z) < 2 && iter < MAX_ITER) 
      {
		z = z*z+c;
		iter++;
      }
      gif.setPixel(j, i, ((MAX_ITER-iter)*255)/40);
    }
  }
  
  gif.writeToFile(name.c_str());
}
complex<double> cos(complex<double> x) { 
  double cosh(double x);
  double cos(double x);
  double r = cosh(x.imag())*cos(x.real());
  double i = sinh(x.imag())*sin(x.real());
  return(complex<double>(r,i));
}
  void put(index_type i, complex<T> val)
  {
    assert(this->format_ != no_user_format);

    if (this->format_ == array_format)
      this->u_.data_[i] = val;
    else if (this->format_ == interleaved_format)
    {
      this->u_.split_.real_[2*i+0] = val.real();
      this->u_.split_.real_[2*i+1] = val.imag();
    }
    else // if (format_ == split_format)
    {
      this->u_.split_.real_[i] = val.real();
      this->u_.split_.imag_[i] = val.imag();
    }
  }
Exemple #24
0
/* Performed function evaluation. Horners algorithm.
 */
static double feval( const int n, const double a[], const complex<double> z, complex<double> *fz )
   {
   register int i;
   double p, q, r, s, t;

   p = -2.0 * z.real();
   q = z.real() * z.real() + z.imag() * z.imag();
   s = 0; r = a[ 0 ];
   for( i = 1; i < n; i++ )
      {
      t = a[ i ] - p * r - q * s;
      s = r;
      r = t;
      }
   *fz = complex<double>( a[ n ] + z.real() * r - q * s, z.imag() * r );
   
   return fz->real() * fz->real() + fz->imag() * fz->imag();
   }
Exemple #25
0
complex<Y> 
pow(const complex<Y>& x,const Y& y)
{
    std::complex<Y> tmp(x.real(),x.imag());
    
    tmp = std::pow(tmp,y);
    
    return complex<Y>(tmp.real(),tmp.imag());
}
Exemple #26
0
float EnergyToBeat::transform_bwd (complex beat)
{
  m_mutex.lock();
  float pos_mean = m_pos_mean;
  float pos_variance = m_pos_variance;
  m_mutex.unlock();

  float pos = max(0.0f, pos_mean + sqrtf(pos_variance) * beat.real());

  return powf(pos, 3);
}
Exemple #27
0
QTextStream& operator<<(QTextStream& ts, KCValue value)
{
    ts << value.type();
    switch (value.type()) {
    case KCValue::Empty:   break;

    case KCValue::Boolean:
        ts << ": ";
        if (value.asBoolean()) ts << "TRUE";
        else ts << "FALSE"; break;

    case KCValue::Integer:
        ts << ": " << value.asInteger(); break;

    case KCValue::Float:
        ts << ": " << (double) numToDouble(value.asFloat()); break;

    case KCValue::Complex: {
        const complex<KCNumber> complex(value.asComplex());
        ts << ": " << (double) numToDouble(complex.real());
        if (complex.imag() >= 0.0)
            ts << '+';
        ts << (double) numToDouble(complex.imag()) << 'i';
        break;
    }

    case KCValue::String:
        ts << ": " << value.asString(); break;

    case KCValue::Array: {
        ts << ": {" << value.asString();
        const int cols = value.columns();
        const int rows = value.rows();
        for (int row = 0; row < rows; ++row) {
            for (int col = 0; col < cols; ++col) {
                ts << value.element(col, row);
                if (col < cols - 1)
                    ts << ';';
            }
            if (row < rows - 1)
                ts << '|';
        }
        ts << '}';
        break;
    }

    case KCValue::Error:
        ts << '(' << value.errorMessage() << ')'; break;

    default: break;
    }
    return ts;
}
Exemple #28
0
complex<long double> sum_to_all(complex<long double> in) {
  complex<long double> out = in;
#ifdef HAVE_MPI
  if (MPI_LONG_DOUBLE == MPI_DATATYPE_NULL) {
    complex<double> dout;
    dout = sum_to_all(complex<double>(double(in.real()), double(in.imag())));
    out = complex<long double>(dout.real(), dout.imag());
  }
  else
    MPI_Allreduce(&in,&out,2,MPI_LONG_DOUBLE,MPI_SUM,mycomm);
#endif
  return out;
}
void ElectronicStructure::phase(complex<double> Hii,double dt,int i){
/***********************************************************************
  Action of operator: exp(iL_ii*dt) = exp(-(i/hbar)*dt*(H_ii*c_i*d/dc_i)):

  c_i  = exp(-i*H_ii*dt/hbar) * c_i

  Because Hamiltonian is Hermitian its diagonal elements are fully real
***********************************************************************/

  double phi = -dt*Hii.real()/hbar;
  Ccurr->M[i] = std::complex<double>(cos(phi),sin(phi)) * Ccurr->M[i];

}
Exemple #30
0
/* Calculate a upper bound for the rounding errors performed in a
   polynomial at a complex point.
   ( Adam's test )
 */
static double upperbound( const int n, const double a[], const complex<double> z )
   {
   register int i;
   double p, q, r, s, t, u, e;

   p = - 2.0 * z.real();
   q = z.real() * z.real() + z.imag() * z.imag();
   u = sqrt( q );
   s = 0.0; r = a[ 0 ]; e = fabs( r ) * ( 3.5 / 4.5 );
   for( i = 1; i < n; i++ )
      {
      t = a[ i ] - p * r - q * s;
      s = r;
      r = t;
      e = u * e + fabs( t );
      }
   t = a[ n ] + z.real() * r - q * s;
   e = u * e + fabs( t );
   e = ( 9.0 * e - 7.0 * ( fabs( t ) + fabs( r ) * u ) +
       fabs( z.real() ) * fabs( r ) * 2.0 ) * pow( (double)FLT_RADIX, -DBL_MANT_DIG+1);

   return e * e;
   }