bugGNU Octave - Bugs: bug #41799, Example sparse matrix created in...

 
 

bug #41799: Example sparse matrix created in oct-file with 0 index

Submitter:  David Spies <dspyz>
Submitted:  Fri 07 Mar 2014 03:52:16 AM UTC
   
 
Category:  Documentation Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Documentation
Status:  Fixed Assigned to:  None
Originator Name:  Open/Closed:  * Closed
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Tue 10 Jun 2014 08:06:33 AM UTC, comment #8: 

OK, I'll prepare a patch with the changes you suggest to the docs.
c.

Carlo de Falco <cdf>
Group Member
Mon 09 Jun 2014 05:36:01 AM UTC, comment #7: 

Overall, I think the sparse documentation could be enhanced.  I did a bit of that, increasing the number of seealso links between related sparse functions, in this changeset (http://hg.savannah.gnu.org/hgweb/octave/rev/53af80da6781).  For this case, I think it would be good to write up your code usage as a second version of case #1, explaining that it is more efficient than the implementation with ColumnVectors (4 bytes rather than 8 bytes per index), but that it still suffers from the problem of duplicate values in memory at the same time.

Rik <rik5>
Group administrator
Thu 05 Jun 2014 11:27:57 AM UTC, comment #6: 

Rik,

It is true that building the sparse matrix from
aij format requires data duplication, nevertheless
that is THE most common way to assemble a sparse matrix,
AND it is the recommended way in most sparse linear algebra
packages including, e.g., UMFPACK, MUMPS, LIS, PETSC, etc.

While writing CSC data directly avoids this data duplication
it is ofte EXTREMELY inconvenient when matrix elements are
accessed randomly rather than in a given order.

Most often, as for example for circuit simulators, FEM or FVM solvers etc. matrix elements are given  with repetitions and
repeated elements are to be added. Actually in my experience this is THE most common use of sparse matrices.

Therefore, if the first approach in the manual is not the most
recommended, it should actually be as it is the most convenient for most applications I know of.

This said, it is true that the approach with indices in ColumnVector objects is more similar to what is done in m-files,
yet it differs from the other two approaches shown in the same
manual page, which use 0-based indices, which I think could
be confusing ...


c.

Carlo de Falco <cdf>
Group Member
Thu 05 Jun 2014 12:22:52 AM UTC, comment #5: 

The manual lists 3 different ways of creating a sparse matrix.  The first method listed, which has the problem code, is actually not even recommended for significant usage.  The issue is that two copies of the matrix will exist which doubles the host memory requirement.  So, in practice, method 1 should only be used for small problems.  If it is only being used for small problems, then I think it is better to emphasize code readability rather than performance.  In this case, creating a sparse matrix in an oct-file looks pretty much like writing the same code in an m-file.  The C++ code looks very much like the following Octave code.


i = [1 2 2 3];
j = [1 2 4 4];
v = [1 2 3 4];
S = sparse (i, j, v);


versus


ColumnVector ridx (nz);
ColumnVector cidx (nz);
ColumnVector data (nz);

ridx(0) = 1; cidx(0) = 1; data(0) = 1;
ridx(1) = 2; cidx(1) = 2; data(1) = 2;
ridx(2) = 2; cidx(2) = 4; data(2) = 3;
ridx(3) = 3; cidx(3) = 4; data(3) = 4;
SparseMatrix sm (data, ridx, cidx, nr, nc);


Rik <rik5>
Group administrator
Wed 04 Jun 2014 01:50:18 AM UTC, comment #4: 

Rik,

Why do you think it is better to suggest


ColumnVector ridx (nz);
ColumnVector cidx (nz);


for the indices, which requires casting indices
to double then back to octave_idx_type?

Isn't it better to use


Array<octave_idx_type> ridx (nz);
Array<octave_idx_type> cidx (nz);


directly?

AFAICS, the Sparse constructor that accepts
Array objects assumes they are 0-based.

c.

Carlo de Falco <cdf>
Group Member
Tue 03 Jun 2014 09:43:06 PM UTC, comment #3: 

I fixed the documentation on the stable branch in this changeset (http://hg.savannah.gnu.org/hgweb/octave/rev/96f22d6674c4).  The problem was that the row and column indices were assumed to be zero-based, while the SparseMatrix constructor assumes ordinary Octave idx_vectors which are 1-based.

Rik <rik5>
Group administrator
Fri 07 Mar 2014 08:52:26 AM UTC, comment #2: 

Sorry for the typos in the previous post,
it seems posts cannot be edited so I rewriting
 it to correct it.

Actually I think these lines in the example:


ColumnVector ridx (nz);
ColumnVector cidx (nz);
ColumnVector data (nz);


should be changed to


Array<octave_idx_type> ridx (nz);
Array<octave_idx_type> cidx (nz);
Array<double> data (nz);


does the example work for you with this change?


Carlo de Falco <cdf>
Group Member
Fri 07 Mar 2014 07:40:39 AM UTC, comment #1: 

Actually I think these lines in the example:


ColumnVector ridx (nz);
ColumnVector cidx (nz);
ColumnVector data (nz);


should be changed to


Array<octave_idx_type> ridx (nz);
Array<octave_idx_type> (nz);
Array<double> (nz);


does the example work for you with this change?

Carlo de Falco <cdf>
Group Member
Fri 07 Mar 2014 03:52:16 AM UTC, original submission:  

In the example for how to create a sparse matrix in an oct-file from 3 vectors (here: http://www.gnu.org/software/octave/doc/interpreter/Creating-Sparse-Matrices-in-Oct_002dFiles.html#Creating-Sparse-Matrices-in-Oct_002dFiles )

some of the indices are zero.  But in fact the sparse-matrix constructor takes in 1-indexed column vectors for row and column.

Imitating this example gives the standard "indices must be positive integers" error.

David Spies <dspyz>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by cdf (Posted a comment)
  • -email is unavailable- added by dspyz (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 2 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2014-06-03 rik5 StatusNone Fixed
        Open/ClosedOpen Closed

    Back to the top

    Powered by Savane 3.13-d3ae.
    Corresponding source code