Example #1
0
void init() {
    for (int i = 0; i < maxn; i++) {
        for (int j = 0; j < maxm; j++) {
            for (int k = 0; k < 8; k++) {
                dp[i][j][k] = 0;
            }
        }
    }
    dp[0][0][0] = 1;
    for (int i = 1; i < 101; i ++) {
        for (int j = 0; j <= 30 *(i-1); j++) {
            for (int k = 0; k < 8; k++) if (dp[i - 1][j][k] > 0){
                for (int x = 0 ; x <= 10; x++) {
                    for (int y = 0; x + y <= 10; y++) {
                        int jj = j + calc_value(k, x, y) + x + y;
                        int state = calc_state(k , x, y);  
                        dp[i][jj][state] = (dp[i][jj][state] + dp[i - 1][j][k]) % MOD;
                    }
                }
            }
        }
    }
    for (int i = 1; i < 101; i ++) {
        for (int j = 0; j <= 30 *i; j++) {
            for (int k = 0; k < 8; k++) if (dp[i][j][k] > 0){
               if ((k % 2) == 1) { // two ball 
    
                    for (int x = 0 ; x <= 10; x++) {
                        for (int y = 0; y <= 10; y++) {
                            if (x < 10 && (x + y) > 10) continue;

                            int add  = j + calc_value(k, x, y);

                            dp[i][add][0] = (dp[i][add][0] + dp[i][j][k]) % MOD; 
                        }
                    }
                } else if ((k / 4) == 1){ // only one ball
                    for (int x = 0 ; x <= 10; x++) {
                        int add  = j + x;
                        dp[i][add][0] = (dp[i][add][0] + dp[i][j][k] ) % MOD; 
                    }
                }
            }
        }
    }
}
Example #2
0
void simple_cokriging_markI(
    const sugarbox_grid_t & grid,
    const cont_property_array_t & input_prop,
    const cont_property_array_t & secondary_data,
    mean_t primary_mean,
    mean_t secondary_mean,
    double secondary_variance,
    double correlation_coef,
    const neighbourhood_param_t & neighbourhood_params,
    const covariance_param_t & primary_cov_params,
    cont_property_array_t & output_prop)
{
    if (input_prop.size() != output_prop.size())
        throw hpgl_exception("simple_cokriging", boost::format("Input data size: %s. Output data size: %s. Must be equal.") % input_prop.size() % output_prop.size());

    print_algo_name("Simple Colocated Cokriging Markov Model I");
    print_params(neighbourhood_params);
    print_params(primary_cov_params);
    print_param("Primary mean", primary_mean);
    print_param("Secondary mean", secondary_mean);
    print_param("Secondary variance", secondary_variance);
    print_param("Correllation coef", correlation_coef);

    cov_model_t cov(primary_cov_params);

    cross_cov_model_mark_i_t<cov_model_t> cross_cov(correlation_coef, secondary_variance, &cov);

    int data_size = input_prop.size();

    neighbour_lookup_t<sugarbox_grid_t, cov_model_t> n_lookup(&grid, &cov, neighbourhood_params);

    progress_reporter_t report(data_size);

    report.start(data_size);

    // for each node
    for (node_index_t i = 0; i < data_size; ++i)
    {
        // 		calc value
        cont_value_t result = -500;
        if (input_prop.is_informed(i))
        {
            result = input_prop[i];
        }
        else
        {
            cont_value_t secondary_value = secondary_data.is_informed(i) ? secondary_data[i] : secondary_mean;
            if (!calc_value(i, input_prop, secondary_value, primary_mean, secondary_mean,
                            secondary_variance, cov, cross_cov, n_lookup, result))
            {
                result = primary_mean + secondary_value - secondary_mean;
            }
        }
        // 		set value at node
        output_prop.set_at(i, result);
        report.next_lap();
    }
}
Example #3
0
    /**
     * Gets source value at certain \p time value.
     *
     * \param [in] t             Time value.
     * \param [in] current_value Reserved for later use.
     */
    real get_value(real t, real current_value = 0.0) const
    {
        /* calculate source value */
        real val = m_ampl * calc_value(t);

        /* if type == thevenin, consider internal resistance */

        /* else if soft source */
        //return current_value + val;

        /* else -> hard source */
        return val;
    }
