Sfoglia il codice sorgente

Complex algebra works now! Very nice

master
Pete Shadbolt 10 anni fa
parent
commit
b21edae26f
3 ha cambiato i file con 19 aggiunte e 32 eliminazioni
  1. +6
    -4
      run-tests.py
  2. +0
    -5
      run-tests.sh
  3. +13
    -23
      src/permanent.c

+ 6
- 4
run-tests.py Vedi File

@@ -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)

+ 0
- 5
run-tests.sh Vedi File

@@ -1,5 +0,0 @@
#!/bin/bash

#rm lib/*.so
python ./setup.py build_ext --inplace &&
python ./run-tests.py

+ 13
- 23
src/permanent.c Vedi File

@@ -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;



Loading…
Annulla
Salva