## 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 edd@debian.org
%! n = 510;
%! sparse(kron((1:n)', ones(n,1)), kron(ones(n,1), (1:n)'), ones(n)); 

%% segfault tests from Fabian@isas-berlin.de
%!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 splu( sparse( [1,1;1,1]   ) );
%!error splu( sparse( [0,0;0,1]   ) );
%!error splu( sparse( [0,0;0,1+i] ) );
%!error splu( sparse( [0,0;0,0]   ) );
%!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);

%% 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); # fails
%!#assert(nnz(sparse([1,bf,bf])*realmin),1); # fails
%!#assert(nnz(sparse([bf,bf,bf])*realmin),0); # fails

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

%!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); # fails
%!#assert(nnz(sparse([1,bf,bf])*realmin),1); # fails
%!#assert(nnz(sparse([bf,bf,bf])*realmin),0); # fails

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

%!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);

%!assert(sparse(eye(3))/0,sparse([1:3],[1:3],Inf));


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

%!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,af==bf)
%!assert(bf==as,bf==af)

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

%!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))
%!assert(bf.*as,sparse(bf.*af))

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

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

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

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

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

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

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

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

%!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))
%!assert(bf.*as,sparse(bf.*af))

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

%!test
%! sv = as.^bf;
%! fv = af.^bf;
%! idx = find(af~=0);
%! assert(sv(:)(idx),fv(:)(idx),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(is_sparse(as),1)
%!assert(is_sparse(af),0)
%!assert(is_real_sparse(af),0)
%!assert(is_complex_sparse(af),0)
%!assert(spsum(as),sum(af))
%!assert(spsum(as,1),sum(af,1))
%!assert(spsum(as,2),sum(af,2))
%!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(is_real_sparse(spabs(as)))
%!assert(is_real_sparse(spreal(as)))
%!assert(is_real_sparse(spimag(as)))
%!assert(is_sparse(as.'))
%!assert(is_sparse(as'))
%!assert(is_sparse(-as))
%!assert(!is_sparse(~as))
%!assert(spabs(as),sparse(abs(af)))
%!assert(spreal(as),sparse(real(af)))
%!assert(spimag(as),sparse(imag(af)))
%!assert(as.', sparse(af.'));
%!assert(as',  sparse(af'));
%!assert(-as, sparse(-af));
%!assert(~as, ~af); # fails on 2.1.52
%!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(sphcat(as,as)));
%!assert(issparse(spvcat(as,as)));
%!assert(sphcat(as,as), sparse([af,af]));
%!assert(spvcat(as,as), sparse([af;af]));
%!assert(sphcat(as,as,as), sparse([af,af,af]));
%!assert(spvcat(as,as,as), sparse([af;af;af]));

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

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

%!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))
%!assert(bf.*as,sparse(bf.*af))

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

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

%!assert(as==bs,af==bf)
%!#assert(as!=bs,sparse(af!=bf)) # fails (complex sparse returns wrong type)
%!assert(as+bs,sparse(af+bf))
%!assert(as-bs,sparse(af-bf))
%!assert(as.*bs,sparse(af.*bf))
%!assert(as./bs,af./bf,100*eps);
%!test
%! sv = as.^bs;
%! fv = af.^bf;
%! idx = find(af~=0);
%! assert(sv(:)(idx),fv(:)(idx),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'))
%!assert(as/bf,af/bf,100*eps)
%!assert(af/bs,af/bf,100*eps)
%!assert(as/bs,af/bf,100*eps)
%!#assert(bs\af,bf\af,100*eps) # fails 
%!#assert(bf\as,bf\af,100*eps) # fails 
%!#assert(bs\as,bf\af,100*eps) # fails (\ seg-faults for non-square)


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

%!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 bf=real(bf);
%!test as=sparse(af);
%!test bs=sparse(bf);
%% Unary matrix tests (uses af,as)
%!assert(is_sparse(as),1)
%!assert(is_sparse(af),0)
%!assert(is_real_sparse(af),0)
%!assert(is_complex_sparse(af),0)
%!assert(spsum(as),sum(af))
%!assert(spsum(as,1),sum(af,1))
%!assert(spsum(as,2),sum(af,2))
%!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(is_real_sparse(spabs(as)))
%!assert(is_real_sparse(spreal(as)))
%!assert(is_real_sparse(spimag(as)))
%!assert(is_sparse(as.'))
%!assert(is_sparse(as'))
%!assert(is_sparse(-as))
%!assert(!is_sparse(~as))
%!assert(spabs(as),sparse(abs(af)))
%!assert(spreal(as),sparse(real(af)))
%!assert(spimag(as),sparse(imag(af)))
%!assert(as.', sparse(af.'));
%!assert(as',  sparse(af'));
%!assert(-as, sparse(-af));
%!assert(~as, ~af); # fails on 2.1.52
%!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(sphcat(as,as)));
%!assert(issparse(spvcat(as,as)));
%!assert(sphcat(as,as), sparse([af,af]));
%!assert(spvcat(as,as), sparse([af;af]));
%!assert(sphcat(as,as,as), sparse([af,af,af]));
%!assert(spvcat(as,as,as), sparse([af;af;af]));

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

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

%!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))
%!assert(bf.*as,sparse(bf.*af))

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

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

