예제 #1
0
obj* String(char* value) {
	obj* self = Object();
	set_s(self, "value", value);

	set_d(self, "length", strlen(value) + 1);
	set_d(self, "capacity", strlen(value) + 1);

	bind_c(self, "char at", char_at);
	bind(self, "append", append);

	return self;
}
예제 #2
0
 Plane()
 {
     n[0] = rng.uniform(-0.5, 0.5);
     n[1] = rng.uniform(-0.5, 0.5);
     n[2] = -0.3; //rng.uniform(-1.f, 0.5f);
     n = n / cv::norm(n);
     set_d(rng.uniform(-2.0, 0.6));
 }
예제 #3
0
obj* FormattedString(char* format, ...) {
	va_list args;
	va_start(args, format);

	int size_needed = vsnprintf(NULL, 0, format, args);
	char* value = calloc(size_needed + 1, sizeof(char));
	vsnprintf(value, size_needed, format, args);

	obj* self = Object();
	set_s(self, "value", value);
	free(value);

	set_d(self, "length", size_needed - 1);
	set_d(self, "capacity", size_needed);

	bind_c(self, "char at", char_at);
	bind(self, "append", append);

	return self;
}
예제 #4
0
void append(obj* self, va_list* args) {
	char* part_two = va_arg(*args, char*);
	char* part_one = get_s(self, "value");
	char* joined;

	int part_one_length = strlen(part_one);
	int part_two_length = strlen(part_two);
	int capacity = get_d(self, "capacity");

	if (part_one_length + part_two_length + 1 > capacity) {
		int new_capacity = (capacity * 2) + part_two_length + 1;
		joined = calloc(new_capacity, sizeof(char));
		assert(joined);
		strncpy(joined, part_one, part_one_length);
		set_d(self, "capacity", new_capacity);
	}
 	
	strncat(joined, part_two, part_two_length);
	set_s(self, "value", joined);

	set_d(self, "length", part_one_length + part_two_length);

	free(joined);
}
예제 #5
0
	void copy(const midpoint_eval_data_d & other)
	{


		SolverDoublePrecision::copy(other);

		this->num_mid_vars = other.num_mid_vars;
		this->num_top_vars = other.num_top_vars;
		this->num_bottom_vars = other.num_bottom_vars;

		this->top_memory = other.top_memory;
		this->bottom_memory = other.bottom_memory;
		this->mid_memory = other.mid_memory;

		this->SLP_bottom = other.SLP_bottom;
		this->SLP_mid = other.SLP_mid;
		this->SLP_top = other.SLP_top;


		for (int ii=0; ii<other.num_projections; ii++) {
			add_projection(other.pi[ii]);
		}

		//patch already lives in the base class.

		set_d(v_target,other.v_target);
		set_d(u_target,other.u_target);

		randomizer_bottom = other.randomizer_bottom;
		randomizer_top = other.randomizer_top;

		set_d(crit_val_left,other.crit_val_left);
		set_d(crit_val_right,other.crit_val_right);

		set_d(u_start, other.u_start);
		set_d(v_start, other.v_start);
	}
