fixed null pointer checking
[pspp-builds.git] / src / math / linreg / coefficient.c
index be189baa7c81ab8f586853158f1b29eea16b00b5..c372cd0e25124bcf9eaa2a3b492c478aca54f1e6 100644 (file)
 /*
   Accessor functions for matching coefficients and variables.
  */
-#include <assert.h>
 #include <math/linreg/coefficient.h>
 #include <math/linreg/linreg.h>
 #include "src/math/design-matrix.h"
-#include "src/data/variable.h"
-#include "src/data/value.h"
 
 #include <gl/xalloc.h>
 
@@ -102,12 +99,40 @@ pspp_linreg_coeff_set_std_err (struct pspp_linreg_coeff *c, double std_err)
   c->std_err = std_err;
 }
 
+/*
+  Return the estimated value of the coefficient.
+ */
+double
+pspp_linreg_coeff_get_est (const struct pspp_linreg_coeff *c)
+{
+  if (c == NULL)
+    {
+      return 0.0;
+    }
+  return c->estimate;
+}
+/*
+  Return the standard error of the estimated coefficient.
+*/
+double 
+pspp_linreg_coeff_get_std_err (const struct pspp_linreg_coeff *c)
+{
+  if (c == NULL)
+    {
+      return 0.0;
+    }
+  return c->std_err;
+}
 /*
   How many variables are associated with this coefficient?
  */
 int
 pspp_linreg_coeff_get_n_vars (struct pspp_linreg_coeff *c)
 {
+  if (c == NULL)
+    {
+      return 0;
+    }
   return c->n_vars;
 }
 
@@ -117,6 +142,10 @@ pspp_linreg_coeff_get_n_vars (struct pspp_linreg_coeff *c)
 const struct variable *
 pspp_linreg_coeff_get_var (struct pspp_linreg_coeff *c, int i)
 {
+  if (c == NULL)
+    {
+      return NULL;
+    }
   assert (i < c->n_vars);
   return (c->v_info + i)->v;
 }
@@ -131,6 +160,10 @@ pspp_linreg_coeff_get_value (struct pspp_linreg_coeff *c,
   int i = 0;
   const struct variable *candidate;
 
+  if (c == NULL || v == NULL)
+    {
+      return NULL;
+    }
   while (i < c->n_vars)
     {
       candidate = pspp_linreg_coeff_get_var (c, i);
@@ -155,12 +188,14 @@ pspp_linreg_get_coeff (const pspp_linreg_cache *c,
   struct pspp_linreg_coeff *result;
   const struct variable *tmp;
 
-  assert (c != NULL);
-  assert (c->coeff != NULL);
-  assert (c->n_indeps > 0);
-  assert (v != NULL);
-  assert (val != NULL);
-  
+  if (c == NULL)
+    {
+      return NULL;
+    }
+  if ( c->coeff == NULL || c->n_indeps == NULL || v == NULL)
+    {
+      return NULL;
+    }
   result = c->coeff + i;
   tmp = pspp_linreg_coeff_get_var (result, 0);
   while (tmp->index != v->index && i < c->n_coeffs)
@@ -177,7 +212,7 @@ pspp_linreg_get_coeff (const pspp_linreg_cache *c,
     {
       return result;
     }
-  else
+  else if (val != NULL)
     {
       /*
        If v is categorical, we need to ensure the coefficient
@@ -196,5 +231,6 @@ pspp_linreg_get_coeff (const pspp_linreg_cache *c,
        }
       return result;
     }
+  return NULL;
 }