+static inline bool
+not_dropped (size_t j, const bool *ff)
+{
+ return ! ff[j];
+}
+
+static void
+fill_submatrix (const gsl_matrix * cov, gsl_matrix * submatrix, bool *dropped_f)
+{
+ size_t i;
+ size_t j;
+ size_t n = 0;
+ size_t m = 0;
+
+ for (i = 0; i < cov->size1; i++)
+ {
+ if (not_dropped (i, dropped_f))
+ {
+ m = 0;
+ for (j = 0; j < cov->size2; j++)
+ {
+ if (not_dropped (j, dropped_f))
+ {
+ gsl_matrix_set (submatrix, n, m,
+ gsl_matrix_get (cov, i, j));
+ m++;
+ }
+ }
+ n++;
+ }
+ }
+}
+
+
+/*
+ Type 1 sums of squares.
+ Populate SSQ with the Type 1 sums of squares according to COV
+ */
+static void
+ssq_type1 (struct covariance *cov, gsl_vector *ssq, const struct glm_spec *cmd)
+{
+ const gsl_matrix *cm = covariance_calculate_unnormalized (cov);
+ size_t i;
+ size_t k;
+ bool *model_dropped = XCALLOC (covariance_dim (cov), bool);
+ bool *submodel_dropped = XCALLOC (covariance_dim (cov), bool);
+ const struct categoricals *cats = covariance_get_categoricals (cov);
+
+ size_t n_dropped_model = 0;
+ size_t n_dropped_submodel = 0;
+
+ for (i = cmd->n_dep_vars; i < covariance_dim (cov); i++)
+ {
+ n_dropped_model++;
+ n_dropped_submodel++;
+ model_dropped[i] = true;
+ submodel_dropped[i] = true;
+ }
+
+ for (k = 0; k < cmd->n_interactions; k++)
+ {
+ gsl_matrix *model_cov = NULL;
+ gsl_matrix *submodel_cov = NULL;
+
+ n_dropped_submodel = n_dropped_model;
+ for (i = cmd->n_dep_vars; i < covariance_dim (cov); i++)
+ {
+ submodel_dropped[i] = model_dropped[i];
+ }
+
+ for (i = cmd->n_dep_vars; i < covariance_dim (cov); i++)
+ {
+ const struct interaction * x =
+ categoricals_get_interaction_by_subscript (cats, i - cmd->n_dep_vars);
+
+ if (x == cmd->interactions [k])
+ {
+ model_dropped[i] = false;
+ n_dropped_model--;
+ }
+ }
+
+ model_cov = gsl_matrix_alloc (cm->size1 - n_dropped_model, cm->size2 - n_dropped_model);
+ submodel_cov = gsl_matrix_alloc (cm->size1 - n_dropped_submodel, cm->size2 - n_dropped_submodel);
+
+ fill_submatrix (cm, model_cov, model_dropped);
+ fill_submatrix (cm, submodel_cov, submodel_dropped);
+
+ reg_sweep (model_cov, 0);
+ reg_sweep (submodel_cov, 0);
+
+ gsl_vector_set (ssq, k + 1,
+ gsl_matrix_get (submodel_cov, 0, 0) - gsl_matrix_get (model_cov, 0, 0)
+ );
+
+ gsl_matrix_free (model_cov);
+ gsl_matrix_free (submodel_cov);
+ }
+
+ free (model_dropped);
+ free (submodel_dropped);
+}
+
+/*
+ Type 2 sums of squares.
+ Populate SSQ with the Type 2 sums of squares according to COV
+ */
+static void
+ssq_type2 (struct covariance *cov, gsl_vector *ssq, const struct glm_spec *cmd)
+{
+ const gsl_matrix *cm = covariance_calculate_unnormalized (cov);
+ size_t i;
+ size_t k;
+ bool *model_dropped = XCALLOC (covariance_dim (cov), bool);
+ bool *submodel_dropped = XCALLOC (covariance_dim (cov), bool);
+ const struct categoricals *cats = covariance_get_categoricals (cov);
+
+ for (k = 0; k < cmd->n_interactions; k++)
+ {
+ gsl_matrix *model_cov = NULL;
+ gsl_matrix *submodel_cov = NULL;
+ size_t n_dropped_model = 0;
+ size_t n_dropped_submodel = 0;
+ for (i = cmd->n_dep_vars; i < covariance_dim (cov); i++)
+ {
+ const struct interaction * x =
+ categoricals_get_interaction_by_subscript (cats, i - cmd->n_dep_vars);
+
+ model_dropped[i] = false;
+ submodel_dropped[i] = false;
+ if (interaction_is_subset (cmd->interactions [k], x))
+ {
+ assert (n_dropped_submodel < covariance_dim (cov));
+ n_dropped_submodel++;
+ submodel_dropped[i] = true;
+
+ if (cmd->interactions [k]->n_vars < x->n_vars)
+ {
+ assert (n_dropped_model < covariance_dim (cov));
+ n_dropped_model++;
+ model_dropped[i] = true;
+ }
+ }
+ }
+
+ model_cov = gsl_matrix_alloc (cm->size1 - n_dropped_model, cm->size2 - n_dropped_model);
+ submodel_cov = gsl_matrix_alloc (cm->size1 - n_dropped_submodel, cm->size2 - n_dropped_submodel);
+
+ fill_submatrix (cm, model_cov, model_dropped);
+ fill_submatrix (cm, submodel_cov, submodel_dropped);
+
+ reg_sweep (model_cov, 0);
+ reg_sweep (submodel_cov, 0);