%!assert(as==bs,af==bf)
%!#assert(as!=bs,sparse(af!=bf)) # fails (complex sparse returns wrong type)
%!assert(as+bs,sparse(af+bf))
%!assert(as-bs,sparse(af-bf))
%!assert(as.*bs,sparse(af.*bf))
%!assert(as./bs,af./bf,100*eps);
%!test
%! sv = as.^bs;
%! fv = af.^bf;
%! idx = find(af~=0);
%! assert(sv(:)(idx),fv(:)(idx),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'))
%!assert(as/bf,af/bf,100*eps)
%!assert(af/bs,af/bf,100*eps)
%!assert(as/bs,af/bf,100*eps)
%!#assert(bs\af,bf\af,100*eps) # fails 
%!#assert(bf\as,bf\af,100*eps) # fails 
%!#assert(bs\as,bf\af,100*eps) # fails (\ seg-faults for non-square)

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

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

%!test af=real(af);
%!test as=sparse(af);
%!test bs=sparse(bf);
%% Unary matrix tests (uses af,as)
%!assert(is_sparse(as),1)
%!assert(is_sparse(af),0)
%!assert(is_real_sparse(af),0)
%!assert(is_complex_sparse(af),0)
%!assert(spsum(as),sum(af))
%!assert(spsum(as,1),sum(af,1))
%!assert(spsum(as,2),sum(af,2))
%!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(is_real_sparse(spabs(as)))
%!assert(is_real_sparse(spreal(as)))
%!assert(is_real_sparse(spimag(as)))
%!assert(is_sparse(as.'))
%!assert(is_sparse(as'))
%!assert(is_sparse(-as))
%!assert(!is_sparse(~as))
%!assert(spabs(as),sparse(abs(af)))
%!assert(spreal(as),sparse(real(af)))
%!assert(spimag(as),sparse(imag(af)))
%!assert(as.', sparse(af.'));
%!assert(as',  sparse(af'));
%!assert(-as, sparse(-af));
%!assert(~as, ~af); # fails on 2.1.52
%!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(sphcat(as,as)));
%!assert(issparse(spvcat(as,as)));
%!assert(sphcat(as,as), sparse([af,af]));
%!assert(spvcat(as,as), sparse([af;af]));
%!assert(sphcat(as,as,as), sparse([af,af,af]));
%!assert(spvcat(as,as,as), sparse([af;af;af]));

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

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

%!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))
%!assert(bf.*as,sparse(bf.*af))

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

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

%!assert(as==bs,af==bf)
%!#assert(as!=bs,sparse(af!=bf)) # fails (complex sparse returns wrong type)
%!assert(as+bs,sparse(af+bf))
%!assert(as-bs,sparse(af-bf))
%!assert(as.*bs,sparse(af.*bf))
%!assert(as./bs,af./bf,100*eps);
%!test
%! sv = as.^bs;
%! fv = af.^bf;
%! idx = find(af~=0);
%! assert(sv(:)(idx),fv(:)(idx),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'))
%!assert(as/bf,af/bf,100*eps)
%!assert(af/bs,af/bf,100*eps)
%!assert(as/bs,af/bf,100*eps)
%!#assert(bs\af,bf\af,100*eps) # fails 
%!#assert(bf\as,bf\af,100*eps) # fails 
%!#assert(bs\as,bf\af,100*eps) # fails (\ seg-faults for non-square)


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

%!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 bf=bf+1i*(bf~=0);
%!test as=sparse(af);
%!test bs=sparse(bf);
%% Unary matrix tests (uses af,as)
%!assert(is_sparse(as),1)
%!assert(is_sparse(af),0)
%!assert(is_real_sparse(af),0)
%!assert(is_complex_sparse(af),0)
%!assert(spsum(as),sum(af))
%!assert(spsum(as,1),sum(af,1))
%!assert(spsum(as,2),sum(af,2))
%!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(is_real_sparse(spabs(as)))
%!assert(is_real_sparse(spreal(as)))
%!assert(is_real_sparse(spimag(as)))
%!assert(is_sparse(as.'))
%!assert(is_sparse(as'))
%!assert(is_sparse(-as))
%!assert(!is_sparse(~as))
%!assert(spabs(as),sparse(abs(af)))
%!assert(spreal(as),sparse(real(af)))
%!assert(spimag(as),sparse(imag(af)))
%!assert(as.', sparse(af.'));
%!assert(as',  sparse(af'));
%!assert(-as, sparse(-af));
%!assert(~as, ~af); # fails on 2.1.52
%!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(sphcat(as,as)));
%!assert(issparse(spvcat(as,as)));
%!assert(sphcat(as,as), sparse([af,af]));
%!assert(spvcat(as,as), sparse([af;af]));
%!assert(sphcat(as,as,as), sparse([af,af,af]));
%!assert(spvcat(as,as,as), sparse([af;af;af]));

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

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

%!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))
%!assert(bf.*as,sparse(bf.*af))

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

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

