# bug #40105: Logical diagonal matrices are not supported

 Submitter: David Spies Submitted: Wed 25 Sep 2013 11:00:56 PM UTC Category: Octave Function Severity: 1 - Wish Priority: 1 - Later Item Group: Feature Request Status: Confirmed Assigned to: None Originator Name: Open/Closed:  * Open Release:  * dev Operating System:  * GNU/Linux Fixed Release: None Planned Release: None
* Mandatory Fields

Add a New Comment Rich Markup

## Discussion

Tue 04 Jun 2019 03:43:53 PM UTC, comment #5:

Lowering the priority and changing the severity to "Wish".  There are workarounds available, either using spdiags to stay in all sparse matrices, or using diagonal matrice of class double (storage is 8 bytes per entry rather than 1 byte for logical, but since only the N elements of the diagonal are stored I really don't think this is going to be a problem).

And as David B. noted 6 years ago, this would require a hell of a lot of re-working of the diagonal class and all of the operators to support this use case.

Rik <rik5>
Tue 22 Oct 2013 06:08:56 PM UTC, comment #4:

You can always use spdiags. For example

logical (speye(n)) * spdiags (true (n,1), 0, n, n)

and sparse matrices are on both sides of the multiplication operator, and the conversion to a full matrix never happens. THis has the advantage of working similarly with matlab.

I agree it might be nice to have this but it seems a hell of a lot of effort to introduce a new type for this an instaniate all of the operators between thix new type and the existing types.

David

David Bateman <dbateman>
Mon 21 Oct 2013 11:25:39 PM UTC, comment #3:

Logical matrix multiplication comes up quite a lot.  Of course, I could just convert to a matrix of doubles, but that would be a workaround.

As an example, suppose I have a logical matrix A where each row represents a set (and all sets are unique).  Now suppose I want to remove any set which is a subset of another row (such as when finding all maximal cliques in an undirected graph).

cardinality = sum(A,2);
coincidence = A * A'; %Logical matrix multiplication
subsets = any(coincidence == cardinality, 2); % Using automatic broadcasting with ==
A = A(~subsets,:);

As an example of where logical diagonal matrix multiplication comes in handy, suppose I want to mux the rows from matrices A and B according to a logical column vector s

res = (diag(~s) A) | (diag(s) B);

David Spies <dspyz>
Sun 20 Oct 2013 07:33:34 PM UTC, comment #2:

Matlab doesn't have diagonal matrices at all, and should you really be trying to multiply logical matrices. These seems like a nice to have option but really quite a low priority

D.

David Bateman <dbateman>
Thu 26 Sep 2013 05:00:32 PM UTC, comment #1:

I think the problem is actually that diag doesn't support logical matrices.  If I break the problem up I can see that the multiplication is between a SPARSE matrix and a FULL matrix and the rules are that the SPARSE matrix is coerced into a full matrix.

x = logical (speye (3))
x =

Compressed Column Sparse (rows = 3, cols = 3, nnz = 3 [33%])

(1, 1) ->  1
(2, 2) ->  1
(3, 3) ->  1

typeinfo (x)
ans = sparse bool matrix

y = diag (logical ([1 1 1]))
y =

1   0   0
0   1   0
0   0   1
typeinfo  (y)
ans = bool matrix
z = x * y
z =

1   0   0
0   1   0
0   0   1
typeinfo (z)
ans = matrix

On the other hand, If I use double values which diag() does support in the special diagonal matrix class then I get the right result.

x = speye (3);
typeinfo (x)
ans = sparse matrix
y = diag ([1 1 1])
typeinfo (y)

Diagonal Matrix

1   0   0
0   1   0
0   0   1

z = x * y;
typeinfo (z)
ans = sparse matrix

I'm going to re-title the report to indicate that logical diagonal matrices are not supported.

Simple test code for this is

typeinfo (diag ([true true true]))

which should return "logical diagonal matrix".

Rik <rik5>
Wed 25 Sep 2013 11:00:56 PM UTC, original submission:

When multiplying a logical sparse matrix by a logical diagonal matrix, the result is a full matrix

logical(speye(3)) * diag(logical([1,1,1]))

ans =

1   0   0
0   1   0
0   0   1

It should be a sparse matrix

David Spies <dspyz>

## Attached Files

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

Attach Files:

Comment:

No files currently attached

## Dependencies

Depends on the following items: None found

Items that depend on this one: None found

Carbon-Copy List
• -email is unavailable- added by dbateman (Posted a comment)
• -email is unavailable- added by rik5 (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.