inline static void calc_new_pts_normal(double (*calc_x)(), double (*calc_y)(), double (*calc_z)(), t_vec3 **arg) { t_vec3 *d; t_vec3 *ori; t_vec3 tmp; t_intersect inter; d = arg[0]; ori = arg[1]; update_inter_normal(&inter, ori, 0.000000001, 0.000000001); inter.pos.z = -(d[0].x * (inter.pos.x - ori->x) + d[0].y * (inter.pos.y - ori->y)) / d[0].z + ori->z; calc_tmp_normal(&tmp, &inter); d[1].x = calc_x(&inter, tmp); d[1].y = calc_y(&inter, tmp); d[1].z = calc_z(&inter, tmp); update_inter_normal(&inter, ori, -0.000000001, -0.000000002); inter.pos.z = -(d[0].x * (inter.pos.x - ori->x) + d[0].y * (inter.pos.y - ori->y)) / d[0].z + ori->z; calc_tmp_normal(&tmp, &inter); d[2].x = calc_x(&inter, tmp); d[2].y = calc_y(&inter, tmp); d[2].z = calc_z(&inter, tmp); }
void calc_normale(double (*calc_x)(), double (*calc_y)(), double (*calc_z)(), t_intersect *inter) { t_vec3 d[3]; t_vec3 v[2]; t_vec3 tmp; calc_tmp_normal(&tmp, inter); d[0].x = calc_x(inter, tmp); d[0].y = calc_y(inter, tmp); d[0].z = calc_z(inter, tmp); calc_new_pts_normal(calc_x, calc_y, calc_z, (t_vec3*[2]){d, &inter->pos});
int main() { string main_string = "some string where we want to find substring or sub of string or just sub"; string substring = "sub"; string working_string = substring + main_string; vector<int> z; calc_z(working_string, z); //after this z[i] is maximal length of prefix of working_string //which is equal to string which starting from i-th position of //working_string. So the positions where z[i] >= substring.size() //are positions of substrings. for(int i = substring.size(); i < working_string.size(); ++i) if(z[i] >=substring.size()) cout << i - substring.size() << endl; //to get position in main_string }
void dirichlet_fit_main(struct data_t *data, int rseed) { const int N = data->N, S = data->S, K = data->K; int i, j, k; gsl_rng *ptGSLRNG; gsl_rng_env_setup(); gsl_set_error_handler_off(); ptGSLRNG = gsl_rng_alloc(gsl_rng_default); gsl_set_error_handler_off(); gsl_rng_set(ptGSLRNG, rseed); /* allocate matrices */ double **aadZ, **aadLambda, **aadErr, *adW; adW = (double *) calloc(K, sizeof(double)); aadZ = (double **) calloc(K, sizeof(double *)); aadLambda = (double **) calloc(K, sizeof(double *)); aadErr = (double **) calloc(K, sizeof(double*)); aadZ[0] = (double *) calloc(K * N, sizeof(double)); aadLambda[0] = (double *) calloc(K * S, sizeof(double)); aadErr[0] = (double *) calloc(K * S, sizeof(double)); for (k = 1; k < K; k++) { aadZ[k] = aadZ[0] + k * N; aadLambda[k] = aadLambda[0] + k * S; aadErr[k] = aadErr[0] + k * S; } /* soft k means initialiser */ kmeans(data, ptGSLRNG, adW, aadZ, aadLambda); for (k = 0; k < K; k++) { adW[k] = 0.0; for (i = 0; i < N; i++) adW[k] += aadZ[k][i]; } if (data->verbose) Rprintf(" Expectation Maximization setup\n"); for (k = 0; k < K; k++) { for (j = 0; j < S; j++) { const double x = aadLambda[k][j]; aadLambda[k][j] = (x > 0.0) ? log(x) : -10; } optimise_lambda_k(aadLambda[k], data, aadZ[k]); } /* simple EM algorithm */ int iter = 0; double dNLL = 0.0, dNew, dChange = BIG_DBL; if (data->verbose) Rprintf(" Expectation Maximization\n"); while (dChange > 1.0e-6 && iter < 100) { calc_z(aadZ, data, adW, aadLambda); /* latent var expectation */ for (k = 0; k < K; k++) /* mixture components, given pi */ optimise_lambda_k(aadLambda[k], data, aadZ[k]); for (k = 0; k < K; k++) { /* current likelihood & weights */ adW[k] = 0.0; for(i = 0; i < N; i++) adW[k] += aadZ[k][i]; } dNew = neg_log_likelihood(adW, aadLambda, data); dChange = fabs(dNLL - dNew); dNLL = dNew; iter++; R_CheckUserInterrupt(); if (data->verbose && (iter % 10) == 0) Rprintf(" iteration %d change %f\n", iter, dChange); } /* hessian */ if (data->verbose) Rprintf(" Hessian\n"); gsl_matrix *ptHessian = gsl_matrix_alloc(S, S), *ptInverseHessian = gsl_matrix_alloc(S, S); gsl_permutation *p = gsl_permutation_alloc(S); double dLogDet = 0., dTemp; int signum, status; for (k = 0; k < K; k++) { data->adPi = aadZ[k]; if (k > 0) dLogDet += 2.0 * log(N) - log(adW[k]); hessian(ptHessian, aadLambda[k], data); status = gsl_linalg_LU_decomp(ptHessian, p, &signum); gsl_linalg_LU_invert(ptHessian, p, ptInverseHessian); for (j = 0; j < S; j++) { aadErr[k][j] = gsl_matrix_get(ptInverseHessian, j, j); dTemp = gsl_matrix_get(ptHessian, j, j); dLogDet += log(fabs(dTemp)); } } gsl_matrix_free(ptHessian); gsl_matrix_free(ptInverseHessian); gsl_permutation_free(p); /* results */ double dP = K * S + K - 1; data->NLE = dNLL; data->LogDet = dLogDet; data->fit_laplace = dNLL + 0.5 * dLogDet - 0.5 * dP * log(2. * M_PI); data->fit_bic = dNLL + 0.5 * log(N) * dP; data->fit_aic = dNLL + dP; group_output(data, aadZ); mixture_output(data, adW, aadLambda, aadErr); free(aadErr[0]); free(aadErr); free(aadLambda[0]); free(aadLambda); free(aadZ[0]); free(aadZ); free(adW); }