AI-MaxEntropy
view release on metacpan or search on metacpan
AI-MaxEntropy.xs view on Meta::CPAN
/**************************************************************************
* XS of AI:MaxEntropy
* -> by Laye Suen
**************************************************************************/
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
/* Macros for debugging */
/* uncomment the line below to enable tracing and timing */
/*#define __ENABLE_TRACING__*/
#ifdef __ENABLE_TRACING__
#include "time.h"
#define TRACE(msg) \
printf(_fn); printf(": "); printf(msg); \
printf(": %0.10f s\n", 1.0 * (clock() - _t) / CLOCKS_PER_SEC); \
fflush(stdout); _t = clock()
#define dTRACE(fn) clock_t _t = clock(); char* _fn = fn
#else
#define TRACE(msg)
#define dTRACE
#endif
/* Other macros */
#define hvref_fetch(hvref, key) \
hv_fetch((HV*)SvRV(hvref), key, strlen(key), 0)
#define hvref_exists(hvref, key) \
hv_exists((HV*)SvRV(hvref), key, strlen(key))
#define hvref_store(hvref, key, value) \
hv_store((HV*)SvRV(hvref), key, strlen(key), value, 0)
#define hvref_delete(hvref, key) \
hv_delete((HV*)SvRV(hvref), key, strlen(key), G_DISCARD)
/* internal structures */
struct samples_t {
int s_num;
int* x_len;
int** x;
int* y;
double* w;
};
struct f_map_t {
int y_num;
int** lambda_idx;
};
/**************************************************************************
* EXPORTED XSUBS
**************************************************************************/
MODULE = AI::MaxEntropy PACKAGE = AI::MaxEntropy
void
_neg_log_likelihood(lambda_in, step, self, OUTLIST SV* f, OUTLIST SV* g)
AV* lambda_in
SV* step
SV* self
PREINIT:
dTRACE("_neg_log_likelihood");
/* fetch the pre-cached samples and f_map */
SV* _c = *hvref_fetch(self, "_c");
struct samples_t* samples =
INT2PTR(struct samples_t*, SvIV(*hvref_fetch(_c, "samples")));
struct f_map_t* f_map =
INT2PTR(struct f_map_t*, SvIV(*hvref_fetch(_c, "f_map")));
int** lambda_idx = f_map->lambda_idx;
/* fetch other useful data */
SV* smoother = *hvref_fetch(self, "smoother");
int x_num = SvIV(*hvref_fetch(self, "x_num"));
int y_num = SvIV(*hvref_fetch(self, "y_num"));
int f_num = SvIV(*hvref_fetch(self, "f_num"));
( run in 1.481 second using v1.01-cache-2.11-cpan-d8267643d1d )