ISC_STRING ToDoubleByte(const wstring& wSrcStr) { INIT_FUNCTION_SCOPE_LOGGER(); const int NULL_PAD = 1; ISC_STRING dest = new ISC_CHAR[wSrcStr.length() + NULL_PAD]; wcsncpy(TW(dest), wSrcStr.c_str(), wSrcStr.length()); return dest; }
void trace_register_changes (void) { int i; int any = 0; if (!trace) return; #define TB(name,nv,ov) if (nv != ov) { printf ("%s: \033[31m%02x \033[32m%02x\033[0m ", name, ov, nv); ov = nv; any = 1; } #define TW(name,nv,ov) if (nv != ov) { printf ("%s: \033[31m%04x \033[32m%04x\033[0m ", name, ov, nv); ov = nv; any = 1; } if (trace_register_words) { #define TRW(name, idx) TW (name, memory[REGISTER_ADDRESS + (idx)], old_regs[idx]) for (i = 0; i < 32; i += 2) { char buf[10]; int o, n, a; switch (i) { case 0: strcpy (buf, "AX"); break; case 2: strcpy (buf, "BC"); break; case 4: strcpy (buf, "DE"); break; case 6: strcpy (buf, "HL"); break; default: sprintf (buf, "r%d", i); break; } a = REGISTER_ADDRESS + (i ^ 0x18); o = old_regs[i ^ 0x18] + old_regs[(i ^ 0x18) + 1] * 256; n = memory[a] + memory[a + 1] * 256; TW (buf, n, o); old_regs[i ^ 0x18] = n; old_regs[(i ^ 0x18) + 1] = n >> 8; } } else { for (i = 0; i < 32; i ++)
SEXP e_step_theta(SEXP _W, SEXP _P, SEXP _zeta, SEXP _probz, SEXP _Theta) { // the following parameters are inputs and are not updated NumericMatrix Theta(_Theta); // the following parameters serve both as input, but will be updated in M step as output NumericMatrix W(_W); NumericMatrix P(_P); double zeta = as<double>(_zeta); NumericVector probz(_probz); // extract the dimensions int I = P.nrow(); int S = P.ncol(); int K = W.nrow() / S; int J = probz.size(); double _LOW = 1e-10; // the following quantities are outputs NumericVector b_mean(I); NumericVector Z_mean(J); NumericMatrix W_max(K * S, J); NumericMatrix predZ(I, J); // iterators int i, j, k, s;//, likid; // Intermediate matrices NumericVector TP(I); NumericMatrix TW(I, J); NumericMatrix Zcond(I, J); for(i = 0; i < I; i ++){ TP(i) = 0; for(j = 0; j < J; j ++) TW(i, j) = 0; for(k = 0; k < K; k ++){ for(s = 0; s < S; s ++){ if(Theta(k, i + I * s) > 0){ TP(i) += log(P(i, s)); for(j = 0; j < J; j ++) TW(i, j) += log(W(k + K * s, j)); } } } } for(k = 0; k < K; k ++) for(j = 0; j < J; j ++) for(s = 0; s < S; s ++) W_max(k + K * s, j) = 0; for(j = 0; j < J; j ++) Z_mean(j) = 0; for(i = 0; i < I; i ++){ // b_mean double exp1 = 0; if(zeta > 0) { exp1 = log(zeta) + TP(i); } double exp2 = log(1 - zeta); double maxexp = TW(i, 0); for(j = 1; j < J; j ++) if(maxexp < TW(i, j)) maxexp = TW(i, j); double tmp = 0; for(j = 0; j < J; j ++) tmp += probz[ j ] * exp(TW(i, j) - maxexp); exp2 += log(tmp) + maxexp; if(zeta > 0) { if(exp1 > exp2) b_mean(i) = 1 / (1 + exp(exp2 - exp1)); else b_mean(i) = exp(exp1 - exp2) / (1 + exp(exp1 - exp2)); } else { b_mean(i) = 0; } // predZ double tmpexp[ J ]; for(j = 0; j < J; j ++){ tmpexp[ j ] = log(probz[ j ]); if(TW(i, j) > TP(i) && zeta > 0) tmpexp[ j ] += log((1 - zeta) + zeta * exp(TP(i) - TW(i, j))) + TW(i, j); else tmpexp[ j ] += log((1 - zeta) * exp(TW(i, j) - TP(i)) + zeta) + TP(i); } maxexp = tmpexp[ 0 ]; for(j = 1; j < J; j ++) if(maxexp < tmpexp[ j ]) maxexp = tmpexp[ j ]; double total = 0; for(j = 0; j < J; j ++) total += exp(tmpexp[ j ] - maxexp); for(j = 0; j < J; j ++){ predZ(i, j) = exp(tmpexp[ j ] - maxexp) / total; Z_mean(j) += predZ(i, j); } // Zcond for(j = 0; j < J; j ++){ exp1 = predZ(i, j) - probz(j) * b_mean(i); if(exp1 < _LOW) Zcond(i, j) = _LOW; else if(exp1 >= (1 - _LOW)) Zcond(i, j) = 1 - _LOW; else Zcond(i, j) = exp1; for(k = 0; k < K; k ++) for(s = 0; s < S; s ++){ if(Theta(k, i + I * s) > 0){ W_max(k + K * s, j) += Zcond(i, j); break; } } } } if(zeta > 0) { zeta = 0; for(i = 0; i < I; i ++) zeta += b_mean[ i ]; zeta /= I; if(zeta < _LOW) zeta = _LOW; else if(zeta > 1 - _LOW) zeta = 1 - _LOW; } for(j = 0; j < J; j ++){ Z_mean(j) /= I; if(Z_mean(j) < _LOW) Z_mean(j) = _LOW; else if(Z_mean(j) > 1 - _LOW) Z_mean(j) = 1 - _LOW; } for(k = 0; k < K; k ++) for(j = 0; j < J; j ++){ double total = 0; for(s = 0; s < S; s ++) total += W_max(k + K * s, j); for(s = 0; s < S; s ++){ if(total == 0) W_max(k + K * s, j) = 1 / S; else if(W_max(k + K * s, j) < _LOW * total) W_max(k + K * s, j) = _LOW; else if(W_max(k + K * s, j) > (1 - _LOW) * total) W_max(k + K * s, j) = 1 - _LOW; else W_max(k + K * s, j) /= total; } } Rcpp::List ret = Rcpp::List::create( Rcpp::Named("zeta") = zeta, Rcpp::Named("probz") = Z_mean, Rcpp::Named("W") = W_max, Rcpp::Named("b_prob") = b_mean, Rcpp::Named("Z") = predZ, Rcpp::Named("Zcond") = Zcond ); return(ret); }
wstring ToWstring(const ISC_STRING &msg) { return wstring(TW(msg)); }