1 /* vm: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 
3 /*
4  * gog-polynom-reg.c :
5  *
6  * Copyright (C) 2005 Jean Brefort (jean.brefort@normalesup.org)
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) version 3.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
21  * USA
22  */
23 
24 #include <goffice/goffice-config.h>
25 #include "gog-polynom-reg.h"
26 #include <goffice/graph/gog-reg-curve.h>
27 #include <goffice/math/go-math.h>
28 #include <goffice/math/go-regression.h>
29 #include <glib/gi18n-lib.h>
30 
31 #include <gsf/gsf-impl-utils.h>
32 
33 #define UNICODE_MINUS_SIGN_C 0x2212
34 static int minus_utf8_len;
35 static char minus_utf8[6];
36 
37 static GogObjectClass *gog_polynom_reg_curve_parent_klass;
38 
39 static int
gog_polynom_reg_curve_build_values(GogLinRegCurve * rc,double const * x_vals,double const * y_vals,int n)40 gog_polynom_reg_curve_build_values (GogLinRegCurve *rc, double const *x_vals,
41 					double const *y_vals, int n)
42 {
43 	double x, y, xx;
44 	double xmin, xmax;
45 	int i, j, used;
46 
47 	gog_reg_curve_get_bounds (&rc->base, &xmin, &xmax);
48 	if (rc->x_vals == NULL)
49 		rc->x_vals = g_new0 (double*, rc->dims);
50 	for (i = 0; i < rc->dims; i++) {
51 		g_free (rc->x_vals[i]);
52 		rc->x_vals[i] = g_new (double, n);
53 	}
54 	g_free (rc->y_vals);
55 	rc->y_vals = g_new (double, n);
56 	for (i = 0, used = 0; i < n; i++) {
57 		x = (x_vals)? x_vals[i]: i + 1;
58 		y = y_vals[i];
59 		if (!go_finite (x) || !go_finite (y)) {
60 			if (rc->base.skip_invalid)
61 				continue;
62 			used = 0;
63 			break;
64 		}
65 		if (x < xmin || x > xmax)
66 			continue;
67 		xx = 1.;
68 		for (j = 0; j < rc->dims; j++) {
69 			xx *= x;
70 			rc->x_vals[j][used] = xx;
71 		}
72 		rc->y_vals[used] = y;
73 		used++;
74 	}
75 	return (used > rc->dims)?  used: 0;
76 }
77 
78 static double
gog_polynom_reg_curve_get_value_at(GogRegCurve * curve,double x)79 gog_polynom_reg_curve_get_value_at (GogRegCurve *curve, double x)
80 {
81 	GogLinRegCurve *lin = GOG_LIN_REG_CURVE (curve);
82 	double result = curve->a[0] + curve->a[1] * x, xx = x;
83 	int i;
84 	for (i = 1; i < lin->dims;) {
85 		xx *= x;
86 		i++;
87 		result += xx * curve->a[i];
88 	}
89 	return result;
90 }
91 
92 static const char *const exponent[10] = {
93 	"\xE2\x81\xB0",
94 	"\xC2\xB9",
95 	"\xC2\xB2",
96 	"\xC2\xB3",
97 	"\xE2\x81\xB4",
98 	"\xE2\x81\xB5",
99 	"\xE2\x81\xB6",
100 	"\xE2\x81\xB7",
101 	"\xE2\x81\xB8",
102 	"\xE2\x81\xB9"
103 };
104 
105 /* Add an exponent, i.e., a super-scripted number.  */
106 static void
append_exponent(GString * res,unsigned int i)107 append_exponent (GString *res, unsigned int i)
108 {
109 	if (i >= 10) {
110 		append_exponent (res, i / 10);
111 		i %= 10;
112 	}
113 	g_string_append (res, exponent[i]);
114 }
115 
116 
117 static void
append_number(GString * res,double c,gboolean suppress1)118 append_number (GString *res, double c, gboolean suppress1)
119 {
120 	size_t prelen = res->len;
121 
122 	g_string_append_printf (res, "%g", c);
123 
124 	if (suppress1 && res->len == prelen + 1 && res->str[prelen] == '1')
125 		g_string_truncate (res, prelen);
126 	else {
127 		/* Handle the minuses as in -1.2222e-3.  */
128 		size_t i;
129 		for (i = prelen; i < res->len; i++)
130 			if (res->str[i] == '-') {
131 				res->str[i] = minus_utf8[0];
132 				g_string_insert_len (res, i + 1, minus_utf8 + 1, minus_utf8_len - 1);
133 				i += minus_utf8_len - 1;
134 			}
135 	}
136 }
137 
138 
139 static gchar const*
gog_polynom_reg_curve_get_equation(GogRegCurve * curve)140 gog_polynom_reg_curve_get_equation (GogRegCurve *curve)
141 {
142 	if (!curve->equation) {
143 		GogLinRegCurve *lin = GOG_LIN_REG_CURVE (curve);
144 		GString *str = g_string_new ("y =");
145 		int i, lasti = (lin->affine ? 0 : 1), j = 0;
146 
147 		for (i = lin->dims; i >= lasti; i--) {
148 			double c_i = curve->a[i];
149 
150 			if (c_i == 0.) /* a very rare event */
151 				continue;
152 
153 			/* Break after every three terms present.  */
154 			if (j > 0 && j % 3 == 0)
155 				g_string_append_c (str, '\n');
156 			j++;
157 
158 			g_string_append_c (str, ' ');
159 			if (j != 1) {
160 				if (c_i >= 0)
161 					g_string_append_c (str, '+');
162 				else {
163 					g_string_append_len (str, minus_utf8, minus_utf8_len);
164 					c_i = -c_i;
165 				}
166 				g_string_append_c (str, ' ');
167 			}
168 
169 			append_number (str, c_i, i >= 1);
170 
171 			if (i >= 1) {
172 				g_string_append_c (str, 'x');
173 				if (i > 1)
174 					append_exponent (str, i);
175 			}
176 		}
177 		if (j == 0) {
178 			/* Nothing whatsoever.  */
179 			g_string_append (str, " 0");
180 		}
181 
182 		curve->equation = g_string_free (str, FALSE);
183 	}
184 	return curve->equation;
185 }
186 
187 #ifdef GOFFICE_WITH_GTK
188 #include <gtk/gtk.h>
189 static void
order_changed_cb(GtkSpinButton * btn,GObject * obj)190 order_changed_cb (GtkSpinButton *btn, GObject *obj)
191 {
192 	g_object_set (obj, "dims", gtk_spin_button_get_value_as_int (btn), NULL);
193 }
194 
195 static void
gog_polynom_reg_curve_populate_editor(GogRegCurve * reg_curve,gpointer table)196 gog_polynom_reg_curve_populate_editor (GogRegCurve *reg_curve, gpointer table)
197 {
198 	GtkWidget *l, *w;
199 	GogLinRegCurve *lin = GOG_LIN_REG_CURVE (reg_curve);
200 
201 	((GogRegCurveClass*) gog_polynom_reg_curve_parent_klass)->populate_editor (reg_curve, table);
202 	l = gtk_label_new (_("Order:"));
203 	gtk_misc_set_alignment (GTK_MISC (l), 0., 0.5);
204 	gtk_label_set_justify (GTK_LABEL (l), GTK_JUSTIFY_LEFT);
205 	gtk_widget_show (l);
206 	gtk_grid_attach_next_to (table, l, NULL, GTK_POS_BOTTOM, 1, 1);
207 	w = gtk_spin_button_new_with_range (2, 10, 1);
208 	gtk_spin_button_set_digits (GTK_SPIN_BUTTON (w), 0);
209 	gtk_widget_show (w);
210 	gtk_grid_attach_next_to (table, w, l, GTK_POS_RIGHT, 1, 1);
211 	gtk_spin_button_set_value (GTK_SPIN_BUTTON (w), lin->dims);
212 	g_signal_connect (G_OBJECT (w), "value-changed", G_CALLBACK (order_changed_cb), lin);
213 }
214 #endif
215 
216 static char const *
gog_polynom_reg_curve_type_name(G_GNUC_UNUSED GogObject const * item)217 gog_polynom_reg_curve_type_name (G_GNUC_UNUSED GogObject const *item)
218 {
219 	/* xgettext : the base for how to name scatter plot objects
220 	 * eg The 2nd plot in a chart will be called
221 	 * 	Polynomial regression2 */
222 	return N_("Polynomial regression");
223 }
224 
225 static void
gog_polynom_reg_curve_class_init(GogRegCurveClass * reg_curve_klass)226 gog_polynom_reg_curve_class_init (GogRegCurveClass *reg_curve_klass)
227 {
228 	GogLinRegCurveClass *lin_reg_klass = (GogLinRegCurveClass *) reg_curve_klass;
229 	GogObjectClass *gog_object_klass = (GogObjectClass *) reg_curve_klass;
230 
231 	gog_polynom_reg_curve_parent_klass = g_type_class_peek_parent (reg_curve_klass);
232 
233 	lin_reg_klass->build_values = gog_polynom_reg_curve_build_values;
234 	lin_reg_klass->max_dims = 10;
235 
236 	reg_curve_klass->get_value_at = gog_polynom_reg_curve_get_value_at;
237 	reg_curve_klass->get_equation = gog_polynom_reg_curve_get_equation;
238 #ifdef GOFFICE_WITH_GTK
239 	reg_curve_klass->populate_editor = gog_polynom_reg_curve_populate_editor;
240 #endif
241 
242 	gog_object_klass->type_name	= gog_polynom_reg_curve_type_name;
243 
244 	minus_utf8_len = g_unichar_to_utf8 (UNICODE_MINUS_SIGN_C, minus_utf8);
245 }
246 
247 static void
gog_polynom_reg_curve_init(GogLinRegCurve * model)248 gog_polynom_reg_curve_init (GogLinRegCurve *model)
249 {
250 	GogRegCurve *curve = GOG_REG_CURVE (model);
251 	g_free (curve->a);
252 	curve->a = g_new (double, 3);
253 	curve->a[0] = curve->a[1] = curve->a[2] = go_nan;
254 	model->dims = 2;
255 }
256 
257 GSF_DYNAMIC_CLASS (GogPolynomRegCurve, gog_polynom_reg_curve,
258 	gog_polynom_reg_curve_class_init, gog_polynom_reg_curve_init,
259 	GOG_TYPE_LIN_REG_CURVE)
260