octave-maintainers
[Top][All Lists]
Advanced

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

Tests for new features in 2.9.x


From: John W. Eaton
Subject: Tests for new features in 2.9.x
Date: Wed, 30 Mar 2005 12:58:14 -0500

On 30-Mar-2005, Quentin Spencer <address@hidden> wrote:

| I'm working on making RPM packages of umfpack and glpk. I don't really 
| use LP or sparse stuff myself, so I was wondering if anyone can suggest 
| some tests to help me verify that octave 2.9.1 is linking to the new 
| libraries and they are working properly.

For glpk, look at the two test scripts in the scripts/optimization
directory (not .m because I did not want them to be installed with the
other .m files).

For the sparse code, see the attached file.  You'll need the test code
from octave-forge to run the tests.  I think splu uses umfpack.

jwe

## THIS IS AN AUTOMATICALLY GENERATED FILE --- DO NOT EDIT ---
## instead modify buildtests.sh to generate the tests that you want.
##
## sptest
##
##    run preset sparse tests.  All should pass.
function [passes,tests] = sptest
  disp('writing test output to sptest.log');
  test('sptest','normal','sptest.log');
endfunction


# ==============================================================


# ==============================================================


%!test # segfault test from address@hidden
%! n = 510;
%! sparse(kron((1:n)', ones(n,1)), kron(ones(n,1), (1:n)'), ones(n)); 

%% segfault tests from address@hidden
%!assert(spinv(sparse([1,1;1,1+i])),sparse([1-1i,1i;1i,-1i]),10*eps);
%!error spinv( sparse( [1,1;1,1]   ) );
%!error spinv( sparse( [0,0;0,1]   ) );
%!error spinv( sparse( [0,0;0,1+i] ) );
%!error spinv( sparse( [0,0;0,0]   ) );

%% error handling in constructor
%!error sparse(1,[2,3],[1,2,3]);
%!error sparse([1,1],[1,1],[1,2],3,3,'bogus');
%!error sparse([1,3],[1,-4],[3,5],2,2);
%!error sparse([1,3],[1,-4],[3,5i],2,2);
%!error sparse(-1,-1,1);

# ==============================================================

%!shared bf
%!test bf=realmin;
%% Make sure newly introduced zeros get eaten
%!assert(nnz(sparse([bf,bf,1]).^realmax),1);
%!assert(nnz(sparse([1,bf,bf]).^realmax),1);
%!assert(nnz(sparse([bf,bf,bf]).^realmax),0); 

%!assert(nnz(sparse([bf;bf;1]).^realmax),1);
%!assert(nnz(sparse([1;bf;bf]).^realmax),1);
%!assert(nnz(sparse([0.5;bf;bf]).^realmax),0);

%!assert(nnz(sparse([bf,bf,1])*realmin),1);
%!assert(nnz(sparse([1,bf,bf])*realmin),1);
%!assert(nnz(sparse([bf,bf,bf])*realmin),0);

%!assert(nnz(sparse([bf;bf;1])*realmin),1);
%!assert(nnz(sparse([1;bf;bf])*realmin),1);
%!assert(nnz(sparse([bf;bf;bf])*realmin),0);

%!test bf=realmin+realmin*1i;
%% Make sure newly introduced zeros get eaten
%!assert(nnz(sparse([bf,bf,1]).^realmax),1);
%!assert(nnz(sparse([1,bf,bf]).^realmax),1);
%!assert(nnz(sparse([bf,bf,bf]).^realmax),0); 

%!assert(nnz(sparse([bf;bf;1]).^realmax),1);
%!assert(nnz(sparse([1;bf;bf]).^realmax),1);
%!assert(nnz(sparse([0.5;bf;bf]).^realmax),0);

%!assert(nnz(sparse([bf,bf,1])*realmin),1);
%!assert(nnz(sparse([1,bf,bf])*realmin),1);
%!assert(nnz(sparse([bf,bf,bf])*realmin),0);

%!assert(nnz(sparse([bf;bf;1])*realmin),1);
%!assert(nnz(sparse([1;bf;bf])*realmin),1);
%!assert(nnz(sparse([bf;bf;bf])*realmin),0);

%!assert(nnz(sparse([-1,realmin,realmin]).^1.5),1);
%!assert(nnz(sparse([-1,realmin,realmin,1]).^1.5),2);

%!assert(nnz(sparse(1,1,0)),0); # Make sure scalar v==0 doesn't confuse matters
%!assert(nnz(sparse(eye(3))*0),0);
%!assert(nnz(sparse(eye(3))-sparse(eye(3))),0);

%!test
%! warn_divide_by_zero=0;
%! assert(sparse(eye(3))/0,sparse(eye(3)/0,1));


# ==============================================================

%!shared as,af,bs,bf
%!test af=[1+1i,2-1i,0,0;0,0,0,3+2i;0,0,0,4];
%!test bf=3;
%!test as=sparse(af);
%!test bs=bf;
%% Elementwise binary tests (uses as,af,bs,bf,scalar)
%!assert(as==bs,sparse(af==bf,true))
%!assert(bf==as,sparse(bf==af,true))

%!assert(as!=bf,sparse(af!=bf,true))
%!assert(bf!=as,sparse(bf!=af,true))

%!assert(as+bf,af+bf)
%!assert(bf+as,bf+af)

%!assert(as-bf,af-bf)
%!assert(bf-as,bf-af)

%!assert(as.*bf,sparse(af.*bf,true))
%!assert(bf.*as,sparse(bf.*af,true))

%!assert(as./bf,sparse(af./bf,true),100*eps)
%!assert(bf.\as,sparse(bf.\af,true),100*eps)

%!test
%! sv = as.^bf;
%! fv = af.^bf;
%! idx = find(af~=0);
%! assert(sv(:)(idx),sparse(fv(:)(idx),true),100*eps)

%% real values can be ordered (uses as,af)
%!assert(as<=bf,sparse(af<=bf,true))
%!assert(bf<=as,sparse(bf<=af,true))

%!assert(as>=bf,sparse(af>=bf,true))
%!assert(bf>=as,sparse(bf>=af,true))

%!assert(as<bf,sparse(af<bf,true))
%!assert(bf<as,sparse(bf<af,true))

%!assert(as>bf,sparse(af>bf,true))
%!assert(bf>as,sparse(bf>af,true))

%!test bf=bf+1i;
%!test bs=bf;
%% Elementwise binary tests (uses as,af,bs,bf,scalar)
%!assert(as==bs,sparse(af==bf,true))
%!assert(bf==as,sparse(bf==af,true))

%!assert(as!=bf,sparse(af!=bf,true))
%!assert(bf!=as,sparse(bf!=af,true))

%!assert(as+bf,af+bf)
%!assert(bf+as,bf+af)

%!assert(as-bf,af-bf)
%!assert(bf-as,bf-af)

%!assert(as.*bf,sparse(af.*bf,true))
%!assert(bf.*as,sparse(bf.*af,true))

%!assert(as./bf,sparse(af./bf,true),100*eps)
%!assert(bf.\as,sparse(bf.\af,true),100*eps)

%!test
%! sv = as.^bf;
%! fv = af.^bf;
%! idx = find(af~=0);
%! assert(sv(:)(idx),sparse(fv(:)(idx),true),100*eps)


# ==============================================================

%!test af=[1+1i,2-1i,0,0;0,0,0,3+2i;0,0,0,4];
%!test bf=[0,1-1i,0,0;2+1i,0,0,0;3-1i,2+3i,0,0];
%!test as=sparse(af);
%!test bs=sparse(bf);
%% Unary matrix tests (uses af,as)
%!assert(issparse(as))
%!assert(!issparse(af))
%!assert(!(issparse(af)&&iscomplex(af)))
%!assert(!(issparse(af)&&isreal(af)))
%!assert(spsum(as),sparse(sum(af),true))
%!assert(spsum(as,1),sparse(sum(af,1),true))
%!assert(spsum(as,2),sparse(sum(af,2),true))
%!assert(spcumsum(as),sparse(cumsum(af),true))
%!assert(spcumsum(as,1),sparse(cumsum(af,1),true))
%!assert(spcumsum(as,2),sparse(cumsum(af,2),true))
%!assert(spsumsq(as),sparse(sumsq(af),true))
%!assert(spsumsq(as,1),sparse(sumsq(af,1),true))
%!assert(spsumsq(as,2),sparse(sumsq(af,2),true))
%!assert(spprod(as),sparse(prod(af),true))
%!assert(spprod(as,1),sparse(prod(af,1),true))
%!assert(spprod(as,2),sparse(prod(af,2),true))
%!assert(spcumprod(as),sparse(cumprod(af),true))
%!assert(spcumprod(as,1),sparse(cumprod(af,1),true))
%!assert(spcumprod(as,2),sparse(cumprod(af,2),true))

%!assert(spmin(as),sparse(min(af),true))
%!assert(spmin(as(:)),min(af(:)))
%!assert(spmin(as,[],1),sparse(min(af,[],1),true))
%!assert(spmin(as,[],2),sparse(min(af,[],2),true))
%!assert(spmin(as,[],1),sparse(min(af,[],1),true))
%!assert(spmin(as,0),sparse(min(af,0),true))
%!assert(spmin(as,bs),sparse(min(af,bf),true))
%!assert(spmax(as),sparse(max(af),true))
%!assert(spmax(as(:)),max(af(:)))
%!assert(spmax(as,[],1),sparse(max(af,[],1),true))
%!assert(spmax(as,[],2),sparse(max(af,[],2),true))
%!assert(spmax(as,[],1),sparse(max(af,[],1),true))
%!assert(spmax(as,0),sparse(max(af,0),true))
%!assert(spmax(as,bs),sparse(max(af,bf),true))

%!assert(as==as)
%!assert(as==af)
%!assert(af==as)
%!test
%! [ii,jj,vv,nr,nc] = spfind(as);
%! assert(af,full(sparse(ii,jj,vv,nr,nc)));
%!assert(nnz(as),sum(af(:)!=0))
%!assert(nnz(as),nnz(af))
%!assert(issparse(as.'))
%!assert(issparse(as'))
%!assert(issparse(-as))
%!assert(~as,sparse(~af,true))
%!assert(as.', sparse(af.',true));
%!assert(as',  sparse(af',true));
%!assert(-as, sparse(-af,true));
%!assert(~as, sparse(~af,true));
%!error [i,j]=size(af);as(i-1,j+1);
%!error [i,j]=size(af);as(i+1,j-1);
%!test
%! [Is,Js,Vs] = spfind(as);
%! [If,Jf,Vf] = find(af);
%! assert(Is,If);
%! assert(Js,Jf);
%! assert(Vs,Vf);
%!error as(0,1);
%!error as(1,0);
%!assert(spfind(as),find(af))
%!test
%! [i,j,v] = spfind(as);
%! [m,n] = size(as);
%! x = sparse(i,j,v,m,n);
%! assert(x,as);
%!test
%! [i,j,v,m,n] = spfind(as);
%! x = sparse(i,j,v,m,n);
%! assert(x,as);
%!assert(issparse(horzcat(as,as)));
%!assert(issparse(vertcat(as,as)));
%!assert(issparse(cat(1,as,as)));
%!assert(issparse(cat(2,as,as)));
%!assert(issparse([as,as]));
%!assert(issparse([as;as]));
%!assert(horzcat(as,as), sparse([af,af]));
%!assert(vertcat(as,as), sparse([af;af]));
%!assert(horzcat(as,as,as), sparse([af,af,af]));
%!assert(vertcat(as,as,as), sparse([af;af;af]));
%!assert([as,as], sparse([af,af]));
%!assert([as;as], sparse([af;af]));
%!assert([as,as,as], sparse([af,af,af]));
%!assert([as;as;as], sparse([af;af;af]));
%!assert(cat(2,as,as), sparse([af,af]));
%!assert(cat(1,as,as), sparse([af;af]));
%!assert(cat(2,as,as,as), sparse([af,af,af]));
%!assert(cat(1,as,as,as), sparse([af;af;af]));
%!assert(issparse([as,af]));
%!assert(issparse([af,as]));
%!assert([as,af], sparse([af,af]));
%!assert([as;af], sparse([af;af]));

%% Elementwise binary tests (uses as,af,bs,bf,scalar)
%!assert(as==bs,sparse(af==bf,true))
%!assert(bf==as,sparse(bf==af,true))

%!assert(as!=bf,sparse(af!=bf,true))
%!assert(bf!=as,sparse(bf!=af,true))

%!assert(as+bf,af+bf)
%!assert(bf+as,bf+af)

%!assert(as-bf,af-bf)
%!assert(bf-as,bf-af)

%!assert(as.*bf,sparse(af.*bf,true))
%!assert(bf.*as,sparse(bf.*af,true))

%!assert(as./bf,sparse(af./bf,true),100*eps)
%!assert(bf.\as,sparse(bf.\af,true),100*eps)

%!test
%! sv = as.^bf;
%! fv = af.^bf;
%! idx = find(af~=0);
%! assert(sv(:)(idx),sparse(fv(:)(idx),true),100*eps)

%!assert(as==bs,sparse(af==bf,true))
%!assert(as!=bs,sparse(af!=bf,true))
%!assert(as+bs,sparse(af+bf,true))
%!assert(as-bs,sparse(af-bf,true))
%!assert(as.*bs,sparse(af.*bf,true))
%!assert(as./bs,sparse(af./bf,true),100*eps);
%!test
%! sv = as.^bs;
%! fv = af.^bf;
%! idx = find(af~=0);
%! assert(sv(:)(idx),sparse(fv(:)(idx),true),100*eps)

%% Matrix-matrix operators (uses af,as,bs,bf)
%!assert(as*bf',af*bf')
%!assert(af*bs',af*bf')
%!assert(as*bs',sparse(af*bf',true))

%% Matrix diagonal tests (uses af,as,bf,bs)
%!assert(spdiag(as),sparse(diag(af),true))
%!assert(spdiag(bs),sparse(diag(bf),true))
%!assert(spdiag(as,1),sparse(diag(af,1),true))
%!assert(spdiag(bs,1),sparse(diag(bf,1),true))
%!assert(spdiag(as,-1),sparse(diag(af,-1),true))
%!assert(spdiag(bs,-1),sparse(diag(bf,-1),true))
%!assert(spdiag(as(:)),sparse(diag(af(:)),true))
%!assert(spdiag(as(:),1),sparse(diag(af(:),1),true))
%!assert(spdiag(as(:),-1),sparse(diag(af(:),-1),true))
%!assert(spdiag(as(:)'),sparse(diag(af(:)'),true))
%!assert(spdiag(as(:)',1),sparse(diag(af(:)',1),true))
%!assert(spdiag(as(:)',-1),sparse(diag(af(:)',-1),true))
%!assert(spdiags(as,[0,1]),[diag(af,0),diag(af,1)])
%!test [tb,tc]=spdiags(as); 
%! assert(spdiags(tb,tc,sparse(zeros(size(as)))),as)
%! assert(spdiags(tb,tc,size(as,1),size(as,2)),as)

%% Matrix diagonal tests (uses af,as,bf,bs)
%!assert(reshape(as,1,prod(size(as))),sparse(reshape(af,1,prod(size(af))),true))
%!assert(reshape(as,prod(size(as)),1),sparse(reshape(af,prod(size(af)),1),true))
%!assert(reshape(as,fliplr(size(as))),sparse(reshape(af,fliplr(size(af))),true))
%!assert(reshape(bs,1,prod(size(as))),sparse(reshape(bf,1,prod(size(af))),true))
%!assert(reshape(bs,prod(size(as)),1),sparse(reshape(bf,prod(size(af)),1),true))
%!assert(reshape(bs,fliplr(size(as))),sparse(reshape(bf,fliplr(size(af))),true))


# ==============================================================

%!test # save ascii
%! savefile= tmpnam();
%! mark_for_deletion( savefile );
%! as_save=as; save(savefile,'bf','as_save','af');
%! clear as_save;
%! load(savefile,'as_save');
%! assert(as_save,sparse(af));
%!test # save binary
%! savefile= tmpnam();
%! mark_for_deletion( savefile );
%! as_save=as; save('-binary',savefile,'bf','as_save','af');
%! clear as_save;
%! load(savefile,'as_save');
%! assert(as_save,sparse(af));
%!test # save hdf5
%! savefile= tmpnam();
%! mark_for_deletion( savefile );
%! as_save=as; save('-hdf5',savefile,'bf','as_save','af');
%! clear as_save;
%! load(savefile,'as_save');
%! assert(as_save,sparse(af));
%!test bf=real(bf);
%!test as=sparse(af);
%!test bs=sparse(bf);
%% Unary matrix tests (uses af,as)
%!assert(issparse(as))
%!assert(!issparse(af))
%!assert(!(issparse(af)&&iscomplex(af)))
%!assert(!(issparse(af)&&isreal(af)))
%!assert(spsum(as),sparse(sum(af),true))
%!assert(spsum(as,1),sparse(sum(af,1),true))
%!assert(spsum(as,2),sparse(sum(af,2),true))
%!assert(spcumsum(as),sparse(cumsum(af),true))
%!assert(spcumsum(as,1),sparse(cumsum(af,1),true))
%!assert(spcumsum(as,2),sparse(cumsum(af,2),true))
%!assert(spsumsq(as),sparse(sumsq(af),true))
%!assert(spsumsq(as,1),sparse(sumsq(af,1),true))
%!assert(spsumsq(as,2),sparse(sumsq(af,2),true))
%!assert(spprod(as),sparse(prod(af),true))
%!assert(spprod(as,1),sparse(prod(af,1),true))
%!assert(spprod(as,2),sparse(prod(af,2),true))
%!assert(spcumprod(as),sparse(cumprod(af),true))
%!assert(spcumprod(as,1),sparse(cumprod(af,1),true))
%!assert(spcumprod(as,2),sparse(cumprod(af,2),true))

