# HG changeset patch # User David Bateman # Date 1271427285 -7200 # Node ID b4d2080b6df714365c77f734481ce2a663f74ca3 # Parent 660c244d3206ebbf9261416b202bd2bf15642502 Replace nzmax by nnz as needed diff -r 660c244d3206 -r b4d2080b6df7 ChangeLog --- a/ChangeLog Fri Apr 16 12:45:31 2010 +0200 +++ b/ChangeLog Fri Apr 16 16:14:45 2010 +0200 @@ -1,3 +1,7 @@ +2010-04-16 David Bateman + + * PROJECTS: Update for new sparse functionality. + 2010-04-14 Shai Ayal * NEWS: Update. diff -r 660c244d3206 -r b4d2080b6df7 PROJECTS --- a/PROJECTS Fri Apr 16 12:45:31 2010 +0200 +++ b/PROJECTS Fri Apr 16 16:14:45 2010 +0200 @@ -61,41 +61,21 @@ * Improve QR factorization functions, using idea based on CSPARSE cs_dmsol.m + * Improve QR fqctorization by replace CXSPARSE code with SPQR code, and make + the linear solve return 2-norm solutions for ill-conditioned matrices + based on this new code + * Implement fourth argument to the sprand and sprandn, and addition arguments to sprandsym that the leading brand implements. * Sparse logical indexing in idx_vector class so that something like - "a=sprandn(1e6,1e6,1e-6); a(a<1) = 0" won't cause a memory overflow. - - * Make spalloc (r, c, n) actually create an empty sparse with n - non-zero elements? This allows something like - - sm = spalloc (r, c, n) - for j=1:c - for i=1:r - tmp = foo (i,j); - if (tmp != 0.) - sm (i,j) = tmp; - endif - endfor - endfor - - actually make sense. Otherwise the above will cause massive amounts - of memory reallocation. - - The fact is that this doesn't make sense in any case as the assign - function makes another copy of the sparse matrix. So although spalloc - might easily be made to have the correct behavior, the first assign - will cause the matrix to be resized! There seems to be no simple - way to treat this but a complete rewrite of the sparse assignment - functions... + 'a=sprandn(1e6,1e6,1e-6); a(a<1) = 0' won't cause a memory overflow. * Other missing Functions - symmmd Superseded by symamd - colmmd Superseded by colamd - cholinc - bicg Can this be taken from octave-forge? - - cgs - gmres - lsqr - minres diff -r 660c244d3206 -r b4d2080b6df7 liboctave/CSparse.h --- a/liboctave/CSparse.h Fri Apr 16 12:45:31 2010 +0200 +++ b/liboctave/CSparse.h Fri Apr 16 16:14:45 2010 +0200 @@ -80,8 +80,9 @@ SparseComplexMatrix (const Array& a, const idx_vector& r, const idx_vector& c, octave_idx_type nr = -1, - octave_idx_type nc = -1, bool sum_terms = true) - : MSparse (a, r, c, nr, nc, sum_terms) { } + octave_idx_type nc = -1, bool sum_terms = true, + octave_idx_type nzm = -1) + : MSparse (a, r, c, nr, nc, sum_terms, nzm) { } explicit SparseComplexMatrix (const SparseMatrix& a); diff -r 660c244d3206 -r b4d2080b6df7 liboctave/ChangeLog --- a/liboctave/ChangeLog Fri Apr 16 12:45:31 2010 +0200 +++ b/liboctave/ChangeLog Fri Apr 16 16:14:45 2010 +0200 @@ -1,3 +1,17 @@ +2010-04-16 David Bateman + + * Sparse.cc (template Sparse::Sparse (const Array&, + const idx_vector&, const idx_vector&, octave_idx_type, + octave_idx_type, bool, octave_idx_type)): Add argument defining the + minimum storage to allocate for the sparse matrix. + * Sparse.h (template Sparse (const Array&, + const idx_vector&, const idx_vector&, octave_idx_type, + octave_idx_type, bool, octave_idx_type)): ditto. + * MSparse.h : ditto + * CSparse.h : ditto + * dSparse.h : ditto + * boolSparse.h : ditto + 2010-04-14 Jaroslav Hajek * Sparse.cc: Update failing tests. diff -r 660c244d3206 -r b4d2080b6df7 liboctave/MSparse.h --- a/liboctave/MSparse.h Fri Apr 16 12:45:31 2010 +0200 +++ b/liboctave/MSparse.h Fri Apr 16 16:14:45 2010 +0200 @@ -59,8 +59,9 @@ MSparse (const Sparse& a) : Sparse (a) { } MSparse (const Array& a, const idx_vector& r, const idx_vector& c, - octave_idx_type nr = -1, octave_idx_type nc = -1, bool sum_terms = true) - : Sparse (a, r, c, nr, nc, sum_terms) { } + octave_idx_type nr = -1, octave_idx_type nc = -1, + bool sum_terms = true, octave_idx_type nzm = -1) + : Sparse (a, r, c, nr, nc, sum_terms, nzm) { } explicit MSparse (octave_idx_type r, octave_idx_type c, T val) : Sparse (r, c, val) { } diff -r 660c244d3206 -r b4d2080b6df7 liboctave/Sparse.cc --- a/liboctave/Sparse.cc Fri Apr 16 12:45:31 2010 +0200 +++ b/liboctave/Sparse.cc Fri Apr 16 16:14:45 2010 +0200 @@ -257,7 +257,8 @@ template Sparse::Sparse (const Array& a, const idx_vector& r, const idx_vector& c, octave_idx_type nr, - octave_idx_type nc, bool sum_terms) + octave_idx_type nc, bool sum_terms, + octave_idx_type nzm) : rep (nil_rep ()), dimensions () { if (nr < 0) @@ -296,7 +297,7 @@ { if (n == 1 && a(0) != T ()) { - change_capacity (1); + change_capacity (nzm > 1 ? nzm : 1); xridx(0) = r(0); xdata(0) = a(0); for (octave_idx_type j = 0; j < nc; j++) @@ -324,7 +325,7 @@ for (octave_idx_type i = 1; i < n; i++) new_nz += rd[i-1] != rd[i]; // Allocate result. - change_capacity (new_nz); + change_capacity (nzm > new_nz ? nzm : new_nz); xcidx (1) = new_nz; octave_idx_type *rri = ridx (); T *rrd = data (); @@ -407,7 +408,7 @@ xcidx(j+1) = xcidx(j) + nzj; } - change_capacity (xcidx (nc)); + change_capacity (nzm > xcidx (nc) ? nzm : xcidx (nc)); octave_idx_type *rri = ridx (); T *rrd = data (); @@ -463,7 +464,7 @@ for (octave_idx_type i = 1; i < n; i++) new_nz += rd[i-1] != rd[i]; // Allocate result. - change_capacity (new_nz); + change_capacity (nzm > new_nz ? nzm : new_nz); xcidx(1) = new_nz; octave_idx_type *rri = ridx (); T *rrd = data (); @@ -550,7 +551,7 @@ xcidx(j+1) = xcidx(j) + nzj; } - change_capacity (xcidx (nc)); + change_capacity (nzm > xcidx (nc) ? nzm : xcidx (nc)); octave_idx_type *rri = ridx (); T *rrd = data (); diff -r 660c244d3206 -r b4d2080b6df7 liboctave/Sparse.h --- a/liboctave/Sparse.h Fri Apr 16 12:45:31 2010 +0200 +++ b/liboctave/Sparse.h Fri Apr 16 16:14:45 2010 +0200 @@ -221,7 +221,8 @@ Sparse (const Sparse& a, const dim_vector& dv); Sparse (const Array& a, const idx_vector& r, const idx_vector& c, - octave_idx_type nr = -1, octave_idx_type nc = -1, bool sum_terms = true); + octave_idx_type nr = -1, octave_idx_type nc = -1, + bool sum_terms = true, octave_idx_type nzm = -1); // Sparsify a normal matrix Sparse (const Array& a); diff -r 660c244d3206 -r b4d2080b6df7 liboctave/boolSparse.h --- a/liboctave/boolSparse.h Fri Apr 16 12:45:31 2010 +0200 +++ b/liboctave/boolSparse.h Fri Apr 16 16:14:45 2010 +0200 @@ -57,8 +57,9 @@ SparseBoolMatrix (const Array& a, const idx_vector& r, const idx_vector& c, octave_idx_type nr = -1, - octave_idx_type nc = -1, bool sum_terms = true) - : Sparse (a, r, c, nr, nc, sum_terms) { } + octave_idx_type nc = -1, bool sum_terms = true, + octave_idx_type nzm = -1) + : Sparse (a, r, c, nr, nc, sum_terms, nzm) { } SparseBoolMatrix (octave_idx_type r, octave_idx_type c, octave_idx_type num_nz) : Sparse (r, c, num_nz) { } diff -r 660c244d3206 -r b4d2080b6df7 liboctave/dSparse.h --- a/liboctave/dSparse.h Fri Apr 16 12:45:31 2010 +0200 +++ b/liboctave/dSparse.h Fri Apr 16 16:14:45 2010 +0200 @@ -74,8 +74,9 @@ SparseMatrix (const Array& a, const idx_vector& r, const idx_vector& c, octave_idx_type nr = -1, - octave_idx_type nc = -1, bool sum_terms = true) - : MSparse (a, r, c, nr, nc, sum_terms) { } + octave_idx_type nc = -1, bool sum_terms = true, + octave_idx_type nzm = -1) + : MSparse (a, r, c, nr, nc, sum_terms, nzm) { } explicit SparseMatrix (const DiagMatrix& a); diff -r 660c244d3206 -r b4d2080b6df7 src/DLD-FUNCTIONS/__glpk__.cc --- a/src/DLD-FUNCTIONS/__glpk__.cc Fri Apr 16 12:45:31 2010 +0200 +++ b/src/DLD-FUNCTIONS/__glpk__.cc Fri Apr 16 16:14:45 2010 +0200 @@ -519,7 +519,7 @@ mrowsA = A.rows (); octave_idx_type Anc = A.cols (); - octave_idx_type Anz = A.nzmax (); + octave_idx_type Anz = A.nnz (); rn.resize (Anz+1, 1); cn.resize (Anz+1, 1); a.resize (Anz+1, 0.0); diff -r 660c244d3206 -r b4d2080b6df7 src/DLD-FUNCTIONS/amd.cc --- a/src/DLD-FUNCTIONS/amd.cc Fri Apr 16 12:45:31 2010 +0200 +++ b/src/DLD-FUNCTIONS/amd.cc Fri Apr 16 16:14:45 2010 +0200 @@ -91,7 +91,7 @@ print_usage (); else { - octave_idx_type n_row, n_col, nnz; + octave_idx_type n_row, n_col; const octave_idx_type *ridx, *cidx; SparseMatrix sm; SparseComplexMatrix scm; @@ -103,7 +103,6 @@ scm = args(0).sparse_complex_matrix_value (); n_row = scm.rows (); n_col = scm.cols (); - nnz = scm.nzmax (); ridx = scm.xridx (); cidx = scm.xcidx (); } @@ -112,7 +111,6 @@ sm = args(0).sparse_matrix_value (); n_row = sm.rows (); n_col = sm.cols (); - nnz = sm.nzmax (); ridx = sm.xridx (); cidx = sm.xcidx (); } @@ -126,7 +124,6 @@ n_row = sm.rows (); n_col = sm.cols (); - nnz = sm.nzmax (); ridx = sm.xridx (); cidx = sm.xcidx (); } diff -r 660c244d3206 -r b4d2080b6df7 src/DLD-FUNCTIONS/ccolamd.cc --- a/src/DLD-FUNCTIONS/ccolamd.cc Fri Apr 16 12:45:31 2010 +0200 +++ b/src/DLD-FUNCTIONS/ccolamd.cc Fri Apr 16 16:14:45 2010 +0200 @@ -223,7 +223,7 @@ scm = args(0). sparse_complex_matrix_value (); n_row = scm.rows (); n_col = scm.cols (); - nnz = scm.nzmax (); + nnz = scm.nnz (); ridx = scm.xridx (); cidx = scm.xcidx (); } @@ -233,7 +233,7 @@ n_row = sm.rows (); n_col = sm.cols (); - nnz = sm.nzmax (); + nnz = sm.nnz (); ridx = sm.xridx (); cidx = sm.xcidx (); } @@ -247,7 +247,7 @@ n_row = sm.rows (); n_col = sm.cols (); - nnz = sm.nzmax (); + nnz = sm.nnz (); ridx = sm.xridx (); cidx = sm.xcidx (); } @@ -461,7 +461,7 @@ scm = args(0).sparse_complex_matrix_value (); n_row = scm.rows (); n_col = scm.cols (); - nnz = scm.nzmax (); + nnz = scm.nnz (); ridx = scm.xridx (); cidx = scm.xcidx (); } @@ -470,7 +470,7 @@ sm = args(0).sparse_matrix_value (); n_row = sm.rows (); n_col = sm.cols (); - nnz = sm.nzmax (); + nnz = sm.nnz (); ridx = sm.xridx (); cidx = sm.xcidx (); } @@ -484,7 +484,7 @@ n_row = sm.rows (); n_col = sm.cols (); - nnz = sm.nzmax (); + nnz = sm.nnz (); ridx = sm.xridx (); cidx = sm.xcidx (); } diff -r 660c244d3206 -r b4d2080b6df7 src/DLD-FUNCTIONS/colamd.cc --- a/src/DLD-FUNCTIONS/colamd.cc Fri Apr 16 12:45:31 2010 +0200 +++ b/src/DLD-FUNCTIONS/colamd.cc Fri Apr 16 16:14:45 2010 +0200 @@ -347,7 +347,7 @@ scm = args(0). sparse_complex_matrix_value (); n_row = scm.rows (); n_col = scm.cols (); - nnz = scm.nzmax (); + nnz = scm.nnz (); ridx = scm.xridx (); cidx = scm.xcidx (); } @@ -357,7 +357,7 @@ n_row = sm.rows (); n_col = sm.cols (); - nnz = sm.nzmax (); + nnz = sm.nnz (); ridx = sm.xridx (); cidx = sm.xcidx (); } @@ -371,7 +371,7 @@ n_row = sm.rows (); n_col = sm.cols (); - nnz = sm.nzmax (); + nnz = sm.nnz (); ridx = sm.xridx (); cidx = sm.xcidx (); } @@ -556,7 +556,7 @@ scm = args(0).sparse_complex_matrix_value (); n_row = scm.rows (); n_col = scm.cols (); - nnz = scm.nzmax (); + nnz = scm.nnz (); ridx = scm.xridx (); cidx = scm.xcidx (); } @@ -565,7 +565,7 @@ sm = args(0).sparse_matrix_value (); n_row = sm.rows (); n_col = sm.cols (); - nnz = sm.nzmax (); + nnz = sm.nnz (); ridx = sm.xridx (); cidx = sm.xcidx (); } @@ -579,7 +579,7 @@ n_row = sm.rows (); n_col = sm.cols (); - nnz = sm.nzmax (); + nnz = sm.nnz (); ridx = sm.xridx (); cidx = sm.xcidx (); } @@ -681,7 +681,7 @@ scm = args(0).sparse_complex_matrix_value (); n_row = scm.rows (); n_col = scm.cols (); - nnz = scm.nzmax (); + nnz = scm.nnz (); ridx = scm.xridx (); cidx = scm.xcidx (); } @@ -690,7 +690,7 @@ sm = args(0).sparse_matrix_value (); n_row = sm.rows (); n_col = sm.cols (); - nnz = sm.nzmax (); + nnz = sm.nnz (); ridx = sm.xridx (); cidx = sm.xcidx (); } diff -r 660c244d3206 -r b4d2080b6df7 src/DLD-FUNCTIONS/kron.cc --- a/src/DLD-FUNCTIONS/kron.cc Fri Apr 16 12:45:31 2010 +0200 +++ b/src/DLD-FUNCTIONS/kron.cc Fri Apr 16 16:14:45 2010 +0200 @@ -100,7 +100,7 @@ { octave_idx_type idx = 0; MSparse C (A.rows () * B.rows (), A.columns () * B.columns (), - A.nzmax () * B.nzmax ()); + A.nnz () * B.nnz ()); C.cidx (0) = 0; diff -r 660c244d3206 -r b4d2080b6df7 src/DLD-FUNCTIONS/sparse.cc --- a/src/DLD-FUNCTIONS/sparse.cc Fri Apr 16 12:45:31 2010 +0200 +++ b/src/DLD-FUNCTIONS/sparse.cc Fri Apr 16 16:14:45 2010 +0200 @@ -156,7 +156,13 @@ if (! error_state) { - octave_idx_type m = -1, n = -1; + octave_idx_type m = -1, n = -1, nzmax = -1; + if (nargin == 6) + { + nzmax = args(5).idx_type_value (); + nargin --; + } + if (nargin == 5) { if (args(3).is_scalar_type () && args(4).is_scalar_type ()) @@ -181,13 +187,13 @@ if (args(2).is_bool_type ()) retval = SparseBoolMatrix (args(2).bool_array_value (), i, j, - m, n, summation); + m, n, summation, nzmax); else if (args(2).is_complex_type ()) - retval = SparseComplexMatrix (args(2).complex_array_value (), i, j, - m, n, summation); + retval = SparseComplexMatrix (args(2).complex_array_value (), + i, j, m, n, summation, nzmax); else if (args(2).is_numeric_type ()) retval = SparseMatrix (args(2).array_value (), i, j, - m, n, summation); + m, n, summation, nzmax); else gripe_wrong_type_arg ("sparse", args(2)); } diff -r 660c244d3206 -r b4d2080b6df7 src/ov-base-sparse.cc --- a/src/ov-base-sparse.cc Fri Apr 16 12:45:31 2010 +0200 +++ b/src/ov-base-sparse.cc Fri Apr 16 16:14:45 2010 +0200 @@ -361,7 +361,7 @@ // Ensure that additional memory is deallocated matrix.maybe_compress (); - os << "# nnz: " << nzmax () << "\n"; + os << "# nnz: " << nnz () << "\n"; os << "# rows: " << dv (0) << "\n"; os << "# columns: " << dv (1) << "\n"; diff -r 660c244d3206 -r b4d2080b6df7 src/ov-bool-sparse.cc --- a/src/ov-bool-sparse.cc Fri Apr 16 12:45:31 2010 +0200 +++ b/src/ov-bool-sparse.cc Fri Apr 16 16:14:45 2010 +0200 @@ -217,7 +217,7 @@ int nr = d(0); int nc = d(1); - int nz = nzmax (); + int nz = nnz (); int32_t itmp; // Use negative value for ndims to be consistent with other formats @@ -427,7 +427,7 @@ return false; } - tmp = m.nzmax (); + tmp = m.nnz (); retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, &tmp) >= 0; H5Dclose (data_hid); @@ -478,7 +478,7 @@ H5Sclose (space_hid); - hdims[0] = m.nzmax (); + hdims[0] = m.nnz (); hdims[1] = 1; space_hid = H5Screate_simple (2, hdims, 0); @@ -528,8 +528,8 @@ return false; } - OCTAVE_LOCAL_BUFFER (hbool_t, htmp, m.nzmax ()); - for (int i = 0; i < m.nzmax (); i++) + OCTAVE_LOCAL_BUFFER (hbool_t, htmp, m.nnz ()); + for (int i = 0; i < m.nnz (); i++) htmp[i] = m.xdata(i); retval = H5Dwrite (data_hid, H5T_NATIVE_HBOOL, H5S_ALL, H5S_ALL, diff -r 660c244d3206 -r b4d2080b6df7 src/ov-cx-sparse.cc --- a/src/ov-cx-sparse.cc Fri Apr 16 12:45:31 2010 +0200 +++ b/src/ov-cx-sparse.cc Fri Apr 16 16:14:45 2010 +0200 @@ -236,7 +236,7 @@ int nr = d(0); int nc = d(1); - int nz = nzmax (); + int nz = nnz (); int32_t itmp; // Use negative value for ndims to be consistent with other formats @@ -263,7 +263,7 @@ else st = LS_FLOAT; } - else if (matrix.nzmax () > 8192) // FIXME -- make this configurable. + else if (matrix.nnz () > 8192) // FIXME -- make this configurable. { double max_val, min_val; if (matrix.all_integers (max_val, min_val)) @@ -463,7 +463,7 @@ return false; } - tmp = m.nzmax (); + tmp = m.nnz (); retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, &tmp) >= 0; H5Dclose (data_hid); @@ -514,7 +514,7 @@ H5Sclose (space_hid); - hdims[0] = m.nzmax (); + hdims[0] = m.nnz (); hdims[1] = 1; space_hid = H5Screate_simple (2, hdims, 0); diff -r 660c244d3206 -r b4d2080b6df7 src/ov-re-sparse.cc --- a/src/ov-re-sparse.cc Fri Apr 16 12:45:31 2010 +0200 +++ b/src/ov-re-sparse.cc Fri Apr 16 16:14:45 2010 +0200 @@ -269,7 +269,7 @@ int nr = d(0); int nc = d(1); - int nz = nzmax (); + int nz = nnz (); int32_t itmp; // Use negative value for ndims to be consistent with other formats @@ -296,7 +296,7 @@ else st = LS_FLOAT; } - else if (matrix.nzmax () > 8192) // FIXME -- make this configurable. + else if (matrix.nnz () > 8192) // FIXME -- make this configurable. { double max_val, min_val; if (matrix.all_integers (max_val, min_val)) @@ -493,7 +493,7 @@ return false; } - tmp = m.nzmax (); + tmp = m.nnz (); retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, &tmp) >= 0; H5Dclose (data_hid); @@ -544,7 +544,7 @@ H5Sclose (space_hid); - hdims[0] = m.nzmax (); + hdims[0] = m.nnz (); hdims[1] = 1; space_hid = H5Screate_simple (2, hdims, 0); diff -r 660c244d3206 -r b4d2080b6df7 src/sparse-xpow.cc --- a/src/sparse-xpow.cc Fri Apr 16 12:45:31 2010 +0200 +++ b/src/sparse-xpow.cc Fri Apr 16 16:14:45 2010 +0200 @@ -307,7 +307,7 @@ octave_value retval; - octave_idx_type nz = a.nzmax (); + octave_idx_type nz = a.nnz (); if (b <= 0.0) { @@ -474,7 +474,7 @@ retval = octave_value (NDArray (a.dims (), 1)); else { - octave_idx_type nz = a.nzmax (); + octave_idx_type nz = a.nnz (); SparseComplexMatrix result (a); for (octave_idx_type i = 0; i < nz; i++) @@ -602,7 +602,7 @@ } else { - octave_idx_type nz = a.nzmax (); + octave_idx_type nz = a.nnz (); SparseComplexMatrix result (a); @@ -681,7 +681,7 @@ else { - octave_idx_type nz = a.nzmax (); + octave_idx_type nz = a.nnz (); SparseComplexMatrix result (a);