Skip to content

Commit

Permalink
ilu.m: Update BIST tests.
Browse files Browse the repository at this point in the history
* ilu.m: Change %!xtest to %!error because it has beet determined that Octave
is correctly issuing an error rather than proceeding to create a singular
matrix L as Matlab does.  Change %!test which use fail() internally to directly
use %!error syntax.
  • Loading branch information
Rik committed Mar 29, 2018
1 parent b4ef668 commit f32328b
Showing 1 changed file with 62 additions and 40 deletions.
102 changes: 62 additions & 40 deletions scripts/sparse/ilu.m
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,11 @@
%! n = 1600;
%! dtol = 0.1;
%! A = gallery ("neumann", n) + speye (n);

%!test
%! opts.type = "nofill";
%! assert (nnz (ilu (A, opts)), 7840);

## This test has been verified in both Matlab and Octave.
%!test
%! opts.type = "crout";
Expand Down Expand Up @@ -313,7 +315,7 @@

## Tests for real matrices of different sizes for ilu0, iluc and ilutp.
## The difference A - L*U should be not greater than eps because with droptol
## equaling 0, the LU complete factorization is performed.
## equal to 0, the LU complete factorization is performed.
%!shared n_tiny, n_small, n_medium, n_large, A_tiny, A_small, A_medium, A_large
%! n_tiny = 5;
%! n_small = 40;
Expand All @@ -323,7 +325,7 @@
%! A_small = sprand (n_small, n_small, 1/n_small) + speye (n_small);
%! A_medium = sprand (n_medium, n_medium, 1/n_medium) + speye (n_medium);
%! A_large = sprand (n_large, n_large, 1/n_large/10) + speye (n_large);
%!

%!test
%! opts.type = "nofill";
%! [L, U] = ilu (A_tiny);
Expand All @@ -340,7 +342,7 @@
%! opts.type = "nofill";
%! [L, U] = ilu (A_large);
%! assert (norm (A_large - L*U, "fro") / norm (A_large, "fro"), 0, 1);
%!

%!test
%! opts.type = "crout";
%! [L, U] = ilu (A_tiny, opts);
Expand All @@ -357,7 +359,7 @@
%! opts.type = "crout";
%! [L, U] = ilu (A_large, opts);
%! assert (norm (A_large - L*U, "fro") / norm (A_large, "fro"), eps, eps);
%!

%!test
%! opts.type = "ilutp";
%! opts.droptol = 0;
Expand Down Expand Up @@ -397,7 +399,7 @@
%! i * sprand (n_medium, n_medium, 1/n_medium) + speye (n_medium);
%! A_large = sprand (n_large, n_large, 1/n_large/10) + ...
%! i * sprand (n_large, n_large, 1/n_large/10) + speye (n_large);
%!

%!test
%! opts.type = "nofill";
%! [L, U] = ilu (A_tiny);
Expand All @@ -414,7 +416,7 @@
%! opts.type = "nofill";
%! [L, U] = ilu (A_large);
%! assert (norm (A_large - L*U, "fro") / norm (A_large, "fro"), 0, 1);
%!

%!test
%! opts.type = "crout";
%! [L, U] = ilu (A_tiny, opts);
Expand All @@ -431,7 +433,7 @@
%! opts.type = "crout";
%! [L, U] = ilu (A_large, opts);
%! assert (norm (A_large - L*U, "fro") / norm (A_large, "fro"), eps, eps);
%!

%!test
%! opts.type = "ilutp";
%! opts.droptol = 0;
Expand All @@ -458,34 +460,40 @@
%! assert (norm (A_large - L*U, "fro") / norm (A_large, "fro"), eps, eps);

## Specific tests for ilutp

%!shared A
%! A = sparse ([0 0 4 3 1; 5 1 2.3 2 4.5; 0 0 0 2 1;0 0 8 0 2.2; 0 0 9 9 1 ]);
%!

%!test
%! opts.udiag = 1;
%! opts.type = "ilutp";
%! opts.droptol = 0.2;
%! [L, U, P] = ilu (A, opts);
%! assert (norm (U, "fro"), 17.4577, 1e-4);
%! assert (norm (L, "fro"), 2.4192, 1e-4);
%!
%!test

%!error <encountered a pivot equal to 0>
%! opts.type = "ilutp";
%! opts.udiag = 0;
%! opts.droptol = 0.2;
%! fail ("ilu (A, opts)", "ilu: encountered a pivot equal to 0");
%! ilu (A, opts);

