-/* PSPP - Creates design-matrices.
+/* PSPP - a program for statistical analysis.
Copyright (C) 2005 Free Software Foundation, Inc.
- Written by Jason H Stover <jason@sakla.net>.
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License as
- published by the Free Software Foundation; either version 2 of the
- License, or (at your option) any later version.
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
- This program is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- 02110-1301, USA. */
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
/*
Create design matrices for procedures that need them.
#include <stdlib.h>
#include <string.h>
-#include <libpspp/alloc.h>
#include <libpspp/message.h>
#include <data/variable.h>
#include <data/category.h>
+#include <data/value.h>
#include <gsl/gsl_machine.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_matrix.h>
+#include "xalloc.h"
+
#define DM_COLUMN_NOT_FOUND -1
#define DM_INDEX_NOT_FOUND -3
-/*
- Which element of a vector is equal to the value x?
- */
-static size_t
-cat_which_element_eq (const gsl_vector * vec, double x)
-{
- size_t i;
-
- for (i = 0; i < vec->size; i++)
- {
- if (fabs (gsl_vector_get (vec, i) - x) < GSL_DBL_EPSILON)
- {
- return i;
- }
- }
- return CAT_VALUE_NOT_FOUND;
-}
-static int
-cat_is_zero_vector (const gsl_vector * vec)
-{
- size_t i;
-
- for (i = 0; i < vec->size; i++)
- {
- if (gsl_vector_get (vec, i) != 0.0)
- {
- return 0;
- }
- }
- return 1;
-}
-
-/*
- Return the value of v corresponding to the vector vec.
- */
-union value *
-cat_vector_to_value (const gsl_vector * vec, struct variable *v)
-{
- size_t i;
-
- i = cat_which_element_eq (vec, 1.0);
- if (i != CAT_VALUE_NOT_FOUND)
- {
- return cat_subscript_to_value (i + 1, v);
- }
- if (cat_is_zero_vector (vec))
- {
- return cat_subscript_to_value (0, v);
- }
- return NULL;
-}
struct design_matrix *
design_matrix_create (int n_variables,
dm = xmalloc (sizeof *dm);
dm->vars = xnmalloc (n_variables, sizeof *dm->vars);
+ dm->n_cases = xnmalloc (n_variables, sizeof (*dm->n_cases));
dm->n_vars = n_variables;
for (i = 0; i < n_variables; i++)
{
+ dm->n_cases[i] = 0;
v = v_variables[i];
assert ((dm->vars + i) != NULL);
(dm->vars + i)->v = v; /* Allows us to look up the variable from
}
else if (var_is_alpha (v))
{
- assert (v->obs_vals != NULL);
+ size_t n_categories = cat_get_n_categories (v);
(dm->vars + i)->last_column =
- (dm->vars + i)->first_column + v->obs_vals->n_categories - 2;
- n_cols += v->obs_vals->n_categories - 1;
+ (dm->vars + i)->first_column + n_categories - 2;
+ n_cols += n_categories - 1;
}
}
dm->m = gsl_matrix_calloc (n_data, n_cols);
col = 0;
+
return dm;
}
{
free (dm->vars);
gsl_matrix_free (dm->m);
+ free (dm->n_cases);
free (dm);
}
Return the index of the variable for the
given column.
*/
-struct variable *
+const struct variable *
design_matrix_col_to_var (const struct design_matrix *dm, size_t col)
{
size_t i;
{
v = dm->vars[i];
if (v.first_column <= col && col <= v.last_column)
- return (struct variable *) v.v;
+ return v.v;
}
return NULL;
}
}
/*
- Set the appropriate value in the design matrix,
+ Set the appropriate value in the design matrix,
whether that value is from a categorical or numeric
variable. For a categorical variable, only the usual
binary encoding is allowed.
gsl_matrix_set (dm->m, row, col, entry);
}
}
+
void
design_matrix_set_numeric (struct design_matrix *dm, size_t row,
const struct variable *var, const union value *val)
assert (col != DM_COLUMN_NOT_FOUND);
gsl_matrix_set (dm->m, row, col, val->f);
}
+
+struct design_matrix *
+design_matrix_clone (const struct design_matrix *dm)
+{
+ struct design_matrix *result;
+ size_t i;
+
+ assert (dm != NULL);
+ result = xmalloc (sizeof *result);
+ result->vars = xnmalloc (dm->n_vars, sizeof *dm->vars);
+ result->n_vars = dm->n_vars;
+ result->m = gsl_matrix_alloc (dm->m->size1, dm->m->size2);
+
+ gsl_matrix_memcpy (result->m, dm->m);
+ for (i = 0; i < result->n_vars; i++)
+ {
+ result->vars[i] = dm->vars[i];
+ }
+ return result;
+}
+
+/*
+ Increment the number of cases for V.
+ */
+void
+design_matrix_increment_case_count (struct design_matrix *dm, const struct variable *v)
+{
+ size_t i;
+ assert (dm != NULL);
+ assert (dm->n_cases != NULL);
+ assert (v != NULL);
+ i = design_matrix_var_to_column (dm, v);
+ dm->n_cases[i]++;
+}
+
+/*
+ Set the number of cases for V.
+ */
+void
+design_matrix_set_case_count (struct design_matrix *dm, const struct variable *v, size_t n)
+{
+ size_t i;
+ assert (dm != NULL);
+ assert (dm->n_cases != NULL);
+ assert (v != NULL);
+ i = design_matrix_var_to_column (dm, v);
+ dm->n_cases[i] = n;
+}
+
+/*
+ Get the number of cases for V.
+ */
+size_t
+design_matrix_get_case_count (const struct design_matrix *dm, const struct variable *v)
+{
+ size_t i;
+ assert (dm != NULL);
+ assert (dm->n_cases != NULL);
+ assert (v != NULL);
+ i = design_matrix_var_to_column (dm, v);
+ return dm->n_cases[i];
+}
+
+