Example #4
0
/**
 * Returns the value associated with the given terrain (possibly cached).
 * @param[in]  terrain        The terrain whose value is requested.
 * @param[in]  fallback       Consulted if we are missing data.
 * @param[in]  recurse_count  Detects (probable) infinite recursion.
 */
int movetype::terrain_info::data::value(
	const t_translation::t_terrain & terrain,
	const terrain_info * fallback,
	unsigned recurse_count) const
{
	// Check the cache.
	std::pair<cache_t::iterator, bool> cache_it =
		cache_.insert(std::make_pair(terrain, -127)); // Bogus value that should never be seen.
	if ( cache_it.second )
		// The cache did not have an entry for this terrain, so calculate the value.
		cache_it.first->second = calc_value(terrain, fallback, recurse_count);

	return cache_it.first->second;
}
double analysis(char* name) {
    char buffer[100];
    sprintf(buffer, "%s", name);

    FILE *file = fopen(buffer, "r");
    if (fread(header, 1, 44, file));
    size = (header[4]+(header[5]<<8)+(header[6]<<16)+(header[7]<<24))/2;    
    raw = (char*)malloc(2*size);
    if (fread(raw, 2, size, file));
    fclose(file);
    
    data = (double*)fftw_malloc(sizeof(double)*size);    
    for (int i = 0; i < size; i++) {
        data[i] = raw[2*i]+(raw[2*i+1]<<8);
        if (data[i] > 32767) data[i] -= 65536;
    }

    in = (double*)fftw_malloc(sizeof(double)*size);
    out = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*size);

    segment_count = size/segment_len;
    for (int i = 0; i < segment_count; i++) {
       colmax[i] = 0.00001;
       for (int j = min_note; j <= max_note; j++) {
           rowmax[j] = 0.00001;
           value[i][j] = 0;
       }      
    }    
    
    for (int i = 0; i < segment_count; i++) {
        calc_value(i);
    }
    
    free(raw);
    fftw_free(data);
    fftw_free(in);
    fftw_free(out);
    
    if (mode == 1) strong_melody();
    if (mode == 2) high_melody();
    if (mode == 3) tutti_melody();

    track_melody();
    
    for (int i = 0; i < segment_count; i++) {
        //printf("%lf,%i\n", 1.0*i*segment_len/sample_rate, result[i]);
        printf("%i\n", result[i]);
    }
}
Example #6
0
void
fincalc_calc_clicked_cb(GtkButton *button, FinCalcDialog *fcd)
{
    const gchar *text;
    gint i;

    for (i = 0; i < NUM_FIN_CALC_VALUES; i++)
    {
        text = gtk_entry_get_text(GTK_ENTRY(fcd->amounts[i]));
        if ((text != NULL) && (*text != '\0'))
            continue;
        calc_value(fcd, i);
        return;
    }
}
Example #7
0
void colvar::cvc::debug_gradients(cvm::atom_group *group)
{
  // this function should work for any scalar variable:
  // the only difference will be the name of the atom group (here, "group")
  // NOTE: this assumes that groups for this cvc are non-overlapping,
  // since atom coordinates are modified only within the current group

  if (group->b_dummy) return;

  cvm::rotation const rot_0 = group->rot;
  cvm::rotation const rot_inv = group->rot.inverse();

  cvm::real x_0 = x.real_value;
  if ((x.type() == colvarvalue::type_vector) && (x.size() == 1)) x_0 = x[0];

  // cvm::log("gradients     = "+cvm::to_str (gradients)+"\n");

  cvm::atom_group *group_for_fit = group->ref_pos_group ? group->ref_pos_group : group;
  cvm::atom_pos fit_gradient_sum, gradient_sum;

  // print the values of the fit gradients
  if (group->b_rotate || group->b_center) {
    if (group->b_fit_gradients) {
      size_t j;

      // fit_gradients are in the simulation frame: we should print them in the rotated frame
      cvm::log("Fit gradients:\n");
      for (j = 0; j < group_for_fit->fit_gradients.size(); j++) {
        cvm::log((group->ref_pos_group ? std::string("refPosGroup") : group->key) +
                 "[" + cvm::to_str(j) + "] = " +
                 (group->b_rotate ?
                  cvm::to_str(rot_0.rotate(group_for_fit->fit_gradients[j])) :
                  cvm::to_str(group_for_fit->fit_gradients[j])));
      }
    }
  }

  // debug the gradients
  for (size_t ia = 0; ia < group->size(); ia++) {

    // tests are best conducted in the unrotated (simulation) frame
    cvm::rvector const atom_grad = (group->b_rotate ?
                                    rot_inv.rotate((*group)[ia].grad) :
                                    (*group)[ia].grad);
    gradient_sum += atom_grad;

    for (size_t id = 0; id < 3; id++) {
      // (re)read original positions
      group->read_positions();
      // change one coordinate
      (*group)[ia].pos[id] += cvm::debug_gradients_step_size;
      group->calc_required_properties();
      calc_value();
      cvm::real x_1 = x.real_value;
      if ((x.type() == colvarvalue::type_vector) && (x.size() == 1)) x_1 = x[0];
      cvm::log("Atom "+cvm::to_str(ia)+", component "+cvm::to_str(id)+":\n");
      cvm::log("dx(actual) = "+cvm::to_str(x_1 - x_0,
                            21, 14)+"\n");
      cvm::real const dx_pred = (group->fit_gradients.size()) ?
        (cvm::debug_gradients_step_size * (atom_grad[id] + group->fit_gradients[ia][id])) :
        (cvm::debug_gradients_step_size * atom_grad[id]);
      cvm::log("dx(interp) = "+cvm::to_str(dx_pred,
                            21, 14)+"\n");
      cvm::log("|dx(actual) - dx(interp)|/|dx(actual)| = "+
                cvm::to_str(std::fabs(x_1 - x_0 - dx_pred) /
                            std::fabs(x_1 - x_0), 12, 5)+"\n");
    }
  }

  if ((group->b_fit_gradients) && (group->ref_pos_group != NULL)) {
    cvm::atom_group *ref_group = group->ref_pos_group;
    group->read_positions();
    group->calc_required_properties();

    for (size_t ia = 0; ia < ref_group->size(); ia++) {

      // fit gradients are in the unrotated (simulation) frame
      cvm::rvector const atom_grad = ref_group->fit_gradients[ia];
      fit_gradient_sum += atom_grad;

      for (size_t id = 0; id < 3; id++) {
        // (re)read original positions
        group->read_positions();
        ref_group->read_positions();
        // change one coordinate
        (*ref_group)[ia].pos[id] += cvm::debug_gradients_step_size;
        group->calc_required_properties();
        calc_value();

        cvm::real const x_1 = x.real_value;
        cvm::log("refPosGroup atom "+cvm::to_str(ia)+", component "+cvm::to_str (id)+":\n");
        cvm::log("dx(actual) = "+cvm::to_str (x_1 - x_0,
                               21, 14)+"\n");

        cvm::real const dx_pred = cvm::debug_gradients_step_size * atom_grad[id];

        cvm::log("dx(interp) = "+cvm::to_str (dx_pred,
                               21, 14)+"\n");
        cvm::log ("|dx(actual) - dx(interp)|/|dx(actual)| = "+
                  cvm::to_str(std::fabs (x_1 - x_0 - dx_pred) /
                               std::fabs (x_1 - x_0),
                               12, 5)+
                  ".\n");
      }
    }
  }

  cvm::log("Gradient sum: " +  cvm::to_str(gradient_sum) +
        "  Fit gradient sum: " + cvm::to_str(fit_gradient_sum) +
        "  Total " + cvm::to_str(gradient_sum + fit_gradient_sum));

  return;
}
int main()
{
	int i,test_cases;
	scanf("%d",&test_cases);
	for(i=0;i<test_cases;i++)
	{
		int j,b=0,k=0,l=0,ans,res,l1,l2,x=0,l3,y,t1=0,t2=0,count=0;
		char ch1[10000],op,ch='$',d[10000];
		float arr[10000];
		for(j=0;j<10000;j++)
		{
			arr[j] = 0;
			d[j] = '$';
			ch1[j] = '$';
		}
		for(j=0;ch!='\n';j++)
		{
			if(j % 2 == 0)
				scanf("%f",&arr[k++]);
			else
				scanf("%c",&ch1[l++]);
			scanf("%c",&ch);
		}
		l1 = k,l2 = l;
		for(j=0;j<l2;j++)
		{
			if(ch1[j] == '+' || ch1[j] == '-')
			{
				if(d[b-1]=='I'|| d[b-1]==')')
				{
					d[b++] = ch1[j];
					d[b++] = 'I';
				}
				else
				{
					d[b++] = 'I';
					d[b++] = ch1[j];
					d[b++] = 'I';
				}
			}
			else if(ch1[j] == '*' || ch1[j] == '/')
			{
				if(d[b-1] == ')')
				{
					for(l=b;l>=0;l--)
					{
						if(d[l] == '(')
							break;
						d[l] = d[l-1];
					}
					d[l] = '(';
					b++;
					d[b++] = ch1[j];
					d[b++] = 'I';
					d[b++] = ')';
				}
				else
				{
					if(b-1>=0)
						d[b-1] = '(';
					else
						d[b++] = '(';
					d[b++] = 'I';
					d[b++] = ch1[j];
					d[b++] = 'I';
					d[b++] = ')';
				}
			}
		}
		l3 = b;
		int i1=0,i2=0;
		for(j=0;j<l2;j++)
			if(ch1[j] == '+' || ch1[j] == '-')
				count++;
		for(l=0;l<count;l++)
			printf("( ");
		i1=count;
		for(j=0;j<l3;j++)
		{
			if(d[j]=='(')
				i1++;
			else if(d[j] == ')')
				i2++;
			if(d[j]=='+' || d[j] == '-')
			{
				if(t1!=0)
				{
					printf(") ");
					i2++;
				}
				else
					t1++;
			}
			if(d[j]=='I')
				printf("%.0f ",arr[x++]);
			else
				printf("%c ",d[j]);
		}
		if(i1!=i2)
			printf(")\n");
		else
			printf("\n");
		printf("%d\n",calc_value(arr,ch1,l2));
	}
	return 0;
}
Example #9
0
void colvar::cvc::debug_gradients (cvm::atom_group &group)
{
  // this function should work for any scalar variable:
  // the only difference will be the name of the atom group (here, "group")

  if (group.b_dummy) return;

  cvm::rotation const rot_0 = group.rot;
  cvm::rotation const rot_inv = group.rot.inverse();

  cvm::real const x_0 = x.real_value;

  // cvm::log ("gradients     = "+cvm::to_str (gradients)+"\n");

  // it only makes sense to debug the fit gradients
  // when the fitting group is the same as this group
  if (group.b_rotate || group.b_center)
    if (group.b_fit_gradients && (group.ref_pos_group == NULL)) {
      group.calc_fit_gradients();
      if (group.b_rotate) {
        // fit_gradients are in the original frame, we should print them in the rotated frame
        for (size_t j = 0; j < group.fit_gradients.size(); j++) {
          group.fit_gradients[j] = rot_0.rotate (group.fit_gradients[j]);
        }
      }
      cvm::log ("fit_gradients = "+cvm::to_str (group.fit_gradients)+"\n");
      if (group.b_rotate) {
        for (size_t j = 0; j < group.fit_gradients.size(); j++) {
          group.fit_gradients[j] = rot_inv.rotate (group.fit_gradients[j]);
        }
      }
    }

  for (size_t ia = 0; ia < group.size(); ia++) {

    // tests are best conducted in the unrotated (simulation) frame
    cvm::rvector const atom_grad = group.b_rotate ?
      rot_inv.rotate (group[ia].grad) :
      group[ia].grad;

    for (size_t id = 0; id < 3; id++) {
      // (re)read original positions
      group.read_positions();
      // change one coordinate
      group[ia].pos[id] += cvm::debug_gradients_step_size;
      // (re)do the fit (if defined)
      if (group.b_center || group.b_rotate) {
        group.calc_apply_roto_translation();
      }
      calc_value();
      cvm::real const x_1 = x.real_value;
      cvm::log ("Atom "+cvm::to_str (ia)+", component "+cvm::to_str (id)+":\n");
      cvm::log ("dx(actual) = "+cvm::to_str (x_1 - x_0,
                             21, 14)+"\n");
      //cvm::real const dx_pred = (group.fit_gradients.size() && (group.ref_pos_group == NULL)) ?
      cvm::real const dx_pred = (group.fit_gradients.size()) ?
        (cvm::debug_gradients_step_size * (atom_grad[id] + group.fit_gradients[ia][id])) :
        (cvm::debug_gradients_step_size * atom_grad[id]);
      cvm::log ("dx(interp) = "+cvm::to_str (dx_pred,
                             21, 14)+"\n");
      cvm::log ("|dx(actual) - dx(interp)|/|dx(actual)| = "+
                cvm::to_str (std::fabs (x_1 - x_0 - dx_pred) /
                             std::fabs (x_1 - x_0), 12, 5)+"\n");
    }
  }

/*
 * The code below is WIP
 */
//   if (group.ref_pos_group != NULL) {
//     cvm::atom_group &ref = *group.ref_pos_group;
//     group.calc_fit_gradients();
//
//     for (size_t ia = 0; ia < ref.size(); ia++) {
//
//       for (size_t id = 0; id < 3; id++) {
//         // (re)read original positions
//         group.read_positions();
//         ref.read_positions();
//         // change one coordinate
//         ref[ia].pos[id] += cvm::debug_gradients_step_size;
//         group.calc_apply_roto_translation();
//         calc_value();
//         cvm::real const x_1 = x.real_value;
//         cvm::log ("refPosGroup atom "+cvm::to_str (ia)+", component "+cvm::to_str (id)+":\n");
//         cvm::log ("dx(actual) = "+cvm::to_str (x_1 - x_0,
//                                21, 14)+"\n");
//         //cvm::real const dx_pred = (group.fit_gradients.size() && (group.ref_pos_group == NULL)) ?
//         // cvm::real const dx_pred = (group.fit_gradients.size()) ?
//         //  (cvm::debug_gradients_step_size * (atom_grad[id] + group.fit_gradients[ia][id])) :
//         //  (cvm::debug_gradients_step_size * atom_grad[id]);
//         cvm::real const dx_pred = cvm::debug_gradients_step_size * ref.fit_gradients[ia][id];
//         cvm::log ("dx(interp) = "+cvm::to_str (dx_pred,
//                                21, 14)+"\n");
//         cvm::log ("|dx(actual) - dx(interp)|/|dx(actual)| = "+
//                   cvm::to_str (std::fabs (x_1 - x_0 - dx_pred) /
//                                std::fabs (x_1 - x_0),
//                                12, 5)+
//                   ".\n");
//       }
//     }
//   }

  return;
}