%!xtest
## Matlab R2017b doesn't error, but returns a singular L which isn't helpful.
%!error <encountered a pivot equal to 0>
%! A = sparse ([3 1 0 0 4; 3 1 0 0 -2;0 0 8 0 0; 0 4 0 4 -4.5; 0 -1 0 0 1]);
%! opts.type = "ilutp";
%! opts.droptol = 0;
%! opts.thresh = 0;
%! opts.milu = "row";
%! [L, U, P] = ilu (A, opts); % Matlab R2016b passes, no pivoting necessary
%! [L, U, P] = ilu (A, opts);

%!test <*53440>
%! A = sparse (magic (4));
%! opts.type = "ilutp";
%! [L, U] = ilu (A, opts);
%! assert (L * U, A, eps)

## Tests for input validation
%!shared A_tiny
%!shared A_tiny, opts
%! A_tiny = spconvert ([1 4 2 3 3 4 2 5; 1 1 2 3 4 4 5 5; 1 2 3 4 5 6 7 8]');

%!test
Expand All @@ -500,47 +508,61 @@
%! [L, U] = ilu (sparse ([]), opts);
%! assert (isempty (L));
%! assert (isempty (U));

%!error <A must be a sparse square matrix> ilu (0)
%!error <A must be a sparse square matrix> ilu ([])
%!error <zero on the diagonal> ilu (sparse (0))

%!test
%!error <invalid TYPE specified>
%! opts.type = "foo";
%! fail ("ilu (A_tiny, opts)", "invalid TYPE specified");
%! ilu (A_tiny, opts);
%!error <invalid TYPE specified>
%! opts.type = 1;
%! fail ("ilu (A_tiny, opts)", "invalid TYPE specified");
%! ilu (A_tiny, opts);
%!error <invalid TYPE specified>
%! opts.type = [];
%! fail ("ilu (A_tiny, opts)", "invalid TYPE specified");
%!test
%! ilu (A_tiny, opts);

%!error <DROPTOL must be a non-negative real scalar>
%! clear opts;
%! opts.droptol = -1;
%! fail ("ilu (A_tiny, opts)", "DROPTOL must be a non-negative real scalar");
%! ilu (A_tiny, opts);
%!error <DROPTOL must be a non-negative real scalar>
%! opts.droptol = 0.5i;
%! fail ("ilu (A_tiny, opts)", "DROPTOL must be a non-negative real scalar");
%! ilu (A_tiny, opts);
%!error <DROPTOL must be a non-negative real scalar>
%! opts.droptol = [];
%! fail ("ilu (A_tiny, opts)", "DROPTOL must be a non-negative real scalar");
%!test
%! ilu (A_tiny, opts);

%!error <MILU must be one of "off", "col", or "row">
%! clear opts;
%! opts.milu = "foo";
%! fail ("ilu (A_tiny, opts)", 'MILU must be one of "off"');
%! ilu (A_tiny, opts);
%!error <MILU must be one of "off", "col", or "row">
%! opts.milu = 1;
%! fail ("ilu (A_tiny, opts)", 'MILU must be one of "off"');
%! ilu (A_tiny, opts);
%!error <MILU must be one of "off", "col", or "row">
%! opts.milu = [];
%! fail ("ilu (A_tiny, opts)", 'MILU must be one of "off"');
%!test
%! ilu (A_tiny, opts);

%!error <UDIAG must be 0 or 1>
%! clear opts;
%! opts.udiag = -1;
%! fail ("ilu (A_tiny, opts)", "UDIAG must be 0 or 1");
%! ilu (A_tiny, opts);
%!error <UDIAG must be 0 or 1>
%! opts.udiag = 0.5i;
%! fail ("ilu (A_tiny, opts)", "UDIAG must be 0 or 1");
%! ilu (A_tiny, opts);
%!error <UDIAG must be 0 or 1>
%! opts.udiag = [];
%! fail ("ilu (A_tiny, opts)", "UDIAG must be 0 or 1");
%!test
%! ilu (A_tiny, opts);

%!error <THRESH must be a scalar in the range \[0, 1\]>
%! clear opts;
%! opts.thresh = -1;
%! fail ("ilu (A_tiny, opts)", "THRESH must be a scalar");
%! ilu (A_tiny, opts);
%!error <THRESH must be a scalar in the range \[0, 1\]>
%! opts.thresh = 0.5i;
%! fail ("ilu (A_tiny, opts)", "THRESH must be a scalar");
%! ilu (A_tiny, opts);
%!error <THRESH must be a scalar in the range \[0, 1\]>
%! opts.thresh = [];
%! fail ("ilu (A_tiny, opts)", "THRESH must be a scalar");
%!test <*53440>
%! A = sparse (magic (4));
%! opts.type = "ilutp";
%! [L, U] = ilu (A, opts);
%! assert (L * U, A, eps)
%! ilu (A_tiny, opts);

0 comments on commit f32328b

Please sign in to comment.