Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2011
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.21 KB | None | 0 0
  1. #include "Python.h"
  2. #include <math.h>
  3.  
  4. static PyObject *StatsError;
  5.  
  6.  
  7. double intpow( double base, int exponent)
  8.     {
  9.         int i;
  10.         double out = base;
  11.         for( i=1 ; i < exponent ; i++ )
  12.         {
  13.         out *= base;
  14.         }
  15.         return out;
  16.     }
  17.  
  18. double*
  19. avg_func(PyObject *seq, Py_ssize_t count)
  20.     {
  21.         Py_ssize_t a;
  22.         PyObject *tempp = NULL;
  23.         double *returnp = NULL;
  24.         static double temp_avg;
  25.         double sumall = 0.00;
  26.         PyObject **src;     // avg,
  27.         src = PySequence_Fast_ITEMS(seq);
  28.        
  29.         for (a=0; a != count; a++)
  30.             {
  31.             tempp = src[a];
  32.             Py_INCREF(tempp);
  33.             if (PyInt_Check(tempp) || PyLong_Check(tempp) || PyFloat_Check(tempp))
  34.                 {
  35.                 sumall = sumall + PyFloat_AsDouble(tempp);
  36.                 Py_DECREF(tempp);
  37.                 }
  38.             else {
  39.                 Py_DECREF(tempp);
  40.                 return returnp;
  41.                 }
  42.             }
  43.              // tempp = PySequence_Fast_GET_ITEM(seq,a);
  44.              
  45.                 // if (PyInt_Check(tempp) || PyLong_Check(tempp) || PyFloat_Check(tempp)){
  46.                     // sumall += PyFloat_AsDouble(tempp);
  47.                 // }
  48.                 // else {
  49.                     // return returnp;
  50.                 // }
  51.             // }
  52.         temp_avg = sumall / count;
  53.         returnp = &temp_avg;
  54.         return returnp;
  55.     };
  56.  
  57. double*
  58. var(PyObject *seq, Py_ssize_t count, double *avg, int samp, int moment)
  59.     {
  60.         Py_ssize_t a;
  61.         PyObject *tempp = NULL;
  62.         static double temp_r = 0.00;
  63.         double sumall = 0.00;
  64.         double  tempxx = 0.00;//tempx,
  65.         double tempx = 0.00;
  66.         //static double test_var;
  67.         // PyObject **src;      // avg,
  68.         // src = PySequence_Fast_ITEMS(seq);
  69.        
  70.         // for (a=0; a != count; a++) {
  71.                 // tempp = src[a];
  72.                 // Py_INCREF(tempp);
  73.                 // tempx = PyFloat_AsDouble(tempp);
  74.                 // Py_DECREF(tempp);
  75.                 // tempxx = tempx - *avg;
  76.                 // sumall += intpow(tempxx,moment);
  77.                 // }
  78.         for (a = 0; a != count; a++)
  79.             {
  80.             tempp = PySequence_Fast_GET_ITEM(seq,a);
  81.             if (tempp == NULL)
  82.                 {
  83.                 PyErr_SetString(StatsError, "Problem");
  84.                 return NULL;
  85.                 }
  86.             tempx = PyFloat_AsDouble(tempp);
  87.            
  88.  
  89.             tempxx = tempx - *avg;
  90.             sumall = sumall + intpow(tempxx,moment);//pow(tempxx,moment);
  91.             //test_var = (double) moment;
  92.             //break;
  93.  
  94.             }
  95.         temp_r = sumall / (count - samp);
  96.         return &temp_r;
  97.     };
  98.  
  99.  
  100.    
  101. int
  102. sample(PyObject *pytrue)
  103.     {
  104.         if (pytrue == Py_True)
  105.             {
  106.             return 1;
  107.             }
  108.         return 0;
  109.     }
  110.  
  111. double*
  112. stat_avgcheck(PyObject *avgp, PyObject *seq, Py_ssize_t count)
  113.     {
  114.         double *returnp = NULL;
  115.         static double temp;
  116.         Py_ssize_t a;
  117.         PyObject *tempp;
  118.         if  (avgp == NULL)
  119.             {
  120.             returnp =  (double *) avg_func(seq, count);
  121.             if (returnp == NULL)
  122.                 {
  123.                 PyErr_SetString(StatsError, "Must use real numbers");
  124.                 return NULL;
  125.                 }
  126.             return returnp;
  127.             }
  128.         else
  129.             {
  130.             if (PyInt_Check(avgp) || PyLong_Check(avgp) || PyFloat_Check(avgp))
  131.                 {
  132.                 temp = PyFloat_AsDouble(avgp);
  133.                 returnp = &temp;
  134.                 for (a=0; a != count; a++)
  135.                     {
  136.                     tempp = PySequence_Fast_GET_ITEM(seq,a);
  137.                     if (PyInt_Check(tempp) || PyLong_Check(tempp) || PyFloat_Check(tempp))
  138.                         {
  139.                         continue;
  140.                         }
  141.                     else
  142.                         {
  143.                         PyErr_SetString(StatsError, "Must use real numbers");
  144.                         return NULL;
  145.                         }
  146.                     }
  147.                 return returnp;
  148.                 }
  149.             else {
  150.                 PyErr_SetString(StatsError, "avg must be a real number");
  151.                 return NULL;
  152.                 }
  153.             }
  154.     };
  155.    
  156.  
  157.  
  158.  
  159.  
  160.  
  161. static PyObject*
  162. stat_avg(PyObject *self, PyObject *list)
  163.     {
  164.         PyObject *obj = NULL;
  165.         PyObject *seq = NULL;
  166.         double *returnp = NULL;
  167.         Py_ssize_t count;
  168.         double avg = 0.00;
  169.         if (!PyArg_ParseTuple(list, "O", &obj))
  170.             return NULL;
  171.         seq = PySequence_Fast(obj, "Expected a Sequence");
  172.         if (seq == NULL)
  173.             {
  174.             PyErr_SetString(StatsError, "Problem with the sequence #1");
  175.             return NULL;
  176.             }
  177.         count = PySequence_Fast_GET_SIZE(seq);
  178.         if (count < 0)
  179.             {
  180.             Py_DECREF(seq);
  181.             PyErr_SetString(StatsError, "Problem with returning the count of the sequence");
  182.             return NULL;
  183.             }
  184.         if (count == 0)
  185.             {
  186.             Py_DECREF(seq);
  187.             PyErr_SetString(StatsError, "Can not have an empty sequence");
  188.             return NULL;
  189.             }
  190.         returnp = (double *) avg_func(seq, count);
  191.         if (returnp == NULL)
  192.             {
  193.             Py_DECREF(seq);
  194.             PyErr_SetString(StatsError, "Must use real numbers");
  195.             return NULL;
  196.             }
  197.         avg = (double) *returnp;
  198.         Py_DECREF(seq);
  199.         free(returnp);
  200.         return PyFloat_FromDouble(avg);
  201.     };
  202.  
  203.    
  204. static PyObject*
  205. stat_variance(PyObject *self, PyObject *list, PyObject *keywds)
  206.     {
  207.         PyObject *obj = NULL;
  208.         PyObject *seq = NULL;
  209.         PyObject *pytrue = Py_True;
  210.         double *return_avgp = NULL;
  211.         double *return_varp = NULL;
  212.         PyObject *avgp = NULL;
  213.         int samp = 0;
  214.         Py_ssize_t count;
  215.         //double  variance = 0.00;//avg,
  216.        
  217.         static char *kwlist[] = {"list", "avg", "sample", NULL};
  218.        
  219.         if (!PyArg_ParseTupleAndKeywords(list, keywds, "O|OO", kwlist, &obj, &avgp, &pytrue))
  220.             return NULL;
  221.         if (PyBool_Check(pytrue))
  222.             {
  223.             samp = sample(pytrue);
  224.             }
  225.         else{
  226.             PyErr_SetString(StatsError, "sample arg not of the form True / False");
  227.             return NULL;
  228.             }
  229.         seq = PySequence_Fast(obj, "Expected a Sequence");
  230.         if (seq == NULL)
  231.             return NULL;
  232.         count = PySequence_Fast_GET_SIZE(seq);
  233.         if (count < 0)
  234.             return NULL;
  235.         if (count == 0)
  236.             {
  237.             PyErr_SetString(StatsError, "Can not have an empty sequence");
  238.             return NULL;
  239.             }
  240.         return_avgp =  (double *) stat_avgcheck(avgp, seq, count);
  241.         if (return_avgp == NULL)
  242.             {
  243.             return NULL;
  244.             }
  245.         return_varp = var(seq, count, return_avgp, samp, 2);
  246.         if (return_varp == NULL)
  247.             {
  248.             return NULL;
  249.             }
  250.         Py_DECREF(seq);
  251.         free(return_avgp);
  252.         return PyFloat_FromDouble(*return_varp);
  253.     };
  254.    
  255.  
  256. static PyObject*
  257. stat_stdev(PyObject *self, PyObject *list, PyObject *keywds)
  258.     {
  259.         PyObject *obj = NULL;
  260.         PyObject *seq = NULL;
  261.         PyObject *pytrue = Py_True;
  262.         PyObject *avgp = NULL;
  263.         //double *returnp = NULL;
  264.         double *return_avgp = NULL;
  265.         double *return_varp = NULL;
  266.         Py_ssize_t count;
  267.         double  stdev = 0.00;//avg,variance,
  268.         int samp = 0;
  269.        
  270.        
  271.         static char *kwlist[] = {"list", "avg", "sample", NULL};
  272.        
  273.         if (!PyArg_ParseTupleAndKeywords(list, keywds, "O|OO", kwlist, &obj, &avgp, &pytrue))
  274.             return NULL;
  275.         if (PyBool_Check(pytrue))
  276.             {
  277.             samp = sample(pytrue);
  278.             }
  279.         else{
  280.             PyErr_SetString(StatsError, "sample arg not of the form True / False");
  281.             return NULL;
  282.             }
  283.         seq = PySequence_Fast(obj, "Expected a Sequence");
  284.         if (seq == NULL)
  285.             return NULL;
  286.         count = PySequence_Fast_GET_SIZE(seq);
  287.         if (count < 0)
  288.             return NULL;
  289.         if (count == 0)
  290.             {
  291.             PyErr_SetString(StatsError, "Can not have an empty sequence");
  292.             return NULL;
  293.             }
  294.         return_avgp =  (double *) stat_avgcheck(avgp, seq, count);
  295.         if (return_avgp == NULL)
  296.             {
  297.             return NULL;
  298.             }
  299.         //avg = *return_avgp;
  300.         return_varp = var(seq, count, return_avgp, samp, 2);
  301.         stdev = sqrt(*return_varp);
  302.         free(return_avgp);
  303.         Py_DECREF(seq);
  304.         return PyFloat_FromDouble(stdev);
  305.     };
  306.  
  307.  
  308. static PyObject*
  309. stat_skew(PyObject *self, PyObject *list, PyObject *keywds)
  310.     {
  311.         PyObject *obj = NULL;
  312.         PyObject *seq = NULL;
  313.         PyObject *pytrue = Py_True;
  314.         PyObject *avgp = NULL;
  315.         double sampx = 0.00;
  316.         //double avg = 0;
  317.         double *return_avgp = NULL;
  318.         double *return_varp = NULL;
  319.         double *third_moment = NULL;
  320.         Py_ssize_t count = 0;
  321.         double second_moment, skew = 0.00;//, avg, third_moment,variance,  
  322.         int samp = 0;
  323.         static char *kwlist[] = {"list", "avg", "sample", NULL};
  324.        
  325.         if (!PyArg_ParseTupleAndKeywords(list, keywds, "O|OO", kwlist, &obj, &avgp, &pytrue))
  326.             return NULL;
  327.         seq = PySequence_Fast(obj, "Expected a Sequence");
  328.         if (seq == NULL)
  329.             return NULL;
  330.         count = PySequence_Fast_GET_SIZE(seq);
  331.         if (count < 0)
  332.             return NULL;
  333.         if (count == 0)
  334.             {
  335.             PyErr_SetString(StatsError, "Can not have an empty sequence");
  336.             return NULL;
  337.             }
  338.         if (PyBool_Check(pytrue))
  339.             {
  340.             samp = sample(pytrue);
  341.             if (samp == 1)
  342.                 {
  343.                 sampx = sqrt(count*(count-1))/(count-2);
  344.                 }
  345.             else {
  346.                 sampx = 1;
  347.                 }
  348.             }
  349.         else{
  350.             PyErr_SetString(StatsError, "sample arg not of the form True / False");
  351.             return NULL;
  352.             }
  353.         return_avgp =  (double *) stat_avgcheck(avgp, seq, count);
  354.         if (return_avgp == NULL)
  355.             {
  356.             return NULL;
  357.             }
  358.         //avg = *return_avgp;
  359.         samp = 0;
  360.         return_varp = var(seq, count, return_avgp, samp, 2);
  361.         double temp_second = *return_varp;
  362.         third_moment = var(seq, count, return_avgp, 0, 3);
  363.         second_moment = pow(temp_second, 1.5);
  364.         skew = (*third_moment / second_moment)*sampx;
  365.         free(return_avgp);
  366.         Py_DECREF(seq);
  367.         return PyFloat_FromDouble(skew);
  368.     };
  369.    
  370.    
  371.    
  372.    
  373. static PyObject*
  374. stat_kurt(PyObject *self, PyObject *list, PyObject *keywds)
  375.     {
  376.         PyObject *obj = NULL;
  377.         PyObject *seq = NULL;
  378.         PyObject *pytrue = Py_True;
  379.         PyObject *avgp = NULL;
  380.         double sampx = 0.00;
  381.         double *return_avgp = NULL;
  382.         double *return_varp = NULL;
  383.         double *fourth_moment = NULL;
  384.         Py_ssize_t count;
  385.         double second_moment, kurt = 0.00;//, avg, fourth_moment,variance,
  386.         int samp = 0;
  387.        
  388.         static char *kwlist[] = {"list", "avg", "sample", NULL};
  389.        
  390.         if (!PyArg_ParseTupleAndKeywords(list, keywds, "O|OO", kwlist, &obj, &avgp, &pytrue))
  391.             return NULL;
  392.         seq = PySequence_Fast(obj, "Expected a Sequence");
  393.         if (seq == NULL)
  394.             return NULL;
  395.         count = PySequence_Fast_GET_SIZE(seq);
  396.         if (count < 0)
  397.             return NULL;
  398.         if (count == 0) {
  399.             PyErr_SetString(StatsError, "Can not have an empty sequence");
  400.             return NULL;
  401.         }
  402.         if (PyBool_Check(pytrue))
  403.             {
  404.             samp = sample(pytrue);
  405.             if (samp == 1)
  406.                 {
  407.                 sampx = (count-1)/((count-2)*(count-3));
  408.                 //=(J7-1)/((J7-2)*(J7-3))
  409.                 }
  410.             }
  411.         else{
  412.             PyErr_SetString(StatsError, "sample arg not of the form True / False");
  413.             return NULL;
  414.             }
  415.         return_avgp =  (double *) stat_avgcheck(avgp, seq, count);
  416.         if (return_avgp == NULL)
  417.             {
  418.             return NULL;
  419.             }
  420.         return_varp = var(seq, count, return_avgp, 0, 2);
  421.        
  422.         fourth_moment = var(seq, count, return_avgp, 0, 4);
  423.        
  424.         second_moment = intpow(*return_varp, 2);
  425.        
  426.         kurt = (*fourth_moment/second_moment) - 3;
  427.         double btwo = 0.00;
  428.         if (samp ==1)
  429.             {
  430.             btwo = ((count+1)*kurt)+6;
  431.             kurt = btwo*sampx;
  432.             }
  433.        
  434.         free(return_avgp);
  435.         Py_DECREF(seq);
  436.         return PyFloat_FromDouble(kurt);
  437.     };
  438.    
  439.    
  440. static PyMethodDef stat_funcs[] = {
  441.     {"mean", stat_avg, METH_VARARGS},
  442.     {"var", (PyCFunction)stat_variance, METH_VARARGS | METH_KEYWORDS, "finds population and sample variance"},
  443.     {"stdev", (PyCFunction)stat_stdev, METH_VARARGS | METH_KEYWORDS, "finds population and sample standard deviation"},
  444.     {"skew", (PyCFunction)stat_skew, METH_VARARGS | METH_KEYWORDS, "finds population and sample skewness"},
  445.     {"kurt", (PyCFunction)stat_kurt, METH_VARARGS | METH_KEYWORDS, "finds population and sample excess Kurtosis"},
  446.     {NULL, NULL, 0, NULL}
  447. };
  448.  
  449.  
  450. // PyDoc_STRVAR(module_doc,
  451. // "This is a module to hold common tools used in descriptive statistics");
  452. // static struct PyModuleDef stat_funcs_mod = {
  453.     // PyModuleDef_HEAD_INIT,
  454.     // "stats",
  455.     // module_doc,
  456.     // -1,
  457.     // stat_funcs,
  458.     // NULL,
  459.     // NULL,
  460.     // NULL,
  461.     // NULL
  462. // };
  463.    
  464.    
  465. PyMODINIT_FUNC initstats(void)
  466. {
  467.     PyObject *m;
  468.     m = Py_InitModule3("stats", stat_funcs,"statistics module");
  469.     if (StatsError == NULL) {
  470.         StatsError = PyErr_NewException("stats.StatsError", NULL, NULL);
  471.         if (StatsError == NULL)
  472.             return;
  473.     }
  474.     Py_INCREF(StatsError);
  475.     PyModule_AddObject(m, "StatsError", StatsError);
  476.  
  477. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement