0

I am using 2d numpy matrices as adjacency matrices for various graph representations and traversals. These are unweighted graphs, and I only need to check for connectivity, nothing more. Therefore, I am using 1s to represent edges, and 0s to represent the lack of an edge.

I acknowledge that the following question probably depends on many factors - most of which are outside of my understanding.

What is the appropriate dtype to use when doing bitwise operations between two matrices of 1s and 0s (or trues and falses)?

I also have a second question - is numpy even the correct choice for this scenario? I am wondering if maybe there is a python package I don't know about that is better than numpy if you are only doing bitwise operations (no arithmetic).

Thank you.

2
  • You might be able to use sys.getsizeof to compare different implementations Commented Jun 28, 2018 at 14:55
  • Sorry what is the problem with using dtype=bool? It supports bitwise operations out of the box (with &, |, ~ or alternatively with np.logical_and, np.logical_or, np.logical_not) and in my experience it should take care of all the low-level problems. EDIT: well actually it appears to internally use a full byte (like np.int8) so yes, it wastes 7/8th of the space, there might indeed be better options, sorry. Commented Jun 28, 2018 at 15:06

1 Answer 1

1

Why not specify your item size using the itemsize key?

From NumPy's documentation:

The itemsize key allows the total size of the dtype to be set, and must be an integer large enough so all the fields are within the dtype. If the dtype being constructed is aligned, the itemsize must also be divisible by the struct alignment.

(Source: https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.dtypes.html)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.