mailr2387 - in /branches/c_modules/maths_fns: c_chi2.c exponential.c relax_fit.c relax_fit.h


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by edward on March 14, 2006 - 05:51:
Author: bugman
Date: Tue Mar 14 05:50:56 2006
New Revision: 2387

URL: http://svn.gna.org/viewcvs/relax?rev=2387&view=rev
Log:
A few modifications to try to eliminate the segfault.


Modified:
    branches/c_modules/maths_fns/c_chi2.c
    branches/c_modules/maths_fns/exponential.c
    branches/c_modules/maths_fns/relax_fit.c
    branches/c_modules/maths_fns/relax_fit.h

Modified: branches/c_modules/maths_fns/c_chi2.c
URL: 
http://svn.gna.org/viewcvs/relax/branches/c_modules/maths_fns/c_chi2.c?rev=2387&r1=2386&r2=2387&view=diff
==============================================================================
--- branches/c_modules/maths_fns/c_chi2.c (original)
+++ branches/c_modules/maths_fns/c_chi2.c Tue Mar 14 05:50:56 2006
@@ -20,6 +20,15 @@
 
 #include <stdio.h>
 #include <math.h>
+
+/* This include must come first */
+#include <Python.h>
+
+/* The header for all functions which will be called */
+#include "relax_fit.h"
+
+/* The Numeric array object header file, must come second */
+#include <Numeric/arrayobject.h>
 
 #define square(x) (x)*(x)
 
@@ -45,14 +54,14 @@
 
     /* Declarations */
     extern int *num_times;
-    extern double *values, *sd;
+    extern PyArrayObject *values, *sd;
     extern double back_calc[];
        int i;
        double chi2 = 0.0;
 
     /* Loop over the time points and sum the chi-squared components */