%!assert(as==bs,af==bf)
%!#assert(as!=bs,sparse(af!=bf)) # fails (complex sparse returns wrong type)
%!assert(as+bs,sparse(af+bf))
%!assert(as-bs,sparse(af-bf))
%!assert(as.*bs,sparse(af.*bf))
%!assert(as./bs,af./bf,100*eps);
%!test
%! sv = as.^bs;
%! fv = af.^bf;
%! idx = find(af~=0);
%! assert(sv(:)(idx),fv(:)(idx),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'))
%!assert(as/bf,af/bf,100*eps)
%!assert(af/bs,af/bf,100*eps)
%!assert(as/bs,af/bf,100*eps)
%!#assert(bs\af,bf\af,100*eps) # fails 
%!#assert(bf\as,bf\af,100*eps) # fails 
%!#assert(bs\as,bf\af,100*eps) # fails (\ seg-faults for non-square)


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

%!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 ;# invertible matrix
%! bf=af'*bf+max(abs([af(:);bf(:)]))*sparse(eye(columns(as)));
%!test
%! bs = sparse(bf);
%! as = sparse(af);

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

%!test ;# simple LU + permutations
%! [L,U,pr,pc] = splu(bs);
%! assert(pr'*L*U*pc,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);


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

%!test bf=real(bf);
%!test
%! bs = sparse(bf);
%! as = sparse(af);

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

%!test ;# simple LU + permutations
%! [L,U,pr,pc] = splu(bs);
%! assert(pr'*L*U*pc,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);


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

%!test af=real(af);
%!test
%! bs = sparse(bf);
%! as = sparse(af);

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

%!test ;# simple LU + permutations
%! [L,U,pr,pc] = splu(bs);
%! assert(pr'*L*U*pc,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);


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

%!test bf=bf+1i*(bf~=0);
%!test
%! bs = sparse(bf);
%! as = sparse(af);

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

%!test ;# simple LU + permutations
%! [L,U,pr,pc] = splu(bs);
%! assert(pr'*L*U*pc,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);


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

%!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),fsum(1:max(r),1:max(c)));
%!#assert(sparse(r,c,1,'sum'),fsum(1:max(r),1:max(c)));     # fails
%!#assert(sparse(r,c,1,'unique'),funiq(1:max(r),1:max(c))); # fails
%!assert(sparse(r,c,1,m,n),fsum);
%!assert(sparse(r,c,1,m,n,'sum'),fsum);
%!assert(sparse(r,c,1,m,n,'unique'),funiq);

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

# generate from sparse
%!assert(sparse(funiq),funiq);
%!#assert(sparse(1i*funiq),1i*funiq); # fails (not yet implemented)

# generate from full
%!assert(sparse(full(funiq)),funiq);
%!assert(sparse(full(1i*funiq)),1i*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(issparse(as(idx))); # fails (not yet implemented)
%!assert(as(idx),af(idx));
%!#assert(as(idx'),af(idx')); # fails (not yet implemented)
%!#assert(as([idx,idx]),af([idx,idx])); # fails (not yet implemented)
%!#assert(as(reshape([idx;idx],[1,length(idx),2])),af([idx',idx'])); # fails

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

%% Test 'end' keyword
%!#assert(as(end),af(end)) # fails (not yet implemented)
%!#assert(as(1,end), af(1,end)) # fails (not yet implemented)
%!#assert(as(end,1), af(end,1)) # fails (not yet implemented)
%!#assert(as(end,end), af(end,end))
%!#assert(as(2:end,2:end), af(2:end,2:end))
%!#assert(as(1:end-1,1:end-1), af(1:end-1,1:end-1))
%!test af=real(af);
%!test as=sparse(af);

%% Point tests
%!test idx=ridx(:)+rows(as)*(cidx(:)-1);
%!#assert(issparse(as(idx))); # fails (not yet implemented)
%!assert(as(idx),af(idx));
%!#assert(as(idx'),af(idx')); # fails (not yet implemented)
%!#assert(as([idx,idx]),af([idx,idx])); # fails (not yet implemented)
%!#assert(as(reshape([idx;idx],[1,length(idx),2])),af([idx',idx'])); # fails

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

%% Test 'end' keyword
%!#assert(as(end),af(end)) # fails (not yet implemented)
%!#assert(as(1,end), af(1,end)) # fails (not yet implemented)
%!#assert(as(end,1), af(end,1)) # fails (not yet implemented)
%!#assert(as(end,end), af(end,end))
%!#assert(as(2:end,2:end), af(2:end,2:end))
%!#assert(as(1:end-1,1:end-1), af(1:end-1,1:end-1))

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

