static Expr* num_lte(Expr* args) { assert(args); if(args == EMPTY_LIST) return TRUE; Expr* cur = scm_car(args); checknum(cur); bool ok = true; double curVal = scm_is_int(cur) ? scm_ival(cur) : scm_rval(cur); args = scm_cdr(args); while(scm_is_pair(args)) { cur = scm_car(args); checknum(cur); double newVal = scm_is_int(cur) ? scm_ival(cur) : scm_rval(cur); if(newVal < curVal) { ok = false; break; } curVal = newVal; args = scm_cdr(args); } if(ok && args != EMPTY_LIST) return scm_mk_error("arguments to <= aren't a proper list"); return ok ? TRUE : FALSE; }
void test_rth_space() { socklen_t len; set_funcname("test_rth_space", sizeof("test_rth_space\0")); /* * Test: invalid routing header type. */ len = inet6_rth_space(~IPV6_RTHDR_TYPE_0, 0); checknum(0, len, 0, "invalid routing header type\0"); /* * Test: valid number of segments. */ len = inet6_rth_space(IPV6_RTHDR_TYPE_0, 0); checknum(0, len, 1, "0 segments\0"); len = inet6_rth_space(IPV6_RTHDR_TYPE_0, 127); checknum(0, len, 1, "0 segments\0"); /* * Test: invalid number of segments. */ len = inet6_rth_space(IPV6_RTHDR_TYPE_0, -1); checknum(0, len, 0, "-1 segments\0"); len = inet6_rth_space(IPV6_RTHDR_TYPE_0, 128); checknum(0, len, 0, "128 segments\0"); }
void test_rth_add() { int i, ret; char buf[10240]; struct addrinfo *res; struct addrinfo hints; set_funcname("test_rth_add", sizeof("test_rth_add\0")); if (NULL == inet6_rth_init(buf, 10240, IPV6_RTHDR_TYPE_0, 127)) abort(); memset((void *)&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_INET6; hints.ai_flags = AI_NUMERICHOST; if (0 != getaddrinfo("::1", NULL, (const struct addrinfo *)&hints, &res)) abort(); for (i = 0; i < 127; i++) inet6_rth_add((void *)buf, &((struct sockaddr_in6 *)(res->ai_addr))->sin6_addr); checknum(127, ((struct ip6_rthdr0 *)buf)->ip6r0_segleft, 0, "add 127 segments\0"); ret = inet6_rth_add((void *)buf, &((struct sockaddr_in6 *)(res->ai_addr))->sin6_addr); checknum(-1, ret, 0, "add 128th segment to 127 segment header\0"); freeaddrinfo(res); }
static Expr* num_eq(Expr* args) { assert(args); if(args == EMPTY_LIST) return TRUE; Expr* cur = scm_car(args); checknum(cur); bool eq = true; bool exact = scm_is_int(cur); long long ex; double in; if(exact) { ex = scm_ival(cur); in = ex; } else { in = scm_rval(cur); ex = in; exact = ((double)ex) == in; } args = scm_cdr(args); while(scm_is_pair(args)) { cur = scm_car(args); checknum(cur); if(exact && scm_is_int(cur)) { if(ex != scm_ival(cur)) { eq = false; break; } } else if(exact) { if(in != scm_rval(cur)) { eq = false; break; } } else if(scm_is_real(cur)) { if(in != scm_rval(cur)) { eq = false; break; } } else { eq = false; break; } args = scm_cdr(args); } if(eq && args != EMPTY_LIST) return scm_mk_error("arguments to = aren't a proper list"); return eq ? TRUE : FALSE; }
void test_rth_segments() { int seg; char buf[10240]; set_funcname("test_rth_segments", sizeof("test_rth_segments\0")); /* * Test: invalid routing header type. */ if (NULL == inet6_rth_init((void *)buf, 10240, IPV6_RTHDR_TYPE_0, 0)) abort(); ((struct ip6_rthdr *)buf)->ip6r_type = ~IPV6_RTHDR_TYPE_0; seg = inet6_rth_segments((const void *)buf); checknum(-1, seg, 0, "invalid routing header type\0"); /* * Test: 0 segments. */ if (NULL == inet6_rth_init((void *)buf, 10240, IPV6_RTHDR_TYPE_0, 0)) abort(); seg = inet6_rth_segments((const void *)buf); checknum(0, seg, 0, "0 segments\0"); /* * Test: 127 segments. */ if (NULL == inet6_rth_init((void *)buf, 10240, IPV6_RTHDR_TYPE_0, 127)) abort(); seg = inet6_rth_segments((const void *)buf); checknum(127, seg, 0, "127 segments\0"); /* * Test: -1 segments. */ /* if (NULL == inet6_rth_init((void *)buf, 10240, IPV6_RTHDR_TYPE_0, 0)) abort(); ((struct ip6_rthdr0 *)buf)->ip6r0_len = -1 * 2; seg = inet6_rth_segments((const void *)buf); checknum(-1, seg, 0, "-1 segments\0"); */ /* * Test: 128 segments. */ /* if (NULL == inet6_rth_init((void *)buf, 10240, IPV6_RTHDR_TYPE_0, 127)) abort(); ((struct ip6_rthdr0 *)buf)->ip6r0_len = 128 * 2; seg = inet6_rth_segments((const void *)buf); checknum(-1, seg, 0, "128 segments\0"); */ }
static int magic_variation_staircase(int dim) // Moran's method C, Variation of the staircase method { int x,y,a,b,count; count=1; x=y=(dim>>1); x++; // Rule one putnum(x,y,count++); while (count<=dim*dim) { a=x; b=y; x++; y--; // Rule two, diagonally upwards if (y<0) y=dim-1; if (x>=dim) x=0; if (checknum(x,y)) putnum(x,y,count++); else { x=a+2; y=b; if (x>=dim) x=x-dim; if (y<0) y=dim-1; putnum(x,y,count++); } } return(1); }
static int magic_staircase(int dim) // Moran's method A, the staircase method { int x,y,a,b,count; count=1; y=0; x=(dim>>1); // Rule one putnum(x,y,count++); while (count<=dim*dim) { a=x; b=y; x++; y--; // Rule two, diagonally upwards if (y<0) y=dim-1; if (x>=dim) x=0; if (checknum(x,y)) putnum(x,y,count++); else { x=a; y=b+1; if (y>=dim) y=0; if (y<0) y=dim-1; putnum(x,y,count++); } } return(1); }
// Test that gradient square norma and gradient elements modes are consistent void gradient_forms_test(void) { xc_functional fun = xc_new_functional(); double d_elements[8] = {1, 2.1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6}; double d_sqnorm[5] = {d_elements[0],d_elements[1]}; int nout,i; double *output; double *out2; xc_set(fun,"blyp",1.0); xc_eval_setup(fun,XC_A_B_AX_AY_AZ_BX_BY_BZ,XC_PARTIAL_DERIVATIVES,1); nout = xc_output_length(fun); check("correct output length 1",nout == 9); // 1 + 8 output = malloc(sizeof(*output)*nout); xc_eval(fun,d_elements,output); xc_eval_setup(fun,XC_A_B_GAA_GAB_GBB, XC_PARTIAL_DERIVATIVES,1); nout = xc_output_length(fun); check("correct output length 1",nout == 6); // 1 + 5 out2 = malloc(sizeof(*out2)*nout); d_sqnorm[2] = d_elements[2]*d_elements[2] + d_elements[3]*d_elements[3] + d_elements[4]*d_elements[4]; d_sqnorm[3] = d_elements[2]*d_elements[5] + d_elements[3]*d_elements[6] + d_elements[4]*d_elements[7]; d_sqnorm[4] = d_elements[5]*d_elements[5] + d_elements[6]*d_elements[6] + d_elements[7]*d_elements[7]; xc_eval(fun,d_sqnorm,out2); checknum("Grad modes energy",output[0] - out2[0],0,1e-14,1e-12); checknum("Grad modes density derivs alpha",output[1] - out2[1],0,1e-14,1e-12); checknum("Grad modes density derivs beta", output[2] - out2[2],0,1e-14,1e-12); // d/dg_ax = d/dgaa * g_ax + d/dgab * g_bx for (i=0;i<3;i++) { checknum("Grad modes density grad alpha",output[3+i] - (2*out2[3]*d_elements[2+i] + out2[4]*d_elements[5+i]) ,0,1e-14,1e-12); checknum("Grad modes density grad beta",output[6+i] - (2*out2[5]*d_elements[5+i] + out2[4]*d_elements[2+i]) ,0,1e-14,1e-12); } free(output); free(out2); xc_free_functional(fun); }
void checkbase(Student *stu) //根据条件查询 { int type; do { printf("\n请输入序号选择相应功能\n"); printf("******** 1.按学号查询 ********\n"); printf("******** 2.按姓名查询 ********\n"); printf("******** 3.按名次查询 ********\n"); printf("******** 0.退出 ********\n"); scanf("%d",&type); switch(type) { case 0:printf("");break; case 1:checknum(stu);break; case 2:checkname(stu);break; case 3:checkrank(stu);break; default:printf("没有该选项,请重输");break; } }while(type!=0); }
double Solver4234::solve(ResidualFunction *f, double *X, GaussNewtonParams param, GaussNewtonReport *report){ int itn = 0; while (1) { double *R = new double[f->nR()]; double *J = new double[f->nR()*f->nX()]; f->eval(R, J, X); std::cout<<"X: "<<X[0]<<" "<<X[1]<<" "<<X[2]<<std::endl; cv::Mat matJr(f->nR(),f->nX(),CV_64FC1,J); cv::Mat matR(f->nR(),1,CV_64FC1,R); std::cout<<matR<<std::endl; //array2mat(f->nR(), f->nX(), matJr, J); //array2mat(f->nR(), 1, matR, R); cv::Mat detX = -((matJr.t())*(matJr)).inv()*(matJr.t())*(matR); std::cout<<detX<<std::endl; //write report. if (itn >= param.max_iter) { if (!checknum(X,f->nX())) { report->stop_type = GaussNewtonReport::STOP_NUMERIC_FAILURE; } else{ std::cout<<"final error: "<<err(R, f->nR())<<std::endl; report->stop_type = GaussNewtonReport::STOP_NO_CONVERGE; } break; } else if (cv::norm(matJr, cv::NORM_INF) < param.gradient_tolerance) { if (!checknum(X,f->nX())) { report->stop_type = GaussNewtonReport::STOP_NUMERIC_FAILURE; } else{ std::cout<<"final error: "<<err(R, f->nR())<<std::endl; report->stop_type = GaussNewtonReport::STOP_GRAD_TOL; } break; } else if(cv::norm(matR, cv::NORM_INF) < param.residual_tolerance){ if (!checknum(X,f->nX())) { report->stop_type = GaussNewtonReport::STOP_NUMERIC_FAILURE; } else{ std::cout<<"final error: "<<err(R, f->nR())<<std::endl; report->stop_type = GaussNewtonReport::STOP_RESIDUAL_TOL; } break; } // cv::solve(matJr, -matR, detX, cv::DECOMP_SVD); std::cout<<"detX: "<<detX<<std::endl; std::cout<<"error: "<<err(R, f->nR())<<std::endl; int a = 1; for (int i = 0; i < f->nR(); i++) { X[i] = X[i] + a*detX.at<double>(i,0); } itn++; report->n_iter = itn; delete [] R; delete [] J; } return 0; }
float calc(char end) { float num=0,sum=0,chengji=1,prenum=0,nextnum=0; int in=false; char op,preop,prein=0,ch; num=checknum(); if(status==1) return 0; scanf("%c",&op); if(op==end) return num; else if(op=='+'||op=='-') sum=num; else if(op=='^') { nextnum=checknum(); if(status==1) return 0; num=pow(num,nextnum); scanf("%c",&op); if(op==end) return num; else if(op=='+'||op=='-') sum=num; } while(op!=end) { prenum=num; preop=op; num=checknum(); if(status==1) return 0; scanf("%c",&op); switch(preop) { case '+': case '-': if(in==true) { if(prein=='-') sum-=chengji; else sum+=chengji; chengji=1; } in=false; if(op=='*'||op=='/') { prein=preop; continue; } else if(op=='^') { scanf("%f",&nextnum); num=pow(num,nextnum); scanf("%c",&op); } switch(preop) { case '+': sum+=num; break; case '-': sum-=num; } break; case '*': case '/': if(in==false) chengji=prenum; in=true; if(op=='^') { scanf("%f",&nextnum); num=pow(num,nextnum); scanf("%c",&op); } switch(preop) { case '*': chengji*=num; break; case '/': chengji/=num; } } } if(in==true) if(prein=='-') sum-=chengji; else sum+=chengji; return sum; }
static int magic_knights_move(int dim,int offs,int move) // Moran's method D, The knight's move method { int x,y,a,b,count; count=1; y=(int)offs/dim; x=offs%dim; // Starting place putnum(x,y,count++); while (count<=dim*dim) { a=x; b=y; switch (move) { default: case 1: y-=2; x+=1; break; // 2 up 1 right case 2: y-=1; x+=2; break; // 1 up 2 right case 3: y+=1; x+=2; break; // 1 down 2 right case 4: y+=2; x+=1; break; // 2 down 1 right case 5: y+=2; x-=1; break; // 2 down 1 left case 6: y+=1; x-=2; break; // 1 down 2 left case 7: y-=1; x-=2; break; // 1 up 2 left case 8: y-=2; x-=1; break; // 2 up 1 left } if (x<0) x=dim+x; if (x>=dim) x=x-dim; if (y<0) y=dim+y; if (y>=dim) y=y-dim; if (checknum(x,y)) putnum(x,y,count++); else { x=a; y=b+1; // Blocked move one down if (x<0) x=dim+x; if (x>=dim) x=x-dim; if (y<0) y=dim+y; if (y>=dim) y=y-dim; putnum(x,y,count++); } } return(1); }
/* Test permutation symmetries over variables and modes etc. */ void consistency_test(void) { xc_functional fun = xc_new_functional(); double d_unpolarized[8] = {1,1, 2,-3,4, 2,-3,4}; double d_pol_a[8] = {1,2.1, 2,-3,4, 7,-8,9}; double d_pol_b[8] = {2.1,1, 7,-8,9, 2,-3,4}; int nout; double *output; double *out2; xc_set(fun,"pbe",1.0); xc_eval_setup(fun,XC_A_B_AX_AY_AZ_BX_BY_BZ,XC_PARTIAL_DERIVATIVES,1); nout = xc_output_length(fun); check("correct output length 1",nout == 9); // 1 + 8 output = malloc(sizeof(*output)*nout); out2 = malloc(sizeof(*output)*nout); xc_eval(fun,d_unpolarized,output); checknum("unpolarized symmetry 1",output[1] - output[2],0,1e-14,1e-12); checknum("unpolarized symmetry 2",output[3] - output[6],0,1e-14,1e-12); checknum("unpolarized symmetry 3",output[4] - output[7],0,1e-14,1e-12); checknum("unpolarized symmetry 4",output[5] - output[8],0,1e-14,1e-12); xc_eval(fun,d_pol_a,output); xc_eval(fun,d_pol_b,out2); checknum("polarized symmetry 1",output[1] - out2[2],0,1e-14,1e-12); checknum("polarized symmetry 2",output[3] - out2[6],0,1e-14,1e-12); checknum("polarized symmetry 3",output[4] - out2[7],0,1e-14,1e-12); checknum("polarized symmetry 4",output[5] - out2[8],0,1e-14,1e-12); checknum("polarized symmetry 5",out2[1] - output[2],0,1e-14,1e-12); checknum("polarized symmetry 6",out2[3] - output[6],0,1e-14,1e-12); checknum("polarized symmetry 7",out2[4] - output[7],0,1e-14,1e-12); checknum("polarized symmetry 8",out2[5] - output[8],0,1e-14,1e-12); free(output); free(out2); xc_free_functional(fun); }
void stdcalc() { double u=0,v=0; /* u:输入的第1个数, v:输入的第2个数 */ int flag=0; /* 输入数据是否有小数点标志:0-无 1-有 */ int sign=0; /* 是否单击了运算符:0-无 其他-运算符字符 */ int x,y; /* (x,y)鼠标当前位置 (xx,yy)鼠标前一位置 */ char s[9]; /* 存储输入的数字符号(含小数点) */ int fget=4; /* 前一次单击的按钮标签 */ int d,dn; /* 当前单击的按钮标签 */ int pn=0; /* 当前键盘输入的标签 */ int i=0,j; standard(); save_as_old_mouse(0,0); outtextxy(OUTX-15,OUTY,"0"); outtextxy(OUTX,OUTY,"."); while(1) /* 单击右键则退出简单计算器 */ { if (kbhit()!=0) pn=bioskey(0); if (rightpress()==1||pn==0x11b) {mode=-1;break;} if(leftpress()!=1 && pn==0) /* 鼠标左键未单击的处理 */ { move_mouse(); } else if(MouseLeftFlag==1||pn!=0) /* 鼠标左键单击的处理 */ { if(MouseLeftFlag==1){ MouseLeftFlag=0; /* 置标志为0,防止单击1次左键而多次进入 */ get_mouse_position(&x,&y); d=returnstdkey(x,y); /* 得到单击按钮的标签 */ } else { d=stdgetKey(pn); pn=0; } if(d==-1) continue; if(d==20||d==21||d==22||d==23) {if(d==20) mode=1;if(d==21) mode=2;if(d==22) mode=3;if(d==23) mode=4; break;} show(std[d][0],std[d][2],std[d][1],std[d][3]); if(d==4) /* 单击C开始使用 */ { clearscreen(); outtextxy(OUTX-15,OUTY,"0"); outtextxy(OUTX,OUTY,"."); v=u=0; sign=0; flag=0; i=0; } else if((dn=checknum(d,0))!=-1) /* 单击'0'-'9'数字键的处理 */ { if(dn==0&&u==0&&flag==0) /* 开始时始终单击'0',就显示0 */ { i=0; s[i++]=itoc(dn); s[i++]='.'; s[i]='\0'; u=atof(s); outch(u); } else { if(fget==4||fget==19||fget==3||fget==9) { clearscreen(); i=0; s[i++]=itoc(dn); s[i++]='.'; s[i]='\0'; u=atof(s); outch(u); } if(checknum(fget,0)!=-1) { if(sign==0) /* 未单击运算符,处理第1个数u */ { if(flag==0) /* 输入数据无小数点 */ { clearscreen(); u=u*10+d-'0'; if((u>0&&u<1e8)||(u<0&&u>-1e8)) { s[--i]=itoc(dn); s[++i]='.'; s[++i]='\0'; u=atof(s); outch(u); } else outch(u); } if(flag==1) /* 输入数据有小数点 */ { if(i<=8) { clearscreen(); s[i]=itoc(dn); s[++i]='\0'; u=atof(s); outch(u); } } } if(sign!=0) /* 单击了运算符,处理第2个数v */ { if(flag==0) { clearscreen(); v=v*10+d-'0'; if((v>0&&v<1e8)||(v<0&&v>-1e8)) { s[--i]=itoc(dn); s[++i]='.'; s[++i]='\0'; v=atof(s); outch(v); } else outch(v); } if(flag==1) { if(i<=8) { clearscreen(); s[i++]=itoc(dn); s[i]='\0'; v=atof(s); outch(v); } } } } if(fget==8||fget==13||fget==14||fget==18) { clearscreen(); i=0; s[i++]=itoc(dn); s[i++]='.'; s[i]='\0'; v=atof(s); outch(v); } if(fget==17) /* 前一次单击的是小数点按钮 */ { clearscreen(); s[i]=itoc(dn); s[++i]='\0'; if(sign==0) { u=atof(s); outch(u); } if(sign!=0) { v=atof(s); outch(v); } } } } else if(d==8||d==13||d==14||d==18) { /* 单击加,减,乘,除按钮的处理 */ if(sign!=0) { if(fget==8||fget==13||fget==14||fget==18); else { if(sign==14&&v==0) { clearscreen(); outtextxy(OUTX-15,OUTY,"Err"); } else{ u=calculate(u,v,sign); outch(u); } } } sign=d; flag=0; i=0; } else if(d==19) /* 单击等号按钮的处理 */ { if(sign!=0) { if(sign=='/'&&fabs(v)<1e-6) { clearscreen(); outtextxy(OUTX-15,OUTY,"Err"); } else { u=calculate(u,v,sign); outch(u); } } flag=0; sign=0; i=0; } else if(d==17) /* 单击小数点按钮的处理 */ { if(flag==0) flag=1; } else if(d==3) /* 单击1/x按钮的处理 */ { if(sign==0) /* 如果是第1个数,输出u的百分数 */ {if(fabs(u)>1e-6) {u=1/u; outch(u);} else {clearscreen(); outtextxy(OUTX-15,OUTY,"Err");}} if(sign!=0) /* 是第2个数(单击过运算符),输出v的百分数 */ {if(fabs(v)>1e-6) {v=1/v; outch(v);} else {clearscreen(); outtextxy(OUTX-15,OUTY,"Err");}} i=0; } else if(d==9) /* 单击求平方根按钮的处理 */ { if(sign==0) /*对输入的第1个数求平方根 */ { if(u<0) { clearscreen(); outtextxy(OUTX-15,OUTY,"Err"); } else{ u=sqrt(u); outch(u); } } if(sign!=0) /*对输入的第2个数求平方根 */ { if(v<0) { clearscreen(); outtextxy(OUTX-15,OUTY,"Er"); } else{ v=sqrt(v); outch(v); } } i=0; } else if(d==16) /* 单击+/-按钮的处理 */ { if(sign==0) { u=-u; outch(u); } else { v=-v; outch(v); } } else continue; fget=d; /* 保存上次单击按钮的标签 */ } /* End of else if(MouseLeftFlag==1) */ } /* End of while(rightpress()!=2) */ }