Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I use my own Python class with numpy or some other matrix library?
    primarykey
    data
    text
    <p>I'd like to be able to do matrix operations using a Python class as the elements—in this case, a simple <a href="http://en.wikipedia.org/wiki/Galois_field" rel="nofollow">Galois field</a> implementation. It implements the necessary <code>__add__</code>, <code>__mul__</code>, <code>__sub__</code> etc.</p> <p>At first, I thought this should be possible with <a href="http://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html" rel="nofollow">numpy arrays</a>, using the <code>dtype</code> parameter, but from <a href="http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html" rel="nofollow">the <code>dtype</code> documentation</a>, it seems that <code>dtype</code> can't be an arbitrary Python class. For example, I have a class <code>Galois</code> which does operations modulo 2:</p> <pre><code>&gt;&gt;&gt; from galois import Galois &gt;&gt;&gt; Galois(1) + Galois(0) Galois(1) &gt;&gt;&gt; Galois(1) + Galois(1) Galois(0) </code></pre> <p>I can try to use this in numpy:</p> <pre><code>&gt;&gt;&gt; import numpy as np &gt;&gt;&gt; a = np.identity(4, Galois) &gt;&gt;&gt; a array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], dtype=object) </code></pre> <p>But if I do operations on the matrices, the elements aren't following the methods of my class:</p> <pre><code>&gt;&gt;&gt; b = np.identity(4, Galois) &gt;&gt;&gt; a+b array([[2, 0, 0, 0], [0, 2, 0, 0], [0, 0, 2, 0], [0, 0, 0, 2]], dtype=object) </code></pre> <p>Is there any way to make this work with numpy?</p> <p>Is there any other Python matrix library that can do matrix operations (including inversion) on an arbitrary number-like class?</p> <h3>Update</h3> <p>Thanks for the answers so far. But I'm still not able to really use it as I hoped. Adds and multiplies seem good, but not matrix inversion. For example, let's try to get the <a href="http://en.wikipedia.org/wiki/Rijndael_S-box#Inverse_S-box" rel="nofollow">AES inverse S-box affine transform matrix</a> from the <a href="http://en.wikipedia.org/wiki/Rijndael_S-box#Forward_S-box" rel="nofollow">forward S-box affine transform matrix</a>.</p> <pre><code>class Galois(object): MODULO = 2 def __init__(self, val): self.val = int(val) % self.MODULO def __add__(self, val): return self.__class__((self.val + int(val)) % self.MODULO) def __sub__(self, val): return self.__class__((self.val - int(val)) % self.MODULO) def __mul__(self, val): return self.__class__((self.val * int(val)) % self.MODULO) def __int__(self): return self.val def __repr__(self): return "%s(%d)" % (self.__class__.__name__, self.val) def __float__(self): return float(self.val) if __name__ == "__main__": import numpy as np Gv = np.vectorize(Galois) a = Gv(np.identity(8)) + Gv(np.eye(8,8,-1)) + Gv(np.eye(8,8,-2)) + Gv(np.eye(8,8,-3)) + Gv(np.eye(8,8,-4)) + Gv(np.eye(8,8,4)) + Gv(np.eye(8,8,5)) + Gv(np.eye(8,8,6)) + Gv(np.eye(8,8,7)) print np.matrix(a) print np.matrix(a).I </code></pre> <p>The result:</p> <pre><code>[[Galois(1) Galois(0) Galois(0) Galois(0) Galois(1) Galois(1) Galois(1) Galois(1)] [Galois(1) Galois(1) Galois(0) Galois(0) Galois(0) Galois(1) Galois(1) Galois(1)] [Galois(1) Galois(1) Galois(1) Galois(0) Galois(0) Galois(0) Galois(1) Galois(1)] [Galois(1) Galois(1) Galois(1) Galois(1) Galois(0) Galois(0) Galois(0) Galois(1)] [Galois(1) Galois(1) Galois(1) Galois(1) Galois(1) Galois(0) Galois(0) Galois(0)] [Galois(0) Galois(1) Galois(1) Galois(1) Galois(1) Galois(1) Galois(0) Galois(0)] [Galois(0) Galois(0) Galois(1) Galois(1) Galois(1) Galois(1) Galois(1) Galois(0)] [Galois(0) Galois(0) Galois(0) Galois(1) Galois(1) Galois(1) Galois(1) Galois(1)]] [[ 0.4 0.4 -0.6 0.4 0.4 -0.6 0.4 -0.6] [-0.6 0.4 0.4 -0.6 0.4 0.4 -0.6 0.4] [ 0.4 -0.6 0.4 0.4 -0.6 0.4 0.4 -0.6] [-0.6 0.4 -0.6 0.4 0.4 -0.6 0.4 0.4] [ 0.4 -0.6 0.4 -0.6 0.4 0.4 -0.6 0.4] [ 0.4 0.4 -0.6 0.4 -0.6 0.4 0.4 -0.6] [-0.6 0.4 0.4 -0.6 0.4 -0.6 0.4 0.4] [ 0.4 -0.6 0.4 0.4 -0.6 0.4 -0.6 0.4]] </code></pre> <p>Not the result I hoped for. It seems that for the matrix inversion, numpy just converts the matrix to floats, then does the inversion with plain real numbers.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload