static int Xlune(struct place *place, double *x, double *y) { double stereox, stereoy; double z1x, z1y, z2x, z2y; double w1x, w1y, w2x, w2y; double numx, numy, denx, deny; if(place->nlat.l < eastpole.nlat.l-FUZZ) return -1; Xstereographic(place, &stereox, &stereoy); stereox *= scale; stereoy *= scale; z1x = 1 + stereox; z1y = stereoy; z2x = 1 - stereox; z2y = -stereoy; cpow(z1x,z1y,&w1x,&w1y,pwr); cpow(z2x,z2y,&w2x,&w2y,pwr); numx = w1x - w2x; numy = w1y - w2y; denx = w1x + w2x; deny = w1y + w2y; cdiv(numx, numy, denx, deny, x, y); return 1; }
int main(void) { double N = 20; double time = 1; double dlt = time / N; double t = 0; double n = 0; double _Complex c[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; double _Complex rev[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; double _Complex tmp = cexp(I*2*PI/N); for (int i=0; i<N; i++){ for (int k=0; k<N; k++) { c[i] += f(t) * cpow(tmp, i*k); t += dlt; } } for (int i=0; i<N; i++) { for (int k=0; k<N; k++) { rev[i] += c[k] * cpow(tmp, -i*k); } rev[i] /= N; } //元の関数 // t = 0; // for (int i=0; i<N; i++) { // printf("%f %f\n", t, f(t)); // t += dlt; // } //フーリエ変換 // for (int i=0; i<N; i++) // { // printf("%d %f\n", i, creal(c[i])); // } // printf("\n"); for (int i=0; i<N; i++) { printf("%d %f\n", i, cimag(c[i])); } printf("\n"); // //フーリエ逆変換 // t = 0; // for (int i=0; i<N; i++) { // printf("%f %f\n", t, creal(rev[i])); // t += dlt; // } }
/* * fft * * Takes an input vector, output vector and a number of points * Performs a Fast Fourier Transform on the input vector * and stores it in the output vector. */ void fft( vector *in, vector *out, int n ) { int k; if( n == 1 ) VEC( out, 0 ) = VEC( in, 0 ); else { complex double wn = cexp( -2*M_PI*I / n ); vector *x, *y, *p, *q; create_vector( &x, n/2 ); create_vector( &y, n/2 ); create_vector( &p, n/2 ); create_vector( &q, n/2 ); for( k = 0; k < n/2; k++ ) { VEC( x, k ) = VEC( in, 2*k ); VEC( y, k ) = VEC( in, 2*k + 1 ); } fft( x, p, n/2 ); fft( y, q, n/2 ); for( k = 0; k < n; k++ ) { VEC( out, k ) = VEC( p, k % (n/2) ) + cpow(wn,k)*VEC( q, k % (n/2) ); } destroy_vector( x ); destroy_vector( y ); destroy_vector( p ); destroy_vector( q ); } }
static GstFlowReturn transform_ip (GstBaseTransform * trans, GstBuffer * buf) { UnaryComplexPow *element = UNARY_COMPLEX_POW (trans); GstAudioFilter *audiofilter = GST_AUDIO_FILTER (trans); GstAudioFormat format = audiofilter->info.finfo->format; GstMapInfo info; gst_buffer_map (buf, &info, GST_MAP_READWRITE); gpointer data = info.data; gpointer data_end = data + info.size; const double n = element->exponent; if (format >= GST_AUDIO_FORMAT_F64) { double complex *ptr, *end = data_end; for (ptr = data; ptr < end; ptr++) *ptr = cpow (*ptr, n); } else if (format >= GST_AUDIO_FORMAT_F32) { float complex *ptr, *end = data_end; for (ptr = data; ptr < end; ptr++) *ptr = cpowf (*ptr, n); } else { g_assert_not_reached (); } gst_buffer_unmap (buf, &info); return GST_FLOW_OK; }
static int _complex_binary_op2(lua_State *L, ArrayBinaryOperation op) { Complex v = *((Complex*) luaL_checkudata(L, 1, "complex")); Complex w = *((Complex*) luaL_checkudata(L, 2, "complex")); Complex *z = (Complex*) lua_newuserdata(L, sizeof(Complex)); luaL_setmetatable(L, "complex"); switch (op) { case ARRAY_OP_ADD: *z = v + w; break; case ARRAY_OP_SUB: *z = v - w; break; case ARRAY_OP_MUL: *z = v * w; break; case ARRAY_OP_DIV: *z = v / w; break; case ARRAY_OP_POW: *z = cpow(v,w); break; case ARRAY_OP_IDIV: case ARRAY_OP_MOD: case ARRAY_OP_BAND: case ARRAY_OP_BOR: case ARRAY_OP_BXOR: case ARRAY_OP_SHL: case ARRAY_OP_SHR: { return luaL_error(L, "Invalid operation"); break; } } return 1; }
void taylor(float x, float y, float z, float kn[], float ks[], int polySize, float L, float k_RF, float v_n, float phi_n, float q, float V_RF, float ps, float c, float vrf){ float complex dpx = 0; float complex dpy = 0; float complex dpt = 0; float complex complex_powered = 0; float realQuanitity = 0; for (int n = polySize; n >= 0 ; n--){ realQuanitity = kn[n-1] * L * cos(v_n - k_RF*z); // real-valued portion of each Eqn, 166-168 complex_powered = cpow((x + I*y), n); // complex-valued portion of each Eqn, 166-168 dpx = -((realQuanitity + I*L*ks[n]*cos(phi_n - k_RF*z))*(complex_powered) + dpx) /n; // Eqn 166 dpy = ((realQuanitity + I*L*ks[n]*cos(phi_n - k_RF*z))*(complex_powered) + dpy) / n; // Eqn 167 dpt = ((realQuanitity + I*L*ks[n]*sin(phi_n - k_RF*z))*(complex_powered) + dpt) / n; // Eqn 168 } dpt = ((q*V_RF)/(ps*c))*sin(vrf - k_RF*z) - (k_RF * dpt) ; // Eqn 168 continued float deltapx = creal(dpx); float deltapy = cimag(dpy); float deltapt = creal(dpt); printf("(%f, %f, %f ) ", deltapx, deltapy, deltapt); } // end taylor
/* Returns the 2 complex roots of a degree 2 polynomial with real coefficients a[2]T^2 + ... + a[0] By convention, the real roots are stored first (same thing for multiple roots) : return value = number of roots, counted with multiplicity */ int rootDeg2(double complex a[3], double complex r[2]){ double complex Delta,delta,r1,r2; if( cabs(a[2])<_MMG5_EPSD ) { if( cabs(a[1])<_MMG5_EPSD ) { r[0] = r[1] = 0.0; return(0); } else{ r[0] = r[1] = -a[0]/a[1]; return(1); } } Delta = a[1]*a[1] - 4.0*a[2]*a[0]; /* ONE square root of Delta */ delta = cpow(Delta,0.5); r1 = 0.5/a[2]*(-a[1] - delta); r2 = 0.5/a[2]*(-a[1] + delta); if( fabs(cimag(r1)) < EPSRO ){ r[0] = creal(r1); r[1] = ( fabs(cimag(r2)) < EPSRO ) ? creal(r2) : r2; } else { r[0] = ( fabs(cimag(r2)) < EPSRO ) ? creal(r2) : r2; r[1] = r1; } return(2); }
/* Y. L. Luke, 'The Special Functions and Their Approximation', vol II, p 304 */ complex float cgamma (complex float z) { static double coeff[7] = {41.624436916439068, -51.224241022374774, 11.338755813488977, -0.747732687772388, 0.008782877493061, -1.899030264e-6, 1.946335e-9}; complex double s,H,w; int n; if(creal(z) < 0.0) { complex double denom = 1.0; int flr = -floor(creal(z)); for (n = 0; n < flr; ++n) denom = denom * (z + n); return cgamma(z + flr) / denom; } else { w = z - 1.0; s = coeff[0]; H=1.0; for(n=1; n<7; n++) { H *= (w+1-n) / (w+n); s += coeff[n] * H; } return( 2.506628274631 * cexp(-w-5.5) * cpow(w+5.5,w+0.5) * s ); } }
int mapValQ(int code, complex* pval, KQVEC(x), QVEC(r)) { TCF* x2p = (TCF*)xp; TCF* r2p = (TCF*)rp; int k; TCF val = *(TCF*)pval; REQUIRES(xn == rn,BAD_SIZE); DEBUGMSG("mapValQ"); switch (code) { OPVb(0,val*x2p[k]) OPVb(1,val/x2p[k]) OPVb(2,val+x2p[k]) OPVb(3,val-x2p[k]) OPVb(4,cpow(val,x2p[k])) OPVb(5,cpow(x2p[k],val)) default: ERROR(BAD_CODE); } }
inline double complex T2(double complex x, double complex z) { if (creal(x) >= -0.125 && cabs(f4(x,z)) < TOLERANCE2) { double complex temp = cpow(( -1.0 - 6.0 * x + 15.0 * x*x - 8.0 * x*x*x), ONE_THIRD) / 3.0; return c05 * (c1 - csqrt(f1(x) / 2.0 + temp) + csqrt( f1(x) - temp - c2 * x / csqrt(f1(x) / 2.0 + temp) )); } else { double complex temp = csqrt(f1(x) / 2.0 + f2(x,z) / (c3 * f4(x,z)) + f4(x,z) / c12); return c05 * (c1 - temp + csqrt( f1(x) - f2(x,z) / (c3 * f4(x,z)) - f4(x,z) / c12 - c2 * x / temp )); } }
QLA_D_Complex QLA_D_cpow(QLA_D_Complex *a, QLA_D_Real b) { double _Complex ca, cr; QLA_D_c99_eq_c(ca, *a); cr = cpow(ca, b); QLA_D_Complex r; QLA_D_c_eq_c99(r, cr); return r; }
static float FrequencyEvaluation(float Delta, float Coefficients[], int NumberOfCoefficients, float MeanSquareDiscrepancy) { double _Complex z = cexp(_Complex_I * Delta); double _Complex sum = 1.0 + 0.0 * _Complex_I; for (int i=1;i<=NumberOfCoefficients;i++) { sum -= Coefficients[i] * cpow(z,i); } return MeanSquareDiscrepancy/(sum*conj(sum)); }
int main(void) { float complex i = I; double _Complex another_i = i; put_complex(i); put_complex(another_i + 5.); put_complex(i * another_i); put_complex(cpow(I, CMPLX(6., 0.))); put_complex(csqrt(i)); }
//## Complex Complex.cpow(); static KMETHOD Complex_cpow(KonohaContext *kctx, KonohaStack *sfp) { kComplex *kx = (kComplex *) sfp[0].asObject; double _Complex x = kx->z; double real = (double)sfp[1].floatValue; double imaginary = (double)sfp[2].floatValue; double _Complex y = real + I * imaginary; double ret = cpow(x, y); KReturnFloatValue(ret); }
int mandel(double complex z) { int maxiter = 80; double complex c = z; for (int n=0; n<maxiter; ++n) { if (cabs(z) > 2.0) { return n; } z = cpow(z,2)+c; } return maxiter; }
int mandel(double complex z) { int n = 0; double complex c = z; for (n=0; n<=79; ++n) { if (cabs(z) > 2.0) { n -= 1; break; } z = cpow(z,2)+c; } return n+1; }
void sdl_draw_mandelbrot(SDL_Surface *surface, complex double center, double zoom) { int f,x,y,n; int maxiter = (WIDTH/2) * 0.049715909 * log10(zoom); complex double z, c; float C; static SDL_Rect rects[HEIGHT/FLIPS]; fprintf(stderr, "zoom: %f\n", zoom); fprintf(stderr, "center point: %f %+fi\n", creal(center), cimag(center) ); fprintf(stderr, "iterations: %d\n", maxiter); for (f = 0; f < FLIPS; f++) { for (y = f; y < HEIGHT; y += FLIPS) { for (x = 0; x < WIDTH; x++) { /* Get the complex poing on gauss space to be calculate */ z = c = creal(center) + ((x - (WIDTH/2))/zoom) + ((cimag(center) + ((y - (HEIGHT/2))/zoom))*_Complex_I); #define X creal(z) #define Y cimag(z) /* Check if point lies within the main cardiod or in the period-2 buld */ if ( (pow(X-.25,2) + pow(Y,2))*(pow(X,2) + (X/2) + pow(Y,2) - .1875) < pow(Y,2)/4 || pow(X+1,2) + pow(Y,2) < .0625 ) n = maxiter; else /* Applies the actual mandelbrot formula on that point */ for (n = 0; n <= maxiter && cabs(z) < BAIL_OUT; n ++) z = cpow(z, 2) + c; C = n - log2f(logf(cabs(z)) / M_LN2 ); /* Paint the pixel calculated depending on the number of iterations found */ ((Uint32*)surface->pixels)[(y*surface->w) + x] = (n >= maxiter)? 0 : SDL_MapRGB( surface->format, (1+sin(C*0.27 + 5))*127., (1+cos(C*0.85))*127., (1+sin(C*0.15))*127. ); } rects[y/FLIPS].x = 0; rects[y/FLIPS].y = y; rects[y/FLIPS].w = WIDTH; rects[y/FLIPS].h = 1; } SDL_UpdateRects(surface, HEIGHT/FLIPS, rects); } }
int main() { double complex z = 0.0 + 2.7 * I; double complex w = 2.7 + 0.0 * I; double complex c = cpow(w, z); // Raise e to the power of i*2.7 printf("%.2f %+.2f × I raised to the power of %.2f %+.2f × I \n" "is %.2f %+.2f × I.\n", creal(w), cimag(w), creal(z), cimag(z), creal(c), cimag(c)); return 0; }
void ovm_dd_pow(oregister_t *l, oregister_t *r) { switch (r->t) { case t_void: l->t = t_float; l->v.d = 1.0; break; case t_word: real(r->v.dd) = r->v.w; imag(r->v.dd) = 0.0; goto cdd; case t_float: real(r->v.dd) = r->v.d; imag(r->v.dd) = 0.0; goto cdd; case t_mpz: real(r->v.dd) = mpz_get_d(ozr(r)); imag(r->v.dd) = 0.0; goto cdd; case t_rat: real(r->v.dd) = rat_get_d(r->v.r); imag(r->v.dd) = 0.0; goto cdd; case t_mpq: real(r->v.dd) = mpq_get_d(oqr(r)); imag(r->v.dd) = 0.0; goto cdd; case t_mpr: mpc_set_fr(occ(r), orr(r), thr_rndc); goto mpc; case t_cdd: cdd: l->v.dd = cpow(l->v.dd, r->v.dd); check_cdd(l); break; case t_cqq: real(r->v.dd) = mpq_get_d(oqr(r)); imag(r->v.dd) = mpq_get_d(oqi(r)); goto cdd; case t_mpc: mpc: l->t = t_mpc; mpc_set_d_d(occ(l), real(l->v.dd), imag(l->v.dd), thr_rndc); mpc_pow(occ(l), occ(l), occ(r), thr_rndc); check_mpc(l); break; default: ovm_raise(except_not_a_number); } }
int m2_solve_quartic_equation(double d4, double d3, double d2, double d1, double d0, double roots[4]) { Complex a3 = d3 / d4; Complex a2 = d2 / d4; Complex a1 = d1 / d4; Complex a0 = d0 / d4; Complex W = 1./3; Complex X = 12*a0 - 3*a1*a3 + a2*a2; Complex P = -72*a0*a2 - 9*a1*a2*a3 + 27*a1*a1 + 27*a0*a3*a3 + 2*a2*a2*a2; Complex Q = cpow(X,3); Complex S = csqrt(-4*Q + cpow(P,2)); Complex T = -8*a1 + 4*a2*a3 - a3*a3*a3; Complex B = cabs(P + S) < 1e-15 ? 0.0 : (cpow(2,W)*X)/(3.*cpow(P + S,W)); Complex U = (-2*a2)/3. + (a3*a3)/4. + B; Complex C = csqrt(U + cpow(P + S,W)/(3.*cpow(2,W)))/2.; Complex D = cpow(P + S,W)/(3.*cpow(2,W)); Complex E = T/(4.*csqrt(U + D)); Complex F = csqrt((-4*a2)/3. + (a3*a3)/2. - B - D - E)/2.; Complex G = csqrt((-4*a2)/3. + (a3*a3)/2. - B - D + E)/2.; Complex r0 = -a3/4. - C - F; Complex r1 = -a3/4. - C + F; Complex r2 = -a3/4. + C - G; Complex r3 = -a3/4. + C + G; roots[0] = creal(r0); roots[1] = creal(r1); roots[2] = creal(r2); roots[3] = creal(r3); if (roots[0] != roots[0] || roots[1] != roots[1] || roots[2] != roots[2] || roots[3] != roots[3]) { /* set breakpoint */ return 0; } /* int nr = 0; */ /* if (fabs(cimag(r0)) < 1e-10) ++nr; */ /* if (fabs(cimag(r1)) < 1e-10) ++nr; */ /* if (fabs(cimag(r2)) < 1e-10) ++nr; */ /* if (fabs(cimag(r3)) < 1e-10) ++nr; */ /* the check for realness of roots is hard to make robust */ return 4; }
double complex Heston::logCF(const double &S, const double complex &u) const { double complex temp1,temp2,temp3, v1, v2,v3, gamma, result; v1 = kappa-I*rho*sigma*u; v2 = u*u+I*u; v3 = kappa*theta/(sigma*sigma); gamma = sigma*sigma*v2+v1*v1; gamma = cpow(gamma,.5); temp1 = cexp(I*u*(log(S)+(r-q)*T)+v3*T*v1); temp2 = ccosh(gamma*T/2.0)+v1/gamma*csinh(gamma*T/2.0); result = temp1/cpow(temp2,2.0*v3); temp1 = v0*v2; //v0 is class variable defined in .h file temp2 = gamma/ctanh(gamma*T/2.0)+v1; temp3 = cexp(-temp1/temp2); result = result*temp3; return result; }
//## Complex Complex.cpowl(); static KMETHOD Complex_cpowl(KonohaContext *kctx, KonohaStack *sfp) { kComplex *kx = (kComplex *) sfp[0].asObject; long double _Complex x = (long double _Complex)kx->z; long double real = (long double)sfp[1].floatValue; long double imaginary = (long double)sfp[2].floatValue; long double _Complex y = real + I * imaginary; #if !defined(__CYGWIN__) long double ret = cpowl(x, y); #else long double ret = cpow(x, y); #endif KReturnFloatValue(ret); }
void cmplx (double _Complex z) { cabs (z); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..complex.h.." "" { target *-*-* } 129 } */ cacos (z); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..complex.h.." "" { target *-*-* } 131 } */ cacosh (z); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..complex.h.." "" { target *-*-* } 133 } */ carg (z); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..complex.h.." "" { target *-*-* } 135 } */ casin (z); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..complex.h.." "" { target *-*-* } 137 } */ casinh (z); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..complex.h.." "" { target *-*-* } 139 } */ catan (z); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..complex.h.." "" { target *-*-* } 141 } */ catanh (z); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..complex.h.." "" { target *-*-* } 143 } */ ccos (z); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..complex.h.." "" { target *-*-* } 145 } */ ccosh (z); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..complex.h.." "" { target *-*-* } 147 } */ cexp (z); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..complex.h.." "" { target *-*-* } 149 } */ cimag (z); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..complex.h.." "" { target *-*-* } 151 } */ clog (z); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..complex.h.." "" { target *-*-* } 153 } */ conj (z); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..complex.h.." "" { target *-*-* } 155 } */ cpow (z, z); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..complex.h.." "" { target *-*-* } 157 } */ cproj (z); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..complex.h.." "" { target *-*-* } 159 } */ creal (z); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..complex.h.." "" { target *-*-* } 161 } */ csin (z); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..complex.h.." "" { target *-*-* } 163 } */ csinh (z); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..complex.h.." "" { target *-*-* } 165 } */ csqrt (z); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..complex.h.." "" { target *-*-* } 167 } */ ctan (z); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..complex.h.." "" { target *-*-* } 169 } */ ctanh (z); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..complex.h.." "" { target *-*-* } 171 } */ }
PYMIC_KERNEL void pymic_offload_array_pow(const int64_t *dtype, const int64_t *n, const void *x_, const int64_t *incx, const void *y_, const int64_t *incy, void *r_, const int64_t *incr) { /* pymic_offload_array_pow(int dtype, int n, type *x, int incx, type *y, int incy, type *result, int incr) */ size_t i, ix, iy, ir; switch(*dtype) { case DTYPE_INT: { const int64_t *x = (const int64_t *)x_; const int64_t *y = (const int64_t *)y_; int64_t *r = (int64_t *)r_; i = ix = iy = ir = 0; for (; i < *n; i++, ix += *incx, iy += *incy, ir += *incr) { size_t j; r[ir] = 1; for (j = 0; j < y[iy]; j++) { r[ir] *= x[ix]; } } } break; case DTYPE_FLOAT: { const double *x = (const double *)x_; const double *y = (const double *)y_; double *r = (double *)r_; i = ix = iy = ir = 0; for (; i < *n; i++, ix += *incx, iy += *incy, ir += *incr) { r[ir] = pow(x[ix], y[iy]); } } break; case DTYPE_COMPLEX: { const double complex *x = (const double complex *)x_; const double complex *y = (const double complex *)y_; double complex *r = (double complex *)r_; i = ix = iy = ir = 0; for (; i < *n; i++, ix += *incx, iy += *incy, ir += *incr) { r[ir] = cpow(x[ix], y[iy]); } } break; } }
int _complex_binary_op2(lua_State *L, enum ArrayOperation op) { Complex v = *((Complex*) luaL_checkudata(L, 1, "complex")); Complex w = *((Complex*) luaL_checkudata(L, 2, "complex")); Complex *z = (Complex*) lua_newuserdata(L, sizeof(Complex)); luaL_getmetatable(L, "complex"); lua_setmetatable(L, -2); switch (op) { case ARRAY_OP_ADD: *z = v + w; break; case ARRAY_OP_SUB: *z = v - w; break; case ARRAY_OP_MUL: *z = v * w; break; case ARRAY_OP_DIV: *z = v / w; break; case ARRAY_OP_POW: *z = cpow(v,w); break; } return 1; }
int main() { double complex a = 32.123 + 24.456 * I; // a is 32.123 + 24.456i double complex b = 23.789 + 42.987 * I; // b is 23.789 + 42.987i double complex c = 3 + 2 * I; // c is 3.0 + 2.0i double complex sum = a + b; // perform complex addition double complex pwr = cpow(a, c); // perform complex exponentiation printf("a is %f + %fi\n", creal(a), cimag(a)); printf("b is %f + %fi\n", creal(b), cimag(b)); printf("a + b is: %f + %fi\n", creal(sum), cimag(sum)); printf("a - b is: %f + %fi\n", creal(a - b), cimag(a - b)); printf("a * b is: %f + %fi\n", creal(a * b), cimag(a * b)); printf("a / b is: %f + %fi\n", creal(a / b), cimag(a / b)); printf("a ^ b is: %f + %fi\n", creal(pwr), cimag(pwr)); return EXIT_SUCCESS; }
double compute_mandelbrot(double _Complex c, int maxiter){ double _Complex z = 0; int value = 0; int diverge = 0; int n; for (n=0; n < maxiter; n++){ z = cpow (z, 2) + c; if (cabs(z) > 2){ value = n; diverge = 1; break; } } if (diverge == 1){ return value; } else{ return 2; } }
/* Computes the Gamma function using the Lanczos approximation */ static double complex gamma_fftlog(double complex z) { /* Lanczos coefficients for g = 7 */ static double p[] = { 0.99999999999980993227684700473478, 676.520368121885098567009190444019, -1259.13921672240287047156078755283, 771.3234287776530788486528258894, -176.61502916214059906584551354, 12.507343278686904814458936853, -0.13857109526572011689554707, 9.984369578019570859563e-6, 1.50563273514931155834e-7 }; if(creal(z) < 0.5) return M_PI / (sin(M_PI*z)*gamma_fftlog(1. - z)); z -= 1; double complex x = p[0]; for(int n = 1; n < 9; n++) x += p[n] / (z + (double)(n)); double complex t = z + 7.5; return sqrt(2*M_PI) * cpow(t, z+0.5) * cexp(-t) * x; }
static double complex mycpow (double complex X, double complex Y) { double complex Z; double yr = creal(Y), yi = cimag(Y); int k; if (X == 0.0) { if (yi == 0.0) Z = R_pow(0.0, yr); else Z = R_NaN + R_NaN*I; } else if (yi == 0.0 && yr == (k = (int) yr) && abs(k) <= 65536) Z = R_cpow_n(X, k); else #ifdef HAVE_CPOW Z = cpow(X, Y); #else { /* Used for FreeBSD and MingGW, hence mainly with gcc */ double rho, r, i, theta; r = hypot(creal(X), cimag(X)); i = atan2(cimag(X), creal(X)); theta = i * yr; if (yi == 0.0) rho = pow(r, yr); else { /* rearrangement of cexp(X * clog(Y)) */ r = log(r); theta += r * yi; rho = exp(r * yr - i * yi); } #ifdef __GNUC__ __real__ Z = rho * cos(theta); __imag__ Z = rho * sin(theta); #else Z = rho * cos(theta) + (rho * sin(theta)) * I; #endif } #endif return Z; }
/* * fft_mpi * * Takes an input vector, output vector and a number of points * Performs a Fast Fourier Transform on the input vector * and stores it in the output vector. * * Uses MPI for parallelization */ void fft_mpi( vector *in, vector *out, int n ) { int rank, size; MPI_Comm_rank( MPI_COMM_WORLD, &rank ); MPI_Comm_size( MPI_COMM_WORLD, &size ); fprintf( stderr, "%d/%d\n", rank, size ); int k; if( n == 1 ) VEC( out, 0 ) = VEC( in, 0 ); else { complex double wn = cexp( -2*M_PI*I / n ); vector *x, *y, *p, *q; create_vector( &x, n/2 ); create_vector( &y, n/2 ); create_vector( &p, n/2 ); create_vector( &q, n/2 ); for( k = 0; k < n/2; k++ ) { //x[k] = in[2*k] //y[k] = in[2*k + 1] VEC( x, k ) = VEC( in, 2*k ); VEC( y, k ) = VEC( in, 2*k + 1 ); } fft( x, p, n/2 ); fft( y, q, n/2 ); for( k = 0; k < n; k++ ) { //out[k] = p[ k % (n/2) ] + pow(w,k)*q[k % (n/2) ]; VEC( out, k ) = VEC( p, k % (n/2) ) + cpow(wn,k)*VEC( q, k % (n/2) ); } destroy_vector( x ); destroy_vector( y ); destroy_vector( p ); destroy_vector( q ); } }