void cosine_of_angle_sum(void) { p2 = cdr(p1); while (iscons(p2)) { B = car(p2); if (isnpi(B)) { push(p1); push(B); subtract(); A = pop(); push(A); cosine(); push(B); cosine(); multiply(); push(A); sine(); push(B); sine(); multiply(); subtract(); return; } p2 = cdr(p2); } cosine_of_angle(); }
/************************************************************* ************************************************************* * ROUNDED TO NEAREST * ************************************************************* *************************************************************/ double scs_cos_rn(double x){ scs_t sc1, sc2; double resd; int N; #if EVAL_PERF crlibm_second_step_taken++; #endif scs_set_d(sc1, x); N = rem_pio2_scs(sc2, sc1); N = N & 0x0000003; /* extract the 2 last bits of N */ switch (N){ case 0: cosine(sc2); scs_get_d(&resd, sc2); return resd; case 1: sine(sc2); scs_get_d(&resd, sc2); return -resd; case 2: cosine(sc2); scs_get_d(&resd, sc2); return -resd; case 3: sine(sc2); scs_get_d(&resd, sc2); return resd; default: fprintf(stderr,"ERREUR: %d is not a valid value in s_cos \n", N); return 0.0; } }
void create_orthocenter(void) { _point *A, *B, *C, *val; double a, b, c, ca, cb, cc, d; C = POP(_point); B = POP(_point); A = POP(_point); get_mem(val, _point); a = distance(B, C); b = distance(C, A); c = distance(A, B); if (ZERO(a) || ZERO(b) || ZERO(c)) runtime_error(_("invalid triangle")); ca = cosine(a, b, c); cb = cosine(b, c, a); cc = cosine(c, a, b); if (ca == 0) { ca = 1; cb = 0; cc = 0; } else if (cb == 0) { ca = 0; cb = 1; cc = 0; } else if (cc == 0) { ca = 0; cb = 0; cc = 1; } else { ca = tangent(ca); cb = tangent(cb); cc = tangent(cc); } d = ca + cb + cc; val->x = (ca*A->x + cb*B->x + cc*C->x)/d; val->y = (ca*A->y + cb*B->y + cc*C->y)/d; PSH(val); }
void carveline2d_circle(struct arena* win, u32 rgb, vec3 vc, vec3 vr, vec3 vf) { #define lineacc (acc*2) int j; float a,c,s; float bb = (float)(rgb&0xff) / 256.0; float gg = (float)((rgb>>8)&0xff) / 256.0; float rr = (float)((rgb>>16)&0xff) / 256.0; float* vbuf; u16* ibuf; int vlen = line2d_vars(win, 0, &vbuf, &ibuf, lineacc, lineacc); for(j=0;j<lineacc;j++) { a = j*tau/lineacc; c = cosine(a); s = sine(a); vbuf[6*j+0] = vc[0] + vr[0]*c + vf[0]*s; vbuf[6*j+1] = vc[1] + vr[1]*c + vf[1]*s; vbuf[6*j+2] = vc[2] + vr[2]*c + vf[2]*s; vbuf[6*j+3] = rr; vbuf[6*j+4] = gg; vbuf[6*j+5] = bb; ibuf[2*j+0] = vlen+j; ibuf[2*j+1] = vlen+(j+1)%lineacc; } }
int main(int argc, char* argv[]) { try { log_init(); if (argc == 1) { std::cout << "load libm.so" << std::endl; cxxtools::dl::Library lib("m"); std::cout << "sym cos" << std::endl; function_type cosine = (function_type)(lib["cos"]); std::cout << "call cos" << std::endl; std::cout << "cos(2.0) = " << cosine(2.0) << std::endl; } else { std::cout << "load " << argv[1] << std::endl; cxxtools::dl::Library lib(argv[1]); for (int a = 2; argv[a]; ++a) { std::cout << "sym " << argv[a] << std::endl; cxxtools::dl::Symbol sym = lib.getSymbol(argv[a]); std::cout << " => " << static_cast<void*>(sym) << std::endl; } } } catch (const std::exception& e) { std::cerr << e.what() << std::endl; } }
int main() { /* 3 main variables to direct the program */ int degree; int n; int exit; /* Declaring exit variale as 0 to indicate exit when returned to 1 */ exit = 0; /* Creating a loop to countinue until user wants to exit */ while (exit!=1) { /* Taking inputs from user */ getInputs(°ree,&n,&exit); /* Calling functions to calculate sine and cosine values using */ /* the angle and n data given by user */ sine(degree,n); cosine(degree,n); } return (0); }
float dtw_windowed(void * seq1v, void * seq2v, int seq1_len, int seq2_len, int window) { // THIS WILL SEGFAULT IF seq1 AND seq2 ARE NOT (n,m)x4 float* seq1 = (float*) seq1v; float* seq2 = (float*) seq2v; float* distance_mat = make_mat(seq1_len, seq2_len, INFINITY); //printf("n: %d, m: %d\n", seq1_len, seq2_len); distance_mat[int_pos(0,0, seq1_len)] = 0; window = MAX(window, abs(seq1_len - seq2_len)); //printf("Window: %d\n", window); int i, j; for (i = 1; i < seq1_len; i++) { //for (j = MAX(1, i-window); j < MIN(seq2_len,i+window); j++) { for (j = 1; j < seq2_len; j++) { // Wish there was a better way to do this... float eucl_dist = euclidean(seq1[int_pos(i,0,4)], seq1[int_pos(i,1,4)], seq2[int_pos(j,0,4)], seq2[int_pos(j,1,4)]); float cosi_dist = cosine(seq1[int_pos(i,2,4)], seq1[int_pos(i,3,4)], seq2[int_pos(j,2,4)], seq2[int_pos(j,3,4)]); distance_mat[int_pos(i,j,seq2_len)] = (eucl_dist + cosi_dist) + MIN(distance_mat[int_pos(i-1,j-1,seq2_len)], MIN(distance_mat[int_pos(i-1,j,seq2_len)], distance_mat[int_pos(i,j-1,seq2_len)])); } } // find minimal value along the last column as our distance float retval = distance_mat[int_pos(seq1_len-1, seq2_len-1, seq2_len)]; free(distance_mat); return retval; }
// TODO: maybe factor out the vector similarity methods. They're not exactly // specific to tone profiling. float ToneProfile::similarity(similarity_measure_t measure, const std::vector<float>& input, int offset) const { if (input.size() != 12) throw Exception("Input vector for similarity must have 12 elements"); if (measure == SIMILARITY_CORRELATION) return correlation(input, offset); else return cosine(input, offset); }
void eval_cos(void) { push(cadr(p1)); eval(); cosine(); }
void cubie(int x,int y,int z) { int i,j; //背景色 for(i=256*x;i<256*x+256;i++) for(j=256*y;j<256*y+256;j++) point(i,j,0x44444444); for(i=256*x;i<256*x+256;i++) { point(i,256*y,0); point(i,256*y+255,0); } for(j=256*y;j<256*y+256;j++) { point(256*x,j,0); point(256*x+255,j,0); } if(z==1) //画0 { double a; for(a=0;a<6.28;a+=0.01) { point( (int)(256*x+128+100*cosine(a)), (int)(256*y+128+100*sine(a)), 0xff0000 ); point( (int)(256*x+129+100*cosine(a)), (int)(256*y+128+100*sine(a)), 0xff0000 ); } } if(z==2) //画x { for(i=28;i<228;i++) { point(256*x+i,256*y+i,0xff); point(256*x+i+1,256*y+i,0xff); point(256*x+i,256*y+256-i,0xff); point(256*x+i+1,256*y+256-i,0xff); } } }
void complex_power_real(float* d, float e) { float mod = complexmodulus(d); float ang = complexangle(d); float ppp = power(mod, e); d[0] = ppp * cosine(ang * e); d[1] = ppp * sine(ang * e); }
void dsin(void) { push(cadr(p1)); push(p2); derivative(); push(cadr(p1)); cosine(); multiply(); }
void estYawDrift(void) { // Don't update Yaw Drift while hovering, since that doesn't work right yet if (gps_nav_valid() && !dcm_flags._.skip_yaw_drift) { if ((estimatedWind[0] == 0 && estimatedWind[1] == 0) || (air_speed_magnitudeXY < WIND_NAV_AIR_SPEED_MIN)) { dirovergndHGPS[0] = -cosine(actual_dir); dirovergndHGPS[1] = sine(actual_dir); dirovergndHGPS[2] = 0; } else { dirovergndHGPS[0] = -cosine(calculated_heading); dirovergndHGPS[1] = sine(calculated_heading); dirovergndHGPS[2] = 0; } } }
int main() { long int t = 0, T = 0; scanf("%ld", &T); while(t++ < T) { int AB, BC, AC, AD, BD, CD; double cos1, cos2, cos3; scanf("%d%d%d%d%d%d", &AB, &AC, &AD, &BC, &BD, &CD); cos1 = cosine(AD, BD, AB); cos2 = cosine(BD, CD, BC); cos3 = cosine(CD, AD, AC); double temp = sqrt( 1 + 2 * cos1 * cos2 * cos3 - cos1*cos1 - cos2*cos2 - cos3*cos3); double answer = AD * BD * CD * temp / 6; printf("%.4lf\n", answer); } return 0;}
void FeatureEval::eigen_plane_consine_similarity(){ boost::shared_ptr<PointCloud> cloud = core_->cl_->active_; if(cloud == nullptr) return; std::vector<int> sub_idxs; pcl::PointCloud<pcl::PointXYZI>::Ptr smaller_cloud = octreeDownsample(cloud.get(), subsample_res_, sub_idxs); t_.start(); qDebug() << "Timer started (eigen_plane_consine_similarity)"; boost::shared_ptr<std::vector<Eigen::Vector3f> > pca = getPCA(smaller_cloud.get(), 0.05f, 20); boost::shared_ptr<std::vector<float>> plane_likelyhood = boost::make_shared<std::vector<float>>(pca->size(), 0.0f); Eigen::Vector3f ideal_plane(1.0f, 0.0f, 0.0f); ideal_plane.normalize(); for(uint i = 0; i < pca->size(); i++) { Eigen::Vector3f & val = (*pca)[i]; // Not enough neighbours if(val[1] < val[2]) { (*plane_likelyhood)[i] = 0; continue; } float similarity = cosine(val, ideal_plane); (*plane_likelyhood)[i] = similarity; } qDebug() << "eigen_plane_consine_similarity in " << (time_ = t_.elapsed()) << " ms"; // Correlate computeCorrelation(plane_likelyhood->data(), 1, plane_likelyhood->size(), sub_idxs); if(!visualise_on_) return; // Draw int h = cloud->scan_width(); int w = cloud->scan_height(); /* boost::shared_ptr<std::vector<Eigen::Vector3f> > grid = boost::make_shared<std::vector<Eigen::Vector3f> >(grid_to_cloud->size(), Eigen::Vector3f(0.0f, 0.0f, 0.0f)); for(int i = 0; i < grid_to_cloud->size(); i++) { int idx = (*grid_to_cloud)[i]; if(idx != -1) (*grid)[i] = (*pca)[idx]; } drawVector3f(grid, cloud); */ boost::shared_ptr<const std::vector<float>> img = cloudToGrid(cloud->cloudToGridMap(), w*h, plane_likelyhood); drawFloats(img, cloud); }
void dtan(void) { push(cadr(p1)); push(p2); derivative(); push(cadr(p1)); cosine(); push_integer(-2); power(); multiply(); }
int main() { union dp_item Test; Test.drep = -3.000000000000E+00; union dp_item Test1; Test1.drep = +1.875000000000E+00; union dp_item Test2; Test2.drep = 5.1; union dp_item Test3; Test3.drep = 3.1; double number = absolute(Test.drep); printf("Testing absolute of -3.0: %08x\n", number); double number1 = absolute(Test1.drep); printf("Testing absolute of 1.875: %08x\n", number1); double number2 = modulo(Test2.drep, Test3.drep); printf("Testing 5.1 mod 3.1: %f\n", number2); double Test51 = -20.0; double Test52 = 3.0; double number52 = modulo(Test51, Test52); printf("Testing -20 mod 3: %f\n", number52); unsigned int Test4 = 4; double number3 = factorial(Test4); printf("Testing 4! : %f\n", number3); unsigned int Test5 = 9; double number4 = factorial(Test5); printf("Testing 9! : %f\n", number4); double Test6 = 3.0; unsigned int power1 = 3; double number5 = power(Test6, power1); printf("Testing 3.0^(3): %f\n", number5); double Test7 = 4.25; unsigned int power2 = 5; double number6 = power(Test7, power2); printf("Testing 4.25^(5): %f\n", number6); double Test8 = 8.1; double number7 = cosine(Test8); printf("Testing cos(8.1): %f\n", number7); }
std::vector<float> CosineHcdf::getRateOfChange(const Chromagram& ch, const Parameters& params){ unsigned int hops = ch.getHops(); unsigned int bins = ch.getBins(); unsigned int gaussianSize = params.getHcdfGaussianSize(); float gaussianSigma = params.getHcdfGaussianSigma(); unsigned int padding = 0; // as opposed to gaussianSize/2 std::vector<float> cosine(hops+padding); for (unsigned int hop = 0; hop < hops; hop++){ float top = 0.0; float bottom = 0.0; for (unsigned int bin = 0; bin < bins; bin++){ float mag = ch.getMagnitude(hop, bin); top += mag; bottom += pow(mag,2); } float cos; if(bottom > 0.0) // divzero cos = top / sqrt(bottom) * sqrt(bins * sqrt(2)); else cos = 0.0; cosine[hop] = cos; } // gaussian std::vector<float> gaussian(gaussianSize); for (unsigned int i=0; i<gaussianSize; i++){ gaussian[i] = exp(-1 * (pow(i - gaussianSize/2 , 2) / (2 * gaussianSigma * gaussianSigma))); } std::vector<float> smoothed(hops); for (unsigned int hop = padding; hop < cosine.size(); hop++){ float conv = 0.0; for (unsigned int k=0; k<gaussianSize; k++){ int frm = hop - (gaussianSize/2) + k; if(frm >= 0 && frm < (signed)cosine.size()){ conv += cosine[frm] * gaussian[k]; } } smoothed[hop-padding] = conv; } // rate of change of hcdf signal; look at all hops except first. std::vector<float> rateOfChange(hops); for (unsigned int hop=1; hop<hops; hop++){ float change = (smoothed[hop] - smoothed[hop-1]) / 90.0; change = (change >= 0 ? change : change * -1.0); change = change / 0.16; // very cheeky magic number; for display purposes in KeyFinder GUI app rateOfChange[hop] = change; } // fudge first rateOfChange[0] = rateOfChange[1]; return rateOfChange; }
int main(int argc, char **argv) { w2v_t w2v; if (load_model(argv[1], &w2v) < 0) { return -1; } char st1[kMaxSize], st2[kMaxSize]; while (fscanf(stdin, "%s\t%s", st1, st2) == 2) { fprintf(stdout, "%s\t%s\t%f\n", st1, st2, cosine(&w2v, st1, st2)); } free_model(&w2v); return 0; }
void dcm_set_origin_location(long o_long, long o_lat, long o_alt) { union longbbbb accum_nav ; lat_origin.WW = o_lat ; long_origin.WW = o_long ; alt_origin.WW = o_alt; // scale the latitude from GPS units to gentleNAV units accum_nav.WW = __builtin_mulss( LONGDEG_2_BYTECIR , lat_origin._.W1 ) ; lat_cir = accum_nav.__.B2 ; // estimate the cosine of the latitude, which is used later computing desired course cos_lat = cosine ( lat_cir ) ; return ; }
void complex_power_complex(float* d, float* s) { /* =(R*exp(i(θ+2kπ))) ^ (a+ib) =(exp(i(θ+2kπ)+lnR)) ^ (a+ib) =exp((i(θ+2kπ)+lnR) * (a+ib)) =exp(alnR-(θ+2kπ)*b)*exp(i((θ+2kπ)*a+blnR)) =exp(alnR-(θ+2kπ)*b)*(cos((θ+2kπ)*a+blnR)+i*sin((θ+2kπ)*a+blnR)) */ float lnr = 0.5 * ln(d[0]*d[0] + d[1]*d[1]); float ang = arctan2(d[1], d[0]); float j = power(2.71828, s[0]*lnr - s[1]*ang); float k = s[0]*ang + s[1]*lnr; d[0] = j * cosine(k); d[1] = j * sine(k); }
void dcm_set_origin_location(int32_t o_lon, int32_t o_lat, int32_t o_alt) { union longbbbb accum_nav; unsigned char lat_cir; lat_origin.WW = o_lat; lon_origin.WW = o_lon; alt_origin.WW = o_alt; // scale the low 16 bits of latitude from GPS units to gentleNAV units accum_nav.WW = __builtin_mulss(LONGDEG_2_BYTECIR, lat_origin._.W1); lat_cir = accum_nav.__.B2; // effectively divides by 256 // estimate the cosine of the latitude, which is used later computing desired course cos_lat = cosine(lat_cir); }
double sine(double x) { int i; double negative=1; if(x<0){ negative=-1; x=-x; } if(x<3.141592653/2) { i=(int)(x/(3.141592653/2)*158); return negative*table[i]; } if(x<3.141592653) { return negative*cosine(x-3.141592653/2); } return 0; }
cv::Mat NFringeStructuredLight::WrapPhase( vector<cv::Mat> fringeImages, cv::Ptr<cv::FilterEngine> filter ) { Utils::AssertOrThrowIfFalse(fringeImages.size() == m_numberOfFringes, "Invalid number of fringes passed into phase wrapper"); // Should be the same size as our fringe images // and floating point precision for decimal phase values cv::Mat sine(fringeImages[0].size(), CV_32F, 0.0f); cv::Mat cosine(fringeImages[0].size(), CV_32F, 0.0f); cv::Mat phase(fringeImages[0].size(), CV_32F, 0.0f); for(int row = 0; row < phase.rows; ++row) { for(int col = 0; col < phase.cols; ++col) { for(int fringe = 0; fringe < m_numberOfFringes; ++fringe) { sine.at<float>(row, col) += ( float( fringeImages[fringe].at<uchar>(row, col) ) / 255.0 ) * sin(2.0 * M_PI * float(fringe) / float(m_numberOfFringes)); cosine.at<float>(row, col) += ( float( fringeImages[fringe].at<uchar>(row, col) ) / 255.0 ) * cos(2.0 * M_PI * float(fringe) / float(m_numberOfFringes)); } } } // Filter out noise in the sine and cosine components if( !filter.empty( ) ) { filter->apply( sine, sine ); filter->apply( cosine, cosine ); } // Now perform phase wrapping for(int row = 0; row < phase.rows; ++row) { for(int col = 0; col < phase.cols; ++col) { // This is negative so that are phase gradient increases from 0 -> rows or 0 -> cols phase.at<float>(row, col) = -atan2( sine.at<float>( row, col ), cosine.at<float>( row, col ) ); } } return phase; }
void carvesolid2d_circle(struct arena* win, u32 rgb, vec3 vc, vec3 vr, vec3 vf) { int a,b,j; float c,s; float bb = (float)(rgb&0xff) / 256.0; float gg = (float)((rgb>>8)&0xff) / 256.0; float rr = (float)((rgb>>16)&0xff) / 256.0; float* vbuf; u16* ibuf; int vlen = trigon2d_vars(win, 0, &vbuf, &ibuf, acc+1, acc); for(j=0;j<acc;j++) { a = j*6; b = j*3; c = cosine(j*tau/acc); s = sine(j*tau/acc); vbuf[a+0] = vc[0] + vr[0]*c + vf[0]*s; vbuf[a+1] = vc[1] + vr[1]*c + vf[1]*s; vbuf[a+2] = vc[2] + vr[2]*c + vf[2]*s; vbuf[a+3] = rr; vbuf[a+4] = gg; vbuf[a+5] = bb; ibuf[b+0] = vlen + acc; ibuf[b+1] = vlen + j; ibuf[b+2] = vlen + (j+1)%acc; } a = acc*6; vbuf[a+0] = vc[0]; vbuf[a+1] = vc[1]; vbuf[a+2] = vc[2]; vbuf[a+3] = rr; vbuf[a+4] = gg; vbuf[a+5] = bb; }
static void calculateEquatorialCentreCorrection(SunState *state) { /* TODO: SINCE G IS NOW Q1.15, CAN WE JUST TAKE sin(2G) INSTEAD OF MODIFYING CORDIC TO OUTPUT BOTH SINE AND COSINE TO USE THE MULTIPLICATION BY IDENTITY ? SINCE IF 2G OVERFLOWS, WE ONLY NEED THE 16 LSbs ANYWAY - WE GET FREE CIRCLE MODULO (AND A LEFT-SHIFT BY ONE PLACE IS QUICK) CONSIDER THE cosc CALCULATION THAT REQUIRES SINE AND COSINE OF TWO TERMS, HOWEVER - THE TIME OVERHEAD IS A LOT GREATER */ /* ec = 1.915 * sin(G) + 0.02 * sin(2 * G) = 1.915 * sin(G) + 0.02 * (2 * sin(G) * cos(G)) = sin(G) + 0.915 * sin(G) + 0.04 * sin(G) * cos(G) Units: degrees (Q17.15) */ FixedQ1_15 sinG = sine(state->meanAnomaly); FixedQ1_15 cosG = cosine(state->meanAnomaly); FixedQ1_15 sinCosProduct = ((int) sinG * cosG) >> 15; Accumulator accumulator = sinG; accumulator <<= 15; accumulator += LITERAL_0_915_Q1_15 * sinG; accumulator += LITERAL_0_04_Q1_15 * sinCosProduct; accumulator >>= 15; state->equatorialCentreCorrection = accumulator; if (!state->flags.quiet) { printf( "\tEquatorial Centre Correction: 0x%.8x (%.8g deg)\n", state->equatorialCentreCorrection, ((double) state->equatorialCentreCorrection) / (1 << 15)); } }
void step_rat(void) { // The keepout is used to know where to -not- put the paddle // the 'bouncepos' is where we expect the ball's y-coord to be when // it intersects with the paddle area static uint8_t right_keepout_top, right_keepout_bot; static uint8_t left_keepout_top, left_keepout_bot; static uint16_t dest_paddle_pos; static uint16_t right_dest, left_dest; // Save old ball location so we can do some vector stuff oldball_x = ball_x; oldball_y = ball_y; // move ball according to the vector ball_x += ball_dx; ball_y += ball_dy; /************************************* TOP & BOTTOM WALLS */ // bouncing off bottom wall, reverse direction // if (ball_y > (SCREEN_H_FIXED - BALL_RADIUS*2*FIXED_MATH - BOTBAR_H_FIXED)) { if (ball_y > (SCREEN_H - BOTBAR_H - BALL_RADIUS*2)*FIXED_MATH) { //DEBUG(putstring_nl("bottom wall bounce")); ball_y = (SCREEN_H - BOTBAR_H - BALL_RADIUS*2)*FIXED_MATH; ball_dy = -ball_dy; } // bouncing off top wall, reverse direction if (ball_y < TOPBAR_H_FIXED) { //DEBUG(putstring_nl("top wall bounce")); ball_y = TOPBAR_H_FIXED; ball_dy = -ball_dy; } /************************************* LEFT & RIGHT WALLS */ // the ball hits either wall, the ball resets location & angle if ( ((INT_MSB(ball_x)) > (SCREEN_W - BALL_RADIUS*2)) || ((int8_t)(INT_MSB(ball_x)) <= 0) || ((ball_dx ==0) && (ball_dy==0)) ) { if(DEBUGGING) { if ((int8_t)(INT_MSB(ball_x)) <= 0) { putstring("Left wall collide"); if (! minute_changed) { putstring_nl("...on accident"); } else { putstring_nl("...on purpose"); } } else { putstring("Right wall collide"); if (! hour_changed) { putstring_nl("...on accident"); } else { putstring_nl("...on purpose"); } } } // place ball in the middle of the screen ball_x = (SCREEN_W/2 - BALL_RADIUS)*FIXED_MATH; ball_y = (SCREEN_H/2 - BALL_RADIUS)*FIXED_MATH; // TODO JMM: don't use cosine/sine... pick one randomly and calc the other one. int8_t angle = random_angle(); ball_dx = (int16_t) (((int32_t) MAX_BALL_SPEED * cosine(angle)) / 0x7FFF); ball_dy = (int16_t) (((int32_t) MAX_BALL_SPEED * sine(angle)) / 0x7FFF); glcdFillRectangle(LEFTPADDLE_X, left_keepout_top, PADDLE_W, left_keepout_bot - left_keepout_top, 0); glcdFillRectangle(RIGHTPADDLE_X, right_keepout_top, PADDLE_W, right_keepout_bot - right_keepout_top, 0); right_keepout_top = right_keepout_bot = 0; left_keepout_top = left_keepout_bot = 0; redraw_time_rat = 1; minute_changed = hour_changed = 0; ticksremaining = calculate_dest_pos(&left_dest, &right_dest, &dest_paddle_pos, ball_dx > 0); //left_score = time_h; //right_score = time_m; setscore_rat(); } // save old paddle position oldleftpaddle_y = leftpaddle_y; oldrightpaddle_y = rightpaddle_y; /* if(ball_dx > 0) { // For debugging, print the ball location DEBUG(putstring("ball @ (")); DEBUG(uart_putw_dec(ball_x)); DEBUG(putstring(", ")); DEBUG(uart_putw_dec(ball_y)); DEBUG(putstring(")")); DEBUG(putstring(" ball_dx @ (")); DEBUG(uart_putw_dec(ball_dx)); DEBUG(putstring(")")); DEBUG(putstring(" ball_dy @ (")); DEBUG(uart_putw_dec(ball_dy)); DEBUG(putstring(")")); DEBUG(putstring(" ball_dy @ (")); DEBUG(uart_putw_dec(ball_dy)); DEBUG(putstring(")")); }*/ /*if(!minute_changed) { if((ball_dx < 0) && (ball_x < (SCREEN_W/2)*FIXED_MATH)) { move_paddle(&leftpaddle_y, ball_y); } } else { //Minute changed. We now have to miss the ball on purpose, if at all possible. //If we don't succeed this time around, we will try again next time around. if((ball_dx < 0) && (ball_x < (SCREEN_W/2)*FIXED_MATH) ) { move_paddle(&leftpaddle_y, dest_paddle_pos); } }*/ //ticksremaining--; if((ball_dx < 0) && (ball_x < (SCREEN_W/2)*FIXED_MATH) ) { move_paddle(&leftpaddle_y, minute_changed?dest_paddle_pos:(ball_y-(PADDLE_H_FIXED/3))); } else if((ball_dx > 0) && (ball_x > (SCREEN_W/2)*FIXED_MATH) ) { move_paddle(&rightpaddle_y, hour_changed?dest_paddle_pos:(ball_y-(PADDLE_H_FIXED/3))); } else { if(ball_dx < 0) ticksremaining = calculate_dest_pos(&left_dest, &right_dest, &dest_paddle_pos, 1); else ticksremaining = calculate_dest_pos(&left_dest, &right_dest, &dest_paddle_pos, 0); } // make sure the paddles dont hit the top or bottom if (leftpaddle_y < TOPBAR_H_FIXED +1) leftpaddle_y = TOPBAR_H_FIXED + 1; if (rightpaddle_y < TOPBAR_H_FIXED + 1) rightpaddle_y = TOPBAR_H_FIXED + 1; if (leftpaddle_y > ((SCREEN_H - PADDLE_H - BOTBAR_H)*FIXED_MATH - 1)) leftpaddle_y = ((SCREEN_H - PADDLE_H - BOTBAR_H)*FIXED_MATH - 1); if (rightpaddle_y> ((SCREEN_H - PADDLE_H - BOTBAR_H)*FIXED_MATH - 1)) rightpaddle_y = ((SCREEN_H - PADDLE_H - BOTBAR_H)*FIXED_MATH - 1); if ((ball_dx > 0) && intersectrect(INT_MSB(ball_x), INT_MSB(ball_y), BALL_RADIUS*2, BALL_RADIUS*2, RIGHTPADDLE_X, INT_MSB(rightpaddle_y), PADDLE_W, PADDLE_H)) { ball_dx = -ball_dx; ball_x = RIGHTPADDLE_X_FIXED - (BALL_RADIUS*2*FIXED_MATH); //ball_y = right_dest; //ticksremaining = calculate_dest_pos(&left_dest, &right_dest, &dest_paddle_pos, 1); } if ((ball_dx < 0) && intersectrect(INT_MSB(ball_x), INT_MSB(ball_y), BALL_RADIUS*2, BALL_RADIUS*2, LEFTPADDLE_X, INT_MSB(leftpaddle_y), PADDLE_W, PADDLE_H)) { ball_dx = -ball_dx; ball_x = LEFTPADDLE_X_FIXED + PADDLE_W_FIXED; //ball_y = left_dest; //ticksremaining = calculate_dest_pos(&left_dest, &right_dest, &dest_paddle_pos, 0); } }
static int compute_petal(XPoint * points, int lines, int sizex, int sizey) /*- * Description: * * Basically, it's combination spirograph and string art. * Instead of doing lines, we just use a complex polygon, * and use an even/odd rule for filling in between. * * The spirograph, in mathematical terms is a polar * plot of the form r = sin (theta * c); * The string art of this is that we evaluate that * function only on certain multiples of theta. That is * we let theta advance in some random increment. And then * we draw a straight line between those two adjacent points. * * Eventually, the lines will start repeating themselves * if we've evaluated theta on some rational portion of the * whole. * * The number of lines generated is limited to the * ratio of the increment we put on theta to the whole. * If we say that there are 360 degrees in a circle, then we * will never have more than 360 lines. * * Return: * * The number of points. * */ { int a, b, d; /* These describe a unique petal */ double r; int theta = 0; XPoint *p = points; int count; int npoints; for (;;) { d = lines; a = rand_range(1, d); b = rand_range(1, d); npoints = numlines(a, b, d); if (npoints >= MINLINES) break; } /* it might be nice to try to move as much sin and cos computing * (or at least the argument computing) out of the loop. */ for (count = npoints; count--;) { r = sine(theta * a, d); /* Convert from polar to cartesian coordinates */ /* We could round the results, but coercing seems just fine */ p->x = (int) (sine(theta, d) * r * sizex + sizex); p->y = (int) (cosine(theta, d) * r * sizey + sizey); /* Advance index into array */ p++; /* Advance theta */ theta += b; theta %= d; } *p = points[0]; /* Tack on another for XDrawLines */ return (npoints); }
int main(int argc, char** argv) { loadConfig(CONFIG_FILE); indexMovie(); if (!hasSimMatrix) { readData(&A, nMovies, nUsers, trainSet); #ifdef DEBUG readArr(A, nMovies, nUsers); #endif /* CHUAN HOA */ printf("\n\nChuan hoa: "); for (int i=0; i<nMovies; i++) { float sum = 0; //Tong cac rating int t = 0; //So rating for (int j=0; j<nUsers; j++) { if (A[i][j] != 0) { sum += A[i][j]; t++; } } float rowMean = 0; if (t!=0) rowMean = sum/t; for (int j=0; j<nUsers; j++) { if (A[i][j] != 0) A[i][j] -= rowMean; } printf("\r\%-10d/%10d", i, nMovies); } #ifdef DEBUG puts(""); printf("\nSTANDARDIZED MARTRIX: \n"); readArr(A, nMovies, nUsers); puts(""); #endif /* TINH SIMILARY MATRIX */ printf("\n\nTinh sim: "); sim = calloc(nMovies, sizeof(float*)); for (int i=0; i<nMovies; i++) sim[i] = calloc(nMovies, sizeof(float*)); for (int i=0; i<nMovies; i++) { for (int j=i+1; j<nMovies; j++) { sim[i][j] = cosine(A[i], A[j], nUsers); } printf("\r%-10d/%10d", i, nMovies); } for (int i=0; i<nMovies; i++) for (int j=0; j<=i; j++) { if (i == j) sim[i][j] = 1; else sim[i][j] = sim[j][i]; } writeDataToFile(sim, nMovies, nMovies, pathToSimMatrix); freeArr(A, nMovies); }
void yypower(void) { int n; p2 = pop(); p1 = pop(); // both base and exponent are rational numbers? if (isrational(p1) && isrational(p2)) { push(p1); push(p2); qpow(); return; } // both base and exponent are either rational or double? if (isnum(p1) && isnum(p2)) { push(p1); push(p2); dpow(); return; } if (istensor(p1)) { power_tensor(); return; } if (p1 == symbol(E) && car(p2) == symbol(LOG)) { push(cadr(p2)); return; } if (p1 == symbol(E) && isdouble(p2)) { push_double(exp(p2->u.d)); return; } // 1 ^ a -> 1 // a ^ 0 -> 1 if (equal(p1, one) || iszero(p2)) { push(one); return; } // a ^ 1 -> a if (equal(p2, one)) { push(p1); return; } // (a * b) ^ c -> (a ^ c) * (b ^ c) if (car(p1) == symbol(MULTIPLY)) { p1 = cdr(p1); push(car(p1)); push(p2); power(); p1 = cdr(p1); while (iscons(p1)) { push(car(p1)); push(p2); power(); multiply(); p1 = cdr(p1); } return; } // (a ^ b) ^ c -> a ^ (b * c) if (car(p1) == symbol(POWER)) { push(cadr(p1)); push(caddr(p1)); push(p2); multiply(); power(); return; } // (a + b) ^ n -> (a + b) * (a + b) ... if (expanding && isadd(p1) && isnum(p2)) { push(p2); n = pop_integer(); // this && n != 0x80000000 added by DDC // as it's not always the case that 0x80000000 // is negative if (n > 1 && n != 0x80000000) { power_sum(n); return; } } // sin(x) ^ 2n -> (1 - cos(x) ^ 2) ^ n if (trigmode == 1 && car(p1) == symbol(SIN) && iseveninteger(p2)) { push_integer(1); push(cadr(p1)); cosine(); push_integer(2); power(); subtract(); push(p2); push_rational(1, 2); multiply(); power(); return; } // cos(x) ^ 2n -> (1 - sin(x) ^ 2) ^ n if (trigmode == 2 && car(p1) == symbol(COS) && iseveninteger(p2)) { push_integer(1); push(cadr(p1)); sine(); push_integer(2); power(); subtract(); push(p2); push_rational(1, 2); multiply(); power(); return; } // complex number? (just number, not expression) if (iscomplexnumber(p1)) { // integer power? // n will be negative here, positive n already handled if (isinteger(p2)) { // / \ n // -n | a - ib | // (a + ib) = | -------- | // | 2 2 | // \ a + b / push(p1); conjugate(); p3 = pop(); push(p3); push(p3); push(p1); multiply(); divide(); push(p2); negate(); power(); return; } // noninteger or floating power? if (isnum(p2)) { #if 1 // use polar form push(p1); mag(); push(p2); power(); push_integer(-1); push(p1); arg(); push(p2); multiply(); push(symbol(PI)); divide(); power(); multiply(); #else // use exponential form push(p1); mag(); push(p2); power(); push(symbol(E)); push(p1); arg(); push(p2); multiply(); push(imaginaryunit); multiply(); power(); multiply(); #endif return; } } if (simplify_polar()) return; push_symbol(POWER); push(p1); push(p2); list(3); }