%!assert(spmin(as),sparse(min(af),true))
%!assert(spmin(as(:)),min(af(:)))
%!assert(spmin(as,[],1),sparse(min(af,[],1),true))
%!assert(spmin(as,[],2),sparse(min(af,[],2),true))
%!assert(spmin(as,[],1),sparse(min(af,[],1),true))
%!assert(spmin(as,0),sparse(min(af,0),true))
%!assert(spmin(as,bs),sparse(min(af,bf),true))
%!assert(spmax(as),sparse(max(af),true))
%!assert(spmax(as(:)),max(af(:)))
%!assert(spmax(as,[],1),sparse(max(af,[],1),true))
%!assert(spmax(as,[],2),sparse(max(af,[],2),true))
%!assert(spmax(as,[],1),sparse(max(af,[],1),true))
%!assert(spmax(as,0),sparse(max(af,0),true))
%!assert(spmax(as,bs),sparse(max(af,bf),true))

%!assert(as==as)
%!assert(as==af)
%!assert(af==as)
%!test
%! [ii,jj,vv,nr,nc] = spfind(as);
%! assert(af,full(sparse(ii,jj,vv,nr,nc)));
%!assert(nnz(as),sum(af(:)!=0))
%!assert(nnz(as),nnz(af))
%!assert(issparse(as.'))
%!assert(issparse(as'))
%!assert(issparse(-as))
%!assert(~as,sparse(~af,true))
%!assert(as.', sparse(af.',true));
%!assert(as',  sparse(af',true));
%!assert(-as, sparse(-af,true));
%!assert(~as, sparse(~af,true));
%!error [i,j]=size(af);as(i-1,j+1);
%!error [i,j]=size(af);as(i+1,j-1);
%!test
%! [Is,Js,Vs] = spfind(as);
%! [If,Jf,Vf] = find(af);
%! assert(Is,If);
%! assert(Js,Jf);
%! assert(Vs,Vf);
%!error as(0,1);
%!error as(1,0);
%!assert(spfind(as),find(af))
%!test
%! [i,j,v] = spfind(as);
%! [m,n] = size(as);
%! x = sparse(i,j,v,m,n);
%! assert(x,as);
%!test
%! [i,j,v,m,n] = spfind(as);
%! x = sparse(i,j,v,m,n);
%! assert(x,as);
%!assert(issparse(horzcat(as,as)));
%!assert(issparse(vertcat(as,as)));
%!assert(issparse(cat(1,as,as)));
%!assert(issparse(cat(2,as,as)));
%!assert(issparse([as,as]));
%!assert(issparse([as;as]));
%!assert(horzcat(as,as), sparse([af,af]));
%!assert(vertcat(as,as), sparse([af;af]));
%!assert(horzcat(as,as,as), sparse([af,af,af]));
%!assert(vertcat(as,as,as), sparse([af;af;af]));
%!assert([as,as], sparse([af,af]));
%!assert([as;as], sparse([af;af]));
%!assert([as,as,as], sparse([af,af,af]));
%!assert([as;as;as], sparse([af;af;af]));
%!assert(cat(2,as,as), sparse([af,af]));
%!assert(cat(1,as,as), sparse([af;af]));
%!assert(cat(2,as,as,as), sparse([af,af,af]));
%!assert(cat(1,as,as,as), sparse([af;af;af]));
%!assert(issparse([as,af]));
%!assert(issparse([af,as]));
%!assert([as,af], sparse([af,af]));
%!assert([as;af], sparse([af;af]));

%% Elementwise binary tests (uses as,af,bs,bf,scalar)
%!assert(as==bs,sparse(af==bf,true))
%!assert(bf==as,sparse(bf==af,true))

%!assert(as!=bf,sparse(af!=bf,true))
%!assert(bf!=as,sparse(bf!=af,true))

%!assert(as+bf,af+bf)
%!assert(bf+as,bf+af)

%!assert(as-bf,af-bf)
%!assert(bf-as,bf-af)

%!assert(as.*bf,sparse(af.*bf,true))
%!assert(bf.*as,sparse(bf.*af,true))

%!assert(as./bf,sparse(af./bf,true),100*eps)
%!assert(bf.\as,sparse(bf.\af,true),100*eps)

%!test
%! sv = as.^bf;
%! fv = af.^bf;
%! idx = find(af~=0);
%! assert(sv(:)(idx),sparse(fv(:)(idx),true),100*eps)

%!assert(as==bs,sparse(af==bf,true))
%!assert(as!=bs,sparse(af!=bf,true))
%!assert(as+bs,sparse(af+bf,true))
%!assert(as-bs,sparse(af-bf,true))
%!assert(as.*bs,sparse(af.*bf,true))
%!assert(as./bs,sparse(af./bf,true),100*eps);
%!test
%! sv = as.^bs;
%! fv = af.^bf;
%! idx = find(af~=0);
%! assert(sv(:)(idx),sparse(fv(:)(idx),true),100*eps)

%% Matrix-matrix operators (uses af,as,bs,bf)
%!assert(as*bf',af*bf')
%!assert(af*bs',af*bf')
%!assert(as*bs',sparse(af*bf',true))

%% Matrix diagonal tests (uses af,as,bf,bs)
%!assert(spdiag(as),sparse(diag(af),true))
%!assert(spdiag(bs),sparse(diag(bf),true))
%!assert(spdiag(as,1),sparse(diag(af,1),true))
%!assert(spdiag(bs,1),sparse(diag(bf,1),true))
%!assert(spdiag(as,-1),sparse(diag(af,-1),true))
%!assert(spdiag(bs,-1),sparse(diag(bf,-1),true))
%!assert(spdiag(as(:)),sparse(diag(af(:)),true))
%!assert(spdiag(as(:),1),sparse(diag(af(:),1),true))
%!assert(spdiag(as(:),-1),sparse(diag(af(:),-1),true))
%!assert(spdiag(as(:)'),sparse(diag(af(:)'),true))
%!assert(spdiag(as(:)',1),sparse(diag(af(:)',1),true))
%!assert(spdiag(as(:)',-1),sparse(diag(af(:)',-1),true))
%!assert(spdiags(as,[0,1]),[diag(af,0),diag(af,1)])
%!test [tb,tc]=spdiags(as); 
%! assert(spdiags(tb,tc,sparse(zeros(size(as)))),as)
%! assert(spdiags(tb,tc,size(as,1),size(as,2)),as)

%% Matrix diagonal tests (uses af,as,bf,bs)
%!assert(reshape(as,1,prod(size(as))),sparse(reshape(af,1,prod(size(af))),true))
%!assert(reshape(as,prod(size(as)),1),sparse(reshape(af,prod(size(af)),1),true))
%!assert(reshape(as,fliplr(size(as))),sparse(reshape(af,fliplr(size(af))),true))
%!assert(reshape(bs,1,prod(size(as))),sparse(reshape(bf,1,prod(size(af))),true))
%!assert(reshape(bs,prod(size(as)),1),sparse(reshape(bf,prod(size(af)),1),true))
%!assert(reshape(bs,fliplr(size(as))),sparse(reshape(bf,fliplr(size(af))),true))

%!assert(as<=bs,sparse(af<=bf,true))
%!assert(as>=bs,sparse(af>=bf,true))
%!assert(as<bs,sparse(af<bf,true))
%!assert(as>bs,sparse(af>bf,true))

# ==============================================================

