File Coverage

pp-hmmpath.c
Criterion Covered Total %
statement 33 54 61.1
branch 27 124 21.7
condition n/a
subroutine n/a
pod n/a
total 60 178 33.7


line stmt bran cond sub pod time code
1              
2             #line 453 "lib/PDL/PP.pm"
3             /*
4             * THIS FILE WAS GENERATED BY PDL::PP from HMM.pd! Do not modify!
5             */
6              
7             #define PDL_FREE_CODE(trans, destroy, comp_free_code, ntpriv_free_code) \
8             if (destroy) { \
9             comp_free_code \
10             } \
11             if ((trans)->dims_redone) { \
12             ntpriv_free_code \
13             }
14              
15             #include "EXTERN.h"
16             #include "perl.h"
17             #include "XSUB.h"
18             #include "pdl.h"
19             #include "pdlcore.h"
20             #define PDL PDL_HMM
21             extern Core* PDL; /* Structure hold core C functions */
22             #line 23 "pp-hmmpath.c"
23              
24             #include
25              
26             /*#define DEBUG_ALPHA*/
27             /*#define DEBUG_BETA*/
28             /*#define DEBUG_VITERBI*/
29              
30              
31             /* logadd(x,y) = log(exp(x)+exp(y))
32             * + Code from Manning & Schütze (1997), Sec. 9.4, page 337
33             *
34             * LOG_BIG = log(1E31)
35             */
36             #define LOG_BIG 71.3801378828154
37             #define LOG_ZERO -1E+38
38             #define LOG_ONE 0
39             #define LOG_NONE 1
40             static inline double logadd1(double x, double y) {
41             if (y-x > LOG_BIG) return y;
42             else if (x-y > LOG_BIG) return x;
43             /*else return min(x,y) + log(exp(x-min(x,y)) + exp(y-min(x,y))); */
44             else if (x
45             else return y + log(exp(x-y) + 1);
46             }
47             static inline double logadd0(double x, double y) {
48             return log(exp(x)+exp(y));
49             }
50              
51             /* logdiff(x,y) = log(exp(x)-exp(y))
52             * + adapted from above
53             * + always returns positive (i.e. symmetric difference)
54             */
55             static inline double logdiff1(double x, double y) {
56             if (y-x > LOG_BIG) { return y; }
57             else if (x-y > LOG_BIG) { return x; }
58             /*else { return max(x,y) + log(exp(max(x,y)-max(x,y)) - exp(min(x,y)-max(x,y))); } */
59             /* = max(x,y) + log( 1 - exp(min(x,y)-max(x,y))); } */
60             else if (x>y) { return x + log( 1 - exp(y-x)); }
61             else { return y + log( 1 - exp(x-y)); }
62             }
63             static inline double logdiff0(double x, double y) {
64             return log(x>y ? (exp(x)-exp(y)) : (exp(y)-exp(x)));
65             }
66              
67             /*
68             #define logadd(x,y) logadd0(x,y)
69             #define logdiff(x,y) logdiff0(x,y)
70             */
71              
72             #define logadd(x,y) logadd1(x,y)
73             #define logdiff(x,y) logdiff1(x,y)
74              
75              
76             #line 1857 "lib/PDL/PP.pm"
77             pdl_error pdl_hmmpath_readdata(pdl_trans *__privtrans) {
78             pdl_error PDL_err = {0, NULL, 0};
79             #line 80 "pp-hmmpath.c"
80 2 50         if (!__privtrans->broadcast.incs) return PDL->make_error(PDL_EUSERERROR, "Error in hmmpath:" "broadcast.incs NULL");
81             /* broadcastloop declarations */
82             int __brcloopval;
83             register PDL_Indx __tind0,__tind1; /* counters along dim */
84 2           register PDL_Indx __tnpdls = __privtrans->broadcast.npdls;
85             /* dims here are how many steps along those dims */
86 2           register PDL_Indx __tinc0_psi = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,0,0);
87 2           register PDL_Indx __tinc0_qfinal = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,1,0);
88 2           register PDL_Indx __tinc0_path = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,2,0);
89 2           register PDL_Indx __tinc1_psi = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,0,1);
90 2           register PDL_Indx __tinc1_qfinal = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,1,1);
91 2           register PDL_Indx __tinc1_path = PDL_BRC_INC(__privtrans->broadcast.incs,__tnpdls,2,1);
92             #define PDL_BROADCASTLOOP_START_hmmpath_readdata PDL_BROADCASTLOOP_START( \
93             readdata, \
94             __privtrans->broadcast, \
95             __privtrans->vtable, \
96             psi_datap += __offsp[0]; \
97             qfinal_datap += __offsp[1]; \
98             path_datap += __offsp[2]; \
99             , \
100             ( ,psi_datap += __tinc1_psi - __tinc0_psi * __tdims0 \
101             ,qfinal_datap += __tinc1_qfinal - __tinc0_qfinal * __tdims0 \
102             ,path_datap += __tinc1_path - __tinc0_path * __tdims0 \
103             ), \
104             ( ,psi_datap += __tinc0_psi \
105             ,qfinal_datap += __tinc0_qfinal \
106             ,path_datap += __tinc0_path \
107             ) \
108             )
109             #define PDL_BROADCASTLOOP_END_hmmpath_readdata PDL_BROADCASTLOOP_END( \
110             __privtrans->broadcast, \
111             psi_datap -= __tinc1_psi * __tdims1 + __offsp[0]; \
112             qfinal_datap -= __tinc1_qfinal * __tdims1 + __offsp[1]; \
113             path_datap -= __tinc1_path * __tdims1 + __offsp[2]; \
114             )
115 2           register PDL_Indx __inc_path_T = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,2,0)]; (void)__inc_path_T;
116 2           register PDL_Indx __inc_psi_N = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,0,0)]; (void)__inc_psi_N;register PDL_Indx __inc_psi_T = __privtrans->inc_sizes[PDL_INC_ID(__privtrans->vtable,0,1)]; (void)__inc_psi_T;
117             #ifndef PDL_DECLARE_PARAMS_hmmpath_1
118             #define PDL_DECLARE_PARAMS_hmmpath_1(PDL_TYPE_OP,PDL_PPSYM_OP,PDL_TYPE_PARAM_qfinal,PDL_PPSYM_PARAM_qfinal,PDL_TYPE_PARAM_path,PDL_PPSYM_PARAM_path) \
119             PDL_DECLARE_PARAMETER(PDL_TYPE_OP, psi, (__privtrans->pdls[0]), 1, PDL_PPSYM_OP) \
120             PDL_DECLARE_PARAMETER(PDL_TYPE_PARAM_qfinal, qfinal, (__privtrans->pdls[1]), 1, PDL_PPSYM_PARAM_qfinal) \
121             PDL_DECLARE_PARAMETER(PDL_TYPE_PARAM_path, path, (__privtrans->pdls[2]), 1, PDL_PPSYM_PARAM_path)
122             #endif
123             #define PDL_IF_BAD(t,f) f
124 2           switch (__privtrans->__datatype) { /* Start generic switch */
125 0           case PDL_F: {
126 0 0         PDL_DECLARE_PARAMS_hmmpath_1(PDL_Float,F,PDL_Indx,N,PDL_Indx,N)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
127 0 0         PDL_BROADCASTLOOP_START_hmmpath_readdata {
    0          
    0          
    0          
    0          
    0          
    0          
128             /*-- Initialize: t==T-1: state_(t)==final() --*/
129 0           int t = __privtrans->ind_sizes[1]-1;
130 0           (path_datap)[0+(__inc_path_T*(t))] = (qfinal_datap)[0];
131              
132             /*-- Main: T-1 > t >= 0: Loop: time==t --*/
133 0 0         for (t--; t>=0; t--) {
134 0           int q_tp1 = (path_datap)[0+(__inc_path_T*(t+1))];
135 0           (path_datap)[0+(__inc_path_T*(t))] = (psi_datap)[0+(__inc_psi_N*(q_tp1))+(__inc_psi_T*(t+1))];
136             }
137 0 0         }PDL_BROADCASTLOOP_END_hmmpath_readdata
    0          
138 0           } break;
139 0           case PDL_D: {
140 0 0         PDL_DECLARE_PARAMS_hmmpath_1(PDL_Double,D,PDL_Indx,N,PDL_Indx,N)
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
141 0 0         PDL_BROADCASTLOOP_START_hmmpath_readdata {
    0          
    0          
    0          
    0          
    0          
    0          
142             /*-- Initialize: t==T-1: state_(t)==final() --*/
143 0           int t = __privtrans->ind_sizes[1]-1;
144 0           (path_datap)[0+(__inc_path_T*(t))] = (qfinal_datap)[0];
145              
146             /*-- Main: T-1 > t >= 0: Loop: time==t --*/
147 0 0         for (t--; t>=0; t--) {
148 0           int q_tp1 = (path_datap)[0+(__inc_path_T*(t+1))];
149 0           (path_datap)[0+(__inc_path_T*(t))] = (psi_datap)[0+(__inc_psi_N*(q_tp1))+(__inc_psi_T*(t+1))];
150             }
151 0 0         }PDL_BROADCASTLOOP_END_hmmpath_readdata
    0          
152 0           } break;
153 2           case PDL_LD: {
154 2 50         PDL_DECLARE_PARAMS_hmmpath_1(PDL_LDouble,E,PDL_Indx,N,PDL_Indx,N)
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
155 10 50         PDL_BROADCASTLOOP_START_hmmpath_readdata {
    50          
    50          
    50          
    50          
    100          
    100          
156             /*-- Initialize: t==T-1: state_(t)==final() --*/
157 4           int t = __privtrans->ind_sizes[1]-1;
158 4           (path_datap)[0+(__inc_path_T*(t))] = (qfinal_datap)[0];
159              
160             /*-- Main: T-1 > t >= 0: Loop: time==t --*/
161 10 100         for (t--; t>=0; t--) {
162 6           int q_tp1 = (path_datap)[0+(__inc_path_T*(t+1))];
163 6           (path_datap)[0+(__inc_path_T*(t))] = (psi_datap)[0+(__inc_psi_N*(q_tp1))+(__inc_psi_T*(t+1))];
164             }
165 2 50         }PDL_BROADCASTLOOP_END_hmmpath_readdata
    50          
166 2           } break;
167 0           default: return PDL->make_error(PDL_EUSERERROR, "PP INTERNAL ERROR in hmmpath: unhandled datatype(%d), only handles (FDE)! PLEASE MAKE A BUG REPORT\n", __privtrans->__datatype);
168             }
169             #undef PDL_IF_BAD
170 2           return PDL_err;
171             }
172              
173             static pdl_datatypes pdl_hmmpath_vtable_gentypes[] = { PDL_F, PDL_D, PDL_LD, -1 };
174             static PDL_Indx pdl_hmmpath_vtable_realdims[] = { 2, 0, 1 };
175             static char *pdl_hmmpath_vtable_parnames[] = { "psi","qfinal","path" };
176             static short pdl_hmmpath_vtable_parflags[] = {
177             0,
178             PDL_PARAM_ISTYPED,
179             PDL_PARAM_ISCREAT|PDL_PARAM_ISOUT|PDL_PARAM_ISTYPED|PDL_PARAM_ISWRITE
180             };
181             static pdl_datatypes pdl_hmmpath_vtable_partypes[] = { -1, PDL_IND, PDL_IND };
182             static PDL_Indx pdl_hmmpath_vtable_realdims_starts[] = { 0, 2, 2 };
183             static PDL_Indx pdl_hmmpath_vtable_realdims_ind_ids[] = { 0, 1, 1 };
184             static char *pdl_hmmpath_vtable_indnames[] = { "N","T" };
185             pdl_transvtable pdl_hmmpath_vtable = {
186             PDL_TRANS_DO_BROADCAST, 0, pdl_hmmpath_vtable_gentypes, 2, 3, NULL /*CORE21*/,
187             pdl_hmmpath_vtable_realdims, pdl_hmmpath_vtable_parnames,
188             pdl_hmmpath_vtable_parflags, pdl_hmmpath_vtable_partypes,
189             pdl_hmmpath_vtable_realdims_starts, pdl_hmmpath_vtable_realdims_ind_ids, 3,
190             2, pdl_hmmpath_vtable_indnames,
191             NULL, pdl_hmmpath_readdata, NULL,
192             NULL,
193             0,"PDL::HMM::hmmpath"
194             };
195              
196              
197 2           pdl_error pdl_run_hmmpath(pdl *psi,pdl *qfinal,pdl *path) {
198 2           pdl_error PDL_err = {0, NULL, 0};
199 2 50         if (!PDL) return (pdl_error){PDL_EFATAL, "PDL core struct is NULL, can't continue",0};
200 2           pdl_trans *__privtrans = PDL->create_trans(&pdl_hmmpath_vtable);
201 2 50         if (!__privtrans) return PDL->make_error_simple(PDL_EFATAL, "Couldn't create trans");
202 2           __privtrans->pdls[0] = psi;
203 2           __privtrans->pdls[1] = qfinal;
204 2           __privtrans->pdls[2] = path;
205 2 50         PDL_RETERROR(PDL_err, PDL->type_coerce(__privtrans));
206 2 50         PDL_RETERROR(PDL_err, PDL->make_trans_mutual(__privtrans));
207 2           return PDL_err;
208             }