source: inundation-numpy-branch/pypar_dist/mandelbrot_example/mandel_ext.c @ 3236

Last change on this file since 3236 was 1855, checked in by ole, 19 years ago

Better interface to meshing for karratha + some pypar work

File size: 1.5 KB
Line 
1/* Python - C extension for fast computation of the Mandelbrot iteration
2//
3// To compile:
4//  python compile.py mandel_ext.c
5//
6// Example of how to use within Python:
7//
8// from mandel_ext import calculate_point
9//
10// kmax = 256
11// c = complex(0.5, 0.5)
12// k = calculate_point(c, kmax)
13//
14// Ole Nielsen, SUT 2003 */
15       
16#include "Python.h"
17
18/* Computational function */
19int _calculate_point(Py_complex c, int kmax) {
20  int count;
21  double temp, lengthsq = 0.0; 
22  Py_complex z;
23 
24  z.real = 0.0; z.imag = 0.0; 
25  count = 0;
26
27  while (count < kmax && lengthsq <= 4) {
28       temp = z.real * z.real - z.imag * z.imag + c.real;
29       z.imag = 2.0 * z.real * z.imag + c.imag;
30       z.real = temp;
31       lengthsq = z.real * z.real + z.imag * z.imag;
32       count++;   
33  }
34
35  return count;
36}
37
38
39/* Interface to Python */
40PyObject *calculate_point(PyObject *self, PyObject *args) {
41  PyComplexObject *C;  /* Python Complex object */
42  Py_complex c;        /* C Complex structure */
43  int kmax, count;
44
45  /* Convert Python arguments to C  */
46  if (!PyArg_ParseTuple(args, "Oi", &C, &kmax))
47    return NULL;
48  c = PyComplex_AsCComplex((PyObject*) C);   
49 
50  /* Call underlying routine */
51  count = _calculate_point(c, kmax); 
52
53  /* Return results as a Python object */
54  return Py_BuildValue("i", count);
55}
56
57
58/* Method table for python module */
59static struct PyMethodDef MethodTable[] = {
60  {"calculate_point", calculate_point, METH_VARARGS}, 
61  {NULL, NULL}
62};
63
64
65/* Module initialisation  */
66void initmandel_ext(void){
67  Py_InitModule("mandel_ext", MethodTable);
68}
69
Note: See TracBrowser for help on using the repository browser.