void InfiniteDimensionalMCMCSampler::_write_state()
{
  this->_append_scalar_dataset(this->_acc_dset, this->acc_prob());
  this->_append_scalar_dataset(this->_avg_acc_dset, this->avg_acc_prob());
  this->_append_scalar_dataset(this->_neg_log_llhd_dset, this->_llhd_val);
  this->_append_scalar_dataset(this->_L2_norm_samples_dset, this->current_physical_state->L2_norm());
  this->_append_scalar_dataset(this->_L2_norm_mean_dset, this->current_physical_mean->L2_norm());
  this->_append_scalar_dataset(this->_L2_norm_var_dset, this->current_physical_var->L2_norm());

  // Now to write the fields.  It appears to be a pain in the arse to write a
  // method to spit this out to HDF5 format.  Also, this won't scale to
  // non-uniform finite element meshes.  Therefore, I'm going to spit out an
  // ExodusII file for each sample, average and variance.  Got disk space?
  std::stringstream basename;
  basename << this->m_ov->m_dataOutputDirName;
  basename << (this->m_env).subIdString();
  basename << "/";  // Sigh

  std::ostringstream curr_iter;
  curr_iter << this->iteration();

  std::string sample_name(basename.str() + "sample.e-s.");
  sample_name += curr_iter.str();
  this->current_physical_state->save_function(sample_name, this->iteration());

  std::string mean_name(basename.str() + "mean.e-s.");
  mean_name += curr_iter.str();
  this->current_physical_mean->save_function(mean_name, this->iteration());

  std::string var_name(basename.str() + "var.e-s.");
  var_name += curr_iter.str();
  this->current_physical_var->save_function(var_name, this->iteration());
}
Пример #2
0
// This is where most of the strict/lazy distinction is.
static value_t *e_fncall(env_t *env, expr_t *expr)
{
  eli_closure_t c;
  binding_t *fn;

  // Call-by-value (strict function calls): evaluate each argument to
  // a value in the given environment.
  c.env = env;
  c.list = list_empty();
  list_iterate(fncall_args(expr), e_expr_list_i, &c);
  list_reverse(c.list);

  switch (fncall_fn(expr)->type) {

  case p_var:
    // The function is literally the name of a function, and is
    // defined in the global environment.

    fn = (binding_t *)env_lookup(global_env, var_name(fncall_fn(expr)));
    assert(fn != NULL);

    // We must have exactly as many arguments as parameters.
    assert(list_length(c.list) == list_length(fn->params));

    // Bind the function's parameters to the given arguments in a new
    // scope derived from the global scope.
    env = global_env;
    env_new_scope(&env);
    list_zip_with(fn->params,
                  c.list,
                  e_bind_params_i, env);

    // Evaluate the function's body in the new environment.
    return e_expr(env, fn->body);

  case p_datacons:
    {
      value_t *result;

      result = alloc_value(v_datacons);
      datacons_tag(result) = datacons_tag(fncall_fn(expr));
      datacons_params(result) = c.list;

      // FIXME we'd like to assert that we got the right number of
      // arguments, but we don't know how many the data constructor
      // wanted.

      return result;
    }

    default:
      fprintf(stdout, "e_fncall: expression:\n");
      pp_expr(stdout, fncall_fn(expr), 2);
      fprintf(stdout, "\non line %d is not a function-variable or a data constructor.\n", fn->line_num);
      error("");
      return NULL;
  }
}
Пример #3
0
void ada_read_sys(PolySys& sys)
{
	int fail;
	std::cout << "testing reading and writing a system" << std::endl;
	//fail = syscon_read_system();
	std::cout << "the system is .." << std::endl;
	fail = syscon_write_system();

	// Get variable names
	int s_dim = 80;
	char *s = (char*) calloc(80,sizeof(char));
	fail = syscon_string_of_symbols(&s_dim, s);
	string* x_names;
	var_name(s, s_dim, x_names);

	int dim = 4;
	int i = 1;
	double c[2];
	int d[dim];

	int n_eq = 0;
	fail = syscon_number_of_polynomials(&n_eq);

	sys.n_eq = n_eq;
	sys.dim  = dim;
	sys.eq_space = new PolyEq[n_eq];
	sys.pos_var = x_names;

	PolyEq* tmp_eq = sys.eq_space;

	for(int i=1; i<n_eq+1; i++){
		int nt;
		fail = syscon_number_of_terms(i,&nt);
		//std::cout << "  #terms in polynomial " << i << " : " << nt << std::endl;
		tmp_eq->n_mon = nt;
		tmp_eq->dim = dim;
		for(int j=1; j<=nt; j++)
		{
			fail = syscon_retrieve_term(i,j,dim,d,c);
			//std::cout << c[0] << " " << c[1] << std::endl;
			//for (int k=0; k<n; k++) std::cout << " " << d[k];
			//std::cout << std::endl;
			bool constant_term = true;
			for (int k=0; k<dim; k++){
				if(d[k]!=0){
					constant_term = false;
				}
			}

			if(constant_term==true){
				tmp_eq->n_mon--;
				tmp_eq->constant += CT(c[0],c[1]);
				//std::cout << "constant " << c[0] \
				          << " " << c[1] << std::endl;
			}
			else{
Пример #4
0
    /**
     * Generate code to validate the specified variable declaration
     * using the specified indentation level and stream.
     * Checks any defined bounds or constraints on specialized types.
     * NOTE:  bounded / specialized types are mutually exclusive
     *
     * @param[in] decl variable declaration
     * @param[in] indent indentation level
     * @param[in,out] o stream for generating
     */
    void generate_validate_var_decl(const block_var_decl decl,
                                    int indent, std::ostream& o) {
      std::string var_name(decl.name());
      std::vector<expression> ar_lens(decl.type().array_lens());

      block_var_type btype = decl.type().innermost_type();
      if (btype.has_def_bounds()) {
        range bounds = btype.bounds();
        write_begin_array_dims_loop(decl, true, indent, o);
        if (bounds.has_low()) {
          generate_indent(indent + ar_lens.size(), o);
          o << "check_greater_or_equal(function__, ";
          o << "\"" << var_name;
          write_var_idx_array_dims(ar_lens.size(), o);
          o << "\", " << var_name;
          write_var_idx_array_dims(ar_lens.size(), o);
          o << ", ";
          generate_expression(bounds.low_.expr_, NOT_USER_FACING, o);
          o << ");" << EOL;
        }
        if (bounds.has_high()) {
          generate_indent(indent + ar_lens.size(), o);
          o << "check_less_or_equal(function__, ";
          o << "\"" << var_name;
          write_var_idx_array_dims(ar_lens.size(), o);
          o << "\", " << var_name;
          write_var_idx_array_dims(ar_lens.size(), o);
          o << ", ";
          generate_expression(bounds.high_.expr_, NOT_USER_FACING, o);
          o << ");" << EOL;
        }
        write_end_loop(ar_lens.size(), indent, o);
      } else if (btype.is_specialized()) {
        write_begin_array_dims_loop(decl, true, indent, o);
        generate_indent(indent + ar_lens.size(), o);
        o << "stan::math::check_";
        // kludge - inconsistent naming specialized cholesky_factor types
        if (btype.name() == "cholesky_factor_cov")
          o << "cholesky_factor";
        else
          o << btype.name();
        o << "(function__, \"" << var_name;
        write_var_idx_array_dims(ar_lens.size(), o);
        o << "\", " << var_name;
        write_var_idx_array_dims(ar_lens.size(), o);
        o << ");" << EOL;
        write_end_loop(ar_lens.size(), indent, o);
      }
    }
Пример #5
0
 /**
  * Generate code to to the specified stream to instantiate
  * a member variable declared in data block by calling the appropriate constructor.
  * Doesn't check variable dimensions - should have already been done 
  * as part of checks on var_context.
  *
  * @param[in] var_decl block variable declaration
  * @param[in] indent indentation level
  * @param[in,out] o stream for generating
  */
 void generate_data_var_ctor(const block_var_decl& var_decl,
                             int indent, std::ostream& o) {
   std::string var_name(var_decl.name());
   block_var_type btype = var_decl.type().innermost_type();
   generate_indent(indent, o);
   o << var_name << " = ";
   if (var_decl.bare_type().is_int_type()) {
     o << "int(0)";
   } else if (var_decl.bare_type().is_double_type()) {
     o << "double(0)";
   } else {
     generate_var_constructor(var_decl, "double", o);
   }
   o << ";" << EOL;
 }
Пример #6
0
static int do_printenv(struct command *cmdtp, int argc, char *argv[])
{
	struct variable_d *var;
	struct env_context *c, *current_c;

	if (argc == 2) {
		const char *val = getenv(argv[1]);
		if (val) {
			printf("%s=%s\n", argv[1], val);
			return 0;
		}
		printf("## Error: \"%s\" not defined\n", argv[1]);
		return 1;
	}

	current_c = get_current_context();
	var = current_c->local->next;
	printf("locals:\n");
	while (var) {
		printf("%s=%s\n", var_name(var), var_val(var));
		var = var->next;
	}

	printf("globals:\n");
	c = get_current_context();
	while(c) {
		var = c->global->next;
		while (var) {
			printf("%s=%s\n", var_name(var), var_val(var));
			var = var->next;
		}
		c = c->parent;
	}

	return 0;
}
Пример #7
0
bool MsdpNetwork::process_var(cursor& c)
{
     const tbyte* p = c.p;
     if (p == c.e || *p != MSDP_VAR)
         return false;
     const tbyte* b = p+1;
     while (b != c.e && *b != MSDP_VAL)
     {
         if (*b < ' ') return false;
         b++;
     }
     if (b == c.e)
         return false;
     std::string var_name((const char*)(p+1), b-p-1);
     lua_pushstring(L, var_name.c_str());
     c.p = b;
     return true;
}
Пример #8
0
void add_row(GtkToolButton* toolbutton, gpointer user_data)
{
    /* Context data */
    GtkListStore* restrictions =
        GTK_LIST_STORE(gtk_tree_view_get_model(restrictions_view));
    int vars = gtk_spin_button_get_value_as_int(variables);

    GValue initi = G_VALUE_INIT;
    g_value_init(&initi, G_TYPE_INT);

    GValue inits = G_VALUE_INIT;
    g_value_init(&inits, G_TYPE_STRING);

    GtkTreeIter iter;
    gtk_list_store_append(restrictions, &iter);

    int size = vars + 2;
    for(int i = 0; i < vars; i++) {

        g_value_set_int(&initi, 1);
        gtk_list_store_set_value(restrictions, &iter, i, &initi);

        char* text = var_name(1, i, false);
        g_value_set_string(&inits, text);
        gtk_list_store_set_value(restrictions, &iter, size + i, &inits);
        free(text);

        g_value_set_string(&inits, PLUS);
        gtk_list_store_set_value(restrictions, &iter, 2 * size + i, &inits);
    }

    /* Set type */
    g_value_set_int(&initi, GE);
    gtk_list_store_set_value(restrictions, &iter, size - 2, &initi);

    g_value_set_string(&inits, GES);
    gtk_list_store_set_value(restrictions, &iter, 2 * size - 2, &inits);

    g_value_set_string(&inits, "");
    gtk_list_store_set_value(restrictions, &iter, 3 * size - 2, &inits);

    /* Set equality */
    g_value_set_int(&initi, 0);
    gtk_list_store_set_value(restrictions, &iter, size - 1, &initi);

    char* text = num_name(0, false);
    g_value_set_string(&inits, text);
    gtk_list_store_set_value(restrictions, &iter, 2 * size - 1, &inits);
    free(text);

    g_value_set_string(&inits, "");
    gtk_list_store_set_value(restrictions, &iter, 3 * size - 1, &inits);

    /* Select new row */
    GtkTreePath* model_path = gtk_tree_model_get_path(
                                GTK_TREE_MODEL(restrictions), &iter);
    gtk_tree_view_set_cursor(restrictions_view, model_path,
                             gtk_tree_view_get_column(restrictions_view, 0),
                             true);
    gtk_tree_path_free(model_path);
    return;
}
Пример #9
0
bool change_function(int vars)
{
    /* Clear model */
    clear_liststore(function_view);

    /* Create the dynamic types array */
    GType* types = (GType*) malloc(3 * vars * sizeof(GType));
    if(types == NULL) {
        return false;
    }

    /* Set type in the dynamic types array */
    for(int i = 0; i < vars; i++) {
        types[i] = G_TYPE_INT;               /* Coeffs */
        types[vars + i] = G_TYPE_STRING;     /* Text   */
        types[2 * vars + i] = G_TYPE_STRING; /* Signs  */
    }

    /* Create and fill the new model */
    GtkListStore* function = gtk_list_store_newv(3 * vars, types);
    GtkTreeIter iter;

    GValue initi = G_VALUE_INIT;
    g_value_init(&initi, G_TYPE_INT);

    GValue inits = G_VALUE_INIT;
    g_value_init(&inits, G_TYPE_STRING);

    gtk_list_store_append(function, &iter);
    for(int i = 0; i < vars; i++) {

        g_value_set_int(&initi, 1);
        gtk_list_store_set_value(function, &iter, i, &initi);

        char* text = var_name(1, i, false);
        g_value_set_string(&inits, text);
        gtk_list_store_set_value(function, &iter, vars + i, &inits);
        free(text);

        g_value_set_string(&inits, PLUS);
        gtk_list_store_set_value(function, &iter, 2 * vars + i, &inits);
    }

    /* Clear the previous columns */
    for(int i = gtk_tree_view_get_n_columns(function_view) - 1; i >= 0; i--) {
        gtk_tree_view_remove_column(
                                function_view,
                                gtk_tree_view_get_column(function_view, i)
                            );
    }

    /* Create the new columns */
    for(int i = 0; i < vars; i++) {

        /* Create sign column */
        if(i > 0) {
            GtkCellRenderer* sign = gtk_cell_renderer_text_new();
            GtkTreeViewColumn* sign_c =
                gtk_tree_view_column_new_with_attributes(
                                            "", sign,  /* Title, renderer */
                                            "markup", 2 * vars + i,
                                            NULL);
            gtk_tree_view_append_column(function_view, sign_c);
        }

        /* Create text column */
        /* Create and configure cell */
        GtkCellRenderer* cell = gtk_cell_renderer_spin_new();

        gtk_cell_renderer_set_alignment(cell, 1.0, 0.5);
        g_object_set(cell, "editable", true, NULL);

        /* Configure callbacks */
        g_signal_connect(G_OBJECT(cell),
                    "editing-started", G_CALLBACK(function_edit_started_cb),
                    GINT_TO_POINTER(i));
        function_edit_started_cb(cell, NULL, NULL, GINT_TO_POINTER(i));

        g_signal_connect(G_OBJECT(cell),
                         "edited", G_CALLBACK(function_edited_cb),
                         GINT_TO_POINTER(i));

        /* Configure column */
        GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes(
                                        "", cell,  /* Title, renderer */
                                        "markup", vars + i,
                                        NULL);
        gtk_tree_view_column_set_min_width(column, 100);
        gtk_tree_view_append_column(function_view, column);
    }
    gtk_tree_view_append_column(function_view, gtk_tree_view_column_new());

    /* Set the new model */
    gtk_tree_view_set_model(function_view, GTK_TREE_MODEL(function));

    /* Free resources */
    g_object_unref(G_OBJECT(function));
    free(types);

    return true;
}
Пример #10
0
void writeback(GtkTreeModel* model, gchar* path, int vars, bool is_var,
                    gchar* new_text, gpointer user_data)
{
    int column = GPOINTER_TO_INT(user_data);
    DEBUG("%s at (%i, %i)\n", new_text, atoi(path), column);

    /* Get the coefficient */
    int coeff = 0;
    if(!is_empty_string(new_text)) {
        char* end;
        coeff = (int) strtol(new_text, &end, 10);
        if(*end != '\0') { /* Conversion wasn't successful */
            DEBUG("Unable to parse %s\n", new_text);
            return;
        }
    }

    /* Get reference to model */
    GtkTreeIter iter;
    gtk_tree_model_get_iter_from_string(model, &iter, path);

    /* Set the model */
    GValue gvali = G_VALUE_INIT;
    g_value_init(&gvali, G_TYPE_INT);

    GValue gvals = G_VALUE_INIT;
    g_value_init(&gvals, G_TYPE_STRING);

    /* Coeff */
    g_value_set_int(&gvali, coeff);
    gtk_list_store_set_value(
        GTK_LIST_STORE(model), &iter, column, &gvali);
    g_value_unset(&gvali);

    /* Text */
    g_value_set_string(&gvals, "");
    if(!is_var) {
        char* text = num_name(coeff, true);
        g_value_set_string(&gvals, text);
        free(text);
    } else if(coeff != 0) {
        char* text = var_name(coeff, column, column == 0);
        g_value_set_string(&gvals, text);
        free(text);
    }
    gtk_list_store_set_value(
        GTK_LIST_STORE(model), &iter, vars + column, &gvals);

    /* Sign */
    g_value_set_string(&gvals, "");
    if(is_var) {
        if((column != 0)  && (coeff != 0)) {
            if(coeff > 0) {
                g_value_set_string(&gvals, PLUS);
            } else {
                g_value_set_string(&gvals, MINUS);
            }
        }
    }
    gtk_list_store_set_value(
        GTK_LIST_STORE(model), &iter, 2 * vars + column, &gvals);
    g_value_unset(&gvals);
}
Пример #11
0
void option_db_t::parse_token( const std::string& token )
{
  if ( token == "-" )
  {
    parse_file( stdin );
    return;
  }

  std::string::size_type cut_pt = token.find( '=' );

  if ( cut_pt == token.npos )
  {
    io::cfile file = open_file( auto_path, token );
    if ( ! file )
    {
      std::stringstream s;
      s << "Unexpected parameter '" << token << "'. Expected format: name=value";
      throw std::invalid_argument( s.str() );
    }
    parse_file( file );
    return;
  }

  std::string name( token, 0, cut_pt ), value( token, cut_pt + 1, token.npos );

  std::string::size_type start = 0;
  while ( ( start = value.find( "$(", start ) ) != std::string::npos )
  {
    std::string::size_type end = value.find( ')', start );
    if ( end == std::string::npos )
    {
      std::stringstream s;
      s << "Variable syntax error: '" << token << "'";
      throw std::invalid_argument( s.str() );
    }

    value.replace( start, ( end - start ) + 1,
                   var_map[ value.substr( start + 2, ( end - start ) - 2 ) ] );
  }

  if ( name.size() >= 1 && name[ 0 ] == '$' )
  {
    if ( name.size() < 3 || name[ 1 ] != '(' || name[ name.size() - 1 ] != ')' )
    {
      std::stringstream s;
      s << "Variable syntax error: '" << token << "'";
      throw std::invalid_argument( s.str() );
    }
    std::string var_name( name, 2, name.size() - 3 );
    var_map[ var_name ] = value;
  }
  else if ( name == "input" )
  {
    io::cfile file( open_file( auto_path, value ) );
    if ( ! file )
    {
      std::stringstream s;
      s << "Unable to open input parameter file '" << value << "'";
      throw std::invalid_argument( s.str() );
    }
    parse_file( file );
  }
  else
  {
    add( "global", name, value );
  }
}
Пример #12
0
static value_t *e_expr(env_t *env, expr_t *expr)
{
  value_t *result;

  switch (expr->type) {
  default: // This is to handle invalid tags.
  case p_unused:
    if (*(int *)NULL) {
      printf("should crash.\n");
    }
    return NULL;

  case p_and:
    {
      value_t *l = e_expr(env, binary_left(expr));

      if (bool_val(l)) {
        result = e_expr(env, binary_right(expr));
      } else {
        result = l;
      }
    }
    break;
  case p_or:
    {
      value_t *l = e_expr(env, binary_left(expr));

      if (bool_val(l)) {
        result = l;
      } else {
        result = e_expr(env, binary_right(expr));
      }
    }
    break;

  case p_add:
  case p_div:
  case p_ge:
  case p_gt:
  case p_le:
  case p_lt:
  case p_mod:
  case p_mul:
  case p_sub:
    result = e_binary_op(env, expr);
    break;

  case p_bconst:
    result = alloc_value(v_bool);
    bool_val(result) = bool_val(expr);
    break;
  case p_cconst:
    result = alloc_value(v_char);
    char_val(result) = char_val(expr);
    break;
  case p_datacons:
    result = e_datacons(env, expr);
    break;
  case p_eqop:
    result = e_equals(env, binary_left(expr), binary_right(expr));
    break;
  case p_fncall:
    result = e_fncall(env, fncall_fn(expr), fncall_args(expr));
    break;
  case p_nconst:
    result = alloc_value(v_num);
    num_val(result) = num_val(expr);
    break;
  case p_ite:
    result = e_ite(env, expr);
    break;
  case p_let:
    result = e_let(env, expr);
    break;
  case p_listcons:
    result = e_listcons(env, expr);
    break;
  case p_listlit:
    result = e_listlit(env, expr);
    break;
  case p_listempty:
    result = e_listempty();
    break;
  case p_match:
    result = e_match(env, expr);
    break;
  case p_ne:
    result = e_equals(env, binary_left(expr), binary_right(expr));
    bool_val(result) = !bool_val(result);
    break;
  case p_negate:
    result = e_expr(env, unary_expr(expr));
    bool_val(result) = !bool_val(result);
    break;
  case p_tuple:
    result = e_tuple(env, expr);
    break;
  case p_var:
    result = env_lookup(env, var_name(expr));
    if (result == NULL) {
      error("e_expr: variable '%s' not in scope on line %d.\n", var_name(expr), expr->line_num);
    }
    result = thunk_force(result);
    break;
  }

  return result;
}
Пример #13
0
// Mapped over the clauses in a "match" expression.
static int e_match_pat_i(void *data, void *info)
{
  pm_closure_t *pmc = (pm_closure_t *)info;
  clause_t *c = (clause_t *)data;

  // The style of pattern matching depends on the type of the pattern:
  //   - constants match literally
  //   - variables match anything (and extend the environment)
  //   - tuples always match (and extend the environment)
  //   - data constructors are more complex.
  ///

  switch (c->pattern->type) {
    // Constants: match literally, no binding.
  case p_bconst:
    if (bool_val(c->pattern) == bool_val(pmc->val)) {
      pmc->match_body = c->body;
    }
    break;
  case p_cconst:
    if (char_val(c->pattern) == char_val(pmc->val)) {
      pmc->match_body = c->body;
    }
    break;
  case p_nconst:
    if (num_val(c->pattern) == num_val(pmc->val)) {
      pmc->match_body = c->body;
    }
    break;
  case p_listempty:
    if (pmc->val->type == v_datacons && strcmp(datacons_tag(pmc->val), listEmptyTag) == 0) {
      pmc->match_body = c->body;
    }
    break;

  case p_var:
    // Matches anything. Bind it.
    env_add_binding(pmc->env, var_name(c->pattern), pmc->val);
    pmc->match_body = c->body;
    break;

  case p_listcons:
    // Check the list contains at least one element, then bind variables.
    if (pmc->val->type == v_datacons && strcmp(datacons_tag(pmc->val), listConsTag) == 0) {
      value_t *head;
      value_t *tail;

      head = list_nth(datacons_params(pmc->val), 0);
      tail = list_nth(datacons_params(pmc->val), 1);

      env_add_binding(pmc->env, listcons_hvar(c->pattern), head);
      env_add_binding(pmc->env, listcons_tvar(c->pattern), tail);

      pmc->match_body = c->body;
    }
    break;

  case p_datacons:
    // Check the tag matches, then bind the arguments (if any).
    if (strcmp(datacons_tag(c->pattern), datacons_tag(pmc->val)) == 0) {
      list_zip_with(datacons_params(c->pattern),
                    datacons_params(pmc->val),
                    e_bind_params_i, pmc->env);

      pmc->match_body = c->body;
    }
    break;

  case p_tuple:
    // Always matches (assuming the program type checks). Bind the variables.
    list_zip_with(tuple_val(c->pattern),
                  tuple_val(pmc->val),
                  e_bind_params_i, pmc->env);
    pmc->match_body = c->body;
    break;

  default:
    error("INTERNAL pattern match: invalid pattern.\n");
  }

  return pmc->match_body == NULL;
}
Пример #14
0
int fs_optimise_triple_pattern(fs_query_state *qs, fs_query *q, int block, rasqal_triple *patt[], int length, int start)
{
    if (length - start < 2 || q->opt_level < 1) {
        return 1;
    }

    rasqal_triple **pbuf = malloc(length * sizeof(rasqal_triple *));
    memcpy(pbuf, patt, sizeof(rasqal_triple *) * length);
    memset(patt, 0, length * sizeof(rasqal_triple *));
    int append_pos = start;
    for (int i=0; i<start; i++) {
        pbuf[i] = patt[i];
    }

    /* roughly sort into order:
     *   const subject and predicate
     *   const predicate and object
     *   const subject
     *   const object
     *   const graph
     *   const predicate
     *   all variable
     */

#if 0
    /* this code complicates things greatly, so I've removed it for now - swh
       should maybe be reexamined if/when we get histograms back */
    for (int i=start; i<length; i++) {
        if (!pbuf[i]) {
            continue;
        }
        if (fs_bind_freq(qs, q, block, pbuf[i]) == 1) {
            patt[append_pos++] = pbuf[i];
            pbuf[i] = NULL;
        }
    }
#endif
    for (int i=start; i<length; i++) {
        if (!pbuf[i]) {
            continue;
        }
        if (fs_opt_is_const(q->bb[block], pbuf[i]->subject) && fs_opt_is_const(q->bb[block], pbuf[i]->predicate) && fs_opt_is_bound(q->bb[block], pbuf[i]->object)) {
            patt[append_pos++] = pbuf[i];
            pbuf[i] = NULL;
        }
    }

    /* triples like :s :p ?o, where :p != rdf:type */
    for (int i=0; i<length; i++) {
        if (!pbuf[i]) {
            continue;
        }

        if (fs_opt_is_const(q->bb[block], pbuf[i]->subject) && fs_opt_is_const(q->bb[block], pbuf[i]->predicate) && fs_opt_is_bound(q->bb[block], pbuf[i]->object) && !fs_opt_literal_is_rdf_type(pbuf[i]->predicate)) {
            patt[append_pos++] = pbuf[i];
            pbuf[i] = NULL;
        }
    }

    /* triples like :s :p _, where :p != rdf:type */
    for (int i=0; i<length; i++) {
        if (!pbuf[i]) {
            continue;
        }

        if (fs_opt_is_const(q->bb[block], pbuf[i]->subject) && fs_opt_is_const(q->bb[block], pbuf[i]->predicate) && !fs_opt_literal_is_rdf_type(pbuf[i]->predicate)) {
            patt[append_pos++] = pbuf[i];
            pbuf[i] = NULL;
        }
    }

    /* triples like ?s :p :o, where :p != rdf:type */
    for (int i=0; i<length; i++) {
        if (!pbuf[i]) {
            continue;
        }

        if (fs_opt_is_bound(q->bb[block], pbuf[i]->subject) && fs_opt_is_const(q->bb[block], pbuf[i]->predicate) && fs_opt_is_const(q->bb[block], pbuf[i]->object) && !fs_opt_literal_is_rdf_type(pbuf[i]->predicate)) {
            patt[append_pos++] = pbuf[i];
            pbuf[i] = NULL;
        }
    }

    /* triples like ?s rdf:type :o */
    for (int i=0; i<length; i++) {
        if (!pbuf[i]) {
            continue;
        }
        if (fs_opt_is_bound(q->bb[block], pbuf[i]->subject) && fs_opt_is_const(q->bb[block], pbuf[i]->predicate) && fs_opt_is_const(q->bb[block], pbuf[i]->object)) {
            patt[append_pos++] = pbuf[i];
            pbuf[i] = NULL;
        }
    }
    for (int i=0; i<length; i++) {
        if (!pbuf[i]) {
            continue;
        }
        if (fs_opt_is_const(q->bb[block], pbuf[i]->subject) && fs_opt_is_bound(q->bb[block], pbuf[i]->object)) {
            patt[append_pos++] = pbuf[i];
            pbuf[i] = NULL;
        }
    }
    for (int i=0; i<length; i++) {
        if (!pbuf[i]) {
            continue;
        }
        if (fs_opt_is_const(q->bb[block], pbuf[i]->object) && fs_opt_is_bound(q->bb[block], pbuf[i]->subject)) {
            patt[append_pos++] = pbuf[i];
            pbuf[i] = NULL;
        }
    }
    for (int i=0; i<length; i++) {
        if (!pbuf[i]) {
            continue;
        }
        if (fs_opt_is_const(q->bb[block], pbuf[i]->predicate) && (fs_opt_is_bound(q->bb[block], pbuf[i]->subject) || fs_opt_is_bound(q->bb[block], pbuf[i]->object))) {
            patt[append_pos++] = pbuf[i];
            pbuf[i] = NULL;
        }
    }
    for (int i=0; i<length; i++) {
        if (!pbuf[i]) {
            continue;
        }
        if (fs_opt_is_const(q->bb[block], pbuf[i]->origin)) {
            patt[append_pos++] = pbuf[i];
            pbuf[i] = NULL;
        }
    }
    for (int i=0; i<length; i++) {
        if (!pbuf[i]) {
            continue;
        }
        patt[append_pos++] = pbuf[i];
        pbuf[i] = NULL;
    }
    free(pbuf);

    if (append_pos != length) {
        fs_error(LOG_CRIT, "Optimser mismatch error");
    }

#ifdef DEBUG_OPTIMISER
    printf("optimiser choices look like:\n");
    for (int i=start; i<length; i++) {
        printf("%4d: ", i);
        rasqal_triple_print(patt[i], stdout);
        printf("\n");
    }
#endif

    /* If the next two or more pattern's subjects are both variables, we might be able
     * to multi reverse bind them */
    if (var_name(patt[start]->subject) && var_name(patt[start+1]->subject) &&
            !var_name(patt[start]->predicate) &&
            !var_name(patt[start]->object) &&
            fs_opt_num_vals(q->bb[block], patt[start]->predicate) == 1 &&
            fs_opt_num_vals(q->bb[block], patt[start]->origin) == 0 &&
            fs_opt_num_vals(q->bb[block], patt[start+1]->origin) == 0) {
        char *svname = var_name(patt[start]->subject);
        int count = 1;
        while (start+count < length &&
                !fs_opt_is_const(q->bb[block], patt[start+count]->subject) &&
                !strcmp(svname, var_name(patt[start+count]->subject)) &&
                !var_name(patt[start+count]->object) &&
                !var_name(patt[start+count]->predicate)) {
            count++;
        }

        /* if we found a reverse bind pair then we may as well use that, rather
         * than pressing on and using the freq data to pick an order, the
         * backend has more complete information */
        if (count > 1) return count;
    }

    if (length - start > 1) {
        int freq_a = fs_bind_freq(qs, q, block, patt[start]);
        int freq_b = fs_bind_freq(qs, q, block, patt[start+1]);
        /* the 2nd is cheaper than the 1st, then swap them */
        if (freq_b < freq_a) {
            rasqal_triple *tmp = patt[start];
            patt[start] = patt[start+1];
            patt[start+1] = tmp;
        }
    }

    return 1;
}
Пример #15
0
void ada_read_sys ( int verbose, PolySys& sys )
{
   int fail,nbsym;

   fail = syscon_number_of_symbols(&nbsym);

   if(verbose > 0)
   {
      std::cout << "the system is .." << std::endl;
      fail = syscon_write_dobldobl_system();
      std::cout << "the number of symbols : " << nbsym << std::endl;
   }
   int s_dim = 80*nbsym;
   char *s = (char*) calloc(s_dim,sizeof(char));
   fail = syscon_string_of_symbols(&s_dim, s);

   string* x_names;
   int dim = 0;
   var_name(s, s_dim, x_names, dim);
   int i = 1;
   if(verbose > 0) std::cout << "dim = " << dim << std::endl;

   double c[4]; /* two consecutive double doubles are real and imag parts */
   int d[dim];

   int n_eq = 0;
   fail = syscon_number_of_dobldobl_polynomials(&n_eq);

   sys.n_eq = n_eq;
   sys.dim  = dim;
   sys.eq_space = new PolyEq[n_eq];
   sys.pos_var = x_names;

   PolyEq* tmp_eq = sys.eq_space;

   for(int i=1; i<n_eq+1; i++)
   {
      int nt;
      fail = syscon_number_of_dobldobl_terms(i,&nt);
      if(verbose > 0)
         std::cout << " #terms in polynomial " << i << " : " << nt << std::endl;
      tmp_eq->n_mon = nt;
      tmp_eq->dim = dim;
      for(int j=1; j<=nt; j++)
      {
         fail = syscon_retrieve_dobldobl_term(i,j,dim,d,c);
         if(verbose > 0)
         {
            std::cout << c[0] << " " << c[2] << std::endl;
            for (int k=0; k<dim; k++) std::cout << " " << d[k];
            std::cout << std::endl;
         }
         bool constant_term = true;
         for(int k=0; k<dim; k++)
            if(d[k]!=0) constant_term = false;

         if(constant_term==true)
         {
            tmp_eq->n_mon--;
            tmp_eq->constant += CT(c[0],c[2]);
         }
         else
         {
            T1 cffs[2];
            T1 realpcff;
            T1 imagpcff;
            realpcff.x[0] = c[0];
            realpcff.x[1] = c[1];
            imagpcff.x[0] = c[2];
            imagpcff.x[1] = c[3];
            cffs[0] = realpcff;
            cffs[1] = imagpcff;
            PolyMon* a = new PolyMon(dim,d,cffs);
            tmp_eq->mon.push_back(a);
         }
      }
      if(verbose > 0) tmp_eq->print(x_names);
      sys.eq.push_back(tmp_eq);
      tmp_eq++;
   }
   if(verbose > 0)
   {
      sys.print();
      std::cout << "End" << std::endl;
   }
}