Пример #1
0
void trifib(unsigned long long int n) {
	unsigned long long int ans[3][3] = { 0 };
	unsigned long long int a[3][3] = { { 1,1,1 },{ 1,0,0 },{ 0,1,0 } };
	//long long int b[3][3] = { { 1,2 },{ 3,4 } };
	int x, y;
	unsigned long long int ansmat[3] = { 0 };
	unsigned long long int f0[3] = { 2,1,0 };
	while (n > 0) {
		if (n & 1) {
			for (y = 0; y < 3; y++) {
				for (x = 0; x < 3; x++) {
					ansmat[y] = (ansmat[y]+ (a[y][x] * f0[x])%MOD) % MOD;
				}
			}
			for (y = 0; y < 3; y++) {
				f0[y] = ansmat[y];
				ansmat[y]=0;
			}


		}
		mulMat(a, a, 3);		
		n = n >> 1;
	}
	
	printf("%lld\n",f0[0]);
}
Пример #2
0
int main(){
  int n = 9,p=3,method,doPrint;
  scanf("%d %d %d %d",&n,&p,&method,&doPrint);

  srand(time(NULL));
  double ** matA = (double **) malloc(n*sizeof(double));
  double ** matB = (double **) malloc(n*sizeof(double));
  double ** matC = (double **) malloc(n*sizeof(double));
  int i,j;

  for(i = 0; i<n;i++){
    matA[i] = (double *) malloc(n*sizeof(double));
    matB[i] = (double *) malloc(n*sizeof(double));
    matC[i] = (double *) malloc(n*sizeof(double));
  }

  for(i = 0; i<n;i++){
    for(j = 0; j<n;j++){
      matA[i][j] = rand() % 10;
      matB[i][j] = rand() % 10;
      matC[i][j] = 0;
    }
  }
  if(method)
    pBlockMat(matA,matB,matC,n,p);
  else
    mulMat(matA,matB,matC,n);
  if(doPrint)
    printMats(matA,matB,matC,n);

  printf("\n\n\n\n");
  printf("%f\n",diffMat(matA,matB,matC,n));
  // printf("%f",check(matA,matB,matC,r1,r2,n));
  for(i=0;i<n;i++){
    free(matA[i]);
    free(matB[i]);
    free(matC[i]);
  }
  free(matA);
  free(matB);
  free(matC);
  return 0;
}
Пример #3
0
/* Note: Doesn't handle non-existant residues. */
double AlignAlignmentBlocks(int p1, int p2, int count1, int count2, WeightedResiduePositions *r1, WeightedResiduePositions *r2, int len, Matrix *m, double minScore) {
	Matrix A = {0,0,0,0,0,0,0,0,0,0,0,0};

	Matrix m1, m2, m3, m4;

	Vector com1 = {0,0,0}, com2 = {0,0,0};
	double E0 = 0;

	double rmsd;

	int i, j, k;


	double *normalized1;
	double *normalized2;

	double buffer[2000];
	/*
	 * Only malloc when needed, which isn't often.
	 */
	if (len <= 1000) {
		normalized1 = buffer;
	}
	else {
		normalized1 = (double*) malloc(len * sizeof(double) * 2);
	}
	normalized2 = normalized1+len;

	for (i=0; i<len; i++) {
		Vector v;
		Vector com1delta = {0,0,0}, com2delta = {0,0,0};
		normalized1[i] = 0;
		normalized2[i] = 0;
		for (j=0; j<count1; j++) {
			if (!r1[j].res[p1+i].exists) {
				/* Shouldn't ever happen. */
				return -5;
			}
			normalized1[i] += r1[j].weight;
			addVect(&com1delta, &com1delta, mulVect(&v, &r1[j].res[p1+i].coords, r1[j].weight));
		}
		for (j=0; j<count2; j++) {
			if (!r2[j].res[p2+i].exists) {
				/* Shouldn't ever happen. */
				return -5;
			}
			normalized2[i] += r2[j].weight;
			addVect(&com2delta, &com2delta, mulVect(&v, &r2[j].res[p2+i].coords, r2[j].weight));
		}
		addVect(&com1, &com1, divVect(&com1delta, &com1delta, normalized1[i]));
		addVect(&com2, &com2, divVect(&com2delta, &com2delta, normalized2[i]));
	}
	divVect(&com1, &com1, len);
	divVect(&com2, &com2, len);

	for (i=0; i<len;  i++) {
		if (normalized1[i] != 0 && normalized2[i] != 0) {
			Vector moved1 = {0,0,0};
			Vector moved2 = {0,0,0};

			double E0delta1 = 0;
			double E0delta2 = 0;

			for (j=0; j<count1; j++) {
				if (r1[j].res[p1+i].exists) {
					Vector temp1, temp2;
					subVect(&temp1, &r1[j].res[p1+i].coords, &com1);
					mulVect(&temp2, &temp1, r1[j].weight);

					addVect(&moved1, &moved1, &temp2);
					E0delta1 += dotVect(&temp1, &temp2);
				}
			}
			E0delta1 /= normalized1[i];
			divVect(&moved1, &moved1, normalized1[i]);

			for (j=0; j<count2; j++) {
				if (r2[j].res[p2+i].exists) {
					Vector temp1, temp2;
					subVect(&temp1, &r2[j].res[p2+i].coords, &com2);
					mulVect(&temp2, &temp1, r2[j].weight);
					addVect(&moved2, &moved2, &temp2);
					E0delta2 += dotVect(&temp1, &temp2);
				}
			}
			E0delta2 /= normalized2[i];
			divVect(&moved2, &moved2, normalized2[i]);

			E0 += E0delta1 + E0delta2;

			for (j=0; j<3; j++) {
				for (k=0; k<3; k++)
					A.M[j][k] += moved2.v[k] * moved1.v[j];
			}
		}
	}
	E0/=2;


	if (minScore > 0) {
		Matrix ATA;
		rmsd = CalcRMSD(&ATA, &A, E0, len);
		if (RMSDScoreSimple(rmsd, len) < minScore) {
			/* Don't bother with rotation. */
			return rmsd;
		}
		rmsd = CalcRotation(&m3, &A, &ATA, E0, len);
	}
	else {
		rmsd = CalcRMSDAndRotation(&m3, &A, E0, len);
	}

	m3.M[0][3] = 0;
	m3.M[1][3] = 0;
	m3.M[2][3] = 0;

	m2 = m1 = identity;

	m2.M[0][3] = -com2.v[0];
	m2.M[1][3] = -com2.v[1];
	m2.M[2][3] = -com2.v[2];

	m1.M[0][3] = com1.v[0];
	m1.M[1][3] = com1.v[1];
	m1.M[2][3] = com1.v[2];

	mulMat(&m4, &m3, &m2);
	mulMat(m, &m1, &m4);

	if (normalized1 != buffer) free(normalized1);

	return rmsd;
}