void CollisionD2Q9BGK::dataToFile(string path){ if(rho == NULL) rho = allocate2DArray(lm->n.y, lm->n.x); if(ux == NULL) ux = allocate2DArray(lm->n.y, lm->n.x); if(uy == NULL) uy = allocate2DArray(lm->n.y, lm->n.x); cout<<"mem allocated"<<endl; double *rhoUTemp = new double[3]; for(int j = 0; j < lm->n.y; j++){ for(int i = 0; i < lm->n.x; i++){ rho[j][i] = get0moment(i, j); get1moment(i, j, rhoUTemp); ux[j][i] = rhoUTemp[X]/rho[j][i]; uy[j][i] = rhoUTemp[Y]/rho[j][i]; } } cout<<"vals calced"<<endl; stringstream ss, ssTemp; struct stat sb; ss.str(""); ss<<path; if (!stat(ss.str().c_str(), &sb) == 0 || !S_ISDIR(sb.st_mode)){ cout<<"creating directory: "<<ss.str()<<endl; mkdir(ss.str().c_str(), 0775); } // stringstream ss, ssTemp; // struct stat sb; // for(int i = 0; ; i++){ // ss.str(""); // ss<<"vis_scripts/data"; // ss<<""<<i<<"/"; // if (!stat(ss.str().c_str(), &sb) == 0 || !S_ISDIR(sb.st_mode)){ // cout<<"creating directory: "<<ss.str()<<endl; // mkdir(ss.str().c_str(), 0775); // break; // } // } cout<<"bgk base..."<<endl; ssTemp.str(""); ssTemp << ss.str(); ssTemp << "ux.csv"; write2DArray(ux, NULL, ssTemp.str(), lm->n.x, lm->n.y); ssTemp.str(""); ssTemp << ss.str(); ssTemp << "uy.csv"; write2DArray(uy, NULL, ssTemp.str(), lm->n.x, lm->n.y); ssTemp.str(""); ssTemp << ss.str(); ssTemp << "rho.csv"; write2DArray(rho, NULL, ssTemp.str(), lm->n.x, lm->n.y); }
void CollisionD2Q9LNPSource::init(){ cout<<"init LNP with source..."<<endl; double initConc = 0.0; ni = allocate2DArray(lm->n.y, lm->n.x); rhs = allocate2DArray(lm->n.y, lm->n.x); for(int j = 0; j < lm->n.y; j++){ for(int i = 0; i < lm->n.x; i++){ for(int d = 0; d < lm->UDIRS; d++){ f[0][j][i][d] = fEq(d, 0, 0, j*j); } } } double colPrefactor = ( 1 - 0.5 * w ); updateNi(); }
void CollisionD2Q9LPMWang::init() { psi = allocate2DArray(n.y, n.x); for(int j = 0; j < n.y; j++) { for(int i = 0; i < n.x; i++) { psi[j][i] = 0.0; for(int d = 0; d < 9; d++) f[0][j][i][d] = fEq(d, psi[j][i]); } } }
double runAlgorithmCholesky(double **inputMatrix, double *b, int size, double *x) { double **factorizedMatrix = allocate2DArray(size, size); double **factorizedMatrixTrans = allocate2DArray(size, size); double *y = allocade1DArray(size); double timeConsumed = 0; time_t start = time(NULL);//POMIAR CZASU START cholesky(size, inputMatrix, factorizedMatrix); //printf("Sfaktoryzowana macierz Choleskiego (U): \n"); //print2DArray(factorizedMatrix, size, size); matrixTransposition(size, factorizedMatrix, factorizedMatrixTrans); //printf("Sfaktoryzowana transponowana macierz Choleskiego (U): \n"); //print2DArray(factorizedMatrixTrans, size, size); forwardSolutionPhase(size, factorizedMatrixTrans, b, y); //printf("\nWynik po forwardSolutionPhase: \n"); //print1DArray(y, size); backwardSolutionPhase(size, factorizedMatrix, y, x); //printf("\nWynik po backwardSolutionPhase: \n"); //print1DArray(x, size); timeConsumed = (double)(time(NULL) - start);//POMIAR CZASU END //printf("\nSprawdzenie rozwiązania: \n"); //checkSolution(inputMatrix, x, b, size); deallocate2D(factorizedMatrix,size); deallocate2D(factorizedMatrixTrans,size); free(y); return timeConsumed; }
void CollisionD2Q9AD::init() { cout << "Initializing AD collision model..."; rho = allocate2DArray(lm->n.y, lm->n.x); cout << "initC: " << initC << endl; for (int j = 0; j < lm->n.y; j++) { for (int i = 0; i < lm->n.x; i++) { rho[j][i] = initC; for (int d = 0; d < lm->UDIRS; d++) { f[0][j][i][d] = fEq(d, initC, 0, 0); } } } cout << " done." << endl; }
void CollisionD2Q9LNP::init() { cout << "Initializing Nernst-Planck Collision model... "; ni = allocate2DArray(lm->n.y, lm->n.x); for (int j = 0; j < lm->n.y; j++) { for (int i = 0; i < lm->n.x; i++) { ni[j][i] = initC; for (int d = 0; d < lm->UDIRS; d++) { f[0][j][i][d] = fEq(d, 0, 0, initC); } } } colPrefactor = ((1 - 0.5 * w) * PHYS_E_CHARGE * z) / (PHYS_KB * T * Pe); //updateNi(); //superfluous cout << "done" << endl; }
int subsetHasSum(int *set, int n, int sum) { int **subset; allocate2DArray(&subset, sum + 1, n + 1); int i, j ; for(i = 0 ; i < sum + 1; i++) { for(j = 0 ; j < n + 1; j++) { printf("%d ",subset[i][j]); } printf("\n"); } for(i = 0 ; i < n + 1; i++) { subset[0][i] = 1; } for(j = 1 ; j < sum + 1; j++) { subset[j][0] = 0; } printf("\n~~~!~!!\n"); for(i = 0 ; i < sum + 1; i++) { for(j = 0 ; j < n + 1; j++) { printf("%d ",subset[i][j]); } printf("\n"); } for(i = 1 ; i < sum+1 ; i++) { for(j = 1 ; j < n+1; j++) { subset[i][j] = subset[i][j-1]; if( i - set[j] >= 0) subset[i][j] = subset[i][j] || subset[i - set[j]][j-1]; } } return subset[sum][n]; }
/* allocate mem for psi */ void CollisionD2Q9LPMChai::init() { cout << "Initializing Chai collision..."; psi = allocate2DArray(n.y, n.x); reset(); cout << " done" << endl; }
/** * Computes Gram matrix of a specified kernel. Given two sets of vectors * A (n1 vectors) and B (n2 vectors), it returns Gram matrix K (n1 x n2). * * @param kernelType 'linear' | 'poly' | 'rbf' | 'cosine' * * @param kernelParam -- | degree | sigma | -- * * @param A a 1D {@code Vector} array * * @param B a 1D {@code Vector} array * * @return Gram matrix (n1 x n2) */ Matrix& calcKernel(std::string kernelType, double kernelParam, Vector** A, int nA, Vector** B, int nB) { Matrix* K = null; if (kernelType == "linear") { double** resData = allocate2DArray(nA, nB, 0); double* resRow = null; Vector* V = null; for (int i = 0; i < nA; i++) { resRow = resData[i]; V = A[i]; for (int j = 0; j < nB; j++) { resRow[j] = innerProduct(*V, *B[j]); } } K = new DenseMatrix(resData, nA, nB); // K = A.transpose().mtimes(B); } else if (kernelType == "cosine") { double* AA = new double[nA]; Vector* V = null; for (int i = 0; i < nA; i++) { V = A[i]; // AA[i] = sum(V->times(*V)); AA[i] = innerProduct(*V, *V); } double* BB = new double[nB]; for (int i = 0; i < nB; i++) { V = B[i]; // BB[i] = sum(V->times(*V)); BB[i] = innerProduct(*V, *V); } double** resData = allocate2DArray(nA, nB, 0); double* resRow = null; for (int i = 0; i < nA; i++) { resRow = resData[i]; V = A[i]; for (int j = 0; j < nB; j++) { resRow[j] = innerProduct(*V, *B[j]) / sqrt(AA[i] * BB[j]); } } K = new DenseMatrix(resData, nA, nB); delete[] AA; delete[] BB; // K = dotMultiply(scalarDivide(1, sqrt(kron(AA.transpose(), BB))), AB); } else if (kernelType == "poly") { double** resData = allocate2DArray(nA, nB, 0); double* resRow = null; Vector* V = null; for (int i = 0; i < nA; i++) { resRow = resData[i]; V = A[i]; for (int j = 0; j < nB; j++) { resRow[j] = pow(innerProduct(*V, *B[j]), kernelParam); } } K = new DenseMatrix(resData, nA, nB); // K = pow(A.transpose().mtimes(B), kernelParam); } else if (kernelType == "rbf") { K = &l2DistanceSquare(A, nA, B, nB); timesAssign(*K, -1 / (2 * pow(kernelParam, 2))); expAssign(*K); // K = exp(l2DistanceSquare(X1, X2).times(-1 / (2 * Math.pow(kernelParam, 2)))); } return *K; }
/** * Predict the single best label sequence given the features for an * observation sequence by Viterbi algorithm. * * @param Fs a 2D {@code Matrix} array, where F[i][j] is the sparse * feature matrix for the j-th feature of the observation sequence * at position i, i.e., f_{j}^{{\bf x}, i} * * @return the single best label sequence for an observation sequence * */ int* CRF::predict(Matrix*** Fs, int length) { Matrix** Ms = computeTransitionMatrix(Fs, length); /* * Alternative backward recursion with scaling for the Viterbi * algorithm */ int n_x = length; double* b = allocateVector(n_x); // Matrix Beta_tilta = new BlockRealMatrix(numStates, n_x); Vector** Beta_tilta = new Vector*[n_x]; for (int i = n_x - 1; i >= 0; i--) { if ( i == n_x - 1) { // Beta_tilta.setColumnMatrix(i, ones(numStates, 1)); Beta_tilta[i] = new DenseVector(numStates, 1); } else { // Beta_tilta.setColumnMatrix(i, mtimes(Ms[i + 1], Beta_tilta.getColumnMatrix(i + 1))); Beta_tilta[i] = &Ms[i + 1]->operate(*Beta_tilta[i + 1]); } b[i] = 1.0 / sum(*Beta_tilta[i]); // Beta_tilta.setColumnMatrix(i, times(b[i], Beta_tilta.getColumnMatrix(i))); timesAssign(*Beta_tilta[i], b[i]); } /*fprintf("Beta:\n"); display(Beta_tilta);*/ /* * Gammas[i](y_{i-1}, y_[i]) is P(y_i|y_{i-1}, Lambda), thus each row of * Gammas[i] should be sum to one. */ double** Gamma_i = allocate2DArray(numStates, numStates, 0); double** Phi = allocate2DArray(n_x, numStates, 0); double** Psi = allocate2DArray(n_x, numStates, 0); double** M_i = null; double* M_i_Row = null; double* Gamma_i_Row = null; double* Beta_tilta_i = null; double* Phi_i = null; double* Phi_im1 = null; double** maxResult = null; for (int i = 0; i < n_x; i++) { M_i = ((DenseMatrix*) Ms[i])->getData(); Beta_tilta_i = ((DenseVector*) Beta_tilta[i])->getPr(); for (int y_im1 = 0; y_im1 < numStates; y_im1++) { M_i_Row = M_i[y_im1]; Gamma_i_Row = Gamma_i[y_im1]; assign(Gamma_i_Row, M_i_Row, numStates); timesAssign(Gamma_i_Row, Beta_tilta_i, numStates); sum2one(Gamma_i_Row, numStates); } Phi_i = Phi[i]; if (i == 0) { // Initialization log(Phi_i, Gamma_i[startIdx], numStates); } else { Phi_im1 = Phi[i - 1]; for (int y_im1 = 0; y_im1 < numStates; y_im1++) { Gamma_i_Row = Gamma_i[y_im1]; logAssign(Gamma_i_Row, numStates); plusAssign(Gamma_i_Row, Phi_im1[y_im1], numStates); } maxResult = max(Gamma_i, numStates, numStates, 1); Phi[i] = maxResult[0]; Psi[i] = maxResult[1]; } } /* * Predict the single best label sequence. */ // double[] phi_n_x = Phi.getRow(n_x - 1); double* phi_n_x = Phi[n_x - 1]; int* YPred = allocateIntegerVector(n_x); for (int i = n_x - 1; i >= 0; i--) { if (i == n_x - 1) { YPred[i] = argmax(phi_n_x, numStates); } else { // YPred[i] = (int)Psi.getEntry(i + 1, YPred[i + 1]); YPred[i] = (int) Psi[i + 1][YPred[i + 1]]; } } /*display(Phi); display(Psi);*/ /* * Predict the optimal conditional probability: P*(y|x) */ double p = exp(phi_n_x[YPred[n_x - 1]]); fprintf("P*(YPred|x) = %g\n", p); return YPred; }