int numSquares(int n) { int * a = (int *) malloc(sizeof(int)*(n+1)); int temp = 0; int sq = 0; for (int i = 0; i <n+1; i++) a[i] = ~(1 << 31); a[0] = 0; if (n > 0) { a[1] = 1; for (int i = 1; i < n+1; i++) { sq = msqrt(i); if (i == sq * sq) a[i] = 1; for (int j = 1; i + j*j <= n; j++) { if ((a[i]+1) < a[i+j*j]) a[i+j*j] = a[i]+1; } } } temp = a[n]; free(a); return temp; }
int main (int argc, char *argv[]) { MINT *a, *b, *c, *d; short h; mp_set_memory_functions (NULL, NULL, NULL); a = itom (123); b = xtom ("DEADBEEF"); c = itom (0); d = itom (0); move (a, b); madd (a, b, c); msub (a, b, c); mult (a, b, c); mdiv (b, a, c, d); sdiv (b, 2, c, &h); msqrt (a, c, d); pow (b, a, a, c); rpow (a, 3, c); gcd (a, b, c); mcmp (a, b); if (argc > 1) { min (c); mout (a); } mtox (b); mfree(a); exit (0); }
// Standard deviation double sd(int arr[],int times){ double output = 0; double tbp = 0; // tong diem binh phuong double t = 0;// tong diem int i; for (i = 0; i < MPoint; ++i) { tbp += (i+1)*(i+1)*arr[i]; t += arr[i]*(i+1); } output += (tbp - t*t/times)/(times-1); output = msqrt(output,0.000001); return output; }
int main () { double o,ee,input;int c; for(;;){ printf("-----------------------------------------\n1. Input positive number\n2. Caculate sine x\n3. Caculate square root\n4. Caculate e*x\n5. Quit\n\tYour Answer :");scanf("%d",&c);printf("-----------------------------------------\n"); switch(c){ case 1: printf("-----------------------------------------\nInput positive number :");scanf("%lf",&o); printf("Input error in approximation :");scanf("%lf",&ee);printf("-----------------------------------------\n");break; case 3: printf("-----------------------------------------\n"); if (o < 0) {printf("-----------------------------------------\nCan't Caculate suqare root of negative number !\n-----------------------------------------\n");break;} printf("square root of %.5f = %.5f\n",o,msqrt(o,ee));printf("-----------------------------------------\n");break; case 2: input = o; if( input >= 2*PI) while( input >= 2*PI) input = input - 2*PI; printf("sin(%.5lf) = %.5lf\n",o,msin(input,ee));printf("-----------------------------------------\n");break; case 4: printf("e^%.5lf = %.5f\n",o,mex(o,ee));printf("-----------------------------------------\n");break; case 5:return 0; }}}
void test_msqrt(void) { int i; unsigned int *n, *x, *y; logout("testing msqrt\n"); for (i = 0; i < 1000000; i++) { n = mint(i); x = msqrt(n); y = mint((int) (sqrt((double) i) + 1e-10)); if (mcmp(x, y) != 0) { sprintf(logbuf, "failed for %d got %u\n", i, x[0]); logout(logbuf); errout(); } mfree(n); mfree(x); mfree(y); } logout("ok\n"); }
int main(int argc, char **argv) { int n = 200; double r = 10000; if( argc > 1 ) n = atoi(argv[1]); while(n--) { r *= 1.2; if(fork()) { double x = msqrt(r, eps); printf("child %d %lf %lf %lf\n", getpid(), r, x, x*x - r); wait(NULL); exit(0); } struct timeval ts = {.tv_sec = 0, .tv_usec = 100000}; select(0, NULL, NULL, NULL, &ts); } }
unsigned int * mfactor(unsigned int *n) { unsigned int *r, *root, *t, *two, *x, *y; two = mint(2); root = msqrt(n); // y = 1; y = mint(1); // x = 2 isqrt(n) + 1 t = madd(root, root); x = madd(t, y); mfree(t); // r = isqrt(n) ^ 2 - n t = mmul(root, root); r = msub(t, n); mfree(t); mfree(root); while (1) { if (MZERO(r)) { // n = (x - y) / 2 t = msub(x, y); n = mdiv(t, two); mfree(t); mfree(r); mfree(x); mfree(y); mfree(two); return n; } // r = r + x t = madd(r, x); mfree(r); r = t; // x = x + 2 t = madd(x, two); mfree(x); x = t; while (1) { // r = r - y t = msub(r, y); mfree(r); r = t; // y = y + 2 t = madd(y, two); mfree(y); y = t; if (MSIGN(r) == -1 || MZERO(r)) break; } } }
void CCA_caseonly(bool perm, vector<vector<int> > & blperm_case, Set & S, Plink & P) { /////////////// // Output file ofstream EPI; if (!perm) { string f = par::output_file_name+".genepi"; P.printLOG("\nWriting gene-based epistasis tests to [ " + f + " ]\n"); EPI.open(f.c_str(), ios::out); EPI.precision(4); EPI << setw(12) << "NIND" << " " << setw(12) << "GENE1" << " " << setw(12) << "GENE2" << " " << setw(12) << "NSNP1" << " " << setw(12) << "NSNP2" << " " << setw(12) << "CC1" << " " // << setw(12) << "PILLAI" << " " << setw(12) << "BART" << " " << "\n"; } ////////////////////////////////// // Canonical correlation analysis // Number of genes int ns = P.snpset.size(); // Consider each pair of genes for (int s1=0; s1 < ns-1; s1++) { for (int s2 = s1+1; s2 < ns; s2++) { //////////////////////////////////////////////////////// // Step 1. Construct covariance matrix (cases only) // And partition covariance matrix: // S_11 S_21 // S_12 S_22 int n1=0, n2=0; vector<vector<double> > sigma(0); vector<double> mean(0); vector<CSNP*> pSNP(0); ///////////////////////////// // List of SNPs for both loci for (int l=0; l<P.snpset[s1].size(); l++) { if ( S.cur[s1][l] ) { pSNP.push_back( P.SNP[ P.snpset[s1][l] ] ); n1++; } } for (int l=0; l<P.snpset[s2].size(); l++) { if ( S.cur[s2][l] ) { pSNP.push_back( P.SNP[ P.snpset[s2][l] ] ); n2++; } } // NOTE: we need to make sure that n1 < n2. Migth cause problems below if this is not the case.// ********* int n12 = n1 + n2; int ne = n1 < n2 ? n1 : n2;// ne = min(p,q) /////////////////////////////////////////////////// // Choose cases-only P.setFlags(false); vector<Individual*>::iterator person = P.sample.begin(); int ncase=0; while ( person != P.sample.end() ) { if ( (*person)->aff && !(*person)->missing ) { (*person)->flag = true; ncase++; } person++; } int nind = calcGENEPIMeanVariance(pSNP, n1,n2, false, &P, mean, sigma, P.sample , blperm_case[s1], blperm_case[s2] ); /////////////////////////// // Partition covariance matrix vector<vector<double> > I11; vector<vector<double> > I11b; vector<vector<double> > I12; vector<vector<double> > I21; vector<vector<double> > I22; vector<vector<double> > I22b; sizeMatrix( I11, n1, n1); sizeMatrix( I11b, n1, n1); sizeMatrix( I12, n1, n2); sizeMatrix( I21, n2, n1); sizeMatrix( I22, n2, n2); sizeMatrix( I22b, n2, n2); // For step 4b (eigenvectors for gene2) for (int i=0; i<n1; i++) for (int j=0; j<n1; j++) { I11[i][j] = sigma[i][j]; I11b[i][j] = sigma[i][j]; } for (int i=0; i<n1; i++) for (int j=0; j<n2; j++) I12[i][j] = sigma[i][n1+j]; for (int i=0; i<n2; i++) for (int j=0; j<n1; j++) I21[i][j] = sigma[n1+i][j]; for (int i=0; i<n2; i++) for (int j=0; j<n2; j++) { I22[i][j] = sigma[n1+i][n1+j]; I22b[i][j] = sigma[n1+i][n1+j]; } //////////////////////////////////////////////////////// // Step 2. Calculate the p x p matrix M1 = inv(sqrt(sig11)) %*% sig12 %*% inv(sig22) %*% sig21 %*% inv(sqrt(sig11)) bool flag = true; I11 = msqrt(I11); I11 = svd_inverse(I11,flag); I22 = svd_inverse(I22,flag); I22b = msqrt(I22b);// For Step 4b I22b = svd_inverse(I22b,flag); I11b = svd_inverse(I11b,flag); matrix_t tmp; matrix_t M1; multMatrix(I11, I12, tmp); multMatrix(tmp, I22, M1); multMatrix(M1, I21, tmp); multMatrix(tmp, I11, M1); //////////////////////////////////////////////////////// // Step 3. Determine the p eigenvalues of M1. The sqrt(eigen(M)) = // p canonical correlations Identify the largest can corr // Compute evalues and evectors vector_t eigen = eigenvalues(M1); // Sort eigenvalues vector<double> sorted_eigen = eigen; sort(sorted_eigen.begin(),sorted_eigen.end(),greater<double>()); // P-value // long double pillai_pvalue = pillai(ncase,n1,n2,sorted_eigen[0]); long double bartlett_pvalue = bartlett(ncase,n1,n2,sorted_eigen); ///////////////////////////////////////////////////////////////////// // OUTPUT EPI << setw(12) << ncase << " " << setw(12) << P.setname[s1] << " " << setw(12) << P.setname[s2] << " " << setw(12) << n1 << " " << setw(12) << n2 << " " << setw(12) << sqrt(sorted_eigen[0]) << " " // << setw(12) << pillai_pvalue << " " << setw(12) << bartlett_pvalue << " " << "\n"; } // End of loop over genes2 } // End of loop over genes1 EPI.close(); } // End of CCA_caseonly
void CCA_logit(bool perm, vector<vector<int> > & blperm, Set & S, Plink & P) { /////////////// // Output results ofstream EPI; if (!perm) { string f = par::output_file_name+".genepi"; P.printLOG("\nWriting gene-based epistasis tests to [ " + f + " ]\n"); EPI.open(f.c_str(), ios::out); EPI.precision(4); EPI << setw(12) << "NIND" << " " << setw(12) << "GENE1" << " " << setw(12) << "GENE2" << " " << setw(12) << "NSNP1" << " " << setw(12) << "NSNP2" << " " << setw(12) << "P" << " " << "\n"; } ////////////////////////////////// // Canonical correlation analysis int ns = P.snpset.size(); // Consider each pair of genes for (int s1=0; s1 < ns-1; s1++) { for (int s2 = s1+1; s2 < ns; s2++) { //////////////////////////////////////////////////////// // Step 1. Construct covariance matrix (cases and controls together) // And partition covariance matrix: // S_11 S_21 // S_12 S_22 int n1=0, n2=0; vector<vector<double> > sigma(0); vector<double> mean(0); vector<CSNP*> pSNP(0); ///////////////////////////// // List of SNPs for both loci for (int l=0; l<P.snpset[s1].size(); l++) { if ( S.cur[s1][l] ) { pSNP.push_back( P.SNP[ P.snpset[s1][l] ] ); n1++; } } for (int l=0; l<P.snpset[s2].size(); l++) { if ( S.cur[s2][l] ) { pSNP.push_back( P.SNP[ P.snpset[s2][l] ] ); n2++; } } int n12 = n1 + n2; int ne = n1 < n2 ? n1 : n2; /////////////////////////////////// // Construct covariance matrix (cases and controls together) P.setFlags(false); vector<Individual*>::iterator person = P.sample.begin(); while ( person != P.sample.end() ) { (*person)->flag = true; person++; } int nind = calcGENEPIMeanVariance(pSNP, n1,n2, false, &P, mean, sigma, P.sample , blperm[s1], blperm[s2] ); /////////////////////////// // Partition covariance matrix vector<vector<double> > I11; vector<vector<double> > I11b; vector<vector<double> > I12; vector<vector<double> > I21; vector<vector<double> > I22; vector<vector<double> > I22b; sizeMatrix( I11, n1, n1); sizeMatrix( I11b, n1, n1); sizeMatrix( I12, n1, n2); sizeMatrix( I21, n2, n1); sizeMatrix( I22, n2, n2); sizeMatrix( I22b, n2, n2); // For step 4b (eigenvectors for gene2) for (int i=0; i<n1; i++) for (int j=0; j<n1; j++) { I11[i][j] = sigma[i][j]; I11b[i][j] = sigma[i][j]; } for (int i=0; i<n1; i++) for (int j=0; j<n2; j++) I12[i][j] = sigma[i][n1+j]; for (int i=0; i<n2; i++) for (int j=0; j<n1; j++) I21[i][j] = sigma[n1+i][j]; for (int i=0; i<n2; i++) for (int j=0; j<n2; j++) { I22[i][j] = sigma[n1+i][n1+j]; I22b[i][j] = sigma[n1+i][n1+j]; } //////////////////////////////////////////////////////// // Step 2. Calculate the p x p matrix M1 = inv(sqrt(sig11)) %*% sig12 %*% inv(sig22) %*% sig21 %*% inv(sqrt(sig11)) bool flag = true; I11 = msqrt(I11); I11 = svd_inverse(I11,flag); I22 = svd_inverse(I22,flag); I22b = msqrt(I22b);// For Step 4b I22b = svd_inverse(I22b,flag); I11b = svd_inverse(I11b,flag); matrix_t tmp; matrix_t M1; multMatrix(I11, I12, tmp); multMatrix(tmp, I22, M1); multMatrix(M1, I21, tmp); multMatrix(tmp, I11, M1); //////////////////////////////////////////////////////// // Step 4a. Calculate the p eigenvalues and p x p eigenvectors of // M (e). These are required to compute the coefficients used to // build the p canonical variates a[k] for gene1 (see below) double max_cancor = 0.90; // Compute evalues and evectors Eigen gene1_eigen = eigenvectors(M1); // Sort evalues for gene 1. (the first p of these equal the first p of gene 2 (ie M2), if they are sorted) vector<double> sorted_eigenvalues_gene1 = gene1_eigen.d; sort(sorted_eigenvalues_gene1.begin(),sorted_eigenvalues_gene1.end(),greater<double>()); // Position of the largest canonical correlation that is < // max_cancor in the sorted vector of eigenvalues. This will be // needed to use the right gene1 and gene2 coefficients to build // the appropriate canonical variates. double cancor1=0; int cancor1_pos; for (int i=0; i<n1; i++) { if ( sqrt(sorted_eigenvalues_gene1[i]) > cancor1 && sqrt(sorted_eigenvalues_gene1[i]) < max_cancor ) { cancor1 = sqrt(sorted_eigenvalues_gene1[i]); cancor1_pos = i; break; } } // Display largest canonical correlation and its position // cout << "Largest canonical correlation [position]\n" // << cancor1 << " [" << cancor1_pos << "]" << "\n\n" ; // Sort evectors. Rows must be ordered according to cancor value (highest first) matrix_t sorted_eigenvectors_gene1 = gene1_eigen.z; vector<int> order_eigenvalues_gene1(n1); for (int i=0; i<n1; i++) { // Determine position of the vector associated with the ith cancor for (int j=0; j<n1; j++) { if (gene1_eigen.d[j]==sorted_eigenvalues_gene1[i]) { if (i==0) { order_eigenvalues_gene1[i]=j; break; } else { if (j!=order_eigenvalues_gene1[i-1]) { order_eigenvalues_gene1[i]=j; break; } } } } } for (int i=0; i<n1; i++) { sorted_eigenvectors_gene1[i] = gene1_eigen.z[order_eigenvalues_gene1[i]]; } // cout << "Eigenvector matrix - unsorted:\n"; // display(gene1_eigen.z); //cout << "Eigenvector matrix - sorted:\n"; //display(sorted_eigenvectors_gene1); //////////////////////////////////////////////////////// // Step 4b. Calculate the q x q eigenvectors of M2 (f). These are // required to compute the coefficients used to build the p // canonical variates b[k] for gene2 (see below). The first p are // given by: f[k] = (1/sqrt(eigen[k])) * inv_sqrt_I22 %*% I21 %*% // inv_sqrt_sig11 %*% e[k] for (k in 1:p) { e.vectors.gene2[,k] = // (1/sqrt(e.values[k])) * inv.sqrt.sig22 %*% sig21 %*% // inv.sqrt.sig11 %*% e.vectors.gene1[,k] } matrix_t M2; multMatrix(I22b, I21, tmp); multMatrix(tmp, I11b, M2); multMatrix(M2, I12, tmp); multMatrix(tmp, I22b, M2); Eigen gene2_eigen = eigenvectors(M2); //cout << "Eigenvalues Gene 2 - unsorted:\n"; //display(gene2_eigen.d); // Sort evalues for gene2 vector<double> sorted_eigenvalues_gene2 = gene2_eigen.d; sort(sorted_eigenvalues_gene2.begin(),sorted_eigenvalues_gene2.end(),greater<double>()); // Sort eigenvectors for gene2 matrix_t sorted_eigenvectors_gene2 = gene2_eigen.z; vector<int> order_eigenvalues_gene2(n2); for (int i=0; i<n2; i++) { // Determine position of the vector associated with the ith cancor for (int j=0; j<n2; j++) { if (gene2_eigen.d[j]==sorted_eigenvalues_gene2[i]) { if (i==0) { order_eigenvalues_gene2[i]=j; break; } else { if (j!=order_eigenvalues_gene2[i-1]) { order_eigenvalues_gene2[i]=j; break; } } } } } for (int i=0; i<n2; i++) { sorted_eigenvectors_gene2[i] = gene2_eigen.z[order_eigenvalues_gene2[i]]; } //cout << "Eigenvector matrix Gene 2 - unsorted:\n"; //display(gene2_eigen.z); //cout << "Eigenvector matrix Gene 2 - sorted:\n"; //display(sorted_eigenvectors_gene2); //exit(0); ////////////////////////////////////////////////////////////////////////////////// // Step 5 - Calculate the gene1 (pxp) and gene2 (pxq) coefficients // used to create the canonical variates associated with the p // canonical correlations transposeMatrix(gene1_eigen.z); transposeMatrix(gene2_eigen.z); matrix_t coeff_gene1; matrix_t coeff_gene2; multMatrix(gene1_eigen.z, I11, coeff_gene1); multMatrix(gene2_eigen.z, I22b, coeff_gene2); //cout << "Coefficients for Gene 1:\n"; //display(coeff_gene1); //cout << "Coefficients for Gene 2:\n"; //display(coeff_gene2); //exit(0); /////////////////////////////////////////////////////////////////////// // Step 6 - Compute the gene1 and gene2 canonical variates // associated with the highest canonical correlation NOTE: the // original variables of data need to have the mean subtracted first! // Otherwise, the resulting correlation between variate.gene1 and // variate.gene1 != estimated cancor. // For each individual, eg compos.gene1 = // evector.gene1[1]*SNP1.gene1 + evector.gene1[2]*SNP2.gene1 + ... ///////////////////////////////// // Consider each SNP in gene1 vector<double> gene1(nind); for (int j=0; j<n1; j++) { CSNP * ps = pSNP[j]; /////////////////////////// // Iterate over individuals for (int i=0; i< P.n ; i++) { // Only need to look at one perm set bool a1 = ps->one[i]; bool a2 = ps->two[i]; if ( a1 ) { if ( a2 ) // 11 homozygote { gene1[i] += (1 - mean[j]) * coeff_gene1[order_eigenvalues_gene1[cancor1_pos]][j]; } else // 12 { gene1[i] += (0 - mean[j]) * coeff_gene1[order_eigenvalues_gene1[cancor1_pos]][j]; } } else { if ( a2 ) // 21 { gene1[i] += (0 - mean[j]) * coeff_gene1[order_eigenvalues_gene1[cancor1_pos]][j]; } else // 22 homozygote { gene1[i] += (-1 - mean[j]) * coeff_gene1[order_eigenvalues_gene1[cancor1_pos]][j]; } } } // Next individual } // Next SNP in gene1 ///////////////////////////////// // Consider each SNP in gene2 vector<double> gene2(P.n); int cur_snp = -1; for (int j=n1; j<n1+n2; j++) { cur_snp++; CSNP * ps = pSNP[j]; // Iterate over individuals for (int i=0; i<P.n; i++) { // Only need to look at one perm set bool a1 = ps->one[i]; bool a2 = ps->two[i]; if ( a1 ) { if ( a2 ) // 11 homozygote { gene2[i] += (1 - mean[j]) * coeff_gene2[order_eigenvalues_gene2[cancor1_pos]][cur_snp]; } else // 12 { gene2[i] += (0 - mean[j]) * coeff_gene2[order_eigenvalues_gene2[cancor1_pos]][cur_snp]; } } else { if ( a2 ) // 21 { gene2[i] += (0 - mean[j]) * coeff_gene2[order_eigenvalues_gene2[cancor1_pos]][cur_snp]; } else // 22 homozygote { gene2[i] += (-1 - mean[j]) * coeff_gene2[order_eigenvalues_gene2[cancor1_pos]][cur_snp]; } } } // Next individual } // Next SNP in gene2 // Store gene1.variate and gene2.variate in the multiple_covariates field of P.sample // TO DO: NEED TO CHECK IF FIELDS ARE EMPTY FIRST! for (int i=0; i<P.n; i++) { P.sample[i]->clist.resize(2); P.sample[i]->clist[0] = gene1[i]; P.sample[i]->clist[1] = gene2[i]; } /////////////////////////////////////////////// // STEP 7 - Logistic or linear regression epistasis test // Model * lm; if (par::bt) { LogisticModel * m = new LogisticModel(& P); lm = m; } else { LinearModel * m = new LinearModel(& P); lm = m; } // No SNPs used lm->hasSNPs(false); // Set missing data lm->setMissing(); // Main effect of GENE1 1. Assumes that the variable is in position 0 of the clist vector lm->addCovariate(0); lm->label.push_back("GENE1"); // Main effect of GENE 2. Assumes that the variable is in position 1 of the clist vector lm->addCovariate(1); lm->label.push_back("GENE2"); // Epistasis lm->addInteraction(1,2); lm->label.push_back("EPI"); // Build design matrix lm->buildDesignMatrix(); // Prune out any remaining missing individuals // No longer needed (check) // lm->pruneY(); // Fit linear model lm->fitLM(); // Did model fit okay? lm->validParameters(); // Obtain estimates and statistic lm->testParameter = 3; // interaction vector_t b = lm->getCoefs(); double chisq = lm->getStatistic(); double logit_pvalue = chiprobP(chisq,1); // Clean up delete lm; ///////////////////////////// // OUTPUT EPI << setw(12) << nind << " " << setw(12) << P.setname[s1] << " " << setw(12) << P.setname[s2] << " " << setw(12) << n1 << " " << setw(12) << n2 << " " << setw(12) << logit_pvalue << " " << "\n"; } // End of loop over genes2 } // End of loop over genes1 EPI.close(); } // End of CCA_logit()