Pete Shadbolt пре 4 година
родитељ
комит
ab4c40da97
6 измењених фајлова са 74 додато и 17 уклоњено
  1. +0
    -1
      MANIFEST
  2. +1
    -0
      requirements.txt
  3. +16
    -15
      setup.py
  4. +21
    -1
      src/permanent.c
  5. +27
    -0
      tests/test_permanent.py
  6. +9
    -0
      tox.ini

+ 0
- 1
MANIFEST Прегледај датотеку

@@ -1,5 +1,4 @@
# file GENERATED by distutils, do NOT edit
README
setup.py
./src/permanent.c
permanent/__init__.py


+ 1
- 0
requirements.txt Прегледај датотеку

@@ -0,0 +1 @@
numpy

+ 16
- 15
setup.py Прегледај датотеку

@@ -3,19 +3,20 @@
from distutils.core import setup, Extension
import numpy

setup(name = "permanent",
version = "0.1.3",
description = "Calculates the permanent of a Numpy matrix",
author = "Pete Shadbolt",
author_email = "pete.shadbolt@gmail.com",
maintainer = "pete.shadbolt@gmail.com",
url = "https://github.com/peteshadbolt/permanent",
packages = ["permanent"],
ext_modules = [
Extension(
'permanent.permanent', ['./src/permanent.c'],
extra_compile_args=["-Ofast", "-march=native"],
include_dirs=[numpy.get_include()]),
],
setup(
name="permanent",
version="0.1.3",
description="Calculates the permanent of a Numpy matrix",
author="Pete Shadbolt",
author_email="hello@peteshadbolt.co.uk",
maintainer="hello@peteshadbolt.co.uk",
url="https://github.com/peteshadbolt/permanent",
packages=["permanent"],
setup_requires=["numpy"],
ext_modules=[
Extension(
'permanent.permanent', ['./src/permanent.c'],
extra_compile_args=["-Ofast", "-march=native"],
include_dirs=[numpy.get_include()]),
],
)

+ 21
- 1
src/permanent.c Прегледај датотеку

@@ -14,12 +14,32 @@ static PyMethodDef methods[] = {
{ NULL, NULL, 0, NULL } // Sentinel
};

// Module initialization

#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef cModPyDem =
{
PyModuleDef_HEAD_INIT,
"permanent", "Computes the permanent of a numpy using the most appropriate method available",
-1,
methods
};

PyMODINIT_FUNC
PyInit_permanent(void)
{
import_array();
return PyModule_Create(&cModPyDem);
}

#else

PyMODINIT_FUNC initpermanent(void) {
(void) Py_InitModule("permanent", methods);
import_array();
}

#endif

// Ryser's algorithm
static npy_complex128 ryser(PyArrayObject *submatrix) {
int n = (int) PyArray_DIM(submatrix, 0);


+ 27
- 0
tests/test_permanent.py Прегледај датотеку

@@ -0,0 +1,27 @@
import numpy as np
from permanent.permanent import permanent
import pytest


def test_permanent():
""" Test that basic functions work right """
m = np.eye(10, dtype=complex)
assert permanent(m) == 1
m = np.zeros((10, 10), dtype=complex)
assert permanent(m) == 0


def test_floaty():
""" More tests using a precomputed permanent """
np.random.seed(1234)
m = np.random.uniform(0, 1, 16) + 1j * np.random.uniform(0, 1, 16)
m = m.reshape(4, 4)
p = permanent(m)
assert np.allclose(p, -8.766131870776363 + 1.072095650303524j)


def test_error():
""" Should raise a TypeError as we are using the wrong dtype """
with pytest.raises(TypeError):
m = np.eye(10, dtype=float)
permanent(m)

+ 9
- 0
tox.ini Прегледај датотеку

@@ -0,0 +1,9 @@
[tox]
envlist = py27, py36

[testenv]
deps=
-r requirements.txt
pytest
pytest-mock
commands=pytest {posargs}

Loading…
Откажи
Сачувај