void change_mp() { int c = 0; while(c != '+' && c != '-') { c = prompt_minibuffer("Increase or decrease? (+/-)"); } if(c == '+') add_mp(); if(c == '-') red_mp(); }
//this derived from basic_eval_d int sphere_eval_mp(point_mp funcVals, point_mp parVals, vec_mp parDer, mat_mp Jv, mat_mp Jp, point_mp current_variable_values, comp_mp pathVars, void const *ED) { // evaluates a special homotopy type, built for bertini_real // print_comp_mp_matlab(pathVars,"pathvars"); sphere_eval_data_mp *BED = (sphere_eval_data_mp *)ED; // to avoid having to cast every time BED->SLP_memory.set_globals_to_this(); int ii, jj, mm; // counters int offset; comp_mp one_minus_s, gamma_s; comp_mp temp, temp2; comp_mp func_val_sphere, func_val_start; init_mp(one_minus_s); init_mp(gamma_s); init_mp(temp); init_mp(temp2); init_mp(func_val_start); init_mp(func_val_sphere); set_one_mp(one_minus_s); sub_mp(one_minus_s, one_minus_s, pathVars); // one_minus_s = (1 - s) mul_mp(gamma_s, BED->gamma, pathVars); // gamma_s = gamma * s vec_mp patchValues; init_vec_mp(patchValues, 0); vec_mp temp_function_values; init_vec_mp(temp_function_values,0); vec_mp AtimesF; init_vec_mp(AtimesF,BED->randomizer()->num_rand_funcs()); AtimesF->size = BED->randomizer()->num_rand_funcs();// declare // initialize mat_mp temp_jacobian_functions; init_mat_mp(temp_jacobian_functions,BED->randomizer()->num_base_funcs(),BED->num_variables); temp_jacobian_functions->rows = BED->randomizer()->num_base_funcs(); temp_jacobian_functions->cols = BED->num_variables; mat_mp temp_jacobian_parameters; init_mat_mp(temp_jacobian_parameters,0,0); mat_mp Jv_Patch; init_mat_mp(Jv_Patch, 0, 0); mat_mp AtimesJ; init_mat_mp(AtimesJ,BED->randomizer()->num_rand_funcs(),BED->num_variables); AtimesJ->rows = BED->randomizer()->num_rand_funcs(); AtimesJ->cols = BED->num_variables; //set the sizes change_size_vec_mp(funcVals,BED->num_variables); funcVals->size = BED->num_variables; change_size_mat_mp(Jv, BED->num_variables, BED->num_variables); Jv->rows = Jv->cols = BED->num_variables; // -> this should be square!!! for (ii=0; ii<BED->num_variables; ii++) for (jj=0; jj<BED->num_variables; jj++) set_zero_mp(&Jv->entry[ii][jj]); // evaluate the SLP to get the system's whatnot. evalProg_mp(temp_function_values, parVals, parDer, temp_jacobian_functions, temp_jacobian_parameters, current_variable_values, pathVars, BED->SLP); // evaluate the patch patch_eval_mp(patchValues, parVals, parDer, Jv_Patch, Jp, current_variable_values, pathVars, &BED->patch); // Jp is ignored // we assume that the only parameter is s = t and setup parVals & parDer accordingly. // note that you can only really do this AFTER you are done calling other evaluators. // set parVals & parDer correctly // i.e. these must remain here, or below. \/ change_size_point_mp(parVals, 1); change_size_vec_mp(parDer, 1); change_size_mat_mp(Jp, BED->num_variables, 1); Jp->rows = BED->num_variables; Jp->cols = 1; for (ii=0; ii<BED->num_variables; ii++) set_zero_mp(&Jp->entry[ii][0]); parVals->size = parDer->size = 1; set_mp(&parVals->coord[0], pathVars); // s = t set_one_mp(&parDer->coord[0]); // ds/dt = 1 /////////////////////////// // // the original (randomized) functions. // /////////////////////////////////// BED->randomizer()->randomize(AtimesF,AtimesJ,temp_function_values,temp_jacobian_functions,¤t_variable_values->coord[0]); for (ii=0; ii<AtimesF->size; ii++) // for each function, after (real orthogonal) randomization set_mp(&funcVals->coord[ii], &AtimesF->coord[ii]); for (ii = 0; ii < BED->randomizer()->num_rand_funcs(); ii++) for (jj = 0; jj < BED->num_variables; jj++) set_mp(&Jv->entry[ii][jj],&AtimesJ->entry[ii][jj]); //Jp is 0 for the equations. /////////////////// // // the sphere equation. // ////////////////////////// offset = BED->randomizer()->num_rand_funcs(); mul_mp(func_val_sphere, BED->radius, BED->radius); neg_mp(func_val_sphere, func_val_sphere); mul_mp(func_val_sphere, func_val_sphere, ¤t_variable_values->coord[0]); mul_mp(func_val_sphere, func_val_sphere, ¤t_variable_values->coord[0]); //f_sph = -r^2*h^2 for (int ii=1; ii<BED->num_natural_vars; ii++) { mul_mp(temp2, &BED->center->coord[ii-1], ¤t_variable_values->coord[0]); // temp2 = c_{i-1}*h sub_mp(temp, ¤t_variable_values->coord[ii], temp2); // temp = x_i - h*c_{i-1} mul_mp(temp2, temp, temp); // temp2 = (x_i - h*c_{i-1})^2 add_mp(func_val_sphere, func_val_sphere, temp2); // f_sph += (x_i - h*c_{i-1})^2 } set_one_mp(func_val_start); for (mm=0; mm<2; ++mm) { dot_product_mp(temp, BED->starting_linear[mm], current_variable_values); mul_mp(func_val_start, func_val_start, temp); //f_start *= L_i (x) } // combine the function values mul_mp(temp, one_minus_s, func_val_sphere); mul_mp(temp2, gamma_s, func_val_start); add_mp(&funcVals->coord[offset], temp, temp2); // f = (1-t) f_sph + gamma t f_start //// / / / / / / now the derivatives wrt x // first we store the derivatives of the target function, the sphere. then we will add the part for the linear product start. //ddx for sphere for (int ii=1; ii<BED->num_natural_vars; ii++) { mul_mp(temp2, &BED->center->coord[ii-1], ¤t_variable_values->coord[0]); // temp2 = c_{i-1}*h sub_mp(temp, ¤t_variable_values->coord[ii], temp2) // temp = x_i - c_{i-1}*h mul_mp(&Jv->entry[offset][ii], BED->two, temp); // Jv = 2*(x_i - c_{i-1}*h) mul_mp(&Jv->entry[offset][ii], &Jv->entry[offset][ii], one_minus_s); // Jv = (1-t)*2*(x_i - c_{i-1}*h) mul_mp(temp2, &BED->center->coord[ii-1], temp); // temp2 = c_{i-1} * ( x_i - c_{i-1} * h ) add_mp(&Jv->entry[offset][0], &Jv->entry[offset][0], temp2); // Jv[0] += c_{i-1} * ( x_i - c_{i-1} * h ) } // multiply these entries by (1-t) // the homogenizing var deriv mul_mp(temp, ¤t_variable_values->coord[0], BED->radius); mul_mp(temp, temp, BED->radius); // temp = r^2 h add_mp(&Jv->entry[offset][0], &Jv->entry[offset][0], temp); // Jv[0] = \sum_{i=1}^n {c_{i-1} * ( x_i - c_{i-1} * h )} + r^2 h neg_mp(&Jv->entry[offset][0], &Jv->entry[offset][0]); // Jv[0] = -Jv[0] mul_mp(&Jv->entry[offset][0], &Jv->entry[offset][0], BED->two); // Jv[0] *= 2 mul_mp(&Jv->entry[offset][0], &Jv->entry[offset][0], one_minus_s); // Jv[0] *= (1-t) // f = \sum{ ( x_i - c_{i-1} * h )^2 } - r^2 h^2 //Jv = -2(1-t) ( \sum_{i=1}^n { c_{i-1} * ( x_i - c_{i-1} * h ) } + r^2 h ) // a hardcoded product rule for the two linears. for (int ii=0; ii<BED->num_variables; ii++) { dot_product_mp(temp, BED->starting_linear[0], current_variable_values); mul_mp(temp, temp, &BED->starting_linear[1]->coord[ii]); dot_product_mp(temp2, BED->starting_linear[1], current_variable_values); mul_mp(temp2, temp2, &BED->starting_linear[0]->coord[ii]); add_mp(temp, temp, temp2); mul_mp(temp2, temp, gamma_s); //temp2 = gamma s * (L_1(x) * L_0[ii] + L_0(x) * L_1[ii]) //temp2 now has the value of the derivative of the start system wrt x_i add_mp(&Jv->entry[offset][ii], &Jv->entry[offset][ii], temp2); } // finally, the Jp entry for sphere equation's homotopy. //Jp = -f_sph + gamma f_start neg_mp(&Jp->entry[offset][0], func_val_sphere); mul_mp(temp, BED->gamma, func_val_start); add_mp(&Jp->entry[offset][0], &Jp->entry[offset][0], temp); ////////////// // // function values for the static linears // //////////////////// offset++; for (mm=0; mm<BED->num_static_linears; ++mm) { dot_product_mp(&funcVals->coord[mm+offset], BED->static_linear[mm], current_variable_values); } for (mm=0; mm<BED->num_static_linears; ++mm) { for (ii=0; ii<BED->num_variables; ii++) { set_mp(&Jv->entry[offset+mm][ii], &BED->static_linear[mm]->coord[ii]); } } //Jp is 0 for the static linears ////////////// // // the entries for the patch equations. // //////////////////// if (offset+BED->num_static_linears != BED->num_variables-BED->patch.num_patches) { std::cout << color::red() << "mismatch in offset!\nleft: " << offset+BED->num_static_linears << " right " << BED->num_variables-BED->patch.num_patches << color::console_default() << std::endl; mypause(); } offset = BED->num_variables-BED->patch.num_patches; for (ii=0; ii<BED->patch.num_patches; ii++) set_mp(&funcVals->coord[ii+offset], &patchValues->coord[ii]); for (ii = 0; ii<BED->patch.num_patches; ii++) // for each patch equation { // Jv = Jv_Patch for (jj = 0; jj<BED->num_variables; jj++) // for each variable set_mp(&Jv->entry[ii+offset][jj], &Jv_Patch->entry[ii][jj]); } //Jp is 0 for the patch. // done! yay! if (BED->verbose_level()==16 || BED->verbose_level()==-16) { //uncomment to see screen output of important variables at each solve step. print_comp_matlab(pathVars, "t_mp"); print_comp_matlab(BED->gamma, "gamma_mp"); print_point_to_screen_matlab(current_variable_values,"currvars_mp"); print_point_to_screen_matlab(funcVals,"F_mp"); print_matrix_to_screen_matlab(Jv,"Jv_mp"); print_matrix_to_screen_matlab(Jp,"Jp_mp"); } BED->SLP_memory.set_globals_null(); clear_mp(temp); clear_mp(temp2); clear_mp(gamma_s); clear_mp(one_minus_s); clear_mp(func_val_sphere); clear_mp(func_val_start); clear_vec_mp(patchValues); clear_vec_mp(temp_function_values); clear_vec_mp(AtimesF); clear_mat_mp(temp_jacobian_functions); clear_mat_mp(temp_jacobian_parameters); clear_mat_mp(Jv_Patch); clear_mat_mp(AtimesJ); return 0; }