예제 #6
0
// 
// loexp() fits local expectiles.
//
// The input is series of (x0,y0),(x1,y1),...  However, x_i - x_{i-1}
// is assumed constant, and the x values are not considered. Each observation
// can be given non-negative weight. This routine find 
//   E(y) = beta0 + beta1 x + beta2 x^2
// at fitted locally each point, using Gaussian kernel to weigh nearby
// points. The kernel width is determined by sigma (defined in the number
// of data points).
//
// beta's for each data point i were found to minimize
//
//   \sum_{j=1}^n w_j G_{ij} R_j A_j (y_j-E[y|x_j,beta_i])^2
// 
// which is local (G is gaussian kernel), robust (R is Tukey's biweight 
// determined by the median of residuals) and asymmetric (A is the expectile
// weight: 1-alpha if the residual is negative and alpha if positive).
// 
// The robustness and asymmetry weights are based on pooled residuals
// across all data points (they are not local for a given i). 
// However, the robustness weights are determined separately for
// positive and negative residuals.
// 
// The polynomial dummy x's is arbitrary and thus beta's are not
// meaningful and not returned. Ey is invariant to the choice of dummies.
//
// return:
//  0  converge
//  1  iteration limit exceeded
//  2  non-positive definite X'WX encoutered when solving local regression
//  3  invalid order of polynomials
//  
int
loexp (
    int n,                // data length
    const double *y,      // data vector
    const double *w,      // sample weights (assume w[i]=1 if w == NULL)
    int polynomial_order, // 0, 1 or 2
    double sigma,         // gaussian kernel SD (in # of data points)
    double alpha,         // asymmetry weight [0..1]
    double biweight,      // Tukey's biweight tuning

    double tolerance,     // convergence tolerance
    int iter_max,         // maximum iteration
    
    // output (arrays should be allocated by caller)
    double *Ey,           // fitted curve
    double *v_            // final weights (w_biweight * w_asymmetry),
                          // can be NULL if not needed
    )
{

  /*
  printf("------------------------\nParameters:\n");
  printf("length=%d\n",n);
  printf("polynomial_order=%d\n",polynomial_order);
  printf("iter_max=%d\n",iter_max);
  printf("sigma=%f\n",sigma);
  printf("alpha=%f\n",alpha);
  printf("biweight=%f\n",biweight);
  printf("tolerance=%f\n",tolerance);
  printf("------------------------\n");
  */


  const int RIGHT_PAD = 8; // 8*sigma points needed to avoid kernel distortion
  
  const int K = 3;                       // filter coefficients
  int nn = K + n + RIGHT_PAD * sigma;
  
  double *workspace = alloc(nn*8 + 3*n); // 8 is # sums in quadratic regression
  double *v = workspace + nn*8;          // working regression weights
  double *AR = v + n;                    // absolute residual
  double *wAR = AR + n;                  // weights of absolute residual
  set_d ( 8*K, 0, workspace );           // unused left padding

  if( alpha < 0 ) alpha = 0;
  if( alpha > 1 ) alpha = 1;

  double filter_coeff[4];
  recgauss_3coeff_nonneg ( sigma, filter_coeff );  

  set_d ( n, 1, v ); // start with OLS
  
  /* int i;
  for(i=0; i<n; i++) {
    printf("v[%d]=%f\n",i,v[i]);
  } */
  
  double RSS = 0, oldRSS = 0.0;
  int return_status = 1;
  for(int r = 0; r < iter_max; r++ )
    {
	/* printf("Iteration %d.\n",r); */
    // fill in the LHS-RHS of local regression
    double *s = workspace + 8*K;
    for(int i = 0; i < n; i++, s += 8 )
      {
      double x = 4.0/n * i - 2.0; // polynomial dummy vars
      s[0] = v[i];
      s[1] = v[i]*x;
      s[2] = s[1]*x;
      s[3] = s[2]*x;
      s[4] = s[3]*x;
      s[5] = v[i]*y[i];
      s[6] = s[5]*x;
      s[7] = s[6]*x;
      }
    set_d ( RIGHT_PAD * sigma * 8, 0.0, s );
	
	/* int i; 
    for(i=0; i<n*8; i++) {
      printf("s[%d]=%f\n",i,s[i]);
    } 

	printf("After set_d.\n"); */
    
    // smooth them
    recgauss_filter_col( nn, 8, workspace, 3, filter_coeff );
	//printf("After recgauss_filter_col (polynomial_order=%d).\n",polynomial_order);

    // solve and find Ey
    s = workspace + 8*K;
    RSS = 0;
    int ineg = 0, ipos = n; // index to negative and positive residuals
    double swneg = 0, swpos = 0;
    for(int i = 0; i < n; i++, s += 8 )
      {
	  //printf("%d.\n",i);
      double sw = s[0], sx1 = s[1], sx2 = s[2], sx3 = s[3], sx4 = s[4],
             sy = s[5], sxy = s[6], sx2y = s[7];
      double beta0 = 0, beta1 = 0, beta2 = 0;
      if(polynomial_order == 2 )
        {
        double det = sx2*(2*sx1*sx3 + sw*sx4 - sx2*sx2)
                     - sw*sx3*sx3 -sx1*sx1*sx4;
        if( det <= 0 )
          { return_status = 2; goto RETURN; }
        beta0 = ( sx2*(sx3*sxy + sx4*sy - sx2*sx2y)
                  + sx1*(sx3*sx2y - sx4*sxy) - sx3*sx3*sy )/det;
        beta1 = ( sx2*(sx1*sx2y + sx3*sy - sx2*sxy)
                  + sx4*(sw*sxy - sx1*sy) - sw*sx3*sx2y )/det;
        beta2 = ( sx2*(sw*sx2y + sx1*sxy - sx2*sy)
                  + sx1*(sx3*sy - sx1*sx2y) -sw*sx3*sxy )/det;
        }
      else if(polynomial_order == 1 )
        {
        double det = sw*sx2 - sx1*sx1; 
        if( det <= 0 )
          { return_status = 2; goto RETURN; }
        beta0 = (sx2*sy - sx1*sxy)/det;
        beta1 = (sw*sxy - sx1*sy)/det;
        beta2 = 0;
        }
      else if( polynomial_order == 0 )
        {
        if( sw <= 0 )
          { return_status = 2; goto RETURN; }
        beta0 = sy/sw; beta1 = beta2 = 0;
        }
      else
        { return_status = 3; goto RETURN; }

      double x = 4.0/n * i - 2.0; // polynomial dummy vars
      Ey[i] = beta0 + beta1*x + beta2*x*x;
      double res = y[i] - Ey[i];
      RSS += v[i] * res * res;
      v[i] = res;
      
      double wres = w ? w[i] : 1;
      if( wres <= 0 ) continue;
      if( res < 0 )
        {
        swneg += wres;
        wAR[ineg] = wres;
        AR[ineg++] = fabs(res);
        }
      else
        {
        swpos += wres;
        AR[--ipos] = res;
        wAR[ipos] = wres;
        }
      }
/*
#ifdef LOEXP_SHELL
    fprintf(stderr,"%d RSS = %g\n", r, RSS);
#endif
*/
    if( r > 0 && 2*fabs(RSS - oldRSS)/fabs(RSS+oldRSS) < tolerance )
      { return_status = 0; break; }
    oldRSS = RSS;

    // update the weight
    
    // the scale of robustness kernel is MAR/0.6745 
    // (ref: John Fox 2002, appendix to "An R and S-PLUS Companion to
    // Applied Regression"). 
    // The recommended biweight cutoff is 4.685 of the scale.
    // 
    double bwcutneg = biweight
                       * wquantile( ineg, AR, wAR, swneg, swneg/2 )/0.6745;
    double bwcutpos = biweight
           * wquantile( n-ipos, AR + ipos, wAR + ipos, swpos, swpos/2 )/0.6745;
    
    for(int i = 0; i < n; i++ )
      {
      double res = v[i];
      if( res < 0 )
        {
        if( -res < bwcutneg )
          {
          double u = res/bwcutneg;
          u = 1 - u*u;
          v[i] = (1-alpha) * u * u;
          }
        else
          v[i] = 0;
        }
      else
        {
        if( res < bwcutpos )
          {
          double u = res/bwcutpos;
          u = 1 - u*u;
          v[i] = alpha * u * u;
          }
        else
          v[i] = 0;
        }
      if( w ) v[i] *= w[i];
      if( v_ ) v_[i] = v[i];
      }
    }

RETURN:
  free(workspace);
  return return_status;
}
static cigar* banded_sw (const int8_t* ref,
				 const int8_t* read,
				 int32_t refLen,
				 int32_t readLen,
				 int32_t score,
				 const uint32_t weight_gapO,  /* will be used as - */
				 const uint32_t weight_gapE,  /* will be used as - */
				 int32_t band_width,
				 const int8_t* mat,	/* pointer to the weight matrix */
				 int32_t n) {

	uint32_t *c = (uint32_t*)malloc(16 * sizeof(uint32_t)), *c1;
	int32_t i, j, e, f, temp1, temp2, s = 16, s1 = 8, l, max = 0;
	int64_t s2 = 1024;
	char op, prev_op;
	int32_t width, width_d, *h_b, *e_b, *h_c;
	int8_t *direction, *direction_line;
	cigar* result = (cigar*)malloc(sizeof(cigar));
	h_b = (int32_t*)malloc(s1 * sizeof(int32_t));
	e_b = (int32_t*)malloc(s1 * sizeof(int32_t));
	h_c = (int32_t*)malloc(s1 * sizeof(int32_t));
	direction = (int8_t*)malloc(s2 * sizeof(int8_t));

	do {
		width = band_width * 2 + 3, width_d = band_width * 2 + 1;
		while (width >= s1) {
			++s1;
			kroundup32(s1);
			h_b = (int32_t*)realloc(h_b, s1 * sizeof(int32_t));
			e_b = (int32_t*)realloc(e_b, s1 * sizeof(int32_t));
			h_c = (int32_t*)realloc(h_c, s1 * sizeof(int32_t));
		}
		while (width_d * readLen * 3 >= s2) {
			++s2;
			kroundup32(s2);
			if (s2 < 0) {
				fprintf(stderr, "Alignment score and position are not consensus.\n");
				exit(1);
			}
			direction = (int8_t*)realloc(direction, s2 * sizeof(int8_t));
		}
		direction_line = direction;
		for (j = 1; LIKELY(j < width - 1); j ++) h_b[j] = 0;
		for (i = 0; LIKELY(i < readLen); i ++) {
			int32_t beg = 0, end = refLen - 1, u = 0, edge;
			j = i - band_width;	beg = beg > j ? beg : j; // band start
			j = i + band_width; end = end < j ? end : j; // band end
			edge = end + 1 < width - 1 ? end + 1 : width - 1;
			f = h_b[0] = e_b[0] = h_b[edge] = e_b[edge] = h_c[0] = 0;
			direction_line = direction + width_d * i * 3;

			for (j = beg; LIKELY(j <= end); j ++) {
				int32_t b, e1, f1, d, de, df, dh;
				set_u(u, band_width, i, j);	set_u(e, band_width, i - 1, j);
				set_u(b, band_width, i, j - 1); set_u(d, band_width, i - 1, j - 1);
				set_d(de, band_width, i, j, 0);
				set_d(df, band_width, i, j, 1);
				set_d(dh, band_width, i, j, 2);

				temp1 = i == 0 ? -weight_gapO : h_b[e] - weight_gapO;
				temp2 = i == 0 ? -weight_gapE : e_b[e] - weight_gapE;
				e_b[u] = temp1 > temp2 ? temp1 : temp2;
				direction_line[de] = temp1 > temp2 ? 3 : 2;

				temp1 = h_c[b] - weight_gapO;
				temp2 = f - weight_gapE;
				f = temp1 > temp2 ? temp1 : temp2;
				direction_line[df] = temp1 > temp2 ? 5 : 4;

				e1 = e_b[u] > 0 ? e_b[u] : 0;
				f1 = f > 0 ? f : 0;
				temp1 = e1 > f1 ? e1 : f1;
				temp2 = h_b[d] + mat[ref[j] * n + read[i]];
				h_c[u] = temp1 > temp2 ? temp1 : temp2;

				if (h_c[u] > max) max = h_c[u];

				if (temp1 <= temp2) direction_line[dh] = 1;
				else direction_line[dh] = e1 > f1 ? direction_line[de] : direction_line[df];
			}
			for (j = 1; j <= u; j ++) h_b[j] = h_c[j];
		}
		band_width *= 2;
	} while (LIKELY(max < score));
	band_width /= 2;

	// trace back
	i = readLen - 1;
	j = refLen - 1;
	e = 0;	// Count the number of M, D or I.
	l = 0;	// record length of current cigar
	op = prev_op = 'M';
	temp2 = 2;	// h
	while (LIKELY(i > 0)) {
		set_d(temp1, band_width, i, j, temp2);
		switch (direction_line[temp1]) {
			case 1:
				--i;
				--j;
				temp2 = 2;
				direction_line -= width_d * 3;
				op = 'M';
				break;
			case 2:
			 	--i;
				temp2 = 0;	// e
				direction_line -= width_d * 3;
				op = 'I';
				break;
			case 3:
				--i;
				temp2 = 2;
				direction_line -= width_d * 3;
				op = 'I';
				break;
			case 4:
				--j;
				temp2 = 1;
				op = 'D';
				break;
			case 5:
				--j;
				temp2 = 2;
				op = 'D';
				break;
			default:
				fprintf(stderr, "Trace back error: %d.\n", direction_line[temp1 - 1]);
				free(direction);
				free(h_c);
				free(e_b);
				free(h_b);
				free(c);
				free(result);
				return 0;
		}
		if (op == prev_op) ++e;
		else {
			++l;
			while (l >= s) {
				++s;
				kroundup32(s);
				c = (uint32_t*)realloc(c, s * sizeof(uint32_t));
			}
			c[l - 1] = to_cigar_int(e, prev_op);
			prev_op = op;
			e = 1;
		}
	}
	if (op == 'M') {
		++l;
		while (l >= s) {
			++s;
			kroundup32(s);
			c = (uint32_t*)realloc(c, s * sizeof(uint32_t));
		}
		c[l - 1] = to_cigar_int(e + 1, op);
	}else {
		l += 2;
		while (l >= s) {
			++s;
			kroundup32(s);
			c = (uint32_t*)realloc(c, s * sizeof(uint32_t));
		}
		c[l - 2] = to_cigar_int(e, op);
		c[l - 1] = to_cigar_int(1, 'M');
	}

	// reverse cigar
	c1 = (uint32_t*)malloc(l * sizeof(uint32_t));
	s = 0;
	e = l - 1;
	while (LIKELY(s <= e)) {
		c1[s] = c[e];
		c1[e] = c[s];
		++ s;
		-- e;
	}
	result->seq = c1;
	result->length = l;

	free(direction);
	free(h_c);
	free(e_b);
	free(h_b);
	free(c);
	return result;
}
예제 #8
0
int sphere_eval_d(point_d funcVals, point_d parVals, vec_d parDer, mat_d Jv, mat_d Jp,
				  point_d current_variable_values, comp_d pathVars,
				  void const *ED)
{ // evaluates a special homotopy type, built for bertini_real
	
	sphere_eval_data_d *BED = (sphere_eval_data_d *)ED; // to avoid having to cast every time
	
	
	BED->SLP_memory.set_globals_to_this();
	
	int ii, jj, mm; // counters
	int offset;
	comp_d one_minus_s, gamma_s;
	comp_d temp, temp2;
	comp_d func_val_sphere, func_val_start;
	
	set_one_d(one_minus_s);
	sub_d(one_minus_s, one_minus_s, pathVars);  // one_minus_s = (1 - s)
	mul_d(gamma_s, BED->gamma, pathVars);       // gamma_s = gamma * s
	
	
	vec_d patchValues; init_vec_d(patchValues, 0);
	vec_d temp_function_values; init_vec_d(temp_function_values,0);
	vec_d AtimesF; init_vec_d(AtimesF,BED->randomizer()->num_rand_funcs()); AtimesF->size = BED->randomizer()->num_rand_funcs();// declare  // initialize
	
	
	
	mat_d temp_jacobian_functions; init_mat_d(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_d temp_jacobian_parameters; init_mat_d(temp_jacobian_parameters,0,0);
	mat_d Jv_Patch; init_mat_d(Jv_Patch, 0, 0);
	mat_d AtimesJ; init_mat_d(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_d(funcVals,BED->num_variables); funcVals->size = BED->num_variables;
	change_size_mat_d(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_d(&Jv->entry[ii][jj]);
	
	
	
	// evaluate the SLP to get the system's whatnot.
	evalProg_d(temp_function_values, parVals, parDer, temp_jacobian_functions, temp_jacobian_parameters, current_variable_values, pathVars, BED->SLP);
	
	
	// evaluate the patch
	patch_eval_d(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_d(parVals, 1);
	change_size_vec_d(parDer, 1);
	change_size_mat_d(Jp, BED->num_variables, 1); Jp->rows = BED->num_variables; Jp->cols = 1;
	for (ii=0; ii<BED->num_variables; ii++)
		set_zero_d(&Jp->entry[ii][0]);
	
	
	parVals->size = parDer->size = 1;
	set_d(&parVals->coord[0], pathVars); // s = t
	set_one_d(&parDer->coord[0]);       // ds/dt = 1
	
	
	
	///////////////////////////
	//
	// the original (randomized) functions.
	//
	///////////////////////////////////
	
	BED->randomizer()->randomize(AtimesF,AtimesJ,temp_function_values,temp_jacobian_functions,&current_variable_values->coord[0]);

	
	for (ii=0; ii<AtimesF->size; ii++)  // for each function, after (real orthogonal) randomization
		set_d(&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_d(&Jv->entry[ii][jj],&AtimesJ->entry[ii][jj]);
	
	//Jp is 0 for the equations.
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	///////////////////
	//
	//  the sphere equation.
	//
	//////////////////////////
	
	offset = BED->randomizer()->num_rand_funcs();
	
	mul_d(func_val_sphere, BED->radius, BED->radius);
	neg_d(func_val_sphere, func_val_sphere);
	mul_d(func_val_sphere, func_val_sphere, &current_variable_values->coord[0]);
	mul_d(func_val_sphere, func_val_sphere, &current_variable_values->coord[0]);
	//f_sph = -r^2*h^2
	
	
	
	for (int ii=1; ii<BED->num_natural_vars; ii++) {
		mul_d(temp2, &BED->center->coord[ii-1], &current_variable_values->coord[0]); // temp2 = c_{i-1}*h
		
		sub_d(temp, &current_variable_values->coord[ii], temp2);  // temp = x_i - h*c_{i-1}
		mul_d(temp2, temp, temp);                                 // temp2 = (x_i - h*c_{i-1})^2
		add_d(func_val_sphere, func_val_sphere, temp2);           // f_sph += (x_i - h*c_{i-1})^2
	}
	
	
	
	set_one_d(func_val_start);
	for (mm=0; mm<2; ++mm) {
		dot_product_d(temp, BED->starting_linear[mm], current_variable_values);
		mul_d(func_val_start, func_val_start, temp);
		//f_start *= L_i (x)
	}
	
	
	// combine the function values
	mul_d(temp, one_minus_s, func_val_sphere);
	mul_d(temp2, gamma_s, func_val_start);
	add_d(&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_d(temp2, &BED->center->coord[ii-1], &current_variable_values->coord[0]); // temp2 = c_{i-1}*h
		sub_d(temp, &current_variable_values->coord[ii], temp2) // temp = x_i - c_{i-1}*h
		mul_d(&Jv->entry[offset][ii], BED->two, temp); // Jv = 2*(x_i - c_{i-1}*h)
		mul_d(&Jv->entry[offset][ii], &Jv->entry[offset][ii], one_minus_s); // Jv = (1-t)*2*(x_i - c_{i-1}*h)
																			// multiply these entries by (1-t)
		
		mul_d(temp2, &BED->center->coord[ii-1], temp);  // temp2 = c_{i-1} * ( x_i - c_{i-1} * h )
		add_d(&Jv->entry[offset][0], &Jv->entry[offset][0], temp2); // Jv[0] += c_{i-1} * ( x_i - c_{i-1} * h )
	}
	
	
	
	// the homogenizing var deriv
	mul_d(temp, &current_variable_values->coord[0], BED->radius);
	mul_d(temp, temp, BED->radius);  // temp = r^2 h
	
	add_d(&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_d(&Jv->entry[offset][0], &Jv->entry[offset][0]); // Jv[0] = -Jv[0]
	mul_d(&Jv->entry[offset][0], &Jv->entry[offset][0], BED->two);  // Jv[0] *= 2
	mul_d(&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[0] = -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_d(temp, BED->starting_linear[0], current_variable_values);
		mul_d(temp, temp, &BED->starting_linear[1]->coord[ii]);
		
		dot_product_d(temp2, BED->starting_linear[1], current_variable_values);
		mul_d(temp2, temp2, &BED->starting_linear[0]->coord[ii]);
		
		add_d(temp, temp, temp2);
		mul_d(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_d(&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_d(&Jp->entry[offset][0], func_val_sphere);
	mul_d(temp, BED->gamma, func_val_start);
	add_d(&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_d(&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_d(&Jv->entry[mm+offset][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_d(&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_d(&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_point_to_screen_matlab(BED->center,"center");
		print_comp_matlab(BED->radius,"radius");
		
		printf("gamma = %lf+1i*%lf;\n", BED->gamma->r, BED->gamma->i);
		printf("time = %lf+1i*%lf;\n", pathVars->r, pathVars->i);
		print_point_to_screen_matlab(current_variable_values,"currvars");
		print_point_to_screen_matlab(funcVals,"F");
		print_matrix_to_screen_matlab(Jv,"Jv");
		print_matrix_to_screen_matlab(Jp,"Jp");
		
	}
	
	
	BED->SLP_memory.set_globals_null();
	
	clear_vec_d(patchValues);
	clear_vec_d(temp_function_values);
	clear_vec_d(AtimesF);
	
	
	clear_mat_d(temp_jacobian_functions);
	clear_mat_d(temp_jacobian_parameters);
	clear_mat_d(Jv_Patch);
	clear_mat_d(AtimesJ);
	
#ifdef printpathsphere
	BED->num_steps++;
	vec_d dehommed; init_vec_d(dehommed,BED->num_variables-1); dehommed->size = BED->num_variables-1;
	dehomogenize(&dehommed,current_variable_values);
	fprintf(BED->FOUT,"%.15lf %.15lf ", pathVars->r, pathVars->i);
	for (ii=0; ii<BED->num_variables-1; ++ii) {
		fprintf(BED->FOUT,"%.15lf %.15lf ",dehommed->coord[ii].r,dehommed->coord[ii].i);
	}
	fprintf(BED->FOUT,"\n");
	clear_vec_d(dehommed);
#endif
	
	
	return 0;
}
예제 #9
0
int sphere_dehom(point_d out_d, point_mp out_mp, int *out_prec, point_d in_d, point_mp in_mp, int in_prec, void const *ED_d, void const *ED_mp)
{
	sphere_eval_data_d *BED_d = NULL;
	sphere_eval_data_mp *BED_mp = NULL;
	
	*out_prec = in_prec;
	
	
	
	if (in_prec < 64)
	{ // compute out_d
		sphere_eval_data_d *BED_d = (sphere_eval_data_d *)ED_d;
		
		comp_d denom;
		change_size_vec_d(out_d,in_d->size-1);
		out_d->size = in_d->size-1;
		
		set_d(denom, &in_d->coord[0]);
		
		for (int ii=0; ii<BED_d->num_variables-1; ++ii) {
			set_d(&out_d->coord[ii],&in_d->coord[ii+1]);
			div_d(&out_d->coord[ii],&out_d->coord[ii],denom); //  result[ii] = dehom_me[ii+1]/dehom_me[0].
		}
		
		
		
		//		print_point_to_screen_matlab(in_d,"in");
		//		print_point_to_screen_matlab(out_d,"out");
		
		
	}
	else
	{ // compute out_mp
		sphere_eval_data_mp *BED_mp = (sphere_eval_data_mp *)ED_mp;
		
		setprec_point_mp(out_mp, *out_prec);
		
		comp_mp denom; init_mp(denom);
		change_size_vec_mp(out_mp,in_mp->size-1);
		out_mp->size = in_mp->size-1;
		
		set_mp(denom, &in_mp->coord[0]);
		
		for (int ii=0; ii<BED_mp->num_variables-1; ++ii) {
			set_mp(&out_mp->coord[ii],&in_mp->coord[ii+1]);
			div_mp(&out_mp->coord[ii],&out_mp->coord[ii],denom); //  result[ii] = dehom_me[ii+1]/dehom_me[0].
		}
		
		
		clear_mp(denom);
		
		
		// set prec on out_mp
		
		
		//		print_point_to_screen_matlab(in_mp,"in");
		//		print_point_to_screen_matlab(out_mp,"out");
		
	}
	
	
	BED_d = NULL;
	BED_mp = NULL;
	
	
	
	
	return 0;
}