static A jtgrd1spz(J jt,A w,I wf,I wcr){A z;I*ws,zn; ws=AS(w); RE(zn=prod(wf+!!wcr,ws)); GATV(z,INT,zn,1+wf,ws); if(!wcr)*(AS(z)+wf)=1; R z; } /* allocate result for grd1sp__ */
/* * Manual page at fst_man.def */ INT16 CGEN_PUBLIC CFst_Rcs(CFst* _this, INT32 nUnit, FLOAT64 nSeed) { #ifdef __UNENTANGLE_FST return IERROR(_this,FST_INTERNAL,__FILE__,__LINE__,0); /* NOTE: __UNENTANGLE_FST was defined (probably in dlp_config.h or fst.def). * Undefine it to use this feature! */ #else /* #ifdef __UNENTANGLE_FST */ INT32 nU = 0; /* Current unit */ FST_ITYPE nS = 0; /* Current state */ FST_ITYPE nS2 = 0; /* Current state */ FST_ITYPE nFS = 0; /* First state of current unit */ FST_ITYPE nXS = 0; /* Number of states of current unit */ FST_ITYPE nT = 0; /* Current transition */ FST_ITYPE nFT = 0; /* First transition of current unit */ FST_ITYPE nXT = 0; /* Number of tran. of current unit */ CData* idP = NULL; /* Incidence matrix */ CData* idB = NULL; /* Constanct vector of (idP-E)idX=idB */ CData* idI = NULL; /* idI = (idP-E)^-1 */ CData* idX = NULL; /* Solution of (idP-E)idX=idB */ FST_WTYPE nPSum = 0.; /* Probability sum/state */ INT32 nIcW = -1; /* Index of probability comp. in td */ INT32 nIcRcs = -1; /* Index of ref. counter comp. in sd */ INT32 nIcRct = -1; /* Index of ref. counter comp. in td */ /* Validation */ CHECK_THIS_RV(NOT_EXEC); CFst_Check(_this); if (CFst_Wsr_GetType(_this,&nIcW)!=FST_WSR_PROB) return IERROR(_this,FST_MISS,"transition probability component",NC_TD_PSR,"transition table"); /* Initialize - Find or add reference counters */ nIcRcs = CData_FindComp(AS(CData,_this->sd),NC_SD_RC); nIcRct = CData_FindComp(AS(CData,_this->td),NC_TD_RC); if (nIcRcs<0) { CData_AddComp(AS(CData,_this->sd),NC_SD_RC,DLP_TYPE(FST_WTYPE)); nIcRcs = CData_GetNComps(AS(CData,_this->sd))-1; } if (nIcRct<0) { CData_AddComp(AS(CData,_this->td),NC_TD_RC,DLP_TYPE(FST_WTYPE)); nIcRct = CData_GetNComps(AS(CData,_this->td))-1; } /* Initialize - Create auxilary instances */ ICREATEEX(CData,idP,"~CFst_Reverse.idP",NULL); ICREATEEX(CData,idB,"~CFst_Reverse.idB",NULL); ICREATEEX(CData,idI,"~CFst_Reverse.idI",NULL); ICREATEEX(CData,idX,"~CFst_Reverse.idX",NULL); /* Loop over units */ for (nU=nUnit<0?0:nUnit; nU<UD_XXU(_this); nU++) { CData_Reset(BASEINST(idP),TRUE); CData_Reset(BASEINST(idB),TRUE); CData_Reset(BASEINST(idI),TRUE); CData_Reset(BASEINST(idX),TRUE); nFS = UD_FS(_this,nU); nXS = UD_XS(_this,nU); nFT = UD_FT(_this,nU); nXT = UD_XT(_this,nU); /* Export transposed ergodic incidence matrix */ IF_NOK(CData_AddNcomps(idP,DLP_TYPE(FST_WTYPE),UD_XS(_this,nU)+1)) break; IF_NOK(CData_Allocate (idP,UD_XS(_this,nU)+1) ) break; IF_NOK(CData_AddNcomps(idB,DLP_TYPE(FST_WTYPE),1 )) break; IF_NOK(CData_Allocate (idB,UD_XS(_this,nU)+1) ) break; /* Fill transposed incidence matrix (summing up probabilities of parallel transitions) */ for (nT=nFT; nT<nFT+nXT; nT++) *(FST_WTYPE*)CData_XAddr(idP,TD_TER(_this,nT),TD_INI(_this,nT)) += *(FST_WTYPE*)CData_XAddr(AS(CData,_this->td),nT,nIcW); for (nS=0; nS<nXS; nS++) { if ((SD_FLG(_this,nS+nFS)&0x01)==0x01) /* Connect final states with start state */ { for (nS2=1, nPSum=0.; nS2<nXS; nS2++) nPSum += *(FST_WTYPE*)CData_XAddr(idP,nS2,nS); *(FST_WTYPE*)CData_XAddr(idP,0,nS) = 1.-nPSum; } *(FST_WTYPE*)CData_XAddr(idP,nS,nS)-=1.; /* Subtract eigenvalue 1 from main diagonal */ *(FST_WTYPE*)CData_XAddr(idP,nXS,nS)=1.; /* Additional equation implementing constraint sum(P_state)=1 */ *(FST_WTYPE*)CData_XAddr(idP,nS,nXS)=1.; /* Additional variable making incidence matrix quadratic */ } /* Fill constant vector */ CData_Fill(idB,CMPLX(1.),CMPLX(0.)); /* Calculate eigenvector of length 1 and eigenvalue 1 --> stationary state probabilities */ CMatrix_Op(idI,idP,T_INSTANCE,NULL,T_IGNORE,OP_INVT); CMatrix_Op(idX,idB,T_INSTANCE,idI,T_INSTANCE,OP_MULT); /* Fill in state reference counters */ if (nSeed>0.) nSeed /= CData_Dfetch(idX,0,0); else nSeed = 1.; for (nS=0; nS<nXS; nS++) CData_Dstore(AS(CData,_this->sd),nSeed*CData_Dfetch(idX,nS,0),nS+nFS,nIcRcs); /* Calculate stationary transition probabilities */ for (nT=nFT; nT<nFT+nXT; nT++) CData_Dstore ( AS(CData,_this->td), CData_Dfetch(AS(CData,_this->sd),TD_INI(_this,nT)+nFS,nIcRcs) * CData_Dfetch(AS(CData,_this->td),nT,nIcW), nT,nIcRct ); /* Clean up */ IDESTROY(idP); IDESTROY(idB); IDESTROY(idI); IDESTROY(idX); /* Stop in single unit mode */ if (nUnit>=0) break; } return O_K; #endif /* #ifdef __UNENTANGLE_FST */ }
av=AV(a); an=AN(a); RZ(z=exta(INT,1+wr,wn,20L)); zv=AV(z); zz=zv+AN(z); if(1==wn){ *zv++=c=*wv; d=1+c; while(c!=d){ if(zv==zz){i=zv-AV(z); RZ(z=ext(0,z)); zv=AV(z)+i; zz=AV(z)+AN(z);} d=c; if(0>c)c+=an; ASSERT(0<=c&&c<an,EVINDEX); *zv++=c=av[c]; }}else{ ICPY(zv,wv,wn); zu=zv; zv+=wn; while(1){ if(zv==zz){i=zv-AV(z); RZ(z=ext(0,z)); zv=AV(z)+i; zz=AV(z)+AN(z); zu=zv-wn;} b=1; DO(wn, d=c=*zu++; if(0>c)c+=an; ASSERT(0<=c&&c<an,EVINDEX); *zv++=c=av[c]; if(c!=d)b=0;); if(b)break; }} i=zv-AV(z); *AS(z)=d=i/wn-1; AN(z)=d*wn; ICPY(1+AS(z),AS(w),wr); R z; } /* {&a^:(<_) w */ static DF1(jtindexseqlim1){A fs; RZ(w); fs=VAV(self)->f; R AT(w)&B01+INT?tclosure(VAV(fs)->g,w):powseqlim(w,fs); } /* {&x^:(<_) w */ static DF2(jtindexseqlim2){ RZ(a&&w); R 1==AR(a)&&AT(a)&INT&&AT(w)&B01+INT?tclosure(a,w):powseqlim(w,amp(ds(CFROM),a)); } /* a {~^:(<_) w */ static DF1(jtpowseq){A fs,gs,x;I n=IMAX;V*sv;
/* * Manual page at fst_man.def */ INT16 CGEN_PUBLIC CFst_Probs(CFst* _this, INT32 nUnit) { INT32 nU = 0; /* Current unit */ FST_ITYPE nS = 0; /* Current state */ FST_ITYPE nT = 0; /* Current transition */ FST_ITYPE nXS = 0; /* Number of states in current unit */ INT16 nWsrt = 0; /* Weight semiring type */ INT32 nIcW = -1; /* Weight component in td */ INT32 nIcPfl = -1; /* Floor probability component in sd */ INT32 nIcRct = -1; /* Reference counter component in td */ INT32 nTrCnt = 0; /* Number of outgoing trans./state */ FST_WTYPE nW = 0.; /* Current weight */ FST_WTYPE nRc = 0.; /* Current reference counter/prob. */ FST_WTYPE nRcSum = 0.; /* Reference counter sum/state */ FST_WTYPE nTW = 0.; /* trans.weight */ FST_WTYPE nTWAgg = 0.; /* trans.weight aggregator for MAP */ CData* idTd = NULL; /* Pointer to transition table */ FST_TID_TYPE* lpTI = NULL; /* Automaton iterator data structure */ BYTE* lpT = NULL; /* Current transition (iteration) */ FST_WTYPE nMAP = _this->m_bUsemap ? _this->m_nMapexp : 0.; /* MAP exponent (0:off,1:min,-1:max) */ INT32 nMAPi = 0; /* MAP iteration index */ /* Validation */ /* --------------------------------- */ CHECK_THIS_RV(NOT_EXEC); /* Check this pointer */ CFst_Check(_this); /* Check FST(s) */ /* Initialize */ /* --------------------------------- */ idTd = AS(CData,_this->td); /* Get pointer to transition table */ nIcRct = CData_FindComp(AS(CData,_this->td),NC_TD_RC); /* Find reference counters */ nWsrt = CFst_Wsr_GetType(_this,&nIcW); /* Find weights and get type */ switch (nWsrt) /* branch for weight semiring type */ { /* >> */ case FST_WSR_NONE: /* No weights */ CData_AddComp(idTd,NC_TD_PSR,DLP_TYPE(FST_WTYPE)); /* Add some */ nIcW = CData_GetNComps(idTd)-1; /* Get index of new weight comp. */ for (nT=0; nT<UD_XXT(_this); nT++) /* Initialize probabilities */ CData_Dstore(idTd,1.,nT,nIcW); /* ... */ break; /* * */ case FST_WSR_PROB: /* Probabilities */ break; /* * (nothing to be done) */ case FST_WSR_TROP: /* fall through */ /* Tropical weights */ case FST_WSR_LOG: /* Logarithmic weights */ for (nT=0; nT<UD_XXT(_this); nT++) /* Loop over all transitions */ CData_Dstore(idTd, /* Convert to probabilities */ exp(-1.*CData_Dfetch(idTd,nT,nIcW)),nT,nIcW); /* | */ break; /* * */ default: /* Unknown weight semiring */ DLPASSERT(FMSG("Unknown weight semiring")); /* New weight semiring type? */ CData_SetCname(idTd,nIcW,NC_TD_PSR); /* Rename to probabilities */ for (nT=0; nT<UD_XXT(_this); nT++) /* Initialize probabilities */ CData_Dstore(idTd,1.,nT,nIcW); /* ... */ } /* << */ if (_this->m_nSymbols>0 && _this->m_nRcfloor>0.) /* Smoothing enabled */ { /* >> */ if (CData_FindComp(AS(CData,_this->sd),"~PFL")<0) /* No floor prob. comp. at states */ CData_AddComp(AS(CData,_this->sd),"~PFL",DLP_TYPE(FST_WTYPE)); /* Add it */ nIcPfl = CData_FindComp(AS(CData,_this->sd),"~PFL"); /* Get index of floor prob. comp. */ } /* << */ /* Loop over units */ /* --------------------------------- */ for (nU=nUnit<0?0:nUnit; nU<UD_XXU(_this); nU++) /* For all units ... */ { /* >> */ lpTI = CFst_STI_Init(_this,nU,FSTI_SORTINI); /* Get sorted transition iterator */ /* Loop over states */ /* - - - - - - - - - - - - - - - - */ for (nS=0,nXS=UD_XS(_this,nU); nS<nXS; nS++) /* For all states of the unit ... */ /* Loop over MAP-Iterations if MAP enabled */ /* - - - - - - - - - - - - - - - - */ for(nMAPi=0;nMAPi<(nMAP!=0.?10:1);nMAPi++) /* For all MAP-iterations ... */ { /* >> */ /* Pass 1: Count outgoing transitions and sum up reference counters */ /* */ nRcSum = 0.; /* Reset ref. ctr. accumulator */ nTWAgg = 0.; /* Reset MAP TW accumulator */ nTrCnt = 0; /* Reset transition counter */ lpT = NULL; /* Initialize transition pointer */ while ((lpT=CFst_STI_TfromS(lpTI,nS,lpT))!=NULL) /* Enumerate transitions at nS */ { /* >> */ nRc = nIcRct>=0 /* Get ref. ctr. or prob. */ ? CData_Dfetch(idTd,CFst_STI_GetTransId(lpTI,lpT),nIcRct) /* | */ : *CFst_STI_TW(lpTI,lpT); /* | */ if (nRc>=0.) nRcSum+=nRc; /* Accumulate non-neg. values */ if (nMAP!=0. && nRc>0.) /* if MAP enabled and T. used */ { /* >> */ nTW = *CFst_STI_TW(lpTI,lpT); /* get trans.weight */ if(nTW<=0.) nTW=0.0001; /* solve prob. with 0 TW's */ nTWAgg += nTW*log(nTW); /* acc. TW's */ } /* << */ nTrCnt++; /* Count transitions */ } /* << */ if (nIcPfl) /* Smoothing eneabled */ CData_Dstore(AS(CData,_this->sd), /* Store floor probability */ _this->m_nRcfloor / (nRcSum + _this->m_nSymbols*_this->m_nRcfloor), /* | */ nS,nIcPfl); /* | */ /* */ /* Pass 2: Calculate probabilities */ /* */ if (nTrCnt) /* If there were trans. at nS */ { /* >> */ lpT = NULL; /* Initialize transition ptr. */ while ((lpT=CFst_STI_TfromS(lpTI,nS,lpT))!=NULL) /* Enumerate transitions at nS */ { /* >> */ nRc = nIcRct>=0 /* Get ref. ctr. or prob. */ ? CData_Dfetch(idTd,CFst_STI_GetTransId(lpTI,lpT),nIcRct) /* | */ : *CFst_STI_TW(lpTI,lpT); /* | */ if (nMAP!=0.) /* if MAP enabled */ { /* << */ nTW = *CFst_STI_TW(lpTI,lpT); /* get trans.weight */ if(nTW<=0.) nTW=0.0001; /* solve prob. with 0 TW's */ if(nIcRct>=0 && _this->m_nSymbols>0 && _this->m_nRcfloor>0) /* do jeffrey smooth? */ nRc = nRcSum * (nRc + (FST_WTYPE)_this->m_nRcfloor) / /* smooth nRc */ (nRcSum + ((FST_WTYPE)_this->m_nSymbols*_this->m_nRcfloor)); /* | */ nTW = (nRc<=0. ? 0. : (nRc+nMAP*nTW*log(nTW))/(nRcSum+nMAP*nTWAgg));/* do MAP-iteration */ *CFst_STI_TW(lpTI,lpT) = nTW; /* write trans.weight */ } /* >> */ else /* else */ if (nIcRct<0 || _this->m_nSymbols<=0 || _this->m_nRcfloor<=0.) /* No ref.ctrs. or smoothing */ *CFst_STI_TW(lpTI,lpT) = /* Store equally distrib. */ nRcSum==0. ? 1./(FST_WTYPE)nTrCnt : nRc/nRcSum; /* | or renormalized probs.*/ else /* Ref. ctrs. or smoothing */ *CFst_STI_TW(lpTI,lpT) = /* Store Jeffrey smoothed */ (nRc + (FST_WTYPE)_this->m_nRcfloor) / /* | probabilities */ (nRcSum + ((FST_WTYPE)_this->m_nSymbols*_this->m_nRcfloor)); /* | */ } /* << */ } /* << */ } /* << */ CFst_STI_Done(lpTI); /* Destroy iterator */ if (nUnit>=0) break; /* Stop in single unit mode */ } /* << */ /* Convert back to logarithmic/tropical weights */ /* --------------------------------- */ if (nWsrt==FST_WSR_LOG || nWsrt==FST_WSR_TROP) /* Loarithmic or tropical weights */ for (nT=0; nT<UD_XXT(_this); nT++) /* Loop over all transitions */ { /* >> */ nW = CData_Dfetch(idTd,nT,nIcW); /* Get probability */ nW = nW>0. ? -log(nW) : _this->m_nWceil; /* Convert to log./trop. weight */ CData_Dstore(idTd,nW,nT,nIcW); /* Store weight */ } /* << */ /* Clean up */ /* --------------------------------- */ return O_K; /* The end */ }