-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "cleaned up defunct c modules"
This reverts commit 175b69c.
- Loading branch information
Showing
6 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include <Python.h> | ||
#include <numpy/arrayobject.h> | ||
#include "polar.h" | ||
|
||
static char module_docstring[] = | ||
"This module is used to calcuate the area of a triangle"; | ||
static char polar_docstring[] = | ||
"Functions for dealing with polar co-ordinates"; | ||
|
||
static PyObject *polar_polar(PyObject *self, PyObject *args); | ||
|
||
static PyMethodDef module_methods[] = { | ||
{"polar", polar_polar, METH_VARARGS, polar_docstring}, | ||
{NULL, NULL, 0, NULL} | ||
}; | ||
|
||
PyMODINIT_FUNC | ||
#if PY_MAJOR_VERSION >= 3 | ||
PyInit__polar(void) | ||
#else | ||
init_polar(void) | ||
#endif | ||
{ | ||
#if PY_MAJOR_VERSION >= 3 | ||
PyObject *module; | ||
static struct PyModuleDef moduledef = { | ||
PyModuleDef_HEAD_INIT, | ||
"_polar", /* m_name */ | ||
module_docstring, /* m_doc */ | ||
-1, /* m_size */ | ||
module_methods, /* m_methods */ | ||
NULL, /* m_reload */ | ||
NULL, /* m_traverse */ | ||
NULL, /* m_clear */ | ||
NULL, /* m_free */ | ||
}; | ||
#endif | ||
|
||
#if PY_MAJOR_VERSION >= 3 | ||
module = PyModule_Create(&moduledef); | ||
if (!module) | ||
return NULL; | ||
/* Load `numpy` functionality. */ | ||
import_array(); | ||
return module; | ||
#else | ||
PyObject *m = Py_InitModule3("_polar", module_methods, module_docstring); | ||
if (m == NULL) | ||
return; | ||
/* Load `numpy` functionality. */ | ||
import_array(); | ||
#endif | ||
} | ||
|
||
static PyObject *polar_polar(PyObject *self, PyObject *args) | ||
{ | ||
double r1, r2, theta1, theta2; | ||
|
||
/* Parse the input tuple */ | ||
if (!PyArg_ParseTuple(args, "dddd", &r1, &r2, &theta1, &theta2)) | ||
return NULL; | ||
|
||
/* Call the external C function to compute the area. */ | ||
double d = polar_distance(r1,r2,theta1,theta2); | ||
|
||
/* Build the output tuple */ | ||
|
||
PyObject *ret = Py_BuildValue("d",d); | ||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#include <Python.h> | ||
#include <numpy/arrayobject.h> | ||
#include "test.h" | ||
|
||
static char module_docstring[] = | ||
"This module provides an interface for calculating chi-squared using C."; | ||
static char test_docstring[] = | ||
"Calculate the chi-squared of some data given a model."; | ||
|
||
static PyObject *test_test(PyObject *self, PyObject *args); | ||
|
||
static PyMethodDef module_methods[] = { | ||
{"test", test_test, METH_VARARGS, test_docstring}, | ||
{NULL, NULL, 0, NULL} | ||
}; | ||
|
||
PyMODINIT_FUNC | ||
#if PY_MAJOR_VERSION >= 3 | ||
PyInit__test(void) | ||
#else | ||
init_test(void) | ||
#endif | ||
{ | ||
#if PY_MAJOR_VERSION >= 3 | ||
PyObject *module; | ||
static struct PyModuleDef moduledef = { | ||
PyModuleDef_HEAD_INIT, | ||
"_test", /* m_name */ | ||
module_docstring, /* m_doc */ | ||
-1, /* m_size */ | ||
module_methods, /* m_methods */ | ||
NULL, /* m_reload */ | ||
NULL, /* m_traverse */ | ||
NULL, /* m_clear */ | ||
NULL, /* m_free */ | ||
}; | ||
#endif | ||
|
||
#if PY_MAJOR_VERSION >= 3 | ||
module = PyModule_Create(&moduledef); | ||
if (!module) | ||
return NULL; | ||
/* Load `numpy` functionality. */ | ||
import_array(); | ||
return module; | ||
#else | ||
PyObject *m = Py_InitModule3("_test", module_methods, module_docstring); | ||
if (m == NULL) | ||
return; | ||
/* Load `numpy` functionality. */ | ||
import_array(); | ||
#endif | ||
} | ||
|
||
static PyObject *test_test(PyObject *self, PyObject *args) | ||
{ | ||
double m, b; | ||
PyObject *x_obj, *y_obj, *yerr_obj; | ||
|
||
/* Parse the input tuple */ | ||
if (!PyArg_ParseTuple(args, "ddOOO", &m, &b, &x_obj, &y_obj, | ||
&yerr_obj)) | ||
return NULL; | ||
|
||
/* Interpret the input objects as numpy arrays. */ | ||
PyObject *x_array = PyArray_FROM_OTF(x_obj, NPY_DOUBLE, NPY_IN_ARRAY); | ||
PyObject *y_array = PyArray_FROM_OTF(y_obj, NPY_DOUBLE, NPY_IN_ARRAY); | ||
PyObject *yerr_array = PyArray_FROM_OTF(yerr_obj, NPY_DOUBLE, | ||
NPY_IN_ARRAY); | ||
|
||
/* If that didn't work, throw an exception. */ | ||
if (x_array == NULL || y_array == NULL || yerr_array == NULL) { | ||
Py_XDECREF(x_array); | ||
Py_XDECREF(y_array); | ||
Py_XDECREF(yerr_array); | ||
return NULL; | ||
} | ||
|
||
/* How many data points are there? */ | ||
int N = (int)PyArray_DIM(x_array, 0); | ||
|
||
/* Get pointers to the data as C-types. */ | ||
double *x = (double*)PyArray_DATA(x_array); | ||
double *y = (double*)PyArray_DATA(y_array); | ||
double *yerr = (double*)PyArray_DATA(yerr_array); | ||
|
||
/* Call the external C function to compute the chi-squared. */ | ||
double value = test(m, b, x, y, yerr, N); | ||
|
||
/* Clean up. */ | ||
Py_DECREF(x_array); | ||
Py_DECREF(y_array); | ||
Py_DECREF(yerr_array); | ||
|
||
if (value < 0.0) { | ||
PyErr_SetString(PyExc_RuntimeError, | ||
"Chi-squared returned an impossible value."); | ||
return NULL; | ||
} | ||
|
||
/* Build the output tuple */ | ||
PyObject *ret = Py_BuildValue("d", value); | ||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#include "polar.h" | ||
#include "math.h" | ||
|
||
double polar_distance(double r1, double r2, double theta1, double theta2) { | ||
double d; | ||
|
||
d = sqrt(pow(r1,2) + pow(r2,2) - 2*r1*r2*cos(theta2-theta1)); | ||
|
||
return d; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
double polar_distance(double r1, double r2, double theta1, double theta2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include "test.h" | ||
|
||
double test(double m, double b, double *x, double *y, double *yerr, int N) { | ||
int n; | ||
double result = 0.0, diff; | ||
|
||
for (n = 0; n < N; n++) { | ||
diff = (y[n] - (m * x[n] + b)) / yerr[n]; | ||
result += diff * diff; | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
double test(double m, double b, double *x, double *y, double *yerr, int N); |