From b49a67b466de2737cf238b64fb47dffb9fbd330e Mon Sep 17 00:00:00 2001 From: AvanishSalunke Date: Sat, 30 May 2026 10:49:30 +0000 Subject: [PATCH] align term matrix sorting --- inst/parseWilkinsonFormula.m | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/inst/parseWilkinsonFormula.m b/inst/parseWilkinsonFormula.m index 703c260c..9f916b5d 100644 --- a/inst/parseWilkinsonFormula.m +++ b/inst/parseWilkinsonFormula.m @@ -836,7 +836,7 @@ [~, unique_idx] = unique (M, 'rows'); terms_mat = terms_mat (unique_idx, :); - [~, sort_idx] = sortrows ([sum(terms_mat, 2), terms_mat]); + [~, sort_idx] = sortrows ([sum(terms_mat, 2), -terms_mat]); schema.Terms = terms_mat (sort_idx, :); endfunction @@ -1659,6 +1659,12 @@ %! eq = parseWilkinsonFormula ('y ~ A - A', 'equation'); %! expected = string('y = c1'); %! assert (isequal (eq, expected)); +%!test +%! ## Test : term row sorting. +%! eq = parseWilkinsonFormula ('Y ~ x1 * x2 * x3', 'matrix'); +%! expected_terms = [0, 0, 0, 0; 0, 1, 0, 0; 0, 0, 1, 0; 0, 0, 0, 1; 0, 1, 1, 0; 0, 1, 0, 1; 0, 0, 1, 1; 0, 1, 1, 1]; +%! assert (eq.VariableNames, {'Y', 'x1', 'x2', 'x3'}); +%! assert (eq.Terms, expected_terms); %!error parseWilkinsonFormula () %!error parseWilkinsonFormula ('y ~ x', 'invalid_mode') %!error parseWilkinsonFormula ('', 'parse')