mailr25259 - in /trunk/target_functions: c_chi2.c c_chi2.h


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

Header


Content

Posted by edward on August 26, 2014 - 08:58:
Author: bugman
Date: Tue Aug 26 08:58:39 2014
New Revision: 25259

URL: http://svn.gna.org/viewcvs/relax?rev=25259&view=rev
Log:
Implemented the chi-squared gradient as a C module for the target functions.

This simply translates the Python code into C to allow any target function C 
modules to build its
own gradient function.


Modified:
    trunk/target_functions/c_chi2.c
    trunk/target_functions/c_chi2.h

Modified: trunk/target_functions/c_chi2.c
URL: 
http://svn.gna.org/viewcvs/relax/trunk/target_functions/c_chi2.c?rev=25259&r1=25258&r2=25259&view=diff
==============================================================================
--- trunk/target_functions/c_chi2.c     (original)
+++ trunk/target_functions/c_chi2.c     Tue Aug 26 08:58:39 2014
@@ -19,7 +19,9 @@
 
 #include <stdio.h>
 #include <math.h>
+#include "c_chi2.h"
 
+/* Define the function for calculating the square of a number. */
 #define square(x) (x)*(x)
 
 
@@ -27,17 +29,22 @@
     /* Function to calculate the chi-squared value.
 
     The chi-sqared equation
-    ~~~~~~~~~~~~~~~~~~~~~~~
-              _n_
-              \    (yi - yi()) ** 2
-    Chi2()  =  >   ----------------
-              /__    sigma_i ** 2
-              i=1
+    =======================
+
+    The equation is::
+
+                    _n_
+                    \    (yi - yi(theta)) ** 2
+    chi^2(theta)  =  >   ---------------------
+                    /__      sigma_i ** 2
+                    i=1
 
     where:
-        yi are the values of the measured data set.
-        yi() are the values of the back calculated data set.
-        sigma_i are the values of the error set.
+        - i is the index over data sets.
+        - theta is the parameter vector.
+        - yi are the values of the measured data set.
+        - yi(theta) are the values of the back calculated data set.
+        - sigma_i are the values of the error set.
 
     The chi-squared value is returned.
     */
@@ -52,3 +59,54 @@
 
     return chi2;
 }
+
+
+void dchi2(double dchi2[], double data[], double back_calc_vals[], double 
back_calc_grad[][MAX_DATA], double errors[], int num_times, int M) {
+    /* Calculate the full chi-squared gradient.
+
+    The chi-squared gradient
+    ========================
+
+    The equation is::
+
+                             _n_
+        dchi^2(theta)        \   / yi - yi(theta)     dyi(theta) \
+        -------------  =  -2  >  | --------------  .  ---------- |
+           dthetaj           /__ \   sigma_i**2        dthetaj   /
+                             i=1
+
+    where
+        - i is the index over data sets.
+        - j is the parameter index of the gradient.
+        - theta is the parameter vector.
+        - yi are the values of the measured data set.
+        - yi(theta) are the values of the back calculated data set.
+        - dyi(theta)/dthetaj are the values of the back calculated gradient 
for parameter j.
+        - sigma_i are the values of the error set.
+
+
+    @param dchi2:           The chi-squared gradient data structure to place 
the gradient elements
+                            into.
+    @type dchi2:            numpy rank-1 size M array
+    @param data:            The vector of yi values.
+    @type data:             numpy rank-1 size N array
+    @param back_calc_vals:  The vector of yi(theta) values.
+    @type back_calc_vals:   numpy rank-1 size N array
+    @param back_calc_grad:  The matrix of dyi(theta)/dtheta values.
+    @type back_calc_grad:   numpy rank-2 size MxN array
+    @param errors:          The vector of sigma_i values.
+    @type errors:           numpy rank-1 size N array
+    @param M:               The dimensions of the gradient.
+    @type M:                int
+    */
+
+    /* Declarations. */
+    int i, j;
+
+    /* Calculate the chi-squared gradient. */
+    for (j = 0; j < M; ++j) {
+        for (i = 0; i < num_times; ++i) {
+            dchi2[j] += -2.0 / (errors[i]*errors[i]) * (data[i] - 
back_calc_vals[i]) * back_calc_grad[j][i];
+        }
+    }
+}

Modified: trunk/target_functions/c_chi2.h
URL: 
http://svn.gna.org/viewcvs/relax/trunk/target_functions/c_chi2.h?rev=25259&r1=25258&r2=25259&view=diff
==============================================================================
--- trunk/target_functions/c_chi2.h     (original)
+++ trunk/target_functions/c_chi2.h     Tue Aug 26 08:58:39 2014
@@ -21,6 +21,10 @@
 #ifndef RELAX_C_CHI2 
 #define RELAX_C_CHI2
 
+/* The maximum number of data points */
+#define MAX_DATA 50
+
 double chi2(double *values, double *sd, double *back_calc, int num_times);
+void dchi2(double dchi2[], double data[], double back_calc_vals[], double 
back_calc_grad[][MAX_DATA], double errors[], int num_times, int M);
 
 #endif




Related Messages


Powered by MHonArc, Updated Tue Aug 26 09:20:03 2014