From 745005d1f531419734ec49f2b8aa7517d52c2ff9 Mon Sep 17 00:00:00 2001 From: Sonu0305 Date: Mon, 14 Sep 2026 15:19:52 +0400 Subject: [PATCH 1/9] Fix empty array edge cases in nlinfit --- inst/Regression/nlinfit.m | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/inst/Regression/nlinfit.m b/inst/Regression/nlinfit.m index f86cf3d2f..9405a89c0 100644 --- a/inst/Regression/nlinfit.m +++ b/inst/Regression/nlinfit.m @@ -127,6 +127,9 @@ if (! (isnumeric (y) && isreal (y))) error ("nlinfit: Y must be a real numeric vector."); endif + if (! isvector (y) || rows (X) != rows (y)) + error ("nlinfit: Y must be a vector with the same number of rows as X."); + endif beta0 = beta0(:); y = y(:); @@ -242,6 +245,10 @@ beta = beta0; yhat = modelfun (beta, X); + if (! isequal (size (yhat), size (y))) + error (["nlinfit: Nonlinear regression model function must return " ... + "a vector of the same size as Y."]); + endif R = y - yhat; sse = sum (w .* R .^ 2); lambda = 1e-2; # Marquardt damping @@ -609,5 +616,13 @@ %! assert_equal (bhub, [-0.032803877294096; 2.016488145372473], 1e-7); %! assert_equal (bboth, bhub, 1e-12); +%!test +%! ## Edge cases with empty arrays +%!fail ("nlinfit ([], [], @(b,x) b(1).*x, [1])", ... +%! "nlinfit: Y must be a vector with the same number of rows as X.") +%!fail ("nlinfit (zeros(0,3), zeros(0,1), @(b,x) b(1).*x, [1])", ... +%! ["nlinfit: Nonlinear regression model function must return " ... +%! "a vector of the same size as Y."]) + %!error ... %! nlinfit ([1;2], [1;2], @(b, x) b(1) * ones (2, 1), 1, "RobustWgtFun", "bad") From 162d9355dd184864ed1b5480b362035dbf1696f1 Mon Sep 17 00:00:00 2001 From: Sonu0305 Date: Mon, 14 Sep 2026 15:25:17 +0400 Subject: [PATCH 2/9] Refactor long error messages to use strcat --- inst/Regression/nlinfit.m | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/inst/Regression/nlinfit.m b/inst/Regression/nlinfit.m index 9405a89c0..8036e18e0 100644 --- a/inst/Regression/nlinfit.m +++ b/inst/Regression/nlinfit.m @@ -246,8 +246,8 @@ beta = beta0; yhat = modelfun (beta, X); if (! isequal (size (yhat), size (y))) - error (["nlinfit: Nonlinear regression model function must return " ... - "a vector of the same size as Y."]); + error (strcat ("nlinfit: Nonlinear regression model function must return ", ... + "a vector of the same size as Y.")); endif R = y - yhat; sse = sum (w .* R .^ 2); @@ -621,8 +621,8 @@ %!fail ("nlinfit ([], [], @(b,x) b(1).*x, [1])", ... %! "nlinfit: Y must be a vector with the same number of rows as X.") %!fail ("nlinfit (zeros(0,3), zeros(0,1), @(b,x) b(1).*x, [1])", ... -%! ["nlinfit: Nonlinear regression model function must return " ... -%! "a vector of the same size as Y."]) +%! strcat ("nlinfit: Nonlinear regression model function must return ", ... +%! "a vector of the same size as Y.")) %!error ... %! nlinfit ([1;2], [1;2], @(b, x) b(1) * ones (2, 1), 1, "RobustWgtFun", "bad") From 4646f0db9a993f5c8242f884bcec3b5acfca482c Mon Sep 17 00:00:00 2001 From: Sonu0305 Date: Mon, 14 Sep 2026 15:36:16 +0400 Subject: [PATCH 3/9] Fix trailing space issue with strcat in nlinfit --- inst/Regression/nlinfit.m | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/inst/Regression/nlinfit.m b/inst/Regression/nlinfit.m index 8036e18e0..9939e0f1a 100644 --- a/inst/Regression/nlinfit.m +++ b/inst/Regression/nlinfit.m @@ -246,8 +246,8 @@ beta = beta0; yhat = modelfun (beta, X); if (! isequal (size (yhat), size (y))) - error (strcat ("nlinfit: Nonlinear regression model function must return ", ... - "a vector of the same size as Y.")); + error (strcat ("nlinfit: Nonlinear regression model function must return", ... + " a vector of the same size as Y.")); endif R = y - yhat; sse = sum (w .* R .^ 2); @@ -621,8 +621,8 @@ %!fail ("nlinfit ([], [], @(b,x) b(1).*x, [1])", ... %! "nlinfit: Y must be a vector with the same number of rows as X.") %!fail ("nlinfit (zeros(0,3), zeros(0,1), @(b,x) b(1).*x, [1])", ... -%! strcat ("nlinfit: Nonlinear regression model function must return ", ... -%! "a vector of the same size as Y.")) +%! strcat ("nlinfit: Nonlinear regression model function must return", ... +%! " a vector of the same size as Y.")) %!error ... %! nlinfit ([1;2], [1;2], @(b, x) b(1) * ones (2, 1), 1, "RobustWgtFun", "bad") From 9adada0b666cc21721312062c9a69b0230674dc6 Mon Sep 17 00:00:00 2001 From: Sonu0305 Date: Mon, 14 Sep 2026 15:57:46 +0400 Subject: [PATCH 4/9] Refactor tests to use %!error instead of %!fail --- inst/Regression/nlinfit.m | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/inst/Regression/nlinfit.m b/inst/Regression/nlinfit.m index 9939e0f1a..6b595e633 100644 --- a/inst/Regression/nlinfit.m +++ b/inst/Regression/nlinfit.m @@ -618,11 +618,10 @@ %!test %! ## Edge cases with empty arrays -%!fail ("nlinfit ([], [], @(b,x) b(1).*x, [1])", ... -%! "nlinfit: Y must be a vector with the same number of rows as X.") -%!fail ("nlinfit (zeros(0,3), zeros(0,1), @(b,x) b(1).*x, [1])", ... -%! strcat ("nlinfit: Nonlinear regression model function must return", ... -%! " a vector of the same size as Y.")) +%!error ... +%! nlinfit ([], [], @(b,x) b(1).*x, [1]) +%!error ... +%! nlinfit (zeros(0,3), zeros(0,1), @(b,x) b(1).*x, [1]) %!error ... %! nlinfit ([1;2], [1;2], @(b, x) b(1) * ones (2, 1), 1, "RobustWgtFun", "bad") From 128fff11087a4472fed585c3cfdc1ee4ac833c77 Mon Sep 17 00:00:00 2001 From: Sonu0305 Date: Tue, 15 Sep 2026 20:52:24 +0400 Subject: [PATCH 5/9] Fix empty array error message formatting in nlinfit --- inst/Regression/nlinfit.m | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/inst/Regression/nlinfit.m b/inst/Regression/nlinfit.m index 6b595e633..7317a4f49 100644 --- a/inst/Regression/nlinfit.m +++ b/inst/Regression/nlinfit.m @@ -128,7 +128,7 @@ error ("nlinfit: Y must be a real numeric vector."); endif if (! isvector (y) || rows (X) != rows (y)) - error ("nlinfit: Y must be a vector with the same number of rows as X."); + error ("nlinfit: y must be a vector with the same number of rows as X."); endif beta0 = beta0(:); @@ -246,8 +246,8 @@ beta = beta0; yhat = modelfun (beta, X); if (! isequal (size (yhat), size (y))) - error (strcat ("nlinfit: Nonlinear regression model function must return", ... - " a vector of the same size as Y.")); + error (strcat ("nlinfit: nonlinear regression model function", ... + " must return a vector of the same size as y.")); endif R = y - yhat; sse = sum (w .* R .^ 2); @@ -616,11 +616,10 @@ %! assert_equal (bhub, [-0.032803877294096; 2.016488145372473], 1e-7); %! assert_equal (bboth, bhub, 1e-12); -%!test %! ## Edge cases with empty arrays -%!error ... +%!error ... %! nlinfit ([], [], @(b,x) b(1).*x, [1]) -%!error ... +%!error ... %! nlinfit (zeros(0,3), zeros(0,1), @(b,x) b(1).*x, [1]) %!error ... From 75a50f2ccc06856e2eebc74f3eee250fd0da515e Mon Sep 17 00:00:00 2001 From: Sonu0305 Date: Tue, 15 Sep 2026 20:54:23 +0400 Subject: [PATCH 6/9] fix formatting --- inst/Regression/nlinfit.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inst/Regression/nlinfit.m b/inst/Regression/nlinfit.m index 7317a4f49..6cc5b8b57 100644 --- a/inst/Regression/nlinfit.m +++ b/inst/Regression/nlinfit.m @@ -616,7 +616,7 @@ %! assert_equal (bhub, [-0.032803877294096; 2.016488145372473], 1e-7); %! assert_equal (bboth, bhub, 1e-12); -%! ## Edge cases with empty arrays +## Edge cases with empty arrays %!error ... %! nlinfit ([], [], @(b,x) b(1).*x, [1]) %!error ... From b1ee124176d5d1ce46446afa4cdc0069debab8c1 Mon Sep 17 00:00:00 2001 From: Sonu0305 Date: Wed, 16 Sep 2026 06:20:36 +0400 Subject: [PATCH 7/9] Remove rows check and update MODELFUN size error in nlinfit --- inst/Regression/nlinfit.m | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/inst/Regression/nlinfit.m b/inst/Regression/nlinfit.m index 6cc5b8b57..1f6bd6a56 100644 --- a/inst/Regression/nlinfit.m +++ b/inst/Regression/nlinfit.m @@ -127,8 +127,8 @@ if (! (isnumeric (y) && isreal (y))) error ("nlinfit: Y must be a real numeric vector."); endif - if (! isvector (y) || rows (X) != rows (y)) - error ("nlinfit: y must be a vector with the same number of rows as X."); + if (! isvector (y)) + error ("nlinfit: Y must be a vector."); endif beta0 = beta0(:); @@ -246,8 +246,7 @@ beta = beta0; yhat = modelfun (beta, X); if (! isequal (size (yhat), size (y))) - error (strcat ("nlinfit: nonlinear regression model function", ... - " must return a vector of the same size as y.")); + error ("nlinfit: MODELFUN must return a vector of the same size as Y."); endif R = y - yhat; sse = sum (w .* R .^ 2); @@ -617,10 +616,10 @@ %! assert_equal (bboth, bhub, 1e-12); ## Edge cases with empty arrays -%!error ... -%! nlinfit ([], [], @(b,x) b(1).*x, [1]) -%!error ... -%! nlinfit (zeros(0,3), zeros(0,1), @(b,x) b(1).*x, [1]) +%!error ... +%! nlinfit ([], zeros (0, 1), @(b,x) b(1).*x, [1]) +%!error ... +%! nlinfit (zeros (0, 3), zeros (0, 1), @(b,x) b(1).*x, [1]) %!error ... %! nlinfit ([1;2], [1;2], @(b, x) b(1) * ones (2, 1), 1, "RobustWgtFun", "bad") From cee32985d3d31743b1901d6f61809f6a5febe49a Mon Sep 17 00:00:00 2001 From: Sonu0305 Date: Wed, 16 Sep 2026 06:53:40 +0400 Subject: [PATCH 8/9] Fix broadcasting array issue in lm_fit and relax isvector check --- inst/Regression/nlinfit.m | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/inst/Regression/nlinfit.m b/inst/Regression/nlinfit.m index 1f6bd6a56..f007b9776 100644 --- a/inst/Regression/nlinfit.m +++ b/inst/Regression/nlinfit.m @@ -245,9 +245,10 @@ beta = beta0; yhat = modelfun (beta, X); - if (! isequal (size (yhat), size (y))) + if (! isvector (yhat) || numel (yhat) != numel (y)) error ("nlinfit: MODELFUN must return a vector of the same size as Y."); endif + yhat = yhat(:); R = y - yhat; sse = sum (w .* R .^ 2); lambda = 1e-2; # Marquardt damping @@ -266,7 +267,8 @@ A = JtJ + lambda * diagJtJ; delta = pinv (A) * Jtr; bnew = beta + delta; - rnew = y - modelfun (bnew, X); + ynew = modelfun (bnew, X); + rnew = y - ynew(:); ssenew = sum (w .* rnew .^ 2); if (isfinite (ssenew) && ssenew < sse) stepok = true; From 6fc9d063621f58643b016aaea30e9e69c3fab8e2 Mon Sep 17 00:00:00 2001 From: Sonu0305 Date: Wed, 16 Sep 2026 07:04:15 +0400 Subject: [PATCH 9/9] Fix errors --- inst/Regression/nlinfit.m | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/inst/Regression/nlinfit.m b/inst/Regression/nlinfit.m index f007b9776..5e48a6773 100644 --- a/inst/Regression/nlinfit.m +++ b/inst/Regression/nlinfit.m @@ -131,6 +131,10 @@ error ("nlinfit: Y must be a vector."); endif + if (! isequal (size (modelfun (beta0(:), X)), size (y))) + error ("nlinfit: MODELFUN must return a vector of the same size as Y."); + endif + beta0 = beta0(:); y = y(:); n = numel (y); @@ -245,10 +249,7 @@ beta = beta0; yhat = modelfun (beta, X); - if (! isvector (yhat) || numel (yhat) != numel (y)) - error ("nlinfit: MODELFUN must return a vector of the same size as Y."); - endif - yhat = yhat(:); + yhat = yhat(:); R = y - yhat; sse = sum (w .* R .^ 2); lambda = 1e-2; # Marquardt damping