Commit 0e81c949 authored by Jukka-Pekka Iivonen's avatar Jukka-Pekka Iivonen Committed by Arturo Espinosa

Wrote dialogs for these tools. The tools are not implemented yet.



1999-04-08  Jukka-Pekka Iivonen  <iivonen@iki.fi>

	* src/dialog-solver.c, src/dialog-goal-seek.c, src/solver.h:
	Wrote dialogs for these tools. The tools are not implemented yet.

	* src/solver-lp.c:
	New file for linear programming methods created.

	* src/sheet.h:
	Added parameters for the Solver tool.

	* src/fn-stat.c (gnumeric_correl):
	Bug fixed.  If the covariance is zero the correlation coefficient
	should also be zero. Thanks for Vladimir for pointing this one out.
parent 71e0e5dd
1999-04-08 Jukka-Pekka Iivonen <iivonen@iki.fi>
* src/dialog-solver.c, src/dialog-goal-seek.c, src/solver.h:
Wrote dialogs for these tools. The tools are not implemented yet.
* src/solver-lp.c:
New file for linear programming methods created.
* src/sheet.h:
Added parameters for the Solver tool.
* src/fn-stat.c (gnumeric_correl):
Bug fixed. If the covariance is zero the correlation coefficient
should also be zero. Thanks for Vladimir for pointing this one out.
1999-04-07 Miguel de Icaza <miguel@nuclecu.unam.mx>
* src/xml-io.c (xmlGetCoordinates): Include fix from Richard Hult:
......
1999-04-08 Jukka-Pekka Iivonen <iivonen@iki.fi>
* src/dialog-solver.c, src/dialog-goal-seek.c, src/solver.h:
Wrote dialogs for these tools. The tools are not implemented yet.
* src/solver-lp.c:
New file for linear programming methods created.
* src/sheet.h:
Added parameters for the Solver tool.
* src/fn-stat.c (gnumeric_correl):
Bug fixed. If the covariance is zero the correlation coefficient
should also be zero. Thanks for Vladimir for pointing this one out.
1999-04-07 Miguel de Icaza <miguel@nuclecu.unam.mx>
* src/xml-io.c (xmlGetCoordinates): Include fix from Richard Hult:
......
1999-04-08 Jukka-Pekka Iivonen <iivonen@iki.fi>
* src/dialog-solver.c, src/dialog-goal-seek.c, src/solver.h:
Wrote dialogs for these tools. The tools are not implemented yet.
* src/solver-lp.c:
New file for linear programming methods created.
* src/sheet.h:
Added parameters for the Solver tool.
* src/fn-stat.c (gnumeric_correl):
Bug fixed. If the covariance is zero the correlation coefficient
should also be zero. Thanks for Vladimir for pointing this one out.
1999-04-07 Miguel de Icaza <miguel@nuclecu.unam.mx>
* src/xml-io.c (xmlGetCoordinates): Include fix from Richard Hult:
......
1999-04-08 Jukka-Pekka Iivonen <iivonen@iki.fi>
* src/dialog-solver.c, src/dialog-goal-seek.c, src/solver.h:
Wrote dialogs for these tools. The tools are not implemented yet.
* src/solver-lp.c:
New file for linear programming methods created.
* src/sheet.h:
Added parameters for the Solver tool.
* src/fn-stat.c (gnumeric_correl):
Bug fixed. If the covariance is zero the correlation coefficient
should also be zero. Thanks for Vladimir for pointing this one out.
1999-04-07 Miguel de Icaza <miguel@nuclecu.unam.mx>
* src/xml-io.c (xmlGetCoordinates): Include fix from Richard Hult:
......
......@@ -771,7 +771,7 @@ gnumeric_correl (void *tsheet, GList *expr_node_list, int eval_col, int eval_row
{
stat_correl_t pr;
Sheet *sheet = (Sheet *) tsheet;
float_t sum;
float_t sum, tmp;
int count;
GSList *list1, *list2;
......@@ -813,9 +813,13 @@ gnumeric_correl (void *tsheet, GList *expr_node_list, int eval_col, int eval_row
g_slist_free (pr.array1);
g_slist_free (pr.array2);
return value_float ((sum - (pr.sum1*pr.sum2/pr.count)) /
sqrt((pr.sqrsum1-(pr.sum1*pr.sum1)/pr.count) *
(pr.sqrsum2-(pr.sum2*pr.sum2)/pr.count)));
tmp = (pr.sqrsum1-(pr.sum1*pr.sum1)/pr.count) *
(pr.sqrsum2-(pr.sum2*pr.sum2)/pr.count);
if (tmp == 0)
return value_float (0);
else
return value_float ((sum - (pr.sum1*pr.sum2/pr.count)) /
sqrt(tmp));
}
static char *help_negbinomdist = {
......
......@@ -31,11 +31,13 @@ GNUMERIC_BASE_SOURCES = \
dialog-cell-sort.c \
dialog-function-select.c \
dialog-function-wizard.c \
dialog-goal-seek.c \
dialog-goto-cell.c \
dialog-insert-cells.c \
dialog-define-names.c \
dialog-delete-cells.c \
dialog-paste-special.c \
dialog-solver.c \
dialog-zoom.c \
dialogs.h \
dump.c \
......@@ -97,6 +99,7 @@ GNUMERIC_BASE_SOURCES = \
sheet-object.h \
sheet-view.c \
sheet-view.h \
solver-lp.c \
str.c \
str.h \
style.c \
......
/*
* dialog-goal-seek.c:
*
* Author:
* Jukka-Pekka Iivonen <iivonen@iki.fi>
*
*/
#include <config.h>
#include <gnome.h>
#include "gnumeric.h"
#include "gnumeric-util.h"
#include "gnumeric-sheet.h"
#include "dialogs.h"
#include "cell.h"
#include "expr.h"
#define MAX_CELL_NAME_LEN 20
/* Goal seek will find a value for a given cell. User specifies a cell (C1),
* its target value, and another cell (C2) whose value is to be determined.
* If the seek is successful, the value in C1 matches the target value and
* 'f_flag' is set to 1 (otherwise 0). The function returns the value of C2.
*/
static float_t
gnumeric_goal_seek (Workbook *wb, Sheet *sheet,
int set_cell_col, int set_cell_row, /* C1 */
float_t target_value,
int change_cell_col, int change_cell_row, /* C2 */
int *f_flag)
{
/* Not implemented yet */
*f_flag = 0;
return 0;
}
void
dialog_goal_seek (Workbook *wb, Sheet *sheet)
{
static GtkWidget *dialog;
static GtkWidget *set_entry;
static GtkWidget *set_label;
static GtkWidget *target_entry;
static GtkWidget *target_label;
static GtkWidget *change_entry;
static GtkWidget *change_label;
static GtkWidget *label_box;
static GtkWidget *entry_box;
char *set_entry_str;
char *text;
int selection;
g_return_if_fail (sheet != NULL);
g_return_if_fail (IS_SHEET (sheet));
set_entry_str = cell_name (sheet->cursor_col, sheet->cursor_row);
if (!dialog) {
GtkWidget *box;
dialog = gnome_dialog_new (_("Goal Seek..."),
GNOME_STOCK_BUTTON_OK,
GNOME_STOCK_BUTTON_CANCEL,
NULL);
gnome_dialog_close_hides (GNOME_DIALOG (dialog), TRUE);
gnome_dialog_set_parent (GNOME_DIALOG (dialog), GTK_WINDOW (wb->toplevel));
set_entry = gtk_entry_new_with_max_length (MAX_CELL_NAME_LEN);
target_entry = gtk_entry_new_with_max_length(MAX_CELL_NAME_LEN);
change_entry = gtk_entry_new_with_max_length(MAX_CELL_NAME_LEN);
set_label = gtk_label_new("Set Cell:");
target_label = gtk_label_new("To value:");
change_label = gtk_label_new("By changing cell:");
gtk_misc_set_alignment (GTK_MISC(set_label), 0,0);
gtk_misc_set_alignment (GTK_MISC(target_label), 0,0);
gtk_misc_set_alignment (GTK_MISC(change_label), 0,0);
box = gtk_hbox_new (FALSE, 0);
entry_box = gtk_vbox_new (FALSE, 0);
label_box = gtk_vbox_new (FALSE, 0);
gtk_box_pack_start_defaults (GTK_BOX (label_box), set_label);
gtk_box_pack_start_defaults (GTK_BOX (entry_box), set_entry);
gtk_box_pack_start_defaults (GTK_BOX (label_box),
target_label);
gtk_box_pack_start_defaults (GTK_BOX (entry_box),
target_entry);
gtk_box_pack_start_defaults (GTK_BOX (label_box),
change_label);
gtk_box_pack_start_defaults (GTK_BOX (entry_box),
change_entry);
gtk_box_pack_start_defaults (GTK_BOX (box), label_box);
gtk_box_pack_start_defaults (GTK_BOX (box), entry_box);
gtk_box_pack_start_defaults (GTK_BOX (GNOME_DIALOG
(dialog)->vbox), box);
gtk_entry_set_text(GTK_ENTRY (set_entry), set_entry_str);
gtk_entry_set_position(GTK_ENTRY (set_entry), 0);
gtk_entry_select_region(GTK_ENTRY (set_entry), 0,
GTK_ENTRY(set_entry)->text_length);
gtk_widget_show_all (box);
} else {
gtk_entry_set_text(GTK_ENTRY (set_entry), set_entry_str);
gtk_entry_set_position(GTK_ENTRY (set_entry), 0);
gtk_entry_select_region(GTK_ENTRY (set_entry), 0,
GTK_ENTRY(set_entry)->text_length);
gtk_widget_show (dialog);
}
gtk_widget_grab_focus (set_entry);
dialog_loop:
selection = gnome_dialog_run (GNOME_DIALOG (dialog));
if (selection == 0) {
int set_cell_col, set_cell_row;
int change_cell_col, change_cell_row;
float_t target_value;
int f_flag;
float_t value;
/* Check that a cell entered in 'set cell' entry */
text = gtk_entry_get_text (GTK_ENTRY (set_entry));
if (!parse_cell_name (text, &set_cell_col, &set_cell_row)){
gnumeric_notice (wb, GNOME_MESSAGE_BOX_ERROR,
_("You should introduce a valid cell name in 'Set cell'"));
gtk_widget_grab_focus (set_entry);
gtk_entry_set_position(GTK_ENTRY (set_entry), 0);
gtk_entry_select_region(GTK_ENTRY (set_entry), 0,
GTK_ENTRY(set_entry)->text_length);
goto dialog_loop;
}
text = gtk_entry_get_text (GTK_ENTRY (target_entry));
/* Add float input parsing here */
target_value = atof(text);
/* Check that a cell entered in 'by changing cell' entry */
text = gtk_entry_get_text (GTK_ENTRY (change_entry));
if (!parse_cell_name (text, &change_cell_col, &change_cell_row)){
gnumeric_notice (wb, GNOME_MESSAGE_BOX_ERROR,
_("You should introduce a valid cell name in 'By changing cell'"));
gtk_widget_grab_focus (change_entry);
gtk_entry_set_position(GTK_ENTRY (change_entry), 0);
gtk_entry_select_region(GTK_ENTRY (change_entry), 0,
GTK_ENTRY(change_entry)->text_length);
goto dialog_loop;
}
value = gnumeric_goal_seek(wb, sheet,
set_cell_col, set_cell_row,
target_value,
change_cell_col, change_cell_row,
&f_flag);
if (f_flag) {
/* Goal seeking found a solution */
} else {
gnumeric_notice (wb, GNOME_MESSAGE_BOX_ERROR,
_("Goal seek did not find a solution!"));
}
}
gnome_dialog_close (GNOME_DIALOG (dialog));
}
/*
* dialog-solver.c:
*
* Author:
* Jukka-Pekka Iivonen <iivonen@iki.fi>
*/
#include <config.h>
#include <gnome.h>
#include <string.h>
#include "gnumeric.h"
#include "gnumeric-util.h"
#include "dialogs.h"
#include "func.h"
/* Different constraint types */
static char *constraint_strs[] = {
N_("<="),
N_("="),
N_(">="),
N_("int"),
N_("bool"),
NULL
};
static struct {
char *name;
int disables_second_group;
} paste_types [] = {
{ N_("All"), 0 },
{ N_("Formulas"), 0 },
{ N_("Values"), 0 },
{ N_("Formats"), 1 },
{ NULL, 0 }
};
static char *equal_ops [] = {
N_("Max"),
N_("Min"),
NULL
};
static char *estimate_ops [] = {
N_("Tangent"),
N_("Quadratic"),
NULL
};
static char *derivative_ops [] = {
N_("Forward"),
N_("Central"),
NULL
};
static char *search_ops [] = {
N_("Newton"),
N_("Conjugate"),
NULL
};
static char *check_button_left_ops [] = {
N_("Assume Linear Model"),
N_("Assume Non-Negative"),
NULL
};
static char *check_button_right_ops [] = {
N_("Use Automatic Scaling"),
N_("Show Iteration Results"),
NULL
};
static GSList *
add_radio_buttons (GtkWidget *hbox, char *title, char *ops[])
{
GtkWidget *f, *fv;
GSList *group_ops = NULL;
int i;
f = gtk_frame_new (_(title));
fv = gtk_vbox_new (TRUE, 0);
gtk_container_add (GTK_CONTAINER (f), fv);
for (i = 0; ops [i]; i++) {
GtkWidget *r;
r = gtk_radio_button_new_with_label (group_ops, _(ops [i]));
group_ops = GTK_RADIO_BUTTON (r)->group;
gtk_box_pack_start_defaults (GTK_BOX (fv), r);
}
gtk_box_pack_start_defaults (GTK_BOX (hbox), f);
return group_ops;
}
static void
add_check_buttons (GtkWidget *box, char *ops[])
{
GtkWidget *button;
int i;
for (i = 0; ops[i]; i++) {
button = gtk_check_button_new_with_label (ops[i]);
gtk_box_pack_start (GTK_BOX (box), button, TRUE, TRUE, 0);
}
}
static GList *
add_strings_to_glist (char *strs[])
{
int i;
GList *list = NULL;
for (i=0; strs[i] != NULL; i++) {
list = g_list_append (list, (gpointer) strs[i]);
}
return list;
}
typedef struct {
GSList *constraints;
GtkCList *clist;
Sheet *sheet;
} constraint_dialog_t;
static void
dialog_solver_options (Workbook *wb, Sheet *sheet)
{
GtkWidget *dialog;
GtkWidget *radio_buttons, *check_buttons;
GtkWidget *check_buttons_left, *check_buttons_right;
GtkWidget *button;
GSList *group_estimates, *group_derivatives, *group_search;
dialog = gnome_dialog_new (_("Gnumeric Solver Options"),
GNOME_STOCK_BUTTON_OK,
GNOME_STOCK_BUTTON_CANCEL,
NULL);
gnome_dialog_set_parent (GNOME_DIALOG (dialog),
GTK_WINDOW (wb->toplevel));
check_buttons_left = gtk_vbox_new (TRUE, 0);
check_buttons_right = gtk_vbox_new (TRUE, 0);
check_buttons = gtk_hbox_new(TRUE, 0);
radio_buttons = gtk_hbox_new (TRUE, 0);
add_check_buttons(check_buttons_left, check_button_left_ops);
add_check_buttons(check_buttons_right, check_button_right_ops);
group_estimates = add_radio_buttons(radio_buttons,
"Estimates", estimate_ops);
group_derivatives = add_radio_buttons(radio_buttons,
"Derivatives", derivative_ops);
group_search = add_radio_buttons(radio_buttons,
"Search", search_ops);
gtk_box_pack_start (GTK_BOX (check_buttons),
check_buttons_left, TRUE, TRUE, 0);
gtk_box_pack_start (GTK_BOX (check_buttons),
check_buttons_right, TRUE, TRUE, 0);
gtk_box_pack_start (GTK_BOX (GNOME_DIALOG (dialog)->vbox),
check_buttons, TRUE, TRUE, 0);
gtk_box_pack_start (GTK_BOX (GNOME_DIALOG (dialog)->vbox),
radio_buttons, TRUE, TRUE, 0);
gtk_widget_show_all (dialog);
/* Run the dialog */
gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
gnome_dialog_run (GNOME_DIALOG (dialog));
sheet->solver_parameters.options.assume_linear_model = 1;
sheet->solver_parameters.options.assume_non_negative = 0;
sheet->solver_parameters.options.automatic_scaling = 0;
sheet->solver_parameters.options.show_iteration_results = 0;
gtk_object_destroy (GTK_OBJECT (dialog));
}
/* 'Constraint Add' button clicked */
static void
constr_add_click (gpointer data)
{
constraint_dialog_t *constraint_dialog = (constraint_dialog_t *) data;
SolverConstraint *constraint;
GtkWidget *dialog;
GtkWidget *lhs_vbox, *rhs_vbox, *type_vbox;
GtkWidget *lhs_label, *rhs_label, *type_label;
GtkWidget *lhs_entry, *rhs_entry, *type_entry;
GtkWidget *hbox;
GList *constraint_type_strs;
int selection;
char constraint_buf[512];
char *lhs_text, *rhs_text;
int rhs_col, rhs_row;
int lhs_col, lhs_row;
char *constraint_str[2] = { constraint_buf, NULL };
char *type_str;
constraint_type_strs = add_strings_to_glist (constraint_strs);
dialog = gnome_dialog_new (_("Add Constraint"),
GNOME_STOCK_BUTTON_OK,
GNOME_STOCK_BUTTON_CANCEL,
_("Add"),
NULL);
hbox = gtk_hbox_new (FALSE, 0);
lhs_vbox = gtk_vbox_new (FALSE, 0);
rhs_vbox = gtk_vbox_new (FALSE, 0);
type_vbox = gtk_vbox_new (FALSE, 0);
lhs_label = gtk_label_new ("Cell Reference:");
rhs_label = gtk_label_new ("Constraint:");
type_label = gtk_label_new ("Type:");
lhs_entry = gtk_entry_new_with_max_length (20);
rhs_entry = gtk_entry_new_with_max_length (20);
type_entry = gtk_combo_new ();
gtk_combo_set_popdown_strings (GTK_COMBO (type_entry),
constraint_type_strs);
gtk_box_pack_start_defaults (GTK_BOX (lhs_vbox), lhs_label);
gtk_box_pack_start_defaults (GTK_BOX (lhs_vbox), lhs_entry);
gtk_box_pack_start_defaults (GTK_BOX (rhs_vbox), rhs_label);
gtk_box_pack_start_defaults (GTK_BOX (rhs_vbox), rhs_entry);
gtk_box_pack_start_defaults (GTK_BOX (type_vbox), type_label);
gtk_box_pack_start_defaults (GTK_BOX (type_vbox), type_entry);
gtk_box_pack_start_defaults (GTK_BOX (hbox), lhs_vbox);
gtk_box_pack_start_defaults (GTK_BOX (hbox), type_vbox);
gtk_box_pack_start_defaults (GTK_BOX (hbox), rhs_vbox);
gtk_box_pack_start_defaults (GTK_BOX (GNOME_DIALOG
(dialog)->vbox), hbox);
add_dialog:
gtk_widget_show_all (dialog);
/* Run the dialog */
gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
selection = gnome_dialog_run (GNOME_DIALOG (dialog));
if (selection == 1) {
gtk_object_destroy (GTK_OBJECT (dialog));
return;
}
lhs_text = gtk_entry_get_text (GTK_ENTRY (lhs_entry));
if (!parse_cell_name (lhs_text, &lhs_col, &lhs_row)) {
gtk_widget_grab_focus (lhs_entry);
gtk_entry_set_position(GTK_ENTRY (lhs_entry), 0);
gtk_entry_select_region(GTK_ENTRY (lhs_entry), 0,
GTK_ENTRY(lhs_entry)->text_length);
goto add_dialog;
}
rhs_text = gtk_entry_get_text (GTK_ENTRY (rhs_entry));
if (!parse_cell_name (rhs_text, &rhs_col, &rhs_row)) {
gtk_widget_grab_focus (rhs_entry);
gtk_entry_set_position(GTK_ENTRY (rhs_entry), 0);
gtk_entry_select_region(GTK_ENTRY (rhs_entry), 0,
GTK_ENTRY(rhs_entry)->text_length);
goto add_dialog;
}
type_str = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(type_entry)->entry));
sprintf(constraint_buf, "%s %s ",
cell_name (lhs_col, lhs_row),
type_str);
strcat(constraint_buf, cell_name(rhs_col, rhs_row));
gtk_clist_append (constraint_dialog->clist, constraint_str);
constraint = (SolverConstraint *) malloc (sizeof(SolverConstraint));
constraint->lhs = sheet_cell_get (constraint_dialog->sheet,
lhs_col, lhs_row);
if (constraint->lhs == NULL) {
constraint->lhs = sheet_cell_new (constraint_dialog->sheet,
lhs_col, lhs_row);
cell_set_text (constraint->lhs, "");
}
constraint->rhs = sheet_cell_get (constraint_dialog->sheet,
rhs_col, rhs_row);
if (constraint->rhs == NULL) {
constraint->rhs = sheet_cell_new (constraint_dialog->sheet,
rhs_col, rhs_row);
cell_set_text (constraint->rhs, "");
}
constraint->type = (char *) malloc (strlen(type_str) + 1);
strcpy (constraint->type, type_str);
constraint->str = (char *) malloc (strlen(constraint_buf)+1);
strcpy (constraint->str, constraint_buf);
constraint_dialog->constraints =
g_slist_append(constraint_dialog->constraints,
(gpointer) constraint);
if (selection == 2)
goto add_dialog;
gtk_object_destroy (GTK_OBJECT (dialog));
}
/* 'Constraint Change' button clicked */
static void
constr_change_click (gpointer data)
{
printf("Change: Not implemented yet.\n");
}
/* 'Constraint Delete' button clicked */
static void
constr_delete_click (gpointer data)
{
printf("Delete: Not implemented yet.\n");
}
void
dialog_solver (Workbook *wb, Sheet *sheet)
{
static GtkWidget *dialog;
static GtkWidget *target_entry, *input_entry;
static GtkWidget *label;
static GtkWidget *hbox;
static GtkWidget *radio_buttons;
static GtkWidget *constraint_list;
static GtkWidget *constraint_box;
static GtkWidget *constr_add_button;
static GtkWidget *constr_change_button;
static GtkWidget *constr_delete_button;
static GtkWidget *constr_button_box;
static GSList *group_equal;
constraint_dialog_t constraint_dialog;
char *text, *target_entry_str;
int selection, sel_equal_to;