%!test af=real(af);
%!test as=sparse(af);
%!test bs=sparse(bf);
%% Unary matrix tests (uses af,as)
%!assert(issparse(as))
%!assert(!issparse(af))
%!assert(!(issparse(af)&&iscomplex(af)))
%!assert(!(issparse(af)&&isreal(af)))
%!assert(spsum(as),sparse(sum(af),true))
%!assert(spsum(as,1),sparse(sum(af,1),true))
%!assert(spsum(as,2),sparse(sum(af,2),true))
%!assert(spcumsum(as),sparse(cumsum(af),true))
%!assert(spcumsum(as,1),sparse(cumsum(af,1),true))
%!assert(spcumsum(as,2),sparse(cumsum(af,2),true))
%!assert(spsumsq(as),sparse(sumsq(af),true))
%!assert(spsumsq(as,1),sparse(sumsq(af,1),true))
%!assert(spsumsq(as,2),sparse(sumsq(af,2),true))
%!assert(spprod(as),sparse(prod(af),true))
%!assert(spprod(as,1),sparse(prod(af,1),true))
%!assert(spprod(as,2),sparse(prod(af,2),true))
%!assert(spcumprod(as),sparse(cumprod(af),true))
%!assert(spcumprod(as,1),sparse(cumprod(af,1),true))
%!assert(spcumprod(as,2),sparse(cumprod(af,2),true))

%!assert(spmin(as),sparse(min(af),true))
%!assert(spmin(as(:)),min(af(:)))
%!assert(spmin(as,[],1),sparse(min(af,[],1),true))
%!assert(spmin(as,[],2),sparse(min(af,[],2),true))
%!assert(spmin(as,[],1),sparse(min(af,[],1),true))
%!assert(spmin(as,0),sparse(min(af,0),true))
%!assert(spmin(as,bs),sparse(min(af,bf),true))
%!assert(spmax(as),sparse(max(af),true))
%!assert(spmax(as(:)),max(af(:)))
%!assert(spmax(as,[],1),sparse(max(af,[],1),true))
%!assert(spmax(as,[],2),sparse(max(af,[],2),true))
%!assert(spmax(as,[],1),sparse(max(af,[],1),true))
%!assert(spmax(as,0),sparse(max(af,0),true))
%!assert(spmax(as,bs),sparse(max(af,bf),true))

%!assert(as==as)
%!assert(as==af)
%!assert(af==as)
%!test
%! [ii,jj,vv,nr,nc] = spfind(as);
%! assert(af,full(sparse(ii,jj,vv,nr,nc)));
%!assert(nnz(as),sum(af(:)!=0))
%!assert(nnz(as),nnz(af))
%!assert(issparse(as.'))
%!assert(issparse(as'))
%!assert(issparse(-as))
%!assert(~as,sparse(~af,true))
%!assert(as.', sparse(af.',true));
%!assert(as',  sparse(af',true));
%!assert(-as, sparse(-af,true));
%!assert(~as, sparse(~af,true));
%!error [i,j]=size(af);as(i-1,j+1);
%!error [i,j]=size(af);as(i+1,j-1);
%!test
%! [Is,Js,Vs] = spfind(as);
%! [If,Jf,Vf] = find(af);
%! assert(Is,If);
%! assert(Js,Jf);
%! assert(Vs,Vf);
%!error as(0,1);
%!error as(1,0);
%!assert(spfind(as),find(af))
%!test
%! [i,j,v] = spfind(as);
%! [m,n] = size(as);
%! x = sparse(i,j,v,m,n);
%! assert(x,as);
%!test
%! [i,j,v,m,n] = spfind(as);
%! x = sparse(i,j,v,m,n);
%! assert(x,as);
%!assert(issparse(horzcat(as,as)));
%!assert(issparse(vertcat(as,as)));
%!assert(issparse(cat(1,as,as)));
%!assert(issparse(cat(2,as,as)));
%!assert(issparse([as,as]));
%!assert(issparse([as;as]));
%!assert(horzcat(as,as), sparse([af,af]));
%!assert(vertcat(as,as), sparse([af;af]));
%!assert(horzcat(as,as,as), sparse([af,af,af]));
%!assert(vertcat(as,as,as), sparse([af;af;af]));
%!assert([as,as], sparse([af,af]));
%!assert([as;as], sparse([af;af]));
%!assert([as,as,as], sparse([af,af,af]));
%!assert([as;as;as], sparse([af;af;af]));
%!assert(cat(2,as,as), sparse([af,af]));
%!assert(cat(1,as,as), sparse([af;af]));
%!assert(cat(2,as,as,as), sparse([af,af,af]));
%!assert(cat(1,as,as,as), sparse([af;af;af]));
%!assert(issparse([as,af]));
%!assert(issparse([af,as]));
%!assert([as,af], sparse([af,af]));
%!assert([as;af], sparse([af;af]));

%% Elementwise binary tests (uses as,af,bs,bf,scalar)
%!assert(as==bs,sparse(af==bf,true))
%!assert(bf==as,sparse(bf==af,true))

%!assert(as!=bf,sparse(af!=bf,true))
%!assert(bf!=as,sparse(bf!=af,true))

%!assert(as+bf,af+bf)
%!assert(bf+as,bf+af)

%!assert(as-bf,af-bf)
%!assert(bf-as,bf-af)

%!assert(as.*bf,sparse(af.*bf,true))
%!assert(bf.*as,sparse(bf.*af,true))

%!assert(as./bf,sparse(af./bf,true),100*eps)
%!assert(bf.\as,sparse(bf.\af,true),100*eps)

%!test
%! sv = as.^bf;
%! fv = af.^bf;
%! idx = find(af~=0);
%! assert(sv(:)(idx),sparse(fv(:)(idx),true),100*eps)

%!assert(as==bs,sparse(af==bf,true))
%!assert(as!=bs,sparse(af!=bf,true))
%!assert(as+bs,sparse(af+bf,true))
%!assert(as-bs,sparse(af-bf,true))
%!assert(as.*bs,sparse(af.*bf,true))
%!assert(as./bs,sparse(af./bf,true),100*eps);
%!test
%! sv = as.^bs;
%! fv = af.^bf;
%! idx = find(af~=0);
%! assert(sv(:)(idx),sparse(fv(:)(idx),true),100*eps)

%% Matrix-matrix operators (uses af,as,bs,bf)
%!assert(as*bf',af*bf')
%!assert(af*bs',af*bf')
%!assert(as*bs',sparse(af*bf',true))

%% Matrix diagonal tests (uses af,as,bf,bs)
%!assert(spdiag(as),sparse(diag(af),true))
%!assert(spdiag(bs),sparse(diag(bf),true))
%!assert(spdiag(as,1),sparse(diag(af,1),true))
%!assert(spdiag(bs,1),sparse(diag(bf,1),true))
%!assert(spdiag(as,-1),sparse(diag(af,-1),true))
%!assert(spdiag(bs,-1),sparse(diag(bf,-1),true))
%!assert(spdiag(as(:)),sparse(diag(af(:)),true))
%!assert(spdiag(as(:),1),sparse(diag(af(:),1),true))
%!assert(spdiag(as(:),-1),sparse(diag(af(:),-1),true))
%!assert(spdiag(as(:)'),sparse(diag(af(:)'),true))
%!assert(spdiag(as(:)',1),sparse(diag(af(:)',1),true))
%!assert(spdiag(as(:)',-1),sparse(diag(af(:)',-1),true))
%!assert(spdiags(as,[0,1]),[diag(af,0),diag(af,1)])
%!test [tb,tc]=spdiags(as); 
%! assert(spdiags(tb,tc,sparse(zeros(size(as)))),as)
%! assert(spdiags(tb,tc,size(as,1),size(as,2)),as)

%% Matrix diagonal tests (uses af,as,bf,bs)
%!assert(reshape(as,1,prod(size(as))),sparse(reshape(af,1,prod(size(af))),true))
%!assert(reshape(as,prod(size(as)),1),sparse(reshape(af,prod(size(af)),1),true))
%!assert(reshape(as,fliplr(size(as))),sparse(reshape(af,fliplr(size(af))),true))
%!assert(reshape(bs,1,prod(size(as))),sparse(reshape(bf,1,prod(size(af))),true))
%!assert(reshape(bs,prod(size(as)),1),sparse(reshape(bf,prod(size(af)),1),true))
%!assert(reshape(bs,fliplr(size(as))),sparse(reshape(bf,fliplr(size(af))),true))


# ==============================================================