-       for (i = 0; i < (int)num_times; ++i) {
-               chi2 = chi2 + square((values[i] - back_calc[i]) / sd[i]);
+       for (i = 0; i < *num_times; ++i) {
+               chi2 = chi2 + square((*(double *)values->data[i] - 
back_calc[i]) / *(double *)sd->data[i]);
        }
 
        return chi2;

Modified: branches/c_modules/maths_fns/exponential.c
URL: 
http://svn.gna.org/viewcvs/relax/branches/c_modules/maths_fns/exponential.c?rev=2387&r1=2386&r2=2387&view=diff
==============================================================================
--- branches/c_modules/maths_fns/exponential.c (original)
+++ branches/c_modules/maths_fns/exponential.c Tue Mar 14 05:50:56 2006
@@ -22,11 +22,11 @@
 /* This include must come first */
 #include <Python.h>
 
+/* The header for all functions which will be called */
+#include "relax_fit.h"
+
 /* The Numeric array object header file, must come second */
 #include <Numeric/arrayobject.h>
-
-/* The header for all functions which will be called */
-#include "relax_fit.h"
 
 
 
@@ -36,22 +36,21 @@
 
     /* Declarations */
     extern int *num_times;
-    extern double *params, *relax_times;
+    extern PyArrayObject *params, *relax_times;
     extern double back_calc[];
     double Rx, I0;
     int i;
 
 
     /* Loop over the time points */
-    /* for (i = 0; i < num_times; i++) { */
-    for (i = 0; i < (int)num_times; i++) {
+    for (i = 0; i < *num_times; i++) {
         /* Zero Rx value */
         if (params[0] == 0.0)
             back_calc[i] = 0.0;
 
         /* Back calculate */
         else
-            back_calc[i] = params[1] * exp(-relax_times[i] * params[0]);
+            back_calc[i] = (double *)params->data[1] * exp(-(double 
*)relax_times->data[i] * (double *)params->data[0]);
 
     }
 }

Modified: branches/c_modules/maths_fns/relax_fit.c
URL: 
http://svn.gna.org/viewcvs/relax/branches/c_modules/maths_fns/relax_fit.c?rev=2387&r1=2386&r2=2387&view=diff
==============================================================================
--- branches/c_modules/maths_fns/relax_fit.c (original)
+++ branches/c_modules/maths_fns/relax_fit.c Tue Mar 14 05:50:56 2006
@@ -21,22 +21,24 @@
 /* This include must come first */
 #include <Python.h>
 
+/* The header for all functions which will be called */
+#include "relax_fit.h"
+
 /* The Numeric array object header file, must come second */
 #include <Numeric/arrayobject.h>
 
-/* The header for all functions which will be called */
-#include "relax_fit.h"
-
 
 
 static PyObject *
 setup(PyObject *self, PyObject *args, PyObject *keywords) {
+    /* Debugging */
+    printf("Setting up\n");
+
     /* Python declarations */
     PyObject *values_arg, *sd_arg, *relax_times_arg, *scaling_matrix_arg;
-    extern PyArrayObject *numpy_values, *numpy_sd, *numpy_relax_times, 
*numpy_scaling_matrix;
+    extern PyArrayObject *values, *sd, *relax_times, *scaling_matrix;
 
     /* Normal declarations */
-    extern double *values, *sd, *relax_times, *scaling_matrix;
     extern double *relax_time_array;
     extern int *num_params, *num_times;
 
@@ -48,20 +50,29 @@
     if (!PyArg_ParseTupleAndKeywords(args, keywords, "iiOOOO", keyword_list, 
&num_params, &num_times, &values_arg, &sd, &relax_times_arg, 
&scaling_matrix_arg))
         return NULL;
 
+    /* Debugging */
+    printf("Function args have been parsed\n");
+
     /* Make the Numeric arrays contiguous */
-    numpy_values = (PyArrayObject *) 
PyArray_ContiguousFromObject(values_arg, PyArray_DOUBLE, 1, 1);
-    numpy_sd = (PyArrayObject *) PyArray_ContiguousFromObject(sd_arg, 
PyArray_DOUBLE, 1, 1);
-    numpy_relax_times = (PyArrayObject *) 
PyArray_ContiguousFromObject(relax_times_arg, PyArray_DOUBLE, 1, 1);
-    numpy_scaling_matrix = (PyArrayObject *) 
PyArray_ContiguousFromObject(scaling_matrix_arg, PyArray_DOUBLE, 2, 2);
-
-    /* Pointers to the Numeric arrays */
-    values = (double *) numpy_values->data;
-    sd = (double *) numpy_sd->data;
-    relax_times = (double *) numpy_relax_times->data;
-    scaling_matrix = (double *) numpy_scaling_matrix->data;
+    values = (PyArrayObject *) PyArray_ContiguousFromObject(values_arg, 
PyArray_DOUBLE, 1, 1);
+    sd = (PyArrayObject *) PyArray_ContiguousFromObject(sd_arg, 
PyArray_DOUBLE, 1, 1);
+    relax_times = (PyArrayObject *) 
PyArray_ContiguousFromObject(relax_times_arg, PyArray_DOUBLE, 1, 1);
+    scaling_matrix = (PyArrayObject *) 
PyArray_ContiguousFromObject(scaling_matrix_arg, PyArray_DOUBLE, 2, 2);
+
+    /* Debugging */
+    printf("Py_INCREF\n");
+
+    /* ??? */
+    Py_INCREF(Py_None);
+    Py_INCREF(values);
+    Py_INCREF(sd);
+    Py_INCREF(relax_times);
+    Py_INCREF(scaling_matrix);
+
+    /* Debugging */
+    printf("Finished setup\n");
 
     /* Return nothing */
-    Py_INCREF(Py_None);
     return Py_None;
 }
 
@@ -74,9 +85,13 @@
      * calculated
      */
 
+    /* Debugging */
+    printf("Inside func\n");
+
     /* Declarations */
     PyObject *arg1;
-    extern PyArrayObject *numpy_params;
+    extern PyArrayObject *params;
+    extern double *params;
     double chi2(void);
 
 
@@ -85,14 +100,14 @@
         return NULL;
 
     /* Convert the Numeric array to be contiguous */
-    numpy_params = (PyArrayObject *) PyArray_ContiguousFromObject(arg1, 
PyArray_DOUBLE, 1, 1);
-
-    /* Pointers to the Numeric arrays */
-    params = (double *) numpy_params->data;
+    params = (PyArrayObject *) PyArray_ContiguousFromObject(arg1, 
PyArray_DOUBLE, 1, 1);
 
     /* Back calculated the peak intensities */
     exponential();
 
+    /* ??? */
+    Py_INCREF(params);
+
     /* Calculate and return the chi-squared value */
     return Py_BuildValue("f", chi2());
 }
