Esempio n. 1
0
static void
set_vector (GnmNlsolve *nl, const gnm_float *xs)
{
    gnm_solver_set_vars (nl->sol, xs);
}
Esempio n. 2
0
static gboolean
glpk_affine_func (GString *dst, GnmCell *target, GnmSubSolver *ssol,
                  gnm_float const *x1, gnm_float const *x2,
                  gboolean zero_too,
                  gnm_float cst, GError **err)
{
    GnmSolver *sol = GNM_SOLVER (ssol);
    unsigned ui;
    gboolean any = FALSE;
    gnm_float y;
    gboolean ok = TRUE;
    GPtrArray *input_cells = sol->input_cells;
    gnm_float *cs;

    if (!target) {
        gnm_string_add_number (dst, cst);
        return TRUE;
    }

    gnm_solver_set_vars (sol, x1);
    gnm_cell_eval (target);
    y = cst + value_get_as_float (target->value);

    cs = gnm_solver_get_lp_coeffs (sol, target, x1, x2, err);
    if (!cs)
        goto fail;

    /* Adjust constant for choice of x1.  */
    for (ui = 0; ui < input_cells->len; ui++)
        y -= x1[ui] * cs[ui];

    for (ui = 0; ui < input_cells->len; ui++) {
        GnmCell *cell = g_ptr_array_index (input_cells, ui);
        gnm_float x = cs[ui];
        if (x == 0 && !zero_too)
            continue;

        if (any) {
            if (x < 0)
                g_string_append (dst, " - ");
            else
                g_string_append (dst, " + ");
        } else {
            if (x < 0)
                g_string_append_c (dst, '-');
        }
        x = gnm_abs (x);

        if (x != 1) {
            gnm_string_add_number (dst, x);
            g_string_append_c (dst, ' ');
        }

        g_string_append (dst, glpk_var_name (ssol, cell));

        any = TRUE;
    }

    if (!any || y) {
        if (any) {
            g_string_append_c (dst, ' ');
            if (y > 0)
                g_string_append_c (dst, '+');
        }
        gnm_string_add_number (dst, y);
    }

fail:
    g_free (cs);

    return ok;
}