%!test # save ascii
%! savefile= tmpnam();
%! mark_for_deletion( savefile );
%! as_save=as; save(savefile,'bf','as_save','af');
%! clear as_save;
%! load(savefile,'as_save');
%! assert(as_save,sparse(af));
%!test # save binary
%! savefile= tmpnam();
%! mark_for_deletion( savefile );
%! as_save=as; save('-binary',savefile,'bf','as_save','af');
%! clear as_save;
%! load(savefile,'as_save');
%! assert(as_save,sparse(af));
%!test # save hdf5
%! savefile= tmpnam();
%! mark_for_deletion( savefile );
%! as_save=as; save('-hdf5',savefile,'bf','as_save','af');
%! clear as_save;
%! load(savefile,'as_save');
%! assert(as_save,sparse(af));
%!test bf=bf+1i*(bf~=0);
%!test as=sparse(af);
%!test bs=sparse(bf);
%% Unary matrix tests (uses af,as)
%!assert(issparse(as))
%!assert(!issparse(af))
%!assert(!(issparse(af)&&iscomplex(af)))
%!assert(!(issparse(af)&&isreal(af)))
%!assert(spsum(as),sparse(sum(af),true))
%!assert(spsum(as,1),sparse(sum(af,1),true))
%!assert(spsum(as,2),sparse(sum(af,2),true))
%!assert(spcumsum(as),sparse(cumsum(af),true))
%!assert(spcumsum(as,1),sparse(cumsum(af,1),true))
%!assert(spcumsum(as,2),sparse(cumsum(af,2),true))
%!assert(spsumsq(as),sparse(sumsq(af),true))
%!assert(spsumsq(as,1),sparse(sumsq(af,1),true))
%!assert(spsumsq(as,2),sparse(sumsq(af,2),true))
%!assert(spprod(as),sparse(prod(af),true))
%!assert(spprod(as,1),sparse(prod(af,1),true))
%!assert(spprod(as,2),sparse(prod(af,2),true))
%!assert(spcumprod(as),sparse(cumprod(af),true))
%!assert(spcumprod(as,1),sparse(cumprod(af,1),true))
%!assert(spcumprod(as,2),sparse(cumprod(af,2),true))

%!assert(spmin(as),sparse(min(af),true))
%!assert(spmin(as(:)),min(af(:)))
%!assert(spmin(as,[],1),sparse(min(af,[],1),true))
%!assert(spmin(as,[],2),sparse(min(af,[],2),true))
%!assert(spmin(as,[],1),sparse(min(af,[],1),true))
%!assert(spmin(as,0),sparse(min(af,0),true))
%!assert(spmin(as,bs),sparse(min(af,bf),true))
%!assert(spmax(as),sparse(max(af),true))
%!assert(spmax(as(:)),max(af(:)))
%!assert(spmax(as,[],1),sparse(max(af,[],1),true))
%!assert(spmax(as,[],2),sparse(max(af,[],2),true))
%!assert(spmax(as,[],1),sparse(max(af,[],1),true))
%!assert(spmax(as,0),sparse(max(af,0),true))
%!assert(spmax(as,bs),sparse(max(af,bf),true))

%!assert(as==as)
%!assert(as==af)
%!assert(af==as)
%!test
%! [ii,jj,vv,nr,nc] = spfind(as);
%! assert(af,full(sparse(ii,jj,vv,nr,nc)));
%!assert(nnz(as),sum(af(:)!=0))
%!assert(nnz(as),nnz(af))
%!assert(issparse(as.'))
%!assert(issparse(as'))
%!assert(issparse(-as))
%!assert(~as,sparse(~af,true))
%!assert(as.', sparse(af.',true));
%!assert(as',  sparse(af',true));
%!assert(-as, sparse(-af,true));
%!assert(~as, sparse(~af,true));
%!error [i,j]=size(af);as(i-1,j+1);
%!error [i,j]=size(af);as(i+1,j-1);
%!test
%! [Is,Js,Vs] = spfind(as);
%! [If,Jf,Vf] = find(af);
%! assert(Is,If);
%! assert(Js,Jf);
%! assert(Vs,Vf);
%!error as(0,1);
%!error as(1,0);
%!assert(spfind(as),find(af))
%!test
%! [i,j,v] = spfind(as);
%! [m,n] = size(as);
%! x = sparse(i,j,v,m,n);
%! assert(x,as);
%!test
%! [i,j,v,m,n] = spfind(as);
%! x = sparse(i,j,v,m,n);
%! assert(x,as);
%!assert(issparse(horzcat(as,as)));
%!assert(issparse(vertcat(as,as)));
%!assert(issparse(cat(1,as,as)));
%!assert(issparse(cat(2,as,as)));
%!assert(issparse([as,as]));
%!assert(issparse([as;as]));
%!assert(horzcat(as,as), sparse([af,af]));
%!assert(vertcat(as,as), sparse([af;af]));
%!assert(horzcat(as,as,as), sparse([af,af,af]));
%!assert(vertcat(as,as,as), sparse([af;af;af]));
%!assert([as,as], sparse([af,af]));
%!assert([as;as], sparse([af;af]));
%!assert([as,as,as], sparse([af,af,af]));
%!assert([as;as;as], sparse([af;af;af]));
%!assert(cat(2,as,as), sparse([af,af]));
%!assert(cat(1,as,as), sparse([af;af]));
%!assert(cat(2,as,as,as), sparse([af,af,af]));
%!assert(cat(1,as,as,as), sparse([af;af;af]));
%!assert(issparse([as,af]));
%!assert(issparse([af,as]));
%!assert([as,af], sparse([af,af]));
%!assert([as;af], sparse([af;af]));

%% Elementwise binary tests (uses as,af,bs,bf,scalar)
%!assert(as==bs,sparse(af==bf,true))
%!assert(bf==as,sparse(bf==af,true))

%!assert(as!=bf,sparse(af!=bf,true))
%!assert(bf!=as,sparse(bf!=af,true))

%!assert(as+bf,af+bf)
%!assert(bf+as,bf+af)

%!assert(as-bf,af-bf)
%!assert(bf-as,bf-af)

%!assert(as.*bf,sparse(af.*bf,true))
%!assert(bf.*as,sparse(bf.*af,true))

%!assert(as./bf,sparse(af./bf,true),100*eps)
%!assert(bf.\as,sparse(bf.\af,true),100*eps)

%!test
%! sv = as.^bf;
%! fv = af.^bf;
%! idx = find(af~=0);
%! assert(sv(:)(idx),sparse(fv(:)(idx),true),100*eps)

%!assert(as==bs,sparse(af==bf,true))
%!assert(as!=bs,sparse(af!=bf,true))
%!assert(as+bs,sparse(af+bf,true))
%!assert(as-bs,sparse(af-bf,true))
%!assert(as.*bs,sparse(af.*bf,true))
%!assert(as./bs,sparse(af./bf,true),100*eps);
%!test
%! sv = as.^bs;
%! fv = af.^bf;
%! idx = find(af~=0);
%! assert(sv(:)(idx),sparse(fv(:)(idx),true),100*eps)

%% Matrix-matrix operators (uses af,as,bs,bf)
%!assert(as*bf',af*bf')
%!assert(af*bs',af*bf')
%!assert(as*bs',sparse(af*bf',true))

%% Matrix diagonal tests (uses af,as,bf,bs)
%!assert(spdiag(as),sparse(diag(af),true))
%!assert(spdiag(bs),sparse(diag(bf),true))
%!assert(spdiag(as,1),sparse(diag(af,1),true))
%!assert(spdiag(bs,1),sparse(diag(bf,1),true))
%!assert(spdiag(as,-1),sparse(diag(af,-1),true))
%!assert(spdiag(bs,-1),sparse(diag(bf,-1),true))
%!assert(spdiag(as(:)),sparse(diag(af(:)),true))
%!assert(spdiag(as(:),1),sparse(diag(af(:),1),true))
%!assert(spdiag(as(:),-1),sparse(diag(af(:),-1),true))
%!assert(spdiag(as(:)'),sparse(diag(af(:)'),true))
%!assert(spdiag(as(:)',1),sparse(diag(af(:)',1),true))
%!assert(spdiag(as(:)',-1),sparse(diag(af(:)',-1),true))
%!assert(spdiags(as,[0,1]),[diag(af,0),diag(af,1)])
%!test [tb,tc]=spdiags(as); 
%! assert(spdiags(tb,tc,sparse(zeros(size(as)))),as)
%! assert(spdiags(tb,tc,size(as,1),size(as,2)),as)

%% Matrix diagonal tests (uses af,as,bf,bs)
%!assert(reshape(as,1,prod(size(as))),sparse(reshape(af,1,prod(size(af))),true))
%!assert(reshape(as,prod(size(as)),1),sparse(reshape(af,prod(size(af)),1),true))
%!assert(reshape(as,fliplr(size(as))),sparse(reshape(af,fliplr(size(af))),true))
%!assert(reshape(bs,1,prod(size(as))),sparse(reshape(bf,1,prod(size(af))),true))
%!assert(reshape(bs,prod(size(as)),1),sparse(reshape(bf,prod(size(af)),1),true))
%!assert(reshape(bs,fliplr(size(as))),sparse(reshape(bf,fliplr(size(af))),true))


# ==============================================================

