numpy.count_nonzero

540 views
Skip to first unread message

Eileen Ee

unread,
Nov 15, 2013, 3:25:36 AM11/15/13
to sage-s...@googlegroups.com
Hi everyone,

I want to count the number of non-zero entries in a matrix but I can't find a Sage command to do this. Instead, I found a NumPy command numpy.count_nonzero to do it.

I tested it in the online Sage Cell server: https://sagecell.sagemath.org/
The code goes like this:         import numpy as np
                                            mm = matrix(ZZ, 2, 3, [1,0,2,0,0,0])
                      print mm
                      print np.count_nonzero(mm)

It worked in the Sage Cell Server. However, when I tried it out in Sage v5.12 in Linux. There is an error message: " 'module' object has no attribute 'count_nonzero' ". Also, when I typed numpy.<tab> to see all possible commands, numpy.count_nonzero does not appear. It seems as though there is no such command.

I am willing to use other simple commands as long as I get to count the nonzero entries of a matrix. Perhaps someone can help me with this...

Thanks!

Cheers,
Eileen.

John Cremona

unread,
Nov 15, 2013, 4:12:06 AM11/15/13
to SAGE support
Try this:

sage: M = random_matrix(GF(2),4,5)
sage: len([a for a in M.list() if a])
12

Here M.list() is a list of all the entries, we select the nonzero ones
and count.

John Cremona
> --
> You received this message because you are subscribed to the Google Groups
> "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sage-support...@googlegroups.com.
> To post to this group, send email to sage-s...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sage-support.
> For more options, visit https://groups.google.com/groups/opt_out.

Eileen Ee

unread,
Nov 18, 2013, 4:52:27 AM11/18/13
to sage-s...@googlegroups.com
Thanks, John!

That worked well! On a note, before I saw your reply, I used list(M) when I tried to convert the matrix into a list but list(M)  contains segregation in the list. 

Example:
sage: m = matrix(ZZ, 2, 3, [1,0,1,0,1,1])
sage: list(m)
[(1, 0, 1), (0, 1, 1)]
sage: m.list()
[1, 0, 1, 0, 1, 1]

What I wanted is a list without the segregation, ie. m.list(), not list(m). I wonder why these two commands produce different results...

Thanks, John! You have been a great help!

Cheers,
Eileen

John Cremona

unread,
Nov 18, 2013, 5:04:18 AM11/18/13
to SAGE support
I discovered the same difference between M.list() and list(M) when
formulating my reply. It seems that list(M) is the same as M.rows()
rather than M.list(), but I don't know why it was implemented this
way. It may just be an accident, since list(M) calls the python
function list() and what that does with an object depends on the
object's structure somehow. You can also see from M.rows?? that
M.rows() calls list(M).

John

Harald Schilly

unread,
Nov 30, 2013, 5:22:48 AM11/30/13
to sage-s...@googlegroups.com
An idea to throw in:

>>> mm = matrix(ZZ, 2, 3, [1,0,2,0,0,0])

>>> mm.numpy() != 0
array
([[ True, False,  True],
       [False, False, False]], dtype=bool)

>>> (mm.numpy() != 0).sum()
2


Harald

Nils Bruin

unread,
Nov 30, 2013, 11:09:29 AM11/30/13
to sage-s...@googlegroups.com
On Monday, November 18, 2013 2:04:18 AM UTC-8, John Cremona wrote:
I discovered the same difference between M.list() and list(M) when
formulating my reply.  It seems that list(M) is the same as M.rows()
rather than M.list(), but I don't know why it was implemented this
way.  It may just be an accident, since list(M) calls the python
function list() and what that does with an object depends on the
object's structure somehow.  You can also see from M.rows?? that
M.rows() calls list(M).

The default that

for v in M:
    ... do something with v

iterates over the rows of  M is very convenient in a lot of places. I think that is a more common operation than iterating over the entries. That list(M) returns a list of rows (the same as M.rows()) is a corollary of that. In that perspective, M.list() is perhaps unfortunately named (M.entries() perhaps? If we add the alias I think people will still wonder about list)
Reply all
Reply to author
Forward
0 new messages