Verylong operator* (const Verylong &u, const Verylong &v) { Verylong pprod("1"), tempsum("0"); for (int j=0; j<v.vlen; j++) { int digit = v.vlstr[j] - '0'; pprod = u.multdigit(digit); pprod = pprod.mult10(j); tempsum += pprod; } tempsum.vlsign = u.vlsign ^ v.vlsign; return tempsum; }
void _Estepbfactor(vector<double> &expected, vector<double> &r1, vector<double> &ri, const NumericMatrix &itemtrace, const vector<double> &prior, const vector<double> &Priorbetween, const vector<int> &r, const int &ncores, const IntegerMatrix &data, const IntegerMatrix &sitems, const vector<double> &Prior) { #ifdef SUPPORT_OPENMP omp_set_num_threads(ncores); #endif const int sfact = sitems.ncol(); const int nitems = data.ncol(); const int npquad = prior.size(); const int nbquad = Priorbetween.size(); const int nquad = nbquad * npquad; const int npat = r.size(); vector<double> r1vec(nquad*nitems*sfact, 0.0); #pragma omp parallel for for (int pat = 0; pat < npat; ++pat){ vector<double> L(nquad), Elk(nbquad*sfact), posterior(nquad*sfact); vector<double> likelihoods(nquad*sfact, 1.0); for (int fact = 0; fact < sfact; ++fact){ for (int item = 0; item < nitems; ++item){ if (data(pat,item) && sitems(item,fact)) for (int k = 0; k < nquad; ++k) likelihoods[k + nquad*fact] = likelihoods[k + nquad*fact] * itemtrace(k,item); } } vector<double> Plk(nbquad*sfact); for (int fact = 0; fact < sfact; ++fact){ int k = 0; for (int q = 0; q < npquad; ++q){ for (int i = 0; i < nbquad; ++i){ L[k] = likelihoods[k + nquad*fact] * prior[q]; ++k; } } vector<double> tempsum(nbquad, 0.0); for (int i = 0; i < npquad; ++i) for (int q = 0; q < nbquad; ++q) tempsum[q] += L[q + i*nbquad]; for (int i = 0; i < nbquad; ++i) Plk[i + fact*nbquad] = tempsum[i]; } vector<double> Pls(nbquad, 1.0); for (int i = 0; i < nbquad; ++i){ for(int fact = 0; fact < sfact; ++fact) Pls[i] = Pls[i] * Plk[i + fact*nbquad]; expected[pat] += Pls[i] * Priorbetween[i]; } for (int fact = 0; fact < sfact; ++fact) for (int i = 0; i < nbquad; ++i) Elk[i + fact*nbquad] = Pls[i] / Plk[i + fact*nbquad]; for (int fact = 0; fact < sfact; ++fact) for (int i = 0; i < nquad; ++i) posterior[i + nquad*fact] = likelihoods[i + nquad*fact] * r[pat] * Elk[i % nbquad + fact*nbquad] / expected[pat]; #pragma omp critical for (int i = 0; i < nbquad; ++i) ri[i] += Pls[i] * r[pat] * Priorbetween[i] / expected[pat]; for (int item = 0; item < nitems; ++item) if (data(pat,item)) for (int fact = 0; fact < sfact; ++fact) for(int q = 0; q < nquad; ++q) r1vec[q + fact*nquad*nitems + nquad*item] += posterior[q + fact*nquad]; } //end main for (int item = 0; item < nitems; ++item) for (int fact = 0; fact < sfact; ++fact) if(sitems(item, fact)) for(int q = 0; q < nquad; ++q) r1[q + nquad*item] = r1vec[q + nquad*item + nquad*nitems*fact] * Prior[q]; }
bool Reed_Solomon::decode(const bvec &coded_bits, const ivec &erasure_positions, bvec &decoded_message, bvec &cw_isvalid) { bool decoderfailure, no_dec_failure; int j, i, kk, l, L, foundzeros, iterations = floor_i(static_cast<double>(coded_bits.length()) / (n * m)); bvec mbit(m * k); decoded_message.set_size(iterations * k * m, false); cw_isvalid.set_length(iterations); GFX rx(q, n - 1), cx(q, n - 1), mx(q, k - 1), ex(q, n - 1), S(q, 2 * t), Xi(q, 2 * t), Gamma(q), Lambda(q), Psiprime(q), OldLambda(q), T(q), Omega(q); GFX dummy(q), One(q, (char*)"0"), Omegatemp(q); GF delta(q), tempsum(q), rtemp(q), temp(q), Xk(q), Xkinv(q); ivec errorpos; if ( erasure_positions.length() ) { it_assert(max(erasure_positions) < iterations*n, "Reed_Solomon::decode: erasure position is invalid."); } no_dec_failure = true; for (i = 0; i < iterations; i++) { decoderfailure = false; //Fix the received polynomial r(x) for (j = 0; j < n; j++) { rtemp.set(q, coded_bits.mid(i * n * m + j * m, m)); rx[j] = rtemp; } // Fix the Erasure polynomial Gamma(x) // and replace erased coordinates with zeros rtemp.set(q, -1); ivec alphapow = - ones_i(2); Gamma = One; for (j = 0; j < erasure_positions.length(); j++) { rx[erasure_positions(j)] = rtemp; alphapow(1) = erasure_positions(j); Gamma *= (One - GFX(q, alphapow)); } //Fix the syndrome polynomial S(x). S.clear(); for (j = 1; j <= 2 * t; j++) { S[j] = rx(GF(q, b + j - 1)); } // calculate the modified syndrome polynomial Xi(x) = Gamma * (1+S) - 1 Xi = Gamma * (One + S) - One; // Apply Berlekam-Massey algorithm if (Xi.get_true_degree() >= 1) { //Errors in the received word // Iterate to find Lambda(x), which hold all error locations kk = 0; Lambda = One; L = 0; T = GFX(q, (char*)"-1 0"); while (kk < 2 * t) { kk = kk + 1; tempsum = GF(q, -1); for (l = 1; l <= L; l++) { tempsum += Lambda[l] * Xi[kk - l]; } delta = Xi[kk] - tempsum; if (delta != GF(q, -1)) { OldLambda = Lambda; Lambda -= delta * T; if (2 * L < kk) { L = kk - L; T = OldLambda / delta; } } T = GFX(q, (char*)"-1 0") * T; } // Find the zeros to Lambda(x) errorpos.set_size(Lambda.get_true_degree()); foundzeros = 0; for (j = q - 2; j >= 0; j--) { if (Lambda(GF(q, j)) == GF(q, -1)) { errorpos(foundzeros) = (n - j) % n; foundzeros += 1; if (foundzeros >= Lambda.get_true_degree()) { break; } } } if (foundzeros != Lambda.get_true_degree()) { decoderfailure = true; } else { // Forney algorithm... //Compute Omega(x) using the key equation for RS-decoding Omega.set_degree(2 * t); Omegatemp = Lambda * (One + Xi); for (j = 0; j <= 2 * t; j++) { Omega[j] = Omegatemp[j]; } //Find the error/erasure magnitude polynomial by treating them the same Psiprime = formal_derivate(Lambda*Gamma); errorpos = concat(errorpos, erasure_positions); ex.clear(); for (j = 0; j < errorpos.length(); j++) { Xk = GF(q, errorpos(j)); Xkinv = GF(q, 0) / Xk; // we calculate ex = - error polynomial, in order to avoid the // subtraction when recunstructing the corrected codeword ex[errorpos(j)] = (Xk * Omega(Xkinv)) / Psiprime(Xkinv); if (b != 1) { // non-narrow-sense code needs corrected error magnitudes int correction_exp = ( errorpos(j)*(1-b) ) % n; ex[errorpos(j)] *= GF(q, correction_exp + ( (correction_exp < 0) ? n : 0 )); } } //Reconstruct the corrected codeword. // instead of subtracting the error/erasures, we calculated // the negative error with 'ex' above cx = rx + ex; //Code word validation S.clear(); for (j = 1; j <= 2 * t; j++) { S[j] = cx(GF(q, b + j - 1)); } if (S.get_true_degree() >= 1) { decoderfailure = true; } } } else { cx = rx; decoderfailure = false; } //Find the message polynomial mbit.clear(); if (decoderfailure == false) { if (cx.get_true_degree() >= 1) { // A nonzero codeword was transmitted if (systematic) { for (j = 0; j < k; j++) { mx[j] = cx[j]; } } else { mx = divgfx(cx, g); } for (j = 0; j <= mx.get_true_degree(); j++) { mbit.replace_mid(j * m, mx[j].get_vectorspace()); } } } else { //Decoder failure. // for a systematic code it is better to extract the undecoded message // from the received code word, i.e. obtaining a bit error // prob. p_b << 1/2, than setting all-zero (p_b = 1/2) if (systematic) { mbit = coded_bits.mid(i * n * m, k * m); } else { mbit = zeros_b(k); } no_dec_failure = false; } decoded_message.replace_mid(i * m * k, mbit); cw_isvalid(i) = (!decoderfailure); } return no_dec_failure; }