%!test af=[1+1i,2-1i,0,0;0,0,0,3+2i;0,0,0,4];
%! as=sparse(af);
%!test bf=[0,1-1i,0,0;2+1i,0,0,0;3-1i,2+3i,0,0];
%!test ;# invertible matrix
%! bf=af'*bf+max(abs([af(:);bf(:)]))*sparse(eye(columns(as)));
%! bs=sparse(bf);

%% Matrix-matrix operators (uses af,as,bs,bf)
%!assert(as/bf,af/bf,100*eps)
%!assert(af/bs,af/bf,100*eps)
%!assert(as/bs,sparse(af/bf,true),100*eps)
%!assert(bs\af',bf\af',100*eps)
%!assert(bf\as',bf\af',100*eps)
%!assert(bs\as',sparse(bf\af',true),100*eps)

%!assert(spdet(bs+speye(size(bs))),det(bf+eye(size(bf))),100*eps*abs(det(bf+eye(size(bf)))))

%!test 
%! [l,u]=splu(sparse([1,1;1,1]));
%! assert(l*u,[1,1;1,1],10*eps);

%!test
%! [l,u]=splu(sparse([1,1;1,1+i]));
%! assert(l,sparse([1,2,2],[1,1,2],1),10*eps);
%! assert(u,sparse([1,1,2],[1,2,2],[1,1,1i]),10*eps);

%!test ;# permuted LU
%! [L,U] = splu(bs);
%! assert(L*U,bs,1e-10);

%!test ;# simple LU + row permutations
%! [L,U,P] = splu(bs);
%! assert(P'*L*U,bs,1e-10);
%! # triangularity
%! [i,j,v]=spfind(L);
%! assert(i-j>=0);
%! [i,j,v]=spfind(U);
%! assert(j-i>=0);

%!test ;# simple LU + row/col permutations
%! [L,U,P,Q] = splu(bs);
%! assert(P'*L*U*Q',bs,1e-10);
%! # triangularity
%! [i,j,v]=spfind(L);
%! assert(i-j>=0);
%! [i,j,v]=spfind(U);
%! assert(j-i>=0);

%!test ;# LU with fixed column permutation
%! [L,U,P] = splu(bs,colamd(bs));
%! assert(P'*L*U,bs,1e-10);
%! # triangularity
%! [i,j,v]=spfind(L);
%! assert(i-j>=0);
%! [i,j,v]=spfind(U(:,colamd(bs)));
%! assert(j-i>=0);

%!test ;# LU with initial column permutation
%! [L,U,P,Q] = splu(bs,colamd(bs));
%! assert(P'*L*U*Q',bs,1e-10);
%! # triangularity
%! [i,j,v]=spfind(L);
%! assert(i-j>=0);
%! [i,j,v]=spfind(U);
%! assert(j-i>=0);

%!test ;# inverse
%! assert(spinv(bs)*bs,sparse(eye(rows(bs))),1e-10);

%!assert(bf\as',bf\af',100*eps);
%!assert(bs\af',bf\af',100*eps);
%!assert(bs\as',sparse(bf\af'),100*eps);


# ==============================================================

%!test bf=real(bf);
%! bs=sparse(bf);
%% Matrix-matrix operators (uses af,as,bs,bf)
%!assert(as/bf,af/bf,100*eps)
%!assert(af/bs,af/bf,100*eps)
%!assert(as/bs,sparse(af/bf,true),100*eps)
%!assert(bs\af',bf\af',100*eps)
%!assert(bf\as',bf\af',100*eps)
%!assert(bs\as',sparse(bf\af',true),100*eps)

%!assert(spdet(bs+speye(size(bs))),det(bf+eye(size(bf))),100*eps*abs(det(bf+eye(size(bf)))))

%!test 
%! [l,u]=splu(sparse([1,1;1,1]));
%! assert(l*u,[1,1;1,1],10*eps);

%!test
%! [l,u]=splu(sparse([1,1;1,1+i]));
%! assert(l,sparse([1,2,2],[1,1,2],1),10*eps);
%! assert(u,sparse([1,1,2],[1,2,2],[1,1,1i]),10*eps);

%!test ;# permuted LU
%! [L,U] = splu(bs);
%! assert(L*U,bs,1e-10);

%!test ;# simple LU + row permutations
%! [L,U,P] = splu(bs);
%! assert(P'*L*U,bs,1e-10);
%! # triangularity
%! [i,j,v]=spfind(L);
%! assert(i-j>=0);
%! [i,j,v]=spfind(U);
%! assert(j-i>=0);

%!test ;# simple LU + row/col permutations
%! [L,U,P,Q] = splu(bs);
%! assert(P'*L*U*Q',bs,1e-10);
%! # triangularity
%! [i,j,v]=spfind(L);
%! assert(i-j>=0);
%! [i,j,v]=spfind(U);
%! assert(j-i>=0);

%!test ;# LU with fixed column permutation
%! [L,U,P] = splu(bs,colamd(bs));
%! assert(P'*L*U,bs,1e-10);
%! # triangularity
%! [i,j,v]=spfind(L);
%! assert(i-j>=0);
%! [i,j,v]=spfind(U(:,colamd(bs)));
%! assert(j-i>=0);

%!test ;# LU with initial column permutation
%! [L,U,P,Q] = splu(bs,colamd(bs));
%! assert(P'*L*U*Q',bs,1e-10);
%! # triangularity
%! [i,j,v]=spfind(L);
%! assert(i-j>=0);
%! [i,j,v]=spfind(U);
%! assert(j-i>=0);

%!test ;# inverse
%! assert(spinv(bs)*bs,sparse(eye(rows(bs))),1e-10);

%!assert(bf\as',bf\af',100*eps);
%!assert(bs\af',bf\af',100*eps);
%!assert(bs\as',sparse(bf\af'),100*eps);


# ==============================================================

%!test af=real(af);
%! as=sparse(af);
%% Matrix-matrix operators (uses af,as,bs,bf)
%!assert(as/bf,af/bf,100*eps)
%!assert(af/bs,af/bf,100*eps)
%!assert(as/bs,sparse(af/bf,true),100*eps)
%!assert(bs\af',bf\af',100*eps)
%!assert(bf\as',bf\af',100*eps)
%!assert(bs\as',sparse(bf\af',true),100*eps)

%!assert(spdet(bs+speye(size(bs))),det(bf+eye(size(bf))),100*eps*abs(det(bf+eye(size(bf)))))

%!test 
%! [l,u]=splu(sparse([1,1;1,1]));
%! assert(l*u,[1,1;1,1],10*eps);

%!test
%! [l,u]=splu(sparse([1,1;1,1+i]));
%! assert(l,sparse([1,2,2],[1,1,2],1),10*eps);
%! assert(u,sparse([1,1,2],[1,2,2],[1,1,1i]),10*eps);

%!test ;# permuted LU
%! [L,U] = splu(bs);
%! assert(L*U,bs,1e-10);

%!test ;# simple LU + row permutations
%! [L,U,P] = splu(bs);
%! assert(P'*L*U,bs,1e-10);
%! # triangularity
%! [i,j,v]=spfind(L);
%! assert(i-j>=0);
%! [i,j,v]=spfind(U);
%! assert(j-i>=0);

%!test ;# simple LU + row/col permutations
%! [L,U,P,Q] = splu(bs);
%! assert(P'*L*U*Q',bs,1e-10);
%! # triangularity
%! [i,j,v]=spfind(L);
%! assert(i-j>=0);
%! [i,j,v]=spfind(U);
%! assert(j-i>=0);

%!test ;# LU with fixed column permutation
%! [L,U,P] = splu(bs,colamd(bs));
%! assert(P'*L*U,bs,1e-10);
%! # triangularity
%! [i,j,v]=spfind(L);
%! assert(i-j>=0);
%! [i,j,v]=spfind(U(:,colamd(bs)));
%! assert(j-i>=0);

%!test ;# LU with initial column permutation
%! [L,U,P,Q] = splu(bs,colamd(bs));
%! assert(P'*L*U*Q',bs,1e-10);
%! # triangularity
%! [i,j,v]=spfind(L);
%! assert(i-j>=0);
%! [i,j,v]=spfind(U);
%! assert(j-i>=0);

%!test ;# inverse
%! assert(spinv(bs)*bs,sparse(eye(rows(bs))),1e-10);

%!assert(bf\as',bf\af',100*eps);
%!assert(bs\af',bf\af',100*eps);
%!assert(bs\as',sparse(bf\af'),100*eps);


# ==============================================================

%!test bf=bf+1i*(bf~=0);
%! bs=sparse(bf);
%% Matrix-matrix operators (uses af,as,bs,bf)
%!assert(as/bf,af/bf,100*eps)
%!assert(af/bs,af/bf,100*eps)
%!assert(as/bs,sparse(af/bf,true),100*eps)
%!assert(bs\af',bf\af',100*eps)
%!assert(bf\as',bf\af',100*eps)
%!assert(bs\as',sparse(bf\af',true),100*eps)

