From b21edae26f6b5d039bbc71938148a62ef8c292c0 Mon Sep 17 00:00:00 2001 From: Pete Shadbolt Date: Fri, 24 Oct 2014 07:21:15 +1100 Subject: [PATCH] Complex algebra works now! Very nice --- run-tests.py | 10 ++++++---- run-tests.sh | 5 ----- src/permanent.c | 36 +++++++++++++----------------------- 3 files changed, 19 insertions(+), 32 deletions(-) delete mode 100755 run-tests.sh diff --git a/run-tests.py b/run-tests.py index 5062177..8fb8f12 100644 --- a/run-tests.py +++ b/run-tests.py @@ -4,8 +4,10 @@ import multiprocessing as mp import numpy as np import lib -dimension=8 -real=np.random.uniform(0,1,(dimension, dimension)) -imag=np.random.uniform(0,1,(dimension, dimension)) -submatrix=real+1j*imag +#dimension=8 +#real=np.random.uniform(0,1,(dimension, dimension)) +#imag=np.random.uniform(0,1,(dimension, dimension)) +#submatrix=real+1j*imag + +submatrix=np.array([[69+420j]], dtype=np.complex128) print "OUTPUT: ", lib.permanent(submatrix) diff --git a/run-tests.sh b/run-tests.sh deleted file mode 100755 index 5b96cd0..0000000 --- a/run-tests.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash - -#rm lib/*.so -python ./setup.py build_ext --inplace && -python ./run-tests.py diff --git a/src/permanent.c b/src/permanent.c index 115621e..926cdbc 100644 --- a/src/permanent.c +++ b/src/permanent.c @@ -24,25 +24,11 @@ PyMODINIT_FUNC initpermanent(void) { /***************************************************************************** * Array access macros. * *****************************************************************************/ -#define m(x0) (*(npy_float64*)((PyArray_DATA(py_m) + \ - (x0) * PyArray_STRIDES(py_m)[0]))) -#define m_shape(i) (py_m->dimensions[(i)]) - #define r(x0, x1) (*(npy_float64*)((PyArray_DATA(py_r) + \ (x0) * PyArray_STRIDES(py_r)[0] + \ (x1) * PyArray_STRIDES(py_r)[1]))) #define r_shape(i) (py_r->dimensions[(i)]) -#define v(x0, x1) (*(npy_float64*)((PyArray_DATA(py_v) + \ - (x0) * PyArray_STRIDES(py_v)[0] + \ - (x1) * PyArray_STRIDES(py_v)[1]))) -#define v_shape(i) (py_v->dimensions[(i)]) - -#define F(x0, x1) (*(npy_float64*)((PyArray_DATA(py_F) + \ - (x0) * PyArray_STRIDES(py_F)[0] + \ - (x1) * PyArray_STRIDES(py_F)[1]))) -#define F_shape(i) (py_F->dimensions[(i)]) - static PyObject *permanent(PyObject *self, PyObject *args) { // Parse input PyArrayObject *submatrix; @@ -50,20 +36,24 @@ static PyObject *permanent(PyObject *self, PyObject *args) { return NULL; } - // Declare variables. - /*npy_int64 d, i, j;*/ + // Testing algebra + npy_complex128 a; + + /*a.real=1; a.imag=1;*/ + /*b.real=1; b.imag=1;*/ + /*a.real=a.real+b.real;*/ + /*a.imag=a.imag+b.imag;*/ - Py_complex a; - Py_complex b; + // Let's see if we can add to a number from the numpy array + int xpos=0; int ypos=0; + a=(*(npy_complex128*)((PyArray_DATA(submatrix) + \ + (xpos) * PyArray_STRIDES(submatrix)[0] + \ + (ypos) * PyArray_STRIDES(submatrix)[1]))); - a.real=1; a.imag=1; - b.real=1; b.imag=1; - a.real=a.real+b.real; - a.imag=a.imag+b.imag; - // Convert to a python complex number + // Convert to a python complex number and return PyObject *output=PyComplex_FromDoubles(a.real,a.imag); return output;