int main(int argc, char *argv[]) { FILE *inPtr, *alphaPtr; int i, k, m, nin, ei, ti; char *dir, sysCmd[512], filename[64], *NS, alphaFile[128]; float L, *arr, *arrarr[128]; // <- array of pointers to precip arrays float J[100][100]; double Jext; // Get directory name from command line if(argc != 3) { printf("\n\aWrong number of input arguments\n"); exit(0); } dir = argv[1]; L_TARG = atof(argv[2]); NUM_TIMES = (int)ceil( RES_FINT / RES_DT ); sprintf( alphaFile, "%s/alpha_%g_%s", dir, L_TARG, "N" ); arr = getArr(NUM_E, NUM_TIMES); if( (alphaPtr=fopen(alphaFile, "r"))!=NULL ) { nin = fread(arr,sizeof(float),(NUM_E*NUM_TIMES),alphaPtr); fclose(alphaPtr); arrarr[0] = arr; sprintf( alphaFile, "%s/alpha_%g_%s", dir, L_TARG, "S" ); if((alphaPtr=fopen(alphaFile, "r"))==NULL) printf("\n\aprob opening %s\n", alphaFile); arr = getArr(NUM_E, NUM_TIMES); nin = fread(arr,sizeof(float),(NUM_E*NUM_TIMES),alphaPtr); fclose(alphaPtr); arrarr[1] = arr; } else { printf("\n%s cannot be opened\n", alphaFile); } // if alphaFile exists for(k=0; k<2; k++) { compFlux(arrarr[k], L_TARG, k, dir); } // N/S - hemisphere return 0; }
int main(int, char**) { { typedef double T; typedef std::array<T, 3> C; C c = {1, 2, 3.5}; std::get<1>(c) = 5.5; assert(c[0] == 1); assert(c[1] == 5.5); assert(c[2] == 3.5); } #if TEST_STD_VER > 11 { typedef double T; typedef std::array<T, 3> C; constexpr C c = {1, 2, 3.5}; static_assert(std::get<0>(c) == 1, ""); static_assert(std::get<1>(c) == 2, ""); static_assert(std::get<2>(c) == 3.5, ""); } { static_assert(S().k == 3, ""); static_assert(std::get<1>(getArr()) == 4, ""); } #endif return 0; }
/**********************************主函数*****************************/ void main () { cout<<"请输入n×n矩阵的单行元素个数n(500~5000):"; cin>>numArr; if(numArr==0) exit(0); //可以退出 int compCount; for(compCount=0;compCount<numArr;compCount++) //执行比较 { if((compCount+1)%100==0) cout<<compCount+1<<endl; getArr(); statistics(); output(); }
/** * @brief Calculates the trace of a matrix and returns it. Assumes it is given a square matrix * @return The trace of the matrix. */ int IntMatrix :: trace() const { assert (getRow() == getCol()); // go index by index and when the row num equals the col num add that value to the sum int sum = 0; for (int i = 0; i < getRow(); i++) { for(int j = 0; j < getCol(); j++) { if (j == i) { sum += getArr()[(i * getCol()) + j]; } } } return sum; }
/** * @brief Overrides *= operator for IntMatrix to multiply a matrix with current matrix. * @param IntMatrix we wish to multiply the current matrix. * @return IntMatrix& reference to the IntMatrix we updated */ IntMatrix& IntMatrix :: operator*= (const IntMatrix &other) { assert (getCol() == other.getRow()); // initialize and empty matrix of the proper size IntMatrix tempMatrix(getRow(), other.getCol()); // initialize each index in that matrix to be the dot product between a row and column for (int i = 0; i < tempMatrix.getRow(); i++) { for (int j = 0; j < tempMatrix.getCol(); j++) { tempMatrix.getArr()[(i * tempMatrix.getCol()) + j] = dotProduct(&getArr()[i * _colNum], &other.getArr()[j], _colNum, other.getCol()); } } // set our matrix to equal this matrix and return its reference *this = tempMatrix; return *this; }
/***********************主函数************************/ void main() { cout<<"请输入数组元素的个数(500~5000):"; cin>>numArr; if(num==0) exit(0); //可以退出 int compCount; for(compCount=0; compCount<numArr; compCount++) //执行比较 { getArr(); selectionSort(compCount); insertionSort(compCount); bottomupSort(compCount); mergeSort(compCount); quickSort(compCount); } for(int sortMode=0; sortMode<5; sortMode++) statistics(sortMode); output(); }
void compFlux(float *arr, double L, int k, char *dir) { FILE *QPtr, *NPtr; int ei, ti, i, li, nout, iofEbot; double mag, P, Q, epsm, alpha_eq, v, crunch, gamma; double I=0.0, x, g, field, Phi_p, b=1.0, vcm, fcgs; double v_tot_arr[NUM_E], E_tot_arr[NUM_E], dE_arr[NUM_E]; double Jdiff[NUM_E], long_reduce; float alpha, J[100][100], *arrtmp, *Qarr, *Narr; char *NS, QFile[128], NFile[128]; // Open up Phi, Q, N file for writing if(k==0) {NS = "N";} else {NS="S";} sprintf( QFile, "%s/QLong_%g_%s", dir, L, NS ); sprintf( NFile, "%s/NLong_%g_%s", dir, L, NS ); printf("Calculating Q, N for L = %g, E > %g", L, E_MIN); NUM_LONGS = (int) (LONG_TOP - LONG_BOT)/DLONG; if(E_MIN < 1e-3) { iofEbot = 0; } else { // remember E_MIN is in KeV iofEbot = floor((log10(E_MIN*1000)-E_EXP_BOT)/DE_EXP); } arrtmp = getArr(NUM_E, NUM_TIMES); Qarr = getArr( NUM_LONGS, NUM_TIMES ); Narr = getArr( NUM_LONGS, NUM_TIMES ); if( (QPtr=fopen(QFile, "w"))==NULL ) { printf("\nProblem opening %s\n", QFile); exit(0); } if( (NPtr=fopen(NFile, "w"))==NULL ) { printf("\nProblem opening %s\n", NFile); exit(0); } epsm = (1/L)*(R_E+H_IONO)/R_E; crunch = sqrt(1+3*(1-epsm))/pow(epsm,3) ; alpha_eq = asin(sqrt( 1/crunch )); readJ(J); // Precalculate energy and velocity values for(i=0; i<NUM_E; i++) { //Energy in eV E_tot_arr[i] = pow(10, (E_EXP_BOT+(DE_EXP/2)+DE_EXP*i) ); // Energy differential dE in keV dE_arr[i] = 1e-3 * pow(10, (E_EXP_BOT+ (DE_EXP/2))) * exp(DE_EXP*i / log10(NAPe)) * DE_EXP / log10(NAPe); // Differential flux @ this Energy Jdiff[i] = getJdiff( J, E_tot_arr[i], alpha_eq ); // Velocity corresponding to this Energy v_tot_arr[i] = C*sqrt(1 - pow( (E_EL/(E_EL+E_tot_arr[i])) ,2) ); } printf("\n"); for(li=0; li < NUM_LONGS; li++) { // loop over longitudes long_reduce = reduceFactor( li*DLONG ); printf("Now doing long: %g\n", li*DLONG); for(ei=0; ei<NUM_E; ei++) { if(SQUARE) { v = v_tot_arr[ei]; vcm = v*100; // v in cm for distrib fn calculation gamma = 1/sqrt( 1 - v*v/(C*C) ); fcgs = 4.9e5/pow( (vcm*gamma) ,4) - 8.3e14/pow( (vcm*gamma) ,5) + 5.4e23/pow( (vcm*gamma) ,6); // fcgs = 7.034e26 / pow(vcm,6); //10^8/E^2 distribution b = (v*v/M_EL)*pow( sqrt(1 - (v*v)/(C*C)), 3) * 1.6e-8 * fcgs; // b = 1e8 / pow(E_tot_arr[i],2); } else { b = Jdiff[ei]*1000; } for(ti=0; ti<NUM_TIMES; ti++) { alpha = long_reduce * arr[ei*NUM_TIMES+ti] ; mag = 1.4142135623731*alpha; // sqrt(2)*alpha_RMS = peak P = mag/2; //[ alpha_lc - (alpha_lc-mag) ] /2 Q = alpha_eq - mag/2; //[ alpha_lc + (alpha_lc-mag) ] /2 I = 0.0; if(mag > 1e-8 && mag < 1) { for(i=0; i<5; i++) { x = P*t5[i] + Q ; if(SQUARE) { g = (P/PI)*sin(2*x)*( asin((x-alpha_eq)/mag)+ (PI/2) ); } else { g = (P/PI)*sin(2*x)*((x - alpha_eq) * ( asin((x-alpha_eq)/mag)+ (PI/2) ) + sqrt(mag*mag-pow((x-alpha_eq),2))); } I += ( beta5[i]*g ); } // for(i ... ) -> Gauss quad integration } // if mag != 0 Phi_p = PI*crunch*b*I; if(Phi_p<0) { printf("\nPhi_p<0, at ti: %d, ei: %d, li: %d, NS: %s, L: %g\n", ti,ei,li, NS, L_TARG); printf("PI: %g, crunch: %g, b: %g, I: %g, alpha: %g\n", PI, crunch, b, I, alpha); Phi_p = 0; } if( isnan(Phi_p) ) { printf("\nPhi_p isnan, at ti: %d, ei: %d, li: %d, NS: %s, L: %g\n", ti,ei,li, NS, L_TARG); printf("PI: %g, crunch: %g, b: %g, I: %g, alpha: %g\n", PI, crunch, b, I, alpha); Phi_p = 0; } // Now do the integration to get Q and N if(ei >= iofEbot) { Qarr[li*NUM_TIMES+ti] += (float)( Phi_p * E_tot_arr[ei] * dE_arr[ei] * 1.602e-9); Narr[li*NUM_TIMES+ti] += (float)( Phi_p * dE_arr[ei]); //eV->keV } } // for(ti ... ) } // for(ei ... ) } //li nout=fwrite(Qarr, sizeof(float), (NUM_LONGS*NUM_TIMES), QPtr); if(nout!=NUM_LONGS*NUM_TIMES) printf("\n\aProblem writing Q\n"); nout=fwrite(Narr, sizeof(float), (NUM_LONGS*NUM_TIMES), NPtr); if(nout!=NUM_LONGS*NUM_TIMES) printf("\n\aProblem writing N\n"); fclose(QPtr); fclose(NPtr); }