%!assert(spdet(bs+speye(size(bs))),det(bf+eye(size(bf))),100*eps*abs(det(bf+eye(size(bf)))))

%!test 
%! [l,u]=splu(sparse([1,1;1,1]));
%! assert(l*u,[1,1;1,1],10*eps);

%!test
%! [l,u]=splu(sparse([1,1;1,1+i]));
%! assert(l,sparse([1,2,2],[1,1,2],1),10*eps);
%! assert(u,sparse([1,1,2],[1,2,2],[1,1,1i]),10*eps);

%!test ;# permuted LU
%! [L,U] = splu(bs);
%! assert(L*U,bs,1e-10);

%!test ;# simple LU + row permutations
%! [L,U,P] = splu(bs);
%! assert(P'*L*U,bs,1e-10);
%! # triangularity
%! [i,j,v]=spfind(L);
%! assert(i-j>=0);
%! [i,j,v]=spfind(U);
%! assert(j-i>=0);

%!test ;# simple LU + row/col permutations
%! [L,U,P,Q] = splu(bs);
%! assert(P'*L*U*Q',bs,1e-10);
%! # triangularity
%! [i,j,v]=spfind(L);
%! assert(i-j>=0);
%! [i,j,v]=spfind(U);
%! assert(j-i>=0);

%!test ;# LU with fixed column permutation
%! [L,U,P] = splu(bs,colamd(bs));
%! assert(P'*L*U,bs,1e-10);
%! # triangularity
%! [i,j,v]=spfind(L);
%! assert(i-j>=0);
%! [i,j,v]=spfind(U(:,colamd(bs)));
%! assert(j-i>=0);

%!test ;# LU with initial column permutation
%! [L,U,P,Q] = splu(bs,colamd(bs));
%! assert(P'*L*U*Q',bs,1e-10);
%! # triangularity
%! [i,j,v]=spfind(L);
%! assert(i-j>=0);
%! [i,j,v]=spfind(U);
%! assert(j-i>=0);

%!test ;# inverse
%! assert(spinv(bs)*bs,sparse(eye(rows(bs))),1e-10);

%!assert(bf\as',bf\af',100*eps);
%!assert(bs\af',bf\af',100*eps);
%!assert(bs\as',sparse(bf\af'),100*eps);


# ==============================================================

%!shared r,c,m,n,fsum,funiq
%!test
%! r=[1,1,2,1,2,3];
%! c=[2,1,1,1,2,1];
%! m=n=0;
%%Assembly tests
%!test
%! m=max([m;r(:)]);
%! n=max([n;c(:)]);
%! funiq=fsum=zeros(m,n);
%! funiq(r(:) + m*(c(:)-1) ) = ones(size(r(:)));
%! funiq = sparse(funiq);
%! for k=1:length(r), fsum(r(k),c(k)) += 1; end
%! fsum = sparse(fsum);
%!assert(sparse(r,c,1),sparse(fsum(1:max(r),1:max(c))));
%!assert(sparse(r,c,1,'sum'),sparse(fsum(1:max(r),1:max(c))));
%!assert(sparse(r,c,1,'unique'),sparse(funiq(1:max(r),1:max(c))));
%!assert(sparse(r,c,1,m,n),sparse(fsum));
%!assert(sparse(r,c,1,m,n,'sum'),sparse(fsum));
%!assert(sparse(r,c,1,m,n,'unique'),sparse(funiq));

%!assert(sparse(r,c,1i),sparse(fsum(1:max(r),1:max(c))*1i));
%!assert(sparse(r,c,1i,'sum'),sparse(fsum(1:max(r),1:max(c))*1i));
%!assert(sparse(r,c,1i,'unique'),sparse(funiq(1:max(r),1:max(c))*1i));
%!assert(sparse(r,c,1i,m,n),sparse(fsum*1i));
%!assert(sparse(r,c,1i,m,n,'sum'),sparse(fsum*1i));
%!assert(sparse(r,c,1i,m,n,'unique'),sparse(funiq*1i));

%!test
%! if (issparse(funiq))
%!  assert(sparse(full(1i*funiq)),sparse(1i*funiq));
%! endif

%!assert(sparse(full(funiq)),funiq);



# ==============================================================

%!shared ridx,cidx,idx,as,af
%!test
%! af=[1+1i,2-1i,0,0;0,0,0,3+2i;0,0,0,4];
%! ridx=[1,3]; cidx=[2,3];
%!test as=sparse(af);

%% Point tests
%!test idx=ridx(:)+rows(as)*(cidx(:)-1);
%!assert(sparse(as(idx),true),sparse(af(idx),true));
%!assert(as(idx),sparse(af(idx),true));
%!assert(as(idx'),sparse(af(idx'),true));
%!assert(as([idx,idx]),sparse(af([idx,idx]),true));
%!error(as(reshape([idx;idx],[1,length(idx),2])));

%% Slice tests
%!assert(as(ridx,cidx), sparse(af(ridx,cidx),true))
%!assert(as(ridx,:), sparse(af(ridx,:),true))
%!assert(as(:,cidx), sparse(af(:,cidx),true))
%!assert(as(:,:), sparse(af(:,:),true))

%% Test 'end' keyword
%!assert(as(end),af(end))
%!assert(as(1,end), af(1,end))
%!assert(as(end,1), af(end,1))
%!assert(as(end,end), af(end,end))
%!assert(as(2:end,2:end), sparse(af(2:end,2:end),true))
%!assert(as(1:end-1,1:end-1), sparse(af(1:end-1,1:end-1),true))
%!test af=real(af);
%!test as=sparse(af);

%% Point tests
%!test idx=ridx(:)+rows(as)*(cidx(:)-1);
%!assert(sparse(as(idx),true),sparse(af(idx),true));
%!assert(as(idx),sparse(af(idx),true));
%!assert(as(idx'),sparse(af(idx'),true));
%!assert(as([idx,idx]),sparse(af([idx,idx]),true));
%!error(as(reshape([idx;idx],[1,length(idx),2])));

%% Slice tests
%!assert(as(ridx,cidx), sparse(af(ridx,cidx),true))
%!assert(as(ridx,:), sparse(af(ridx,:),true))
%!assert(as(:,cidx), sparse(af(:,cidx),true))
%!assert(as(:,:), sparse(af(:,:),true))

%% Test 'end' keyword
%!assert(as(end),af(end))
%!assert(as(1,end), af(1,end))
%!assert(as(end,1), af(end,1))
%!assert(as(end,end), af(end,end))
%!assert(as(2:end,2:end), sparse(af(2:end,2:end),true))
%!assert(as(1:end-1,1:end-1), sparse(af(1:end-1,1:end-1),true))

# ==============================================================

%!shared 
alpha,beta,df,pdf,lf,plf,uf,puf,bf,cf,bcf,tf,tcf,xf,ds,pds,ls,pls,us,pus,bs,cs,bcs,ts,tcs,xs
%!test alpha=1;beta=1;
%! df = diag(1:8).* alpha; ds = sparse(df);
%! pdf = df(randperm(8),randperm(8)); pds = sparse(pdf);
%! ls = tril(sprandn(8,8,0.2),-1).*alpha + speye(8); lf = full(ls);
%! plf = lf(randperm(8),randperm(8)); pls = sparse(plf);
%! us = triu(sprandn(8,8,0.2),1).*alpha + speye(8); uf = full(us);
%! puf = uf(randperm(8),randperm(8)); pus = sparse(puf);
%! bs = spdiags(repmat([1:8]',1,4),-2:1,8,8).*alpha; bf = full(bs);
%! cf = lf + lf'; cs = sparse(cf);
%! bcf = bf + bf'; bcs = sparse(bcf);
%! ts = spdiags(randn(8,3),-1:1,8,8).*alpha; tf = full(ts);
%! tcf = tf + tf'; tcs = sparse(tcf);
%! xf = diag(1:8) + fliplr(diag(1:8)).*beta; xs = sparse(xf);
%!assert(ds\xf,df\xf),1e-10;
%!assert(ds\xs,sparse(df\xf,1),1e-10);
%!assert(pds\xf,pdf\xf,1e-10);
%!assert(pds\xs,sparse(pdf\xf,1),1e-10);
%!assert(ls\xf,lf\xf,1e-10);
%!assert(sparse(ls\xs),sparse(lf\xf),1e-10);
%!assert(pls\xf,plf\xf,1e-10);
%!assert(sparse(pls\xs),sparse(plf\xf),1e-10);
%!assert(us\xf,uf\xf,1e-10);
%!assert(sparse(us\xs),sparse(uf\xf),1e-10);
%!assert(pus\xf,puf\xf,1e-10);
%!assert(sparse(pus\xs),sparse(puf\xf),1e-10);
%!assert(bs\xf,bf\xf,1e-10);
%!assert(sparse(bs\xs),sparse(bf\xf),1e-10);
%!assert(cs\xf,cf\xf,1e-10);
%!assert(sparse(cs\xs),sparse(cf\xf),1e-10);
%!assert(bcs\xf,bcf\xf,1e-10);
%!assert(sparse(bcs\xs),sparse(bcf\xf),1e-10);
%!assert(ts\xf,tf\xf,1e-10);
%!assert(sparse(ts\xs),sparse(tf\xf),1e-10);
%!assert(tcs\xf,tcf\xf,1e-10);
%!assert(sparse(tcs\xs),sparse(tcf\xf),1e-10);

%!test alpha=1;beta=1i;
%! df = diag(1:8).* alpha; ds = sparse(df);
%! pdf = df(randperm(8),randperm(8)); pds = sparse(pdf);
%! ls = tril(sprandn(8,8,0.2),-1).*alpha + speye(8); lf = full(ls);
%! plf = lf(randperm(8),randperm(8)); pls = sparse(plf);
%! us = triu(sprandn(8,8,0.2),1).*alpha + speye(8); uf = full(us);
%! puf = uf(randperm(8),randperm(8)); pus = sparse(puf);
%! bs = spdiags(repmat([1:8]',1,4),-2:1,8,8).*alpha; bf = full(bs);
%! cf = lf + lf'; cs = sparse(cf);
%! bcf = bf + bf'; bcs = sparse(bcf);
%! ts = spdiags(randn(8,3),-1:1,8,8).*alpha; tf = full(ts);
%! tcf = tf + tf'; tcs = sparse(tcf);
%! xf = diag(1:8) + fliplr(diag(1:8)).*beta; xs = sparse(xf);
%!assert(ds\xf,df\xf),1e-10;
%!assert(ds\xs,sparse(df\xf,1),1e-10);
%!assert(pds\xf,pdf\xf,1e-10);
%!assert(pds\xs,sparse(pdf\xf,1),1e-10);
%!assert(ls\xf,lf\xf,1e-10);
%!assert(sparse(ls\xs),sparse(lf\xf),1e-10);
%!assert(pls\xf,plf\xf,1e-10);
%!assert(sparse(pls\xs),sparse(plf\xf),1e-10);
%!assert(us\xf,uf\xf,1e-10);
%!assert(sparse(us\xs),sparse(uf\xf),1e-10);
%!assert(pus\xf,puf\xf,1e-10);
%!assert(sparse(pus\xs),sparse(puf\xf),1e-10);
%!assert(bs\xf,bf\xf,1e-10);
%!assert(sparse(bs\xs),sparse(bf\xf),1e-10);
%!assert(cs\xf,cf\xf,1e-10);
%!assert(sparse(cs\xs),sparse(cf\xf),1e-10);
%!assert(bcs\xf,bcf\xf,1e-10);
%!assert(sparse(bcs\xs),sparse(bcf\xf),1e-10);
%!assert(ts\xf,tf\xf,1e-10);
%!assert(sparse(ts\xs),sparse(tf\xf),1e-10);
%!assert(tcs\xf,tcf\xf,1e-10);
%!assert(sparse(tcs\xs),sparse(tcf\xf),1e-10);

%!test alpha=1i;beta=1;
%! df = diag(1:8).* alpha; ds = sparse(df);
%! pdf = df(randperm(8),randperm(8)); pds = sparse(pdf);
%! ls = tril(sprandn(8,8,0.2),-1).*alpha + speye(8); lf = full(ls);
%! plf = lf(randperm(8),randperm(8)); pls = sparse(plf);
%! us = triu(sprandn(8,8,0.2),1).*alpha + speye(8); uf = full(us);
%! puf = uf(randperm(8),randperm(8)); pus = sparse(puf);
%! bs = spdiags(repmat([1:8]',1,4),-2:1,8,8).*alpha; bf = full(bs);
%! cf = lf + lf'; cs = sparse(cf);
%! bcf = bf + bf'; bcs = sparse(bcf);
%! ts = spdiags(randn(8,3),-1:1,8,8).*alpha; tf = full(ts);
%! tcf = tf + tf'; tcs = sparse(tcf);
%! xf = diag(1:8) + fliplr(diag(1:8)).*beta; xs = sparse(xf);
%!assert(ds\xf,df\xf),1e-10;
%!assert(ds\xs,sparse(df\xf,1),1e-10);
%!assert(pds\xf,pdf\xf,1e-10);
%!assert(pds\xs,sparse(pdf\xf,1),1e-10);
%!assert(ls\xf,lf\xf,1e-10);
%!assert(sparse(ls\xs),sparse(lf\xf),1e-10);
%!assert(pls\xf,plf\xf,1e-10);
%!assert(sparse(pls\xs),sparse(plf\xf),1e-10);
%!assert(us\xf,uf\xf,1e-10);
%!assert(sparse(us\xs),sparse(uf\xf),1e-10);
%!assert(pus\xf,puf\xf,1e-10);
%!assert(sparse(pus\xs),sparse(puf\xf),1e-10);
%!assert(bs\xf,bf\xf,1e-10);
%!assert(sparse(bs\xs),sparse(bf\xf),1e-10);
%!assert(cs\xf,cf\xf,1e-10);
%!assert(sparse(cs\xs),sparse(cf\xf),1e-10);
%!assert(bcs\xf,bcf\xf,1e-10);
%!assert(sparse(bcs\xs),sparse(bcf\xf),1e-10);
%!assert(ts\xf,tf\xf,1e-10);
%!assert(sparse(ts\xs),sparse(tf\xf),1e-10);
%!assert(tcs\xf,tcf\xf,1e-10);
%!assert(sparse(tcs\xs),sparse(tcf\xf),1e-10);

%!test alpha=1i;beta=1i;
%! df = diag(1:8).* alpha; ds = sparse(df);
%! pdf = df(randperm(8),randperm(8)); pds = sparse(pdf);
%! ls = tril(sprandn(8,8,0.2),-1).*alpha + speye(8); lf = full(ls);
%! plf = lf(randperm(8),randperm(8)); pls = sparse(plf);
%! us = triu(sprandn(8,8,0.2),1).*alpha + speye(8); uf = full(us);
%! puf = uf(randperm(8),randperm(8)); pus = sparse(puf);
%! bs = spdiags(repmat([1:8]',1,4),-2:1,8,8).*alpha; bf = full(bs);
%! cf = lf + lf'; cs = sparse(cf);
%! bcf = bf + bf'; bcs = sparse(bcf);
%! ts = spdiags(randn(8,3),-1:1,8,8).*alpha; tf = full(ts);
%! tcf = tf + tf'; tcs = sparse(tcf);
%! xf = diag(1:8) + fliplr(diag(1:8)).*beta; xs = sparse(xf);
%!assert(ds\xf,df\xf),1e-10;
%!assert(ds\xs,sparse(df\xf,1),1e-10);
%!assert(pds\xf,pdf\xf,1e-10);
%!assert(pds\xs,sparse(pdf\xf,1),1e-10);
%!assert(ls\xf,lf\xf,1e-10);
%!assert(sparse(ls\xs),sparse(lf\xf),1e-10);
%!assert(pls\xf,plf\xf,1e-10);
%!assert(sparse(pls\xs),sparse(plf\xf),1e-10);
%!assert(us\xf,uf\xf,1e-10);
%!assert(sparse(us\xs),sparse(uf\xf),1e-10);
%!assert(pus\xf,puf\xf,1e-10);
%!assert(sparse(pus\xs),sparse(puf\xf),1e-10);
%!assert(bs\xf,bf\xf,1e-10);
%!assert(sparse(bs\xs),sparse(bf\xf),1e-10);
%!assert(cs\xf,cf\xf,1e-10);
%!assert(sparse(cs\xs),sparse(cf\xf),1e-10);
%!assert(bcs\xf,bcf\xf,1e-10);
%!assert(sparse(bcs\xs),sparse(bcf\xf),1e-10);
%!assert(ts\xf,tf\xf,1e-10);
%!assert(sparse(ts\xs),sparse(tf\xf),1e-10);
%!assert(tcs\xf,tcf\xf,1e-10);
%!assert(sparse(tcs\xs),sparse(tcf\xf),1e-10);


# ==============================================================


reply via email to

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