octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] Add tests for diagonal and permutation matrices.


From: Jaroslav Hajek
Subject: Re: [PATCH] Add tests for diagonal and permutation matrices.
Date: Mon, 9 Mar 2009 15:33:45 +0100

On Sun, Mar 8, 2009 at 9:41 PM, Jason Riedy <address@hidden> wrote:
> # HG changeset patch
> # User Jason Riedy <address@hidden>
> # Date 1236544810 14400
> # Node ID e350999f5b54d154c7cf86966fad3ddf16fa92ab
> # Parent  f5408862892fd842103d9dd5dd5086036a4532db
>
> >From dc3ff6f8665215fef2d67978dd1aa8d5f10698d2 Mon Sep 17 00:00:00 2001
> Date: Sat, 7 Mar 2009 16:13:50 -0500
> The tests aren't fancy; they should just catch simple mistakes.
>
> Signed-off-by: Jason Riedy <address@hidden>
> ---
>  Yes, there will be a series that adds some diag/sparse ops without
>  a default promotion.  This is a prelude...
>
>  test/test_diag_perm.m |  142 
> +++++++++++++++++++++++++++++++++++++++++++++++++
>  1 files changed, 142 insertions(+), 0 deletions(-)
>  create mode 100644 test/test_diag_perm.m
>
> diff --git a/test/test_diag_perm.m b/test/test_diag_perm.m
> new file mode 100644
> --- /dev/null
> +++ b/test/test_diag_perm.m
> @@ -0,0 +1,142 @@
> +## Copyright (C) 2009 E. Jason Riedy
> +##
> +## This file is part of Octave.
> +##
> +## Octave is free software; you can redistribute it and/or modify it
> +## under the terms of the GNU General Public License as published by
> +## the Free Software Foundation; either version 3 of the License, or (at
> +## your option) any later version.
> +##
> +## Octave is distributed in the hope that it will be useful, but
> +## WITHOUT ANY WARRANTY; without even the implied warranty of
> +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +## General Public License for more details.
> +##
> +## You should have received a copy of the GNU General Public License
> +## along with Octave; see the file COPYING.  If not, see
> +## <http://www.gnu.org/licenses/>.
> +
> +########################################
> +## Permutation matrices
> +
> +## row permutation
> +%!test
> +%! n = 5;
> +%! A = rand (n);
> +%! perm = randperm (n);
> +%! Prow = eye (n) (perm, :);
> +%! assert (A(perm, :), Prow * A);
> +%! invperm(perm) = 1:n;
> +%! assert (Prow \ A, A(invperm, :));
> +%! assert (Prow' * A, A(invperm, :));
> +
> +## column permutation
> +%!test
> +%! n = 7;
> +%! A = rand (n);
> +%! perm = randperm (n);
> +%! Pcol = eye (n) (:, perm);
> +%! assert (A(:, perm), A * Pcol);
> +%! invperm(perm) = 1:n;
> +%! assert (A / Pcol, A(:, invperm));
> +%! assert (A * Pcol.', A(:, invperm));
> +
> +## fall back to a matrix in addition
> +%!test
> +%! n = 4;
> +%! P1 = eye (n) (:, randperm (n));
> +%! A = zeros (n) + P1;
> +%! assert (sum (A), ones (1, n));
> +%! assert (sum (A, 2), ones (n, 1));
> +
> +## preserve dense matrix structure
> +%!test
> +%! n = 7;
> +%! Pc = eye (n) (:, randperm (n));
> +%! Pr = eye (n) (randperm (n), :);
> +%! assert (typeinfo (rand (n) * Pc), "matrix");
> +%! assert (typeinfo (Pr * rand (n)), "matrix");
> +
> +## permuting a matrix with exceptional values does not introduce new ones.
> +%!test
> +%! n = 5;
> +%! pc = randperm (n);
> +%! Pc = eye (n) (:, pc);
> +%! pr = randperm (n);
> +%! Pr = eye (n) (pr, :);
> +%! A = rand (n);
> +%! A(n, n-2) = NaN;
> +%! A(3, 1) = Inf;
> +%! assert (Pr * A * Pc, A(pr, pc));
> +
> +########################################
> +## Diagonal matrices
> +
> +## square row scaling
> +%!test
> +%! m = 7;
> +%! n = 11;
> +%! A = rand (m, n);
> +%! scalefact = rand (m, 1);
> +%! Dr = diag (scalefact);
> +%! assert (Dr * A, repmat (scalefact, 1, n) .* A);
> +%! assert (Dr \ A, A ./ repmat (scalefact, 1, n));
> +%! scalefact(m-1) = Inf;
> +%! Dr(m-1, m-1) = 0;
> +%! assert (Dr \ A, A ./ repmat (scalefact, 1, n));
> +
> +## square column scaling
> +%!test
> +%! m = 13;
> +%! n = 11;
> +%! A = rand (m, n);
> +%! scalefact = rand (1, n);
> +%! Dc = diag (scalefact);
> +%! assert (A * Dc, repmat (scalefact, m, 1) .* A);
> +%! assert (A / Dc, A ./ repmat (scalefact, m, 1));
> +%! scalefact(n-1) = Inf;
> +%! Dc(n-1, n-1) = 0;
> +%! assert (A / Dc, A ./ repmat (scalefact, m, 1));
> +
> +## arithmetic
> +%!test
> +%! m = 9;
> +%! n = 7;
> +%! mn = min (m, n);
> +%! d1 = rand (mn, 1) + I () * rand (mn, 1);
> +%! D1 = diag (d1, m, n);
> +%! d2 = rand (mn, 1);
> +%! D2 = diag (d2, m, n);
> +%! D1D2 = D1 + D2;
> +%! assert (typeinfo (D1D2), "complex diagonal matrix");
> +%! assert (diag (D1D2), d1 + d2);
> +%! D1D2 = D2.' * D1;
> +%! assert (typeinfo (D1D2), "complex diagonal matrix");
> +%! assert (diag (D1D2), d1 .* d2);
> +
> +## slicing
> +%!test
> +%! m = 13;
> +%! n = 6;
> +%! mn = min (m, n);
> +%! d = rand (mn, 1);
> +%! D = diag (d, m, n);
> +%! Dslice = D (1:(m-3), 1:(n-2));
> +%! assert (typeinfo (Dslice), "diagonal matrix");
> +
> +## preserve dense matrix structure
> +%!assert (typeinfo (rand (8) * (3 * eye (8))), "matrix");
> +%!assert (typeinfo ((3 * eye (8)) * rand (8)), "matrix");
> +
> +## scaling a matrix with exceptional values does not introduce new ones.
> +%!test
> +%! n = 6;
> +%! dr = rand (n, 1);
> +%! Dr = diag (dr);
> +%! dc = rand (1, n);
> +%! Dc = diag (dc);
> +%! A = rand (n);
> +%! A(n, n-2) = NaN;
> +%! A(4, 1) = Inf;
> +%! assert (Dr * A * Dc, A .* kron (dr, dc), eps);
> +
>

I applied this patch.

Thanks

-- 
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz



reply via email to

[Prev in Thread] Current Thread [Next in Thread]