t_float3 surface3d :: cubic3(t_float3 X0, t_float3 X1, t_float3 X2, t_float3 X3, t_float fract) { t_float3 out; out.x = cubic(X0.x,X1.x,X2.x,X3.x,fract); out.y = cubic(X0.y,X1.y,X2.y,X3.y,fract); out.z = cubic(X0.z,X1.z,X2.z,X3.z,fract); return(out); }
void Transform::lerp(const Transform& transform, float t, int spin) { if(transform.curve_type == "quadratic") { t = quadratic(0, c1, 1, t); } else if(transform.curve_type == "cubic") { t = cubic(0, c1, c2, 1, t); } else if(transform.curve_type == "quartic") { t = quartic(0, c1, c2, c3, 1, t); } else if(transform.curve_type == "quintic") { t = quintic(0, c1, c2, c3, c4, 1, t); } x = lerp(x, transform.x, t); y = lerp(y, transform.y, t); alpha = lerp(alpha, transform.alpha, t); // 'spin' is based on what you are coming from (key1) if(spin != 0) { if(spin > 0 && angle > transform.angle) angle = lerp(angle, transform.angle + 360, t); else if(spin < 0 && angle < transform.angle) angle = lerp(angle, transform.angle - 360, t); else angle = lerp(angle, transform.angle, t); } if(angle > 360) { angle = angle - 360; } scale_x = lerp(scale_x, transform.scale_x, t); scale_y = lerp(scale_y, transform.scale_y, t); }
static double interp_cubic(double f0, double fp0, double f1, double fp1, double zl, double zh) { double eta = 3 * (f1 - f0) - 2 * fp0 - fp1; double xi = fp0 + fp1 - 2 * (f1 - f0); double c0 = f0, c1 = fp0, c2 = eta, c3 = xi; double zmin, fminn; double z0, z1; zmin = zl; fminn = cubic(c0, c1, c2, c3, zl); check_extremum(c0, c1, c2, c3, zh, &zmin, &fminn); { int n = gsl_poly_solve_quadratic(3 * c3, 2 * c2, c1, &z0, &z1); if (n == 2) { /* found 2 roots */ if (z0 > zl && z0 < zh) check_extremum(c0, c1, c2, c3, z0, &zmin, &fminn); if (z1 > zl && z1 < zh) check_extremum(c0, c1, c2, c3, z1, &zmin, &fminn); } else if (n == 1) { /* found 1 root */ if (z0 > zl && z0 < zh) check_extremum(c0, c1, c2, c3, z0, &zmin, &fminn); } } return zmin; }
double sin_p(double x) { if (fabs(x) < 0.0001) { return x; } double sx = sin_p(x / 3); return 3 * sx - 4 * cubic(sx); }
PRIVATE void expand_line( double *dest, double *src, int bytes, int old_width, int width ) { double ratio; int x,b; int src_col; double frac; double *s; ratio = old_width / (double) width; /* we can overflow src's boundaries, so we expect our caller to have allocated extra space for us to do so safely (see scale_region ()) */ /* this could be optimized much more by precalculating the coefficients for each x */ for( x = 0; x < width; ++x ) { src_col = ((int) (x * ratio + 2.0 - 0.5)) - 2; /* +2, -2 is there because (int) rounds towards 0 and we need to round down */ frac = (x * ratio - 0.5) - src_col; s = &src[ src_col * bytes ]; for( b = 0 ; b < bytes ; b++ ) dest[ b ] = cubic( frac, (int)s[ b - bytes ], (int)s[ b ], (int)s[ b + bytes ], (int)s[ b + bytes * 2 ] ); dest += bytes; } }
nr_double_t mscross::calcCap (nr_double_t W1, nr_double_t h, nr_double_t W2) { nr_double_t W1h = W1 / h; nr_double_t W2h = W2 / h; nr_double_t X = log10 (W1h) * (86.6 * W2h - 30.9 * sqrt (W2h) + 367) + cubic (W2h) + 74 * W2h + 130; return 1e-12 * W1 * (0.25 * X * pow (W1h, -1.0 / 3.0) - 60 + 1 / W2h / 2 - 0.375 * W1h * (1 - W2h)); }
static gdouble omega (gdouble u, gdouble v, gint i, gint j) { while (i < 0) i += numx; while (j < 0) j += numy; i %= numx; j %= numy; return cubic (u) * cubic (v) * (G[i][j][0]*u + G[i][j][1]*v); }
cl_float *hb_bicubic_weights(cl_float scale, int length) { cl_float *weights = (cl_float*) malloc(length * sizeof(cl_float) * 4); int i; // C rocks cl_float *out = weights; for (i = 0; i < length; ++i) { cl_float x = i / scale; cl_float dx = x - (int)x; *out++ = cubic(-dx - 1.0f); *out++ = cubic(-dx); *out++ = cubic(-dx + 1.0f); *out++ = cubic(-dx + 2.0f); } return weights; }
BFBddVarCube BFBddManager::computeCube(const BFBdd *vars, const int * phase, int n) const { DdNode **vars2 = new DdNode*[n]; for (int i = 0; i < n; i++) vars2[i] = vars[i].node; DdNode *cube = Cudd_bddComputeCube(mgr, vars2, const_cast<int*> (phase), n); BFBddVarCube cubic(mgr, cube, n); delete[] vars2; return cubic; }
namespace perm_details { const double p_grad = atm / (prefix::centi*meter); const double area = square(prefix::centi*meter); const double flux = cubic (prefix::centi*meter) / second; const double velocity = flux / area; const double visc = prefix::centi*Poise; const double darcy = (velocity * visc) / p_grad; // == 1e-7 [m^2] / 101325 // == 9.869232667160130e-13 [m^2] }
int main ( int argc, char **argv ) { double x = 9.8; fprintf( stdout, "Square of %5.2f is %18.6f\n", x, square(x) ); fprintf( stdout, "Cubic of %5.2f is %18.6f\n", x, cubic(x) ); fprintf( stdout, "x^4 of %5.2f is %18.6f\n", x, power4(x) ); fprintf( stdout, "x^5 of %5.2f is %18.6f\n", x, power5(x) ); getchar(); return 0; }
int main (void) { int x; scanf("%d\n",&x); printf("x^2= %d\n",square(x)); printf("x^3= %d\n",cubic(x)); printf("x=-x %d\n",abs(x)); system("pause"); return 0; }
void check_extremum (double c0, double c1, double c2, double c3, double z, double *zmin, double *fmin){ /* could make an early return by testing curvature >0 for minimum */ double y = cubic (c0, c1, c2, c3, z); if (y < *fmin) { *zmin = z; /* accepted new point*/ *fmin = y; } }
static void InterpolateBicubic(const ImageData *image, DBL xcoor, DBL ycoor, RGBFTColour& colour, int *index, bool premul) { int iycoor, ixcoor; int cornerIndex; RGBFTColour cornerColour; DBL factor; DBL factorsX[4]; DBL factorsY[4]; xcoor += 0.5; ycoor += 0.5; iycoor = (int)ycoor; ixcoor = (int)xcoor; cubic(factorsX, xcoor); cubic(factorsY, ycoor); // We're using double precision for the colors here to avoid higher-than-1.0 results due to rounding errors, // which would otherwise lead to stray dot artifacts when clamped to [0..1] range for a color_map or similar. // (Note that strictly speaking we don't avoid such rounding errors, but rather make them small enough that // subsequent rounding to single precision will take care of them.) // (Note that bicubic interpolation may still give values outside the range [0..1] at high-contrast edges; // this is an inherent property of this interpolation method, and is therefore accepted here.) PreciseRGBFTColour tempColour; DBL tempIndex = 0; for (int i = 0; i < 4; i ++) { for (int j = 0; j < 4; j ++) { cornerColour.Clear(); no_interpolation(image, (DBL)ixcoor + i-2, (DBL)iycoor + j-2, cornerColour, &cornerIndex, premul); factor = factorsX[i] * factorsY[j]; tempColour += PreciseRGBFTColour(cornerColour) * factor; tempIndex += cornerIndex * factor; } } colour = RGBFTColour(tempColour); *index = (int)tempIndex; }
BFBddVarCube BFBddManager::computeCube(const std::vector<BFBdd> &vars) const { DdNode **vars2 = new DdNode*[vars.size()]; int *phase = new int[vars.size()]; for (unsigned int i = 0; i < vars.size(); i++) { vars2[i] = vars[i].node; phase[i] = 1; } DdNode *cube = Cudd_bddComputeCube(mgr, vars2, phase, vars.size()); BFBddVarCube cubic(mgr, cube, vars.size()); delete[] vars2; delete[] phase; return cubic; }
double rateCurve(const column_vector& params) { double distances = 0; for (Point target : points) { double distance = _DMAX; for (double t = 0; t <= 1; t += 0.01) { Point pt = cubic(params(0,0), params(1,0), params(2,0), params(3,0), params(4,0), params(5,0), params(6,0), params(7,0), t); double x = pt.x - target.x; double y = pt.y - target.y; distance = std::min(distance, ((x * x) + (y * y)) ); } distances += distance; } Point ptStart = cubic(params(0,0), params(1,0), params(2,0), params(3,0), params(4,0), params(5,0), params(6,0), params(7,0), 0); Point ptEnd = cubic(params(0,0), params(1,0), params(2,0), params(3,0), params(4,0), params(5,0), params(6,0), params(7,0), 1); double x = ptEnd.x - ptStart.x; double y = ptEnd.y - ptStart.y; double distance = (x * x) + (y * y); return distances + sqrt(distance); }
/* Function: rotateByCubic Rotates the image a given angle by applying cubic method Parameters: image - Image to be rotated. width - The image's width. height - The image's height. depth - The image's color depth. angle - clockwise rotation angle new_width - New image's width new_height - New image's height Returns: ret - The image rotated counter-clockwise by an angle */ void rotateByCubic (int *image, int *width, int *height, int *depth, double *angle, int *ret, int *new_width, int *new_height){ int x_center, y_center, x_diff, y_diff; int x, y, d; double x_src_d, y_src_d; int x_src, y_src; double x_weight, y_weight; while (*angle >= 360){ *angle = *angle - 360; } x_center = *width/2; y_center = *height/2; x_diff = (*new_width - *width)/2; y_diff = (*new_height - *height)/2; for (y = -1*y_diff; y < (*new_height - y_diff); y++){ for (x = -1*x_diff; x < (*new_width - x_diff); x++){ x_src_d = (x - x_center) * cos(*angle) + (y - y_center) * sin(*angle) + x_center; y_src_d = (y - y_center) * cos(*angle) - (x - x_center) * sin(*angle) + y_center; x_src = (int) x_src_d; y_src = (int) y_src_d; x_weight = x_src_d - x_src; y_weight = y_src_d - y_src; for (d = 0; d < *depth; d++){ int src = IMGPOS(x_src, y_src, d, *width, *height); int next = IMGPOS(x + x_diff, y + y_diff, d, *new_width, *new_height); if (x_src_d < 0 || y_src_d < 0 || x_src_d >= *width || y_src_d >= *height){ ret[next] = 0; }else{ int a[4][4]; int m,n; for (n = 0; n < 4; n++){ for (m = 0; m < 4; m++){ int xs = x_src - 1 + m, ys = y_src - 1 + n; if (xs < 0 || ys < 0 || xs >= *width || ys >= *height){ a[n][m] = image[src]; }else{ a[n][m] = image[IMGPOS(xs, ys, d, *width, *height)]; } } } ret[next] = (int) cubic(a, x_weight, y_weight, -1.0); } } } } }
/*========================================================================== * calc_virial: calculates the virial overdensity *==========================================================================*/ double calc_virial(double a) { double virial, age, omega_ta, eta, reduce, t1, t2; /* Check for a manual overdensity and return it if specified. Nothing else to do here then. */ if(simu.UserDvir > 0) virial = (double)(simu.UserDvir); else { #ifdef DARK_ENERGY /* Calculation of STH collapse is NOT implemented for Dark Energy models. Print a warning message, here or somewhere else */ fprintf(stderr, "Warning: the calculation of the virial overdensity in dynamical dark energy cosmologies is NOT implemented in AHF.\n"); #else /* Figure out how old the universe is */ age = calc_t(a); /* Figure out how much overdensity we need to reach maximum expansion by half the age of the universe. The overdense expansion factor is defined to be 1 at maximum. */ omega_ta = collapse(age/(double)2., (double)1.e-8); /* Figure out how far an object collapses to Virial equilibrium. */ eta = (double)2.*simu.lambda0/omega_ta; ///pow3(a); this obscure a^3 factor will prevent virial to approach the SCDM value of 178 at high redshift; not sure why it was there in the first place! if (eta < ZERO) { reduce = 0.5; } else { t1 = 2.*eta; t2 = -(2. + eta); reduce = cubic(t1, (double)0., t2, (double)1.); } /* as found in M.A.K. Gross' original version */ virial = pow3(a) * omega_ta/simu.omega0/pow3(reduce); /* check whether we want virial in terms of RhoCrit */ if(simu.UseRhoBack == FALSE) virial *= calc_omega(a); #endif } return (virial); }
QPainterPath ConnectionPainter:: cubicPath(ConnectionGeometry const& geom) { QPointF const& source = geom.source(); QPointF const& sink = geom.sink(); auto c1c2 = geom.pointsC1C2(); // cubic spline QPainterPath cubic(source); cubic.cubicTo(c1c2.first, c1c2.second, sink); return cubic; }
double cubicinterp ( double p1a, double p1b, double p1c, double p1d, double p2a, double p2b, double p2c, double p2d, double valuein ) { double kc0, kc1, kc2, kc3, ac0, ac1, ac2, ac3, r1r, r1i, r2r, r2i, r3r, r3i, value; // -------- assign function points --------- cubicspl(p1a, p1b, p1c, p1d, ac0, ac1, ac2, ac3); cubicspl(p2a, p2b, p2c, p2d, kc0, kc1, kc2, kc3); // recover the original function values // use the normalized time first, but at an arbitrary interval cubic(kc3, kc2, kc1, kc0-valuein, 'R', r1r, r1i, r2r, r2i, r3r, r3i); if ((r1r >= -0.000001) && (r1r <= 1.001)) { value = r1r; } else { if ((r2r >= -0.000001) && (r2r <= 1.001)) { value = r2r; } else { if ((r3r >= -0.000001) && (r3r <= 1.001)) { value = r3r; } else { value = 0.0; printf("error in cubicinterp root %17.14f %11.7f %11.7f %11.7f \n", valuein,r1r,r2r,r3r); } } } return (ac3*pow(value,3) + ac2*value*value + ac1*value + ac0); } // cubicinterp
static PyObject * cubicInterpolation(PyObject * self, PyObject * args) { int i; int ** vals = malloc(sizeof(int *)*4); for (i=0; i < 4; i++) { vals[i] = malloc(sizeof(int)*4); } float p, q; int result; if (! PyArg_ParseTuple(args, "((iiii)(iiii)(iiii)(iiii))ff", &vals[0][0], &vals[0][1], &vals[0][2], &vals[0][3], &vals[1][0], &vals[1][1], &vals[1][2], &vals[1][3], &vals[2][0], &vals[2][1], &vals[2][2], &vals[2][3], &vals[3][0], &vals[3][1], &vals[3][2], &vals[3][3], &p, &q)) { return NULL; } result = cubic(vals, p, q); return Py_BuildValue("i", result); }
double *advection(int N, int M) { double *u = (double *) malloc(N*sizeof(double)); double *v = (double *) malloc(N*sizeof(double)); double dx = (B-A)/N; double *dd = NULL; int i, j; FILE *gpp = gpinit(); for (i = 0; i < N; i++) {u[i] = U(0, X(i,N));} for (i = 0; i < N; i++) {v[i] = V(0, X(i,N));} plot(gpp, u, N); for (j = 0; j < M; j++) { for (i = N-1; i >= 0; i--) { dd = cubic(dx, u[O(i,N)-1], v[O(i,N)-1], u[i], v[i]); u[i] = dd[0]; v[i] = dd[1]; // u[i] = linear(-dx, u[O(i,N)-1], 0, u[i], -K*dx); } plot(gpp, u, N); } sleep(2); pclose(gpp); if(dd) {free(dd);} free(v); return u; }
int main () { root(3.5, 3); root(7.0); std::cout << "Square of 5 is " << square(5) << '\n'; double y= 5.0; std::cout << "Cubic of 5 is " << cubic(y) << '\n'; int x= 3, i= 4; std::cout << "3 ^ 4 is " << power(x-i, x+i) << '\n'; std::cout << "power(x) is " << power(x) << '\n'; std::cout << "divide(5, 3) " << divide(5, 3) << '\n'; std::cout << "divide(5.0, 3.0) " << divide(5.0f, 3.0f) << '\n'; std::cout << "divide(5.0, 3.0) " << divide(5.0, 3.0) << '\n'; std::cout << "sqrt(5.) is " << squareroot(5.) << '\n'; increment(x); //increment(x + 9); return 0 ; }
void ComputeGasPropertiesForAllSystems(void) { int i,j,k; REAL alpha,w,kappa,Tc,Pc,Tr; REAL Amix,Bmix; REAL excess_volume,Pressure,Temperature,FrameworkMass; int NumberOfSolutions; REAL Coefficients[4]; REAL number_of_unit_cells,temp; for(CurrentSystem=0;CurrentSystem<NumberOfSystems;CurrentSystem++) { FrameworkMass=0.0; for(i=0;i<Framework[CurrentSystem].NumberOfFrameworks;i++) FrameworkMass+=Framework[CurrentSystem].FrameworkMassPerComponent[i]; // add the mass of cations to the framework for(i=0;i<NumberOfComponents;i++) if((!Components[i].Swapable)&&(Components[i].ExtraFrameworkMolecule)) FrameworkMass+=Components[i].NumberOfMolecules[CurrentSystem]* Components[i].Mass; Framework[CurrentSystem].FrameworkMass=FrameworkMass; Framework[CurrentSystem].FrameworkDensity=FrameworkMass/(Volume[CurrentSystem]*CUBE(ANGSTROM)*AVOGADRO_CONSTANT); number_of_unit_cells=NumberOfUnitCells[CurrentSystem].x*NumberOfUnitCells[CurrentSystem].y*NumberOfUnitCells[CurrentSystem].z; for(i=0;i<NumberOfComponents;i++) { // molec/uc -> mol/kg Components[i].MOLEC_PER_UC_TO_MOL_PER_KG[CurrentSystem]=1000.0*number_of_unit_cells/FrameworkMass; // molec/uc -> g/g Components[i].MOLEC_PER_UC_TO_GRAM_PER_GRAM_OF_FRAMEWORK[CurrentSystem]=Components[i].Mass*1000.0*number_of_unit_cells/FrameworkMass; } // use global pressure to compute partial pressures if(therm_baro_stats.ExternalPressure[CurrentSystem][CurrentIsothermPressure]>=0.0) { for(i=0;i<NumberOfComponents;i++) Components[i].PartialPressure[CurrentSystem]=therm_baro_stats.ExternalPressure[CurrentSystem][CurrentIsothermPressure]* Components[i].MolFraction[CurrentSystem]; } else // check that all partial pressure are properly defined { for(i=0;i<NumberOfComponents;i++) { if(Components[i].Swapable) { if(Components[i].PartialPressure[CurrentSystem]<0.0) { printf("Error: Partial pressure of component %d [%s] is NOT set !!\n", i,Components[i].Name); exit(-1); } } } } // check: critical T and/or P not set, and also fugacity coefficient is not initialized for(i=0;i<NumberOfComponents;i++) { if(Components[i].Swapable) { Tc=Components[i].CriticalTemperature; Pc=Components[i].CriticalPressure; if((Tc<1e-10)||(Pc<1e-10)) { if(Components[i].FugacityCoefficient[CurrentSystem]<0.0) { printf("Error: Fugacity coefficient of component %d [%s] is NOT set for system %d,\n" " Critial pressure/temperature NOT set either (set to zero in molecule-definition)\n", i,Components[i].Name,CurrentSystem); exit(-1); } } } } // here, use pressure in Pascal and temperature in Kelvin Pressure=therm_baro_stats.ExternalPressure[CurrentSystem][CurrentIsothermPressure]*PRESSURE_CONVERSION_FACTOR; Temperature=therm_baro_stats.ExternalTemperature[CurrentSystem]; for(i=0;i<NumberOfComponents;i++) { if(Components[i].Swapable) { Tc=Components[i].CriticalTemperature; Pc=Components[i].CriticalPressure; w=Components[i].AcentricFactor; Tr=Temperature/Tc; switch(EquationOfState) { case SOAVE_REDLICH_KWONG: kappa=0.480+1.574*w-0.176*SQR(w); alpha=SQR(1.0+kappa*(1.0-sqrt(Tr))); a[i]=0.42748*alpha*SQR(MOLAR_GAS_CONSTANT*Tc)/Pc; b[i]=0.08664*MOLAR_GAS_CONSTANT*Tc/Pc; A[i]=a[i]*Pressure/SQR(MOLAR_GAS_CONSTANT*Temperature); B[i]=b[i]*Pressure/(MOLAR_GAS_CONSTANT*Temperature); break; case PENG_ROBINSON_GASEM: kappa=0.134+0.508*w-0.0467*SQR(w); alpha=exp((2.0+0.836*Tr)*(1.0-pow(Tr,kappa))); a[i]=0.45724*alpha*SQR(MOLAR_GAS_CONSTANT*Tc)/Pc; b[i]=0.07780*MOLAR_GAS_CONSTANT*Tc/Pc; A[i]=a[i]*Pressure/SQR(MOLAR_GAS_CONSTANT*Temperature); B[i]=b[i]*Pressure/(MOLAR_GAS_CONSTANT*Temperature); break; case PENG_ROBINSON: default: kappa=0.37464+1.54226*w-0.26992*SQR(w); alpha=SQR(1.0+kappa*(1.0-sqrt(Tr))); a[i]=0.45724*alpha*SQR(MOLAR_GAS_CONSTANT*Tc)/Pc; b[i]=0.07780*MOLAR_GAS_CONSTANT*Tc/Pc; A[i]=a[i]*Pressure/SQR(MOLAR_GAS_CONSTANT*Temperature); B[i]=b[i]*Pressure/(MOLAR_GAS_CONSTANT*Temperature); break; } } } switch(MultiComponentMixingRules) { default: case VAN_DER_WAALS: for(i=0;i<NumberOfComponents;i++) for(j=0;j<NumberOfComponents;j++) { if(Components[i].Swapable&&Components[j].Swapable) { aij[i][j]=(1.0-BinaryInteractionParameter[i][j])*sqrt(a[i]*a[j]); Aij[i][j]=(1.0-BinaryInteractionParameter[i][j])*sqrt(A[i]*A[j]); } } Amix=0.0; Bmix=0.0; for(i=0;i<NumberOfComponents;i++) { if(Components[i].Swapable) { Bmix+=Components[i].MolFraction[CurrentSystem]*b[i]; for(j=0;j<NumberOfComponents;j++) if(Components[j].Swapable) Amix+=Components[i].MolFraction[CurrentSystem]*Components[j].MolFraction[CurrentSystem]*aij[i][j]; } } Amix*=Pressure/SQR(MOLAR_GAS_CONSTANT*Temperature); Bmix*=Pressure/(MOLAR_GAS_CONSTANT*Temperature); break; } switch(EquationOfState) { case SOAVE_REDLICH_KWONG: Coefficients[3]=1.0; Coefficients[2]=-1.0; Coefficients[1]=Amix-Bmix-SQR(Bmix); Coefficients[0]=-Amix*Bmix; break; case PENG_ROBINSON: case PENG_ROBINSON_GASEM: default: Coefficients[3]=1.0; Coefficients[2]=Bmix-1.0; Coefficients[1]=Amix-3.0*SQR(Bmix)-2.0*Bmix; Coefficients[0]=-(Amix*Bmix-SQR(Bmix)-CUBE(Bmix)); break; } // solve equation-of-state cubic(Coefficients,Compressibility,&NumberOfSolutions); // sort the compressibilities, Compressibility[0] the highest, Compressibility[2] the lowest if (Compressibility[0]<Compressibility[1]) { temp=Compressibility[0]; Compressibility[0]=Compressibility[1]; Compressibility[1]=temp; } if (Compressibility[1]<Compressibility[2]) { temp=Compressibility[1]; Compressibility[1]=Compressibility[2]; Compressibility[2]=temp; } if(Compressibility[0]<Compressibility[1]) { temp=Compressibility[0]; Compressibility[0]=Compressibility[1]; Compressibility[1]=temp; } // In cases where three roots are obtained, the highest value corresponds to the solution for vapour phase, // and the lowest value corresponds to the solution for liquid phase. (The intermediate solution has no // physical meaning). Note that the solutions in this case don't imply that the liquid and vapour phases // are in equilibrium with each other. Rather, one of the phases will be stable and the other will be a // metastable state. Only at the special condition that T = Tsat and P = Psat are vapour and liquid in // equilibrium with each other. // In other cases, only a single root is obtained. This may correspond to liquid, vapour or supercritical fluid, // depending on the relative magnitudes of P and Pc, and T and Tc. switch(EquationOfState) { case SOAVE_REDLICH_KWONG: for(i=0;i<NumberOfComponents;i++) { if(Components[i].Swapable) { for(j=0;j<NumberOfSolutions;j++) { temp=0.0; for(k=0;k<NumberOfComponents;k++) temp+=2.0*Components[k].MolFraction[CurrentSystem]*Aij[i][k]; FugacityCoefficients[i][j]=exp((B[i]/Bmix)*(Compressibility[j]-1.0)-log(Compressibility[j]-Bmix)- Amix*(temp/Amix-B[i]/Bmix)*log(1.0+Bmix/Compressibility[j])/Bmix); } } } break; case PENG_ROBINSON_GASEM: case PENG_ROBINSON: default: for(i=0;i<NumberOfComponents;i++) { if(Components[i].Swapable) { for(j=0;j<NumberOfSolutions;j++) { temp=0.0; for(k=0;k<NumberOfComponents;k++) temp+=2.0*Components[k].MolFraction[CurrentSystem]*Aij[i][k]; FugacityCoefficients[i][j]=exp((B[i]/Bmix)*(Compressibility[j]-1.0)-log(Compressibility[j]-Bmix) -(Amix/(2.0*sqrt(2.0)*Bmix))*(temp/Amix-B[i]/Bmix)* log((Compressibility[j]+(1.0+sqrt(2.0))*Bmix)/(Compressibility[j]+(1.0-sqrt(2))*Bmix))); } } } break; } if(ExcessVolume[CurrentSystem]>0.0) excess_volume=ExcessVolume[CurrentSystem]*HeliumVoidFraction[CurrentSystem]; else excess_volume=Volume[CurrentSystem]*HeliumVoidFraction[CurrentSystem]; // get the gas-phase fugacity coefficient for(i=0;i<NumberOfComponents;i++) { if(Components[i].Swapable) { if(NumberOfSolutions==1) { if(ComputeFugacityCoefficient[CurrentSystem][i]) Components[i].FugacityCoefficient[CurrentSystem]=FugacityCoefficients[i][0]; Components[i].AmountOfExcessMolecules[CurrentSystem]= Components[i].MolFraction[CurrentSystem]*AVOGADRO_CONSTANT* excess_volume*CUBE(ANGSTROM)*Pressure/(Compressibility[0]*MOLAR_GAS_CONSTANT*Temperature); Components[i].BulkFluidDensity[CurrentSystem]=(Pressure/(Compressibility[0]*MOLAR_GAS_CONSTANT*Temperature))* Components[i].Mass/1000.0; Components[i].Compressibility[CurrentSystem]=Compressibility[0]; if((Temperature>Components[i].CriticalTemperature)&&(Pressure>Components[i].CriticalPressure)) FluidState[CurrentSystem]=SUPER_CRITICAL_FLUID; else if((Temperature<Components[i].CriticalTemperature)&&(Pressure<Components[i].CriticalPressure)) FluidState[CurrentSystem]=VAPOUR; else if((Temperature<Components[i].CriticalTemperature)&&(Pressure>Components[i].CriticalPressure)) FluidState[CurrentSystem]=LIQUID; } else { if(Compressibility[2]>0.0) { if(FugacityCoefficients[i][0]<FugacityCoefficients[i][2]) { // vapour (stable) and liquid (metastable) FluidState[CurrentSystem]=VAPOUR_STABLE; if(ComputeFugacityCoefficient[CurrentSystem][i]) Components[i].FugacityCoefficient[CurrentSystem]=FugacityCoefficients[i][0]; Components[i].AmountOfExcessMolecules[CurrentSystem]=Components[i].MolFraction[CurrentSystem]*AVOGADRO_CONSTANT* excess_volume*CUBE(ANGSTROM)*Pressure/(Compressibility[0]*MOLAR_GAS_CONSTANT*Temperature); Components[i].BulkFluidDensity[CurrentSystem]=(Pressure/(Compressibility[0]*MOLAR_GAS_CONSTANT*Temperature))* Components[i].Mass/1000.0; Components[i].Compressibility[CurrentSystem]=Compressibility[0]; } else if(FugacityCoefficients[i][0]>FugacityCoefficients[i][2]) { // vapour (metastable) and liquid (stable) FluidState[CurrentSystem]=LIQUID_STABLE; if(ComputeFugacityCoefficient[CurrentSystem][i]) Components[i].FugacityCoefficient[CurrentSystem]=FugacityCoefficients[i][2]; Components[i].AmountOfExcessMolecules[CurrentSystem]=Components[i].MolFraction[CurrentSystem]*AVOGADRO_CONSTANT* excess_volume*CUBE(ANGSTROM)*Pressure/(Compressibility[2]*MOLAR_GAS_CONSTANT*Temperature); Components[i].BulkFluidDensity[CurrentSystem]=(Pressure/(Compressibility[2]*MOLAR_GAS_CONSTANT*Temperature))* Components[i].Mass/1000.0; Components[i].Compressibility[CurrentSystem]=Compressibility[2]; } else { // vapour (stable) and liquid (stable) FluidState[CurrentSystem]=VAPOUR_LIQUID_STABLE; if(ComputeFugacityCoefficient[CurrentSystem][i]) Components[i].FugacityCoefficient[CurrentSystem]=FugacityCoefficients[i][0]; Components[i].AmountOfExcessMolecules[CurrentSystem]=Components[i].MolFraction[CurrentSystem]*AVOGADRO_CONSTANT* excess_volume*CUBE(ANGSTROM)*Pressure/(Compressibility[0]*MOLAR_GAS_CONSTANT*Temperature); Components[i].BulkFluidDensity[CurrentSystem]=(Pressure/(Compressibility[0]*MOLAR_GAS_CONSTANT*Temperature))* Components[i].Mass/1000.0; Components[i].Compressibility[CurrentSystem]=Compressibility[0]; } } else { if(ComputeFugacityCoefficient[CurrentSystem][i]) Components[i].FugacityCoefficient[CurrentSystem]=FugacityCoefficients[i][0]; Components[i].AmountOfExcessMolecules[CurrentSystem]=Components[i].MolFraction[CurrentSystem]*AVOGADRO_CONSTANT* excess_volume*CUBE(ANGSTROM)*Pressure/(Compressibility[0]*MOLAR_GAS_CONSTANT*Temperature); Components[i].BulkFluidDensity[CurrentSystem]=(Pressure/(Compressibility[0]*MOLAR_GAS_CONSTANT*Temperature))* Components[i].Mass/1000.0; Components[i].Compressibility[CurrentSystem]=Compressibility[0]; if((Temperature>Components[i].CriticalTemperature)&&(Pressure>Components[i].CriticalPressure)) FluidState[CurrentSystem]=SUPER_CRITICAL_FLUID; else if((Temperature<Components[i].CriticalTemperature)&&(Pressure<Components[i].CriticalPressure)) FluidState[CurrentSystem]=VAPOUR; else if((Temperature<Components[i].CriticalTemperature)&&(Pressure>Components[i].CriticalPressure)) FluidState[CurrentSystem]=LIQUID; } } } else { Components[i].FugacityCoefficient[CurrentSystem]=-1.0; Components[i].PartialPressure[CurrentSystem]=-1.0; } } // The term cm^3 (STP) is not a unit of volume but a unit to express the number of gas molecules // It is essentially a term in units of pV, e.g. cm^3 at 1 atm and 273 K. // The volume of 1 mole of a gas is 22.4 liters for(i=0;i<NumberOfComponents;i++) { // molec/uc -> cm^3 (STP)/g Components[i].MOLEC_PER_UC_TO_CC_STP_G[CurrentSystem]=1e6*(MOLAR_GAS_CONSTANT*273.15/ATM_TO_PA)*number_of_unit_cells/FrameworkMass; // molec/uc -> cm^3 (STP)/cm^3 Components[i].MOLEC_PER_UC_TO_CC_STP_CC[CurrentSystem]=number_of_unit_cells*(MOLAR_GAS_CONSTANT*273.15/ATM_TO_PA) /(Volume[CurrentSystem]*CUBE(ANGSTROM)*AVOGADRO_CONSTANT); // mol/kg -> cm^3 (STP)/g Components[i].MOL_PER_KG_TO_CC_STP_G[CurrentSystem]=1e3*(MOLAR_GAS_CONSTANT*273.15/ATM_TO_PA); // mol/kg -> cm^3 (STP)/cm^3 Components[i].MOL_PER_KG_TO_CC_STP_CC[CurrentSystem]=(MOLAR_GAS_CONSTANT*273.15/ATM_TO_PA)*FrameworkMass /(1e3*Volume[CurrentSystem]*CUBE(ANGSTROM)*AVOGADRO_CONSTANT); } for(i=0;i<NumberOfComponents;i++) { if(Components[i].Swapable) { if(Components[i].PartialPressure[CurrentSystem]<0.0) { printf("Error: Partial pressure of component %d [%s] is NOT set !!\n", i,Components[i].Name); exit(-1); } } } } }
void cubic(T * dst, const T * xm1s, const T * xs, const T * xp1s, const T * xp2s, int len, T f){ for(int i=0; i<len; ++i) dst[i] = cubic(f, xm1s[i], xs[i], xp1s[i], xp2s[i]); }
Json::Value SkJSONCanvas::makePath(const SkPath& path) { Json::Value result(Json::objectValue); switch (path.getFillType()) { case SkPath::kWinding_FillType: result[SKJSONCANVAS_ATTRIBUTE_FILLTYPE] = SKJSONCANVAS_FILLTYPE_WINDING; break; case SkPath::kEvenOdd_FillType: result[SKJSONCANVAS_ATTRIBUTE_FILLTYPE] = SKJSONCANVAS_FILLTYPE_EVENODD; break; case SkPath::kInverseWinding_FillType: result[SKJSONCANVAS_ATTRIBUTE_FILLTYPE] = SKJSONCANVAS_FILLTYPE_INVERSEWINDING; break; case SkPath::kInverseEvenOdd_FillType: result[SKJSONCANVAS_ATTRIBUTE_FILLTYPE] = SKJSONCANVAS_FILLTYPE_INVERSEEVENODD; break; } Json::Value verbs(Json::arrayValue); SkPath::Iter iter(path, false); SkPoint pts[4]; SkPath::Verb verb; while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { switch (verb) { case SkPath::kLine_Verb: { Json::Value line(Json::objectValue); line[SKJSONCANVAS_VERB_LINE] = this->makePoint(pts[1]); verbs.append(line); break; } case SkPath::kQuad_Verb: { Json::Value quad(Json::objectValue); Json::Value coords(Json::arrayValue); coords.append(this->makePoint(pts[1])); coords.append(this->makePoint(pts[2])); quad[SKJSONCANVAS_VERB_QUAD] = coords; verbs.append(quad); break; } case SkPath::kCubic_Verb: { Json::Value cubic(Json::objectValue); Json::Value coords(Json::arrayValue); coords.append(this->makePoint(pts[1])); coords.append(this->makePoint(pts[2])); coords.append(this->makePoint(pts[3])); cubic[SKJSONCANVAS_VERB_CUBIC] = coords; verbs.append(cubic); break; } case SkPath::kConic_Verb: { Json::Value conic(Json::objectValue); Json::Value coords(Json::arrayValue); coords.append(this->makePoint(pts[1])); coords.append(this->makePoint(pts[2])); coords.append(Json::Value(iter.conicWeight())); conic[SKJSONCANVAS_VERB_CONIC] = coords; verbs.append(conic); break; } case SkPath::kMove_Verb: { Json::Value move(Json::objectValue); move[SKJSONCANVAS_VERB_MOVE] = this->makePoint(pts[0]); verbs.append(move); break; } case SkPath::kClose_Verb: verbs.append(Json::Value(SKJSONCANVAS_VERB_CLOSE)); break; case SkPath::kDone_Verb: break; } } result[SKJSONCANVAS_ATTRIBUTE_VERBS] = verbs; return result; }
int quartic(double dd[5], double sol[4], double soli[4], int* Nsol) { double AA[4], z[3]; double a, b, c, d, f, p, q, r, zsol, xK2, xL, xK, sqp, sqm; int ncube, i; *Nsol = 0; if (dd[4] == 0.0) { //printf("\n ERROR: NOT A QUARTIC EQUATION"); return 0; } a = dd[4]; b = dd[3]; c = dd[2]; d = dd[1]; f = dd[0]; p = (-3.0*(b*b) + 8.0 *a*c)/(8.0*(a*a)); q = ((b*b*b) - 4.0*a*b*c + 8.0 *d*(a*a)) / (8.0*(a*a*a)); r = (-3.0*(b*b*b*b) + 16.0 *a*(b*b)*c - 64.0 *(a*a)*b*d + 256.0 *(a*a*a)*f)/(256.0*(a*a*a*a)); // Solve cubic resolvent AA[3] = 8.0; AA[2] = -4.0*p; AA[1] = -8.0*r; AA[0] = 4.0*p*r - (q*q); //printf("\n bcubic %.4e\t%.4e\t%.4e\t%.4e ", AA[0], AA[1], AA[2], AA[3]); cubic(AA, z, &ncube); //printf("\n acubic %.4e\t%.4e\t%.4e ", z[0], z[1], z[2]); zsol = - 1.e99; for (i=0;i<ncube;i++) zsol = max(zsol, z[i]); //Not sure C has max fct z[0] =zsol; xK2 = 2.0*z[0] -p; xK = sqrt(xK2); xL = q/(2.0*xK); sqp = xK2 - 4.0 * (z[0] + xL); sqm = xK2 - 4.0 * (z[0] - xL); for (i=0;i<4;i++) soli[i] = 0.0; if ( (sqp >= 0.0) && (sqm >= 0.0)) { //printf("\n case 1 "); const double sq_sqp = sqrt(sqp); const double sq_sqm = sqrt(sqm); sol[0] = 0.5 * (xK + sq_sqp); sol[1] = 0.5 * (xK - sq_sqp); sol[2] = 0.5 * (-xK + sq_sqm); sol[3] = 0.5 * (-xK - sq_sqm); *Nsol = 4; } else if ( (sqp >= 0.0) && (sqm < 0.0)) { //printf("\n case 2 "); const double sq_sqp = sqrt(sqp); const double sq_sqm = sqrt(-.25 * sqm); sol[0] = 0.5 * (xK + sq_sqp); sol[1] = 0.5 * (xK - sq_sqp); sol[2] = -0.5 * xK; sol[3] = -0.5 * xK; soli[2] = sq_sqm; soli[3] = -sq_sqm; *Nsol = 2; } else if ( (sqp < 0.0) && (sqm >= 0.0)) { //printf("\n case 3 "); const double sq_sqp = sqrt(-0.25 * sqp); const double sq_sqm = sqrt(sqm); sol[0] = 0.5 * (-xK + sq_sqm); sol[1] = 0.5 * (-xK - sq_sqm); sol[2] = 0.5 * xK; sol[3] = 0.5 * xK; soli[2] = sq_sqp; soli[3] = -sq_sqp; *Nsol = 2; } else if ( (sqp < 0.0) && (sqm < 0.0)) { //printf("\n case 4 "); const double sq_sqp = sqrt(-0.25 * sqp); const double sq_sqm = sqrt(-0.25 * sqm); sol[0] = -0.5 * xK; sol[1] = -0.5 * xK; soli[0] = sq_sqm; soli[1] = -sq_sqm; sol[2] = 0.5 * xK; sol[3] = 0.5 * xK; soli[2] = sq_sqp; soli[3] = -sq_sqp; *Nsol = 0; } for (i=0;i<4;i++) sol[i] -= b/(4.0*a); return 0; }
/** * \brief Resize texture. * \param[in] in Original texture data. * \param[in] inwidth Original width of texture in pixels. * \param[in] inheight Original height of texture in pixels. * \param[in,out] out Resized texture data. * \param[in] outwidth New width of texture in pixels. * \param[in] outheight New height of texture in pixels. * \param[in] bytes Number of bytes per pixel. * \param[in] interpolation See InterpolationType */ PUBLIC void TM_ResampleTexture( W8 *in, int inwidth, int inheight, W8 *out, int outwidth, int outheight, W16 bytes, InterpolationType interpolation ) { double *src[ 4 ]; W8 *src_tmp; W8 *dest; double *row, *accum; int b; int width, height; int orig_width, orig_height; double y_rat; int i; int old_y = -4; int new_y; int x, y; if( interpolation == INTERPOLATION_NONE ) { scale_region_no_resample( in, inwidth, inheight, out, outwidth, outheight, (char)bytes ); return; } orig_width = inwidth; orig_height = inheight; width = outwidth; height = outheight; #if 0 Com_DPrintf( "scale_region: (%d x %d) -> (%d x %d)\n", orig_width, orig_height, width, height ); #endif /* find the ratios of old y to new y */ y_rat = (double) orig_height / (double) height; /* the data pointers... */ for( i = 0 ; i < 4 ; ++i ) { src[ i ] = (double *) MM_MALLOC( sizeof( double ) * width * bytes ); } dest = (PW8) MM_MALLOC( width * bytes); src_tmp = (PW8) MM_MALLOC( orig_width * bytes ); /* offset the row pointer by 2*bytes so the range of the array is [-2*bytes] to [(orig_width + 2)*bytes] */ row = (double *) MM_MALLOC( sizeof( double ) * (orig_width + 2 * 2) * bytes ); row += bytes * 2; accum = (double *) MM_MALLOC( sizeof( double ) * width * bytes ); /* Scale the selected region */ for( y = 0 ; y < height ; y++ ) { if( height < orig_height ) { int max; double frac; const double inv_ratio = 1.0 / y_rat; if( y == 0 ) /* load the first row if this is the first time through */ { get_scaled_row( &src[0], 0, width, row, src_tmp, in, orig_width, orig_height, bytes ); } new_y = (int)(y * y_rat); frac = 1.0 - (y * y_rat - new_y); for( x = 0 ; x < width * bytes; ++x ) { accum[x] = src[3][x] * frac; } max = (int) ((y + 1) * y_rat) - new_y - 1; get_scaled_row( &src[ 0 ], ++new_y, width, row, src_tmp, in, orig_width, orig_height, bytes ); while( max > 0 ) { for( x = 0 ; x < width * bytes ; ++x ) { accum[x] += src[ 3 ][ x ]; } get_scaled_row( &src[ 0 ], ++new_y, width, row, src_tmp, in, orig_width, orig_height, bytes ); max--; } frac = (y + 1) * y_rat - ((int) ((y + 1) * y_rat)); for( x = 0 ; x < width * bytes ; ++x ) { accum[ x ] += frac * src[ 3 ][ x ]; accum[ x ] *= inv_ratio; } } else if( height > orig_height ) { double p0, p1, p2, p3; double dy; new_y = (int)floor( y * y_rat - 0.5 ); while( old_y <= new_y ) { /* get the necesary lines from the source image, scale them, and put them into src[] */ get_scaled_row( &src[ 0 ], old_y + 2, width, row, src_tmp, in, orig_width, orig_height, bytes ); old_y++; } dy = (y * y_rat - 0.5) - new_y; p0 = cubic( dy, 1, 0, 0, 0 ); p1 = cubic( dy, 0, 1, 0, 0 ); p2 = cubic( dy, 0, 0, 1, 0 ); p3 = cubic( dy, 0, 0, 0, 1 ); for( x = 0 ; x < width * bytes ; ++x ) { accum[ x ] = ( p0 * src[ 0 ][ x ] + p1 * src[ 1 ][ x ] + p2 * src[ 2 ][ x ] + p3 * src[ 3 ][ x ] ); } } else /* height == orig_height */ { get_scaled_row( &src[ 0 ], y, width, row, src_tmp, in, orig_width, orig_height, bytes ); memcpy( accum, src[ 3 ], sizeof( double ) * width * bytes ); } if( pixel_region_has_alpha( bytes ) ) { /* unmultiply the alpha */ double inv_alpha; double *p = accum; int alpha = bytes - 1; int result; W8 *d = dest; for( x = 0 ; x < width ; ++x ) { if( p[ alpha ] > 0.001 ) { inv_alpha = 255.0 / p[ alpha ]; for( b = 0 ; b < alpha ; b++ ) { result = (int)RINT( inv_alpha * p[ b ] ); if( result < 0 ) { d[ b ] = 0; } else if( result > 255 ) { d[ b ] = 255; } else { d[ b ] = result; } } result = (int)RINT( p[ alpha ] ); if( result > 255 ) { d[ alpha ] = 255; } else { d[ alpha ] = result; } } else /* alpha <= 0 */ { for( b = 0 ; b <= alpha ; ++b ) { d[ b ] = 0; } } d += bytes; p += bytes; } } else { int w = width * bytes; for( x = 0 ; x < w ; ++x ) { if( accum[ x ] < 0.0 ) { dest[ x ] = 0; } else if( accum[ x ] > 255.0 ) { dest[ x ] = 255; } else { dest[ x ] = (W8)RINT( accum[ x ] ); } } } pixel_region_set_row( out, bytes, y, width, dest ); } /* free up temporary arrays */ MM_FREE( accum ); for( i = 0 ; i < 4 ; ++i ) { MM_FREE( src[ i ] ); } MM_FREE( src_tmp ); MM_FREE( dest ); row -= 2 * bytes; MM_FREE( row ); }
static inline ALfloat cubic32(const ALfloat *vals, ALuint frac) { return cubic(vals[-1], vals[0], vals[1], vals[2], frac * (1.0f/FRACTIONONE)); }
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { label = new QLabel("0", this); label -> setGeometry(QRect(QPoint(75,25),QSize(50,200))); clear_button = new QPushButton("C", this); clear_button -> setGeometry(QRect(QPoint(100,300),QSize(50,50))); connect(clear_button, SIGNAL(released()), this,SLOT(clear())); equals_button = new QPushButton("=", this); equals_button -> setGeometry(QRect(QPoint(150,300),QSize(50,50))); connect(equals_button, SIGNAL(released()), this,SLOT(equals())); add_button = new QPushButton("+", this); add_button -> setGeometry(QRect(QPoint(200,150),QSize(50,50))); connect(add_button, SIGNAL(released()), this,SLOT(add())); subtract_button = new QPushButton("-", this); subtract_button -> setGeometry(QRect(QPoint(200,200),QSize(50,50))); connect(subtract_button, SIGNAL(released()), this,SLOT(subtract())); multiply_button = new QPushButton("X", this); multiply_button -> setGeometry(QRect(QPoint(200,250),QSize(50,50))); connect(multiply_button, SIGNAL(released()), this,SLOT(multiply())); divide_button = new QPushButton(":", this); divide_button -> setGeometry(QRect(QPoint(200,300),QSize(50,50))); connect(divide_button, SIGNAL(released()), this,SLOT(divide())); sin_button = new QPushButton("sin", this); sin_button -> setGeometry(QRect(QPoint(250,150),QSize(50,50))); connect(sin_button, SIGNAL(released()), this,SLOT(sin())); cos_button = new QPushButton("cos", this); cos_button -> setGeometry(QRect(QPoint(250,200),QSize(50,50))); connect(cos_button, SIGNAL(released()), this,SLOT(cos())); tan_button = new QPushButton("tan", this); tan_button -> setGeometry(QRect(QPoint(250,250),QSize(50,50))); connect(tan_button, SIGNAL(released()), this,SLOT(tan())); sqrt_button = new QPushButton("sqrt", this); sqrt_button -> setGeometry(QRect(QPoint(250,300),QSize(50,50))); connect(sqrt_button, SIGNAL(released()), this,SLOT( sqrt())); pow_button = new QPushButton("^2", this); pow_button -> setGeometry(QRect(QPoint(250,350),QSize(50,50))); connect(pow_button, SIGNAL(released()), this,SLOT(pow())); cubic_button = new QPushButton("^3", this); cubic_button -> setGeometry(QRect(QPoint(200,350),QSize(50,50))); connect(cubic_button, SIGNAL(released()), this,SLOT(cubic())); exp_button = new QPushButton("exp", this); exp_button -> setGeometry(QRect(QPoint(150,350),QSize(50,50))); connect(exp_button, SIGNAL(released()), this,SLOT(exp())); ln_button = new QPushButton("ln", this); ln_button -> setGeometry(QRect(QPoint(100,350),QSize(50,50))); connect(ln_button, SIGNAL(released()), this,SLOT(ln())); for(int i=0; i<10; i++){ QString digit = QString::number(i); buttons[i] = new QPushButton(digit, this); connect(buttons[i], SIGNAL(released()), this,SLOT(buttonPushed())); } setGeo(); }