Delete trailing whitespace at end of lines.
[pspp-builds.git] / src / math / design-matrix.c
index 138813d7fb20f47d432a27bc0b7bf3fd98bc5f9b..dff79de11ea7cb142c930571777941d01053efab 100644 (file)
   Create design matrices for procedures that need them.
 */
 #include <config.h>
-#include <stdlib.h>
-#include <message.h>
-#include "alloc.h"
-#include "message.h"
-#include "variable.h"
-#include "category.h"
+
 #include "design-matrix.h"
-#include <string.h>
+
+#include <assert.h>
 #include <math.h>
+#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>
 #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,
@@ -111,17 +65,17 @@ design_matrix_create (int n_variables,
       (dm->vars + i)->v = v;   /* Allows us to look up the variable from
                                   the design matrix. */
       (dm->vars + i)->first_column = n_cols;
-      if (v->type == NUMERIC)
+      if (var_is_numeric (v))
        {
          (dm->vars + i)->last_column = n_cols;
          n_cols++;
        }
-      else if (v->type == ALPHA)
+      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);
@@ -142,8 +96,8 @@ design_matrix_destroy (struct design_matrix *dm)
   Return the index of the variable for the
   given column.
  */
-static size_t
-design_matrix_col_to_var_index (const struct design_matrix *dm, size_t col)
+const struct variable *
+design_matrix_col_to_var (const struct design_matrix *dm, size_t col)
 {
   size_t i;
   struct design_matrix_var v;
@@ -152,42 +106,11 @@ design_matrix_col_to_var_index (const struct design_matrix *dm, size_t col)
     {
       v = dm->vars[i];
       if (v.first_column <= col && col <= v.last_column)
-       return (v.v)->index;
-    }
-  return DM_INDEX_NOT_FOUND;
-}
-
-/*
-  Return a pointer to the variable whose values
-  are stored in column col.
- */
-struct variable *
-design_matrix_col_to_var (const struct design_matrix *dm, size_t col)
-{
-  size_t index;
-  size_t i;
-  struct design_matrix_var dmv;
-
-  index = design_matrix_col_to_var_index (dm, col);
-  for (i = 0; i < dm->n_vars; i++)
-    {
-      dmv = dm->vars[i];
-      if ((dmv.v)->index == index)
-       {
-         return (struct variable *) dmv.v;
-       }
+       return v.v;
     }
   return NULL;
 }
 
-static size_t
-cmp_dm_var_index (const struct design_matrix_var *dmv, size_t index)
-{
-  if (dmv->v->index == index)
-    return 1;
-  return 0;
-}
-
 /*
   Return the number of the first column which holds the
   values for variable v.
@@ -202,7 +125,7 @@ design_matrix_var_to_column (const struct design_matrix * dm,
   for (i = 0; i < dm->n_vars; i++)
     {
       tmp = dm->vars[i];
-      if (cmp_dm_var_index (&tmp, v->index))
+      if (tmp.v == v)
        {
          return tmp.first_column;
        }
@@ -221,7 +144,7 @@ dm_var_to_last_column (const struct design_matrix *dm,
   for (i = 0; i < dm->n_vars; i++)
     {
       tmp = dm->vars[i];
-      if (cmp_dm_var_index (&tmp, v->index))
+      if (tmp.v == v)
        {
          return tmp.last_column;
        }
@@ -230,7 +153,7 @@ dm_var_to_last_column (const struct design_matrix *dm,
 }
 
 /*
-  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.
@@ -246,7 +169,7 @@ design_matrix_set_categorical (struct design_matrix *dm, size_t row,
   size_t lc;
   double entry;
 
-  assert (var->type == ALPHA);
+  assert (var_is_alpha (var));
   fc = design_matrix_var_to_column (dm, var);
   lc = dm_var_to_last_column (dm, var);
   assert (lc != DM_COLUMN_NOT_FOUND);
@@ -258,13 +181,14 @@ design_matrix_set_categorical (struct design_matrix *dm, size_t row,
       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)
 {
   size_t col;
 
-  assert (var->type == NUMERIC);
+  assert (var_is_numeric (var));
   col = design_matrix_var_to_column ((const struct design_matrix *) dm, var);
   assert (col != DM_COLUMN_NOT_FOUND);
   gsl_matrix_set (dm->m, row, col, val->f);