@@ -104,7 +119,8 @@
 
     /* Declarations */
     PyObject *arg1;
-    extern PyArrayObject *numpy_params;
+    extern PyArrayObject *params;
+    extern double *params;
 
     /* Temp Declarations */
     PyArrayObject *aaa_numpy;
@@ -117,10 +133,7 @@
         return NULL;
 
     /* Convert the Numeric array to be contiguous */
-    numpy_params = (PyArrayObject *) PyArray_ContiguousFromObject(arg1, 
PyArray_DOUBLE, 1, 1);
-
-    /* Pointers to the Numeric arrays */
-    params = (double *) numpy_params->data;
+    params = (PyArrayObject *) PyArray_ContiguousFromObject(arg1, 
PyArray_DOUBLE, 1, 1);
 
     /* Back calculated the peak intensities */
     exponential();
@@ -128,8 +141,7 @@
 
     /* Test code (convert aaa to a Numeric array */
     /* aaa_numpy = (PyArrayObject *) PyArray_FromDimsAndData(1, num_params, 
PyArray_DOUBLE, aaa_pointer); */
-    aaa_numpy = (PyArrayObject *) PyArray_FromDims(1, num_params, 
PyArray_DOUBLE);
-    aaa_pointer = (double *) aaa_numpy->data;
+    aaa_numpy = (PyArrayObject *) PyArray_FromDims(1, (int *) *num_params, 
PyArray_DOUBLE);
 
     /* Fill the Numeric array */
     for (i = 0; i < 2; i++)
@@ -155,11 +167,11 @@
     extern int *num_times;
     int i;
 
-    PyObject *back_calc_py = PyList_New((int)num_times);
+    PyObject *back_calc_py = PyList_New(*num_times);
     assert(PyList_Check(back_calc_py));
 
     /* Copy the values out of the C array into the Python array */
-    for (i = 0; i < (int)num_times; i++)
+    for (i = 0; i < *num_times; i++)
         PyList_SetItem(back_calc_py, i, Py_BuildValue("f", back_calc[i]));
 
     /* Return the Numeric array */

Modified: branches/c_modules/maths_fns/relax_fit.h
URL: 
http://svn.gna.org/viewcvs/relax/branches/c_modules/maths_fns/relax_fit.h?rev=2387&r1=2386&r2=2387&view=diff
==============================================================================
--- branches/c_modules/maths_fns/relax_fit.h (original)
+++ branches/c_modules/maths_fns/relax_fit.h Tue Mar 14 05:50:56 2006
@@ -36,16 +36,11 @@
 /****************************************/
 
 /* Variables sent to the setup function to be stored for later use */
-PyArrayObject *numpy_values, *numpy_sd, *numpy_relax_times, 
*numpy_scaling_matrix;
+PyArrayObject *values, *sd, *relax_times, *scaling_matrix;
 int *num_params, *num_times;
-double *sd;
 
 /* Variables sent to 'func', 'dfunc', and 'd2func' during optimisation */
-PyArrayObject *numpy_params;
-
-/* Pointers to contiguous PyArrayObjects */
-double *values, *sd, *relax_times, *scaling_matrix;
-double *params;
+PyArrayObject *params;
 
 /* Variables used for storage during the function calls of optimisation */
 double back_calc[MAXTIMES];




Related Messages


Powered by MHonArc, Updated Tue Mar 14 06:00:06 2006