Esempio n. 1
0
File: main.cpp Progetto: CCJY/coliru
int main(void)
{
    float a, b, c, d, fa, fb, fc,tole;
/**    std::cout<<"a:";
	std::cin>>a;
	std::cout<<"b:";
	std::cin>>b;
**/	
    a = 1;
    b=-3;
    tole = 0.00001;
    d = abso(a,b);
    
	while(d > tole)
	{
	c = (a+b)/2;
	fa = fnc(a);
	fb = fnc(b);
	fc = fnc(c);
	
	if ((fa*fc)>0){
                  a=c;
                  }
                  
                  else{
                       b=c;
                       }
                       d = abso(a,b);
	}
	
	std::cout<<a<<std::endl;
}
Esempio n. 2
0
File: 2022.c Progetto: Jing0/ACM
int main(){
	long i,j,m,n,tmp,max,maxi,maxj;
	while(scanf("%ld%ld",&m,&n)!=EOF){
		for(i=maxi=maxj=max=0;i<m;++i)
			for(j=0;j<n;++j){
				scanf("%ld",&tmp);
				if(abso(tmp)>abso(max)){
					max=tmp;
					maxi=i;
					maxj=j;
				}
			}
		printf("%ld %ld %ld\n",maxi+1,maxj+1,max);
	}
	return 0;
}
Esempio n. 3
0
/* Function: mdlDerivatives */
static void mdlDerivatives(SimStruct *S)
{
    double wn    = ( mxGetPr(ssGetSFcnParam(S,0)) )[0];
    double E     = ( mxGetPr(ssGetSFcnParam(S,1)) )[0];
    double In    = ( mxGetPr(ssGetSFcnParam(S,2)) )[0];
    double Kva   = ( mxGetPr(ssGetSFcnParam(S,3)) )[0];
    double Kvb   = ( mxGetPr(ssGetSFcnParam(S,4)) )[0];
    double KvlkA = ( mxGetPr(ssGetSFcnParam(S,5)) )[0];
    double KvlkB = ( mxGetPr(ssGetSFcnParam(S,6)) )[0];
    double ps    = ( mxGetPr(ssGetSFcnParam(S,7)) )[0];
    double pt    = ( mxGetPr(ssGetSFcnParam(S,8)) )[0];
    double up_dz = ( mxGetPr(ssGetSFcnParam(S,9)) )[0];
    double lw_dz = ( mxGetPr(ssGetSFcnParam(S,10)) )[0];
    double beta  = ( mxGetPr(ssGetSFcnParam(S,11)) )[0];
    double Va0   = ( mxGetPr(ssGetSFcnParam(S,12)) )[0];
    double Vb0   = ( mxGetPr(ssGetSFcnParam(S,13)) )[0];
    double Aa    = ( mxGetPr(ssGetSFcnParam(S,14)) )[0];
    double Ab    = ( mxGetPr(ssGetSFcnParam(S,15)) )[0];
    double L     = ( mxGetPr(ssGetSFcnParam(S,16)) )[0];
    double leak  = ( mxGetPr(ssGetSFcnParam(S,17)) )[0];

    real_T            *dx   = ssGetdX(S);
    real_T            *x    = ssGetContStates(S);
    InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);

    dx[0]= x[1];
    dx[1]= -wn*wn*x[0] - 2*E*wn*x[1] + wn*wn*sat(U(0),-In,In);

    if ((sat(U(0),-In,In) >= up_dz) && (sat(U(0),-In,In) <= In))
    {
        dx[2] = (beta/(Va0 + Aa*U(1))) * (((Kva *(x[0]/In) + KvlkA) * sgn(ps-x[2]) * sqrt(abso(ps-x[2])) - KvlkA * sgn(x[2]-pt) * sqrt(abso(x[2]-pt))) - Aa*U(2)- leak*(x[2]-x[3]));
        dx[3] = (beta/(Vb0 + Ab*(L - U(1)))) * (Ab*U(2) - ((Kvb *(x[0]/In) + KvlkB) * sgn(x[3]-pt) * sqrt(abso(x[3]-pt)) - KvlkB * sgn(ps-x[3]) * sqrt(abso(ps-x[3]))) + leak*(x[2]-x[3]));
    }
    else if ((sat(U(0),-In,In) >= -In) && (sat(U(0),-In,In) <= lw_dz))
    {
        dx[2] = (beta/(Va0 + Aa*U(1))) * ((-(Kva *(abso(x[0])/In) + KvlkA) * sgn(x[2]-pt) * sqrt(abso(x[2]-pt)) + KvlkA * sgn(ps-x[2]) * sqrt(abso(ps-x[2]))) - Aa*U(2)- leak*(x[2]-x[3]));
        dx[3] = (beta/(Vb0 + Ab*(L - U(1)))) * (Ab*U(2) - (-(Kvb *(abso(x[0])/In) + KvlkB) * sgn(ps-x[3]) * sqrt(abso(ps-x[3])) + KvlkB * sgn(x[3]-pt) * sqrt(abso(x[3]-pt))) + leak*(x[2]-x[3]));
    }
    else
    {
        dx[2] = 0;
        dx[3] = 0;
    }

}
Esempio n. 4
0
void generateW(double * x, double mean, int len, double demand_jump_factor)
{
	std::default_random_engine generator;
	double sigma = sqrt(mean)*demand_jump_factor;
	double sigmaJump = mean / 4;
	std::normal_distribution<double> jumpdistribution(0, sigmaJump);

	x[0] = mean;
	double curP = probabilityDensity(x[0], mean, sigma);

	for (int i = 1; i < len; i++)
	{
		x[i] = x[i - 1];
		double nextX = x[i - 1];
		if (getRandNum(100) <= demand_jump_factor)
			nextX += uniondist(mean);
		else 
			nextX +=jumpdistribution(generator);
		double nextP = probabilityDensity(nextX, mean, sigma);
		if (nextP >= curP)
		{
			curP = nextP;
			x[i] = nextX;
		}
		else
		{
			double u = (0.0+abso(getRandNum(10000))) / 10000;
			if (u < nextP / curP)
			{
				curP = nextP;
				x[i] = nextX;
			}
		}
		if (x[i] < 0)
		{
			x[i] = 0;
			curP = probabilityDensity(0, mean, sigma);
		}
        if (isnan(x[i]))
            printf("Nan...........\n");
	}
}
Esempio n. 5
0
// Not a particularly fast line function, but it works, and clips!
void line(int sx, int sy, int ex, int ey, int colour, s_screen *screen){

	int diffx, diffy;
	int absdiffx, absdiffy;
	int xdir, ydir;
	int thres;


	// Some off-screen lines may slip through this test!
	if(sx<0 && ex<0) return;
	if(sy<0 && ey<0) return;
	if(sx>=screen->width && ex>=screen->width) return;
	if(sy>=screen->height && ey>=screen->height) return;


	// Check clipping and calculate new coords if necessary

	diffx = ex - sx;
	diffy = ey - sy;

	if(sx<0){
		sy -= (sx*diffy/diffx);
		sx = 0;
	}
	if(sy<0){
		sx -= (sy*diffx/diffy);
		sy = 0;
	}
	if(sx>=screen->width){
		sy -= ((sx-screen->width)*diffy/diffx);
		sx = screen->width-1;
	}
	if(sy>=screen->height){
		sx -= ((sy-screen->height)*diffx/diffy);
		sy = screen->height-1;
	}

	if(ex<0){
		ey -= (ex*diffy/diffx);
		ex = 0;
	}
	if(ey<0){
		ex -= (ey*diffx/diffy);
		ey = 0;
	}
	if(ex>=screen->width){
		ey -= ((ex-screen->width)*diffy/diffx);
		ex = screen->width-1;
	}
	if(ey>=screen->height){
		ex -= ((ey-screen->height)*diffx/diffy);
		ey = screen->height-1;
	}


	// Second test: the lines that passed test 1 won't pass this time!
	if(sx<0 || ex<0) return;
	if(sy<0 || ey<0) return;
	if(sx>=screen->width || ex>=screen->width) return;
	if(sy>=screen->height || ey>=screen->height) return;


	// Recalculate directions
	diffx = ex - sx;
	diffy = ey - sy;

	absdiffx = abso(diffx);
	absdiffy = abso(diffy);

	sy *= screen->width;
	ey *= screen->width;


	if(absdiffx > absdiffy){
		// Draw a flat line
		thres = absdiffx >> 1;
		xdir = 1;
		if(diffx<0) xdir = -xdir;
		ydir = screen->width;
		if(diffy<0) ydir = -ydir;
		while(sx!=ex){
			screen->data[sx+sy] = colour;
			sx += xdir;
			if((thres-=absdiffy) <= 0){
				sy += ydir;
				thres += absdiffx;
			}
		}
		screen->data[ex+ey] = colour;
		return;
	}
Esempio n. 6
0
// same as the one in draw.c, this is 16bit version
// blendfp is the blending function pointer
void line16(int sx, int sy, int ex, int ey, unsigned short colour, s_screen *screen, int alpha)
{
    int diffx, diffy;
    int absdiffx, absdiffy;
    int xdir, ydir;
    int thres;
    int d;
    unsigned short *data;
    unsigned short(*blendfp)(unsigned short, unsigned short);

    // Some off-screen lines may slip through this test!
    if(sx < 0 && ex < 0)
    {
        return;
    }
    if(sy < 0 && ey < 0)
    {
        return;
    }
    if(sx >= screen->width && ex >= screen->width)
    {
        return;
    }
    if(sy >= screen->height && ey >= screen->height)
    {
        return;
    }


    // Check clipping and calculate new coords if necessary

    diffx = ex - sx;
    diffy = ey - sy;

    if(sx < 0)
    {
        sy -= (sx * diffy / diffx);
        sx = 0;
    }
    if(sy < 0)
    {
        sx -= (sy * diffx / diffy);
        sy = 0;
    }
    if(sx >= screen->width)
    {
        sy -= ((sx - screen->width) * diffy / diffx);
        sx = screen->width - 1;
    }
    if(sy >= screen->height)
    {
        sx -= ((sy - screen->height) * diffx / diffy);
        sy = screen->height - 1;
    }

    if(ex < 0)
    {
        ey -= (ex * diffy / diffx);
        ex = 0;
    }
    if(ey < 0)
    {
        ex -= (ey * diffx / diffy);
        ey = 0;
    }
    if(ex >= screen->width)
    {
        ey -= ((ex - screen->width) * diffy / diffx);
        ex = screen->width - 1;
    }
    if(ey >= screen->height)
    {
        ex -= ((ey - screen->height) * diffx / diffy);
        ey = screen->height - 1;
    }


    // Second test: the lines that passed test 1 won't pass this time!
    if(sx < 0 || ex < 0)
    {
        return;
    }
    if(sy < 0 || ey < 0)
    {
        return;
    }
    if(sx >= screen->width || ex >= screen->width)
    {
        return;
    }
    if(sy >= screen->height || ey >= screen->height)
    {
        return;
    }


    // Recalculate directions
    diffx = ex - sx;
    diffy = ey - sy;

    absdiffx = abso(diffx);
    absdiffy = abso(diffy);

    sy *= screen->width;
    ey *= screen->width;

    data = (unsigned short *)screen->data;

    blendfp = getblendfunction16(alpha);

    if(absdiffx > absdiffy)
    {
        // Draw a flat line
        thres = absdiffx >> 1;
        xdir = 1;
        if(diffx < 0)
        {
            xdir = -xdir;
        }
        ydir = screen->width;
        if(diffy < 0)
        {
            ydir = -ydir;
        }
        while(sx != ex)
        {
            d = sx + sy;
            __putpixel16(data + d);
            sx += xdir;
            if((thres -= absdiffy) <= 0)
            {
                sy += ydir;
                thres += absdiffx;
            }
        }
        d = ex + ey;
        __putpixel16(data + d);
        return;
    }
Esempio n. 7
0
File: calc.c Progetto: dot1q/Misc
double calculate(int numInputTokens, char **inputString)
{
	int i, limit=0;
	double result = 0.0;
	char *s;
	struct DynArr *stack;

	//set up the stack
	stack = createDynArr(20);
	
	printf("testing info! numInputTOkesn: %d\n", numInputTokens);

	// start at 1 to skip the name of the calculator calc
	for(i=1;i < numInputTokens;i++) 
	{
		s = inputString[i];

		// Hint: General algorithm:
		// (1) Check if the string s is in the list of operators.
		//   (1a) If it is, perform corresponding operations.
		//   (1b) Otherwise, check if s is a number.
		//     (1b - I) If s is not a number, produce an error.
		//     (1b - II) If s is a number, push it onto the stack

		if(strcmp(s, "+") == 0){
			add(stack);
			limit = 0; }
		else if(strcmp(s,"-") == 0){
			subtract(stack);
			limit = 0; }
		else if(strcmp(s, "/") == 0){
			divide(stack);
			limit = 0; }
		else if(strcmp(s, "x") == 0){
			multiply(stack);
			limit = 0; }
		else if(strcmp(s, "^") == 0){
			power(stack);
			limit = 0;
			}
		else if(strcmp(s, "^2") == 0){
			square(stack);
			limit = 0;
			}
		else if(strcmp(s, "^3") == 0 ){
			cube(stack);
			limit = 0;
			}
		else if(strcmp(s, "abs") == 0){
			abso(stack);
			limit = 0;
			}
		else if(strcmp(s, "sqrt") == 0){
			sqf(stack);
			limit = 0;
			}
		else if(strcmp(s, "exp") == 0){
			expo(stack);
			limit = 0;
			}
		else if(strcmp(s, "ln") == 0){
			logg(stack);
			limit = 0;
			}
		else if(strcmp(s, "log") == 0){
			logg10(stack);
			limit = 0;
			}
		else 
		{
			if(limit >=2){
				printf("\nLimit has been reached! You did something wrong!\n");
				break;
			}
			//printf("%c is not an operator! converting to a a number!\n",*s);
			int isNum;
			double num = 0;
			isNum = isNumber(s, &num);
			if(isNum == 1){
				//Is infact a number, push to stack
				pushDynArr(stack, num);
				limit++;
				//printf("value %f is now pushed to the statck\n", num);
				printf("\n%f", num);
				
			}else{
				if(strcmp(s, "pi") == 0){
					pushDynArr(stack, 3.144159265);
					printf("\n3.144159265");
					limit++;
				}else if(strcmp(s, "e") == 0){
					pushDynArr(stack, 2.7182818);
					limit++;
					printf("\n2.7182818");
				}else{
					//ignore because it is not a number
					printf("\nPosition %d is not a number, ignoring...\n", i+1);
				}
			}
				//limit is more than 2, this should not happen!
			// Remember to deal with special values ("pi" and "e")
			
		}
	}	//end for 
	
	/* FIXME: You will write this part of the function (2 steps below) 
	 * (1) Check if everything looks OK and produce an error if needed.
	 * (2) Store the final value in result and print it out.
	 */
	if( stack->size > 1){
		//something went wrong
		printf("\nError computing value, check your entry!\n");
	}else{
	result = topDynArr(stack);
	}
	
	return result;
}
	// calculates the shortest paths for all nodes beginning with "source", intelligently recursively.
	void betweenness_geodesic_single_socio (const double * sociomatrix, 
										double * path_count, // matrix, n^2 by n.
										double * spdists,
										const int * pnn,
										const int * psrc) {

		int nn = *pnn; int src = *psrc;
		int current_node, kk, ll, mm, keep_going, outpath, current_count;	
		int iters = 0;
		double maxval = 0; 
		for (kk = 0; kk<nn*nn; kk++) {
			if (sociomatrix[kk]>0) maxval += 1/sociomatrix[kk];
			for (ll=0; ll<nn; ll++) path_count[kk+nn*nn*ll] = 0;
		}

		int * done = new int[nn]; 	int * node_count = new int[nn]; 		

		//std::cout << " " << nn << " " << spdists[nn-1] << " " << std::endl;
		dijkstra_single_socio (sociomatrix, spdists, pnn, psrc);

		//std::cout << " " << nn << " " << spdists[nn-1] << " " << std::endl;
		//for (kk=0; kk<nn; kk++) {std::cout << spdists[kk] << " ";} std::cout << std::endl;

		//bottom up!
		outpath = 0; iters=0;
		// find the farthest node.
		for (kk=0;kk<nn;kk++) {done[kk]=0; node_count[kk]=0; if (spdists[kk]>spdists[outpath]) outpath = kk;}
		done[src] = 1; node_count[src] = 1;
		keep_going = 1; while(keep_going) {
			current_node = outpath; 
			for (kk=0; kk<nn; kk++) {if (spdists[kk]<=spdists[current_node] & !done[kk]) current_node = kk;}
	
			//std::cout << " " << current_node << " " << spdists[current_node] << " " << std::endl;
			// what are the paths for all predecessors?			
			for (kk=0; kk<nn; kk++) {

				if (sociomatrix[kk+nn*current_node]>0 & abso(spdists[current_node]-spdists[kk]-1/sociomatrix[kk+nn*current_node])<1/maxval) {
					// then, there is a path here.
				//	std::cout << current_node << " " << kk << " ";

					current_count = 0;
					//add all the paths that came from the previous steps, times the path count.
					for (ll=0; ll<nn; ll++) for (mm=0; mm<nn; mm++) {
						path_count[mm+nn*ll+nn*nn*current_node] += path_count[mm+nn*ll+nn*nn*kk];
				//		std::cout << mm << " " << kk << ", ";
					}
				//	std::cout << std::endl;
					//add the direct path!
					path_count[kk+nn*current_node+nn*nn*current_node] += node_count[kk];
				}
			}
			for (kk=0; kk<nn; kk++) if (sociomatrix[kk+nn*current_node]>0 & abso(spdists[current_node]-spdists[kk]-1/sociomatrix[kk+nn*current_node])<1/maxval) {node_count[current_node] += node_count[kk];}


			done[current_node] = 1;
			keep_going = 0; for (kk=0; kk<nn; kk++) keep_going += !done[kk];
		//	for (kk=0; kk<nn; kk++) std::cout << done[kk];			std::cout << std::endl;

			iters++; if (iters>2*nn) {REprintf("Something went wrong. Break 2"); break;}
		}

//		for (kk=0; kk<nn; kk++) std::cout << kk << "," << node_count[kk] << "; ";	std::cout << std::endl;

		delete[] done; delete[] node_count; //delete[] spdists;

//		for (kk=0; kk<nn; kk++) std::cout << kk << ",; ";		std::cout << std::endl;

	}
	// calculates the shortest paths for all nodes beginning with "source", intelligently recursively.
	void betweenness_geodesic_single (
								const int * edges,
								const double * strengths, 

								double * path_count, // matrix, ee by nn.
								double * spdists,

								const int * pnn,
								const int * pedges,

								const int * psrc,
								const int * pdest) {

		int nn = *pnn; int src = *psrc; int ee = *pedges;
		int current_node, kk, ll, mm, keep_going, outpath, current_count;	
		int iters = 0;		double maxval = 0; 
		for (kk = 0; kk<ee; kk++) if (strengths[kk]>0) maxval += 1/strengths[kk];
		for (ll = 0; ll<ee*nn; ll++) path_count[ll] = 0;
		
		int * done = new int[nn]; 	int * node_count = new int[nn]; 		

		dijkstra_single (edges, strengths, spdists, pnn, pedges, psrc, pdest);
	
		//bottom up!
		outpath = 0; iters=0;
		// find the farthest node.
		for (kk=0; kk<nn; kk++) {
			//std::cout << spdists[kk] << " ";
			done[kk]=0; node_count[kk]=0; if (spdists[kk]>spdists[outpath]) outpath = kk;
		}
		done[src] = 1; node_count[src] = 1;

		keep_going = 1; while (keep_going) {
			current_node = outpath; //find the closest untouched node.
			for (ll=0; ll<nn; ll++) {if (spdists[ll] <= spdists[current_node] & !done[ll]) current_node = ll;}
			
			kk=0; while (edges[kk+ee] != current_node && kk < ee) kk++;
			while (kk < ee && edges[kk+ee] == current_node) { // edges.

				if (strengths[kk]>0 & abso(spdists[current_node]-spdists[edges[kk]]-1/strengths[kk])<1/maxval) {
					current_count = 0;//add all the paths that came from the previous steps, times the path count.
					for (ll=0; ll<ee; ll++) {
						path_count[ll+ee*current_node] += path_count[ll+ee*edges[kk]];  //source edge.
					}
					//add the direct path!
					path_count[kk+ee*current_node] += node_count[edges[kk]];
				}

				kk++; while (edges[kk+ee] != current_node && kk < ee) kk++;
			}
			kk=0; while (edges[kk+ee] != current_node && kk < ee) kk++;
			while (kk < ee && edges[kk+ee] == current_node) { // edges.
				if (strengths[kk]>0 & abso(spdists[current_node]-spdists[edges[kk]]-1/strengths[kk])<1/maxval) node_count[current_node] += node_count[edges[kk]];
				kk++; while (edges[kk+ee] != current_node && kk < ee) kk++;
			}

			done[current_node] = 1;	keep_going = 0; for (kk=0; kk<nn; kk++) keep_going += !done[kk];
			iters++; if (iters>2*nn) {REprintf("Something went wrong. Break 2"); break;}
			keep_going = (current_node==(*pdest)?0:keep_going);
		}
		delete[] done; delete[] node_count; //delete[] spdists;
	}