Commit 45f1863f authored by Morten Welinder's avatar Morten Welinder Committed by Morten Welinder

New function.

2001-07-27  Morten Welinder  <terra@diku.dk>

	* src/rangefunc.c (range_min_k, range_min_k_nonconst): New
 	function.

	* src/analysis-tools.c (kth_largest, kth_smallest): Use
 	range_min_k_nonconst.

2001-07-27  Morten Welinder  <terra@diku.dk>

	* fn-stat.c (gnumeric_quartile, gnumeric_percentile,
 	gnumeric_large, gnumeric_small): Cleanup.
parent 3739261a
2001-07-27 Morten Welinder <terra@diku.dk>
* src/rangefunc.c (range_min_k, range_min_k_nonconst): New
function.
* src/analysis-tools.c (kth_largest, kth_smallest): Use
range_min_k_nonconst.
* src/analysis-tools.c (summary_statistics): Constify data.
* src/rangefunc.c (range_median_inter): Respect const.
......
2001-07-27 Morten Welinder <terra@diku.dk>
* src/rangefunc.c (range_min_k, range_min_k_nonconst): New
function.
* src/analysis-tools.c (kth_largest, kth_smallest): Use
range_min_k_nonconst.
* src/analysis-tools.c (summary_statistics): Constify data.
* src/rangefunc.c (range_median_inter): Respect const.
......
2001-07-27 Morten Welinder <terra@diku.dk>
* src/rangefunc.c (range_min_k, range_min_k_nonconst): New
function.
* src/analysis-tools.c (kth_largest, kth_smallest): Use
range_min_k_nonconst.
* src/analysis-tools.c (summary_statistics): Constify data.
* src/rangefunc.c (range_median_inter): Respect const.
......
2001-07-27 Morten Welinder <terra@diku.dk>
* src/rangefunc.c (range_min_k, range_min_k_nonconst): New
function.
* src/analysis-tools.c (kth_largest, kth_smallest): Use
range_min_k_nonconst.
* src/analysis-tools.c (summary_statistics): Constify data.
* src/rangefunc.c (range_median_inter): Respect const.
......
......@@ -2351,21 +2351,15 @@ static char *help_large = {
};
static int
range_large (const gnum_float *xs, int n, gnum_float *res)
range_large (gnum_float *xs, int n, gnum_float *res)
{
int k;
if (n < 2)
return 1;
k = (int)xs[--n] - 1;
if (k < 0 || k >= n)
return 1;
/* OK, so we ignore the constness here. Tough. */
qsort ((gnum_float *)xs, n, sizeof (xs[0]), (void *)&float_compare);
*res = xs[n - 1 - k];
return 0;
k = (int)xs[--n];
return range_min_k_nonconst (xs, n, res, n - k);
}
static Value *
......@@ -2373,7 +2367,7 @@ gnumeric_large (FunctionEvalInfo *ei, GList *expr_node_list)
{
return float_range_function (expr_node_list,
ei,
range_large,
(float_range_function_t)range_large,
COLLECT_IGNORE_STRINGS |
COLLECT_IGNORE_BOOLS |
COLLECT_IGNORE_BLANKS,
......@@ -2404,21 +2398,15 @@ static char *help_small = {
};
static int
range_small (const gnum_float *xs, int n, gnum_float *res)
range_small (gnum_float *xs, int n, gnum_float *res)
{
int k;
if (n < 2)
return 1;
k = (int) xs[--n] - 1;
if (k < 0 || k >= n)
return 1;
/* OK, so we ignore the constness here. Tough. */
qsort ((gnum_float *) xs, n, sizeof (xs[0]), (void *) &float_compare);
*res = xs[k];
return 0;
k = (int)xs[--n];
return range_min_k_nonconst (xs, n, res, k - 1);
}
static Value *
......@@ -2426,7 +2414,7 @@ gnumeric_small (FunctionEvalInfo *ei, GList *expr_node_list)
{
return float_range_function (expr_node_list,
ei,
range_small,
(float_range_function_t)range_small,
COLLECT_IGNORE_STRINGS |
COLLECT_IGNORE_BOOLS |
COLLECT_IGNORE_BLANKS,
......@@ -3046,7 +3034,7 @@ static char *help_percentrank = {
"value. If @significance is omitted, PERCENTRANK uses three "
"digits."
"\n"
"If @array contains not data points, PERCENTRANK returns #NUM! "
"If @array contains no data points, PERCENTRANK returns #NUM! "
"error. "
"If @significance is less than one, PERCENTRANK returns #NUM! "
"error. "
......
......@@ -964,21 +964,8 @@ confidence_level (WorkbookControl *wbc, GPtrArray *data, gnum_float c_level,
}
int
range_min_k (const gnum_float *xs, int n, gnum_float *res, int k)
{
if ((0 < k) && (k <= n)) {
/* OK, so we ignore the constness here. Tough. */
qsort ((gnum_float *) xs, n, sizeof (xs[0]),
(void *) &float_compare);
*res = xs[k - 1];
return 0;
} else
return 1;
}
static void
kth_largest (WorkbookControl *wbc, GPtrArray *data, int k,
kth_largest (WorkbookControl *wbc, GPtrArray *data, int k,
GPtrArray* labels, data_analysis_output_t *dao)
{
gnum_float x;
......@@ -994,8 +981,8 @@ kth_largest (WorkbookControl *wbc, GPtrArray *data, int k,
for (col = 0; col < labels->len; col++) {
the_col = g_ptr_array_index (data, col);
error = range_min_k ((gnum_float *)(the_col->data),
the_col->len, &x, the_col->len - k + 1);
error = range_min_k_nonconst ((gnum_float *)(the_col->data),
the_col->len, &x, the_col->len - k);
if (error == 0) {
set_cell_float (dao, col + 1, 1, x);
} else {
......@@ -1021,7 +1008,8 @@ kth_smallest (WorkbookControl *wbc, GPtrArray *data, int k,
for (col = 0; col < labels->len; col++) {
the_col = g_ptr_array_index (data, col);
error = range_min_k ((gnum_float *)(the_col->data), the_col->len, &x, k);
error = range_min_k_nonconst ((gnum_float *)(the_col->data), the_col->len,
&x, k - 1);
if (error == 0) {
set_cell_float (dao, col + 1, 1, x);
} else {
......
......@@ -104,5 +104,4 @@ void set_cell_na (data_analysis_output_t *dao,
void prepare_output (WorkbookControl *wbc,
data_analysis_output_t *dao, const char *name);
int range_min_k (const gnum_float *xs, int n, gnum_float *res, int k);
#endif
2001-07-27 Morten Welinder <terra@diku.dk>
* fn-stat.c (gnumeric_quartile, gnumeric_percentile): Cleanup.
* fn-stat.c (gnumeric_quartile, gnumeric_percentile,
gnumeric_large, gnumeric_small): Cleanup.
2001-07-20 Morten Welinder <terra@diku.dk>
......
......@@ -2351,21 +2351,15 @@ static char *help_large = {
};
static int
range_large (const gnum_float *xs, int n, gnum_float *res)
range_large (gnum_float *xs, int n, gnum_float *res)
{
int k;
if (n < 2)
return 1;
k = (int)xs[--n] - 1;
if (k < 0 || k >= n)
return 1;
/* OK, so we ignore the constness here. Tough. */
qsort ((gnum_float *)xs, n, sizeof (xs[0]), (void *)&float_compare);
*res = xs[n - 1 - k];
return 0;
k = (int)xs[--n];
return range_min_k_nonconst (xs, n, res, n - k);
}
static Value *
......@@ -2373,7 +2367,7 @@ gnumeric_large (FunctionEvalInfo *ei, GList *expr_node_list)
{
return float_range_function (expr_node_list,
ei,
range_large,
(float_range_function_t)range_large,
COLLECT_IGNORE_STRINGS |
COLLECT_IGNORE_BOOLS |
COLLECT_IGNORE_BLANKS,
......@@ -2404,21 +2398,15 @@ static char *help_small = {
};
static int
range_small (const gnum_float *xs, int n, gnum_float *res)
range_small (gnum_float *xs, int n, gnum_float *res)
{
int k;
if (n < 2)
return 1;
k = (int) xs[--n] - 1;
if (k < 0 || k >= n)
return 1;
/* OK, so we ignore the constness here. Tough. */
qsort ((gnum_float *) xs, n, sizeof (xs[0]), (void *) &float_compare);
*res = xs[k];
return 0;
k = (int)xs[--n];
return range_min_k_nonconst (xs, n, res, k - 1);
}
static Value *
......@@ -2426,7 +2414,7 @@ gnumeric_small (FunctionEvalInfo *ei, GList *expr_node_list)
{
return float_range_function (expr_node_list,
ei,
range_small,
(float_range_function_t)range_small,
COLLECT_IGNORE_STRINGS |
COLLECT_IGNORE_BOOLS |
COLLECT_IGNORE_BLANKS,
......@@ -3046,7 +3034,7 @@ static char *help_percentrank = {
"value. If @significance is omitted, PERCENTRANK uses three "
"digits."
"\n"
"If @array contains not data points, PERCENTRANK returns #NUM! "
"If @array contains no data points, PERCENTRANK returns #NUM! "
"error. "
"If @significance is less than one, PERCENTRANK returns #NUM! "
"error. "
......
......@@ -570,3 +570,39 @@ range_median_inter_nonconst (gnum_float *xs, int n, gnum_float *res)
{
return range_fractile_inter_nonconst (xs, n, res, 0.5);
}
/* k-th smallest. Note: k is zero-based. */
int
range_min_k (const gnum_float *xs, int n, gnum_float *res, int k)
{
gnum_float *ys;
if (k < 0 || k >= n)
return 1;
if (k == 0)
return range_min (xs, n, res);
if (k == n - 1)
return range_max (xs, n, res);
ys = range_sort (xs, n);
*res = ys[k];
g_free (ys);
return 0;
}
/* k-th smallest. Note: k is zero-based. */
/* This version may reorder data points. */
int
range_min_k_nonconst (gnum_float *xs, int n, gnum_float *res, int k)
{
if (k < 0 || k >= n)
return 1;
if (k == 0)
return range_min (xs, n, res);
if (k == n - 1)
return range_max (xs, n, res);
qsort (xs, n, sizeof (xs[0]), (int (*) (const void *, const void *))&float_compare);
*res = xs[k];
return 0;
}
......@@ -40,4 +40,7 @@ int range_fractile_inter_nonconst (gnum_float *xs, int n, gnum_float *res, gnum_
int range_median_inter (const gnum_float *xs, int n, gnum_float *res);
int range_median_inter_nonconst (gnum_float *xs, int n, gnum_float *res);
int range_min_k (const gnum_float *xs, int n, gnum_float *res, int k);
int range_min_k_nonconst (gnum_float *xs, int n, gnum_float *res, int k);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment