Exemplo n.º 1
0
B row_min(const ublas::matrix<B>& M,int row)
{
  B min=M(row,0);
  for(int i=1;i<M.size2();i++)
    min = std::min(min,M(row,i));
  return min;
}
Exemplo n.º 2
0
ublas::vector<int> makeParityCheck(const ublas::vector<int> &dSource, const ublas::matrix<int> &H, const ublas::matrix<int> &L, const ublas::matrix<int> &U) {
  // Get matrix dimensions
  const unsigned int M = H.size1();
  const unsigned int N = H.size2();

  // Find B.dsource
  const ublas::vector<int> z(mod2(ublas::prod(ublas::subrange(H, 0, M, N - M, N), dSource)));

  //std::cout << "z=" << std::endl;
  //printVector<int>(z);

  //std::cout << "L=" << std::endl;
  //printMatrix<int>(L);

  //std::cout << "U=" << std::endl;
  //printMatrix<int>(U);

  // Parity check vector found by solving sparse LU
  const ublas::vector<int> x1(solve(L, z));
  //std::cout << "x1=" << std::endl;
  //printVector<int>(x1);

  const ublas::vector<int> x2(solve(U, x1));
  //std::cout << "x2=" << std::endl;
  //printVector<int>(x2);

  const ublas::vector<int> c(mod2(x2));

  return c;
}
Exemplo n.º 3
0
void writeMatrix(const ublas::matrix<T> &A, const char *fileName) {
  std::ofstream fout(fileName, std::ofstream::out);

  for (unsigned int i = 0; i < A.size1(); i++) {
    for (unsigned int j = 0; j < A.size2(); j++) {
      fout << A(i, j);

      if (j + 1 < A.size2()) {
        fout << ",";
      }
    }

    fout << std::endl;
  }

  fout.close();
}
Exemplo n.º 4
0
void printMatrix(const ublas::matrix<T> &A) {
  for (unsigned int i = 0; i < A.size1(); i++) {
    for (unsigned int j = 0; j < A.size2(); j++) {
      std::cout << A(i, j);

      if (j + 1 < A.size2()) {
        std::cout << ", ";
      }
    }

    if (i + 1 < A.size1()) {
      std::cout << ";";
    }

    std::cout << std::endl;
  }
}
Exemplo n.º 5
0
double compute_tv_21(int y,const ublas::matrix<double>& count1,const ublas::matrix<double>& count2)
{
  y++;

  double D=0;

  assert(count1.size1() == count2.size1());
  assert(count1.size2() == count2.size2());

  for(int x=0;x<count1.size1();x++)
    D += std::abs(count1(x,y)-count2(x,y));

  D /= 2.0;

  assert(D <= 1.0);

  return D;
}
Exemplo n.º 6
0
bool BenchmarkVienna<ScalarType>::is_equal(const ublas::matrix<ScalarType, orientation> &A,
                                           const ublas::matrix<ScalarType, orientation> &B) {
    for (std::size_t i = 0; i < A.size1(); ++i) {
        for (std::size_t j = 0; j < A.size2(); ++j) {
            if ( std::fabs(A(i,j) - B(i,j)) / B(i,j) > 1e-4 ) return false;
        }
    }
    return true;
}
Exemplo n.º 7
0
int check_matrices(const ublas::matrix< ScalarType >& ref_mat, const ublas::matrix< ScalarType >& mat) {

  std::size_t size1, size2;
  ScalarType eps = 0.00001;
  size1 = ref_mat.size1(); size2 = ref_mat.size2();
  if( (size1 != mat.size1()) || (size2 != mat.size2()) )
    return EXIT_FAILURE;

  for (unsigned int i = 0; i < size1; i++)
    for (unsigned int j = 0; j < size2; j++)
      if ( abs(ref_mat(i,j) - mat(i,j)) > eps ) {
        std::cout << "!!Verification failed at " << i <<" : "<< j
                  << "(expected: " << ref_mat(i,j) << " get: " << mat(i,j) << " )" << std::endl;
        return EXIT_FAILURE;
      }

  std::cout << "Everything went well!" << std::endl;
  return EXIT_SUCCESS;
}
Exemplo n.º 8
0
/// Extract column @c from the index-matrix @M
vector<int> get_column(const ublas::matrix<int>& M, int c)
{
  vector<int> column(M.size2(),-1);

  for(int i=0;i<column.size();i++)
    if (M(c,i) >= 0)
      column[i] = M(c,i);

  return column;
}
Exemplo n.º 9
0
long int homologies_total(const ublas::matrix<int>& M1) 
{
  long int total=0;

  for(int column=0;column<M1.size1();column++) 
    for(int i=0;i<M1.size2();i++)
      if (M1(column,i) != alphabet::gap and M1(column,i) != alphabet::unknown)
	total++;

  return total;
}
/**
 * @brief Utility funtion to doing scans for steady states.
 *
 * @param tot
 * @param g
 * @param S
 */
void recalcTotal( vector< double >& tot, ublas::matrix<double>& g, const double* S )
{
    assert( g.size1() == tot.size() );
    for ( size_t i = 0; i < g.size1(); ++i ) 
    {
        double t = 0.0;
        for ( unsigned int j = 0; j < g.size2(); ++j )
            t += g( i, j ) * S[j];
        tot[ i ] = t;
    }
}
Exemplo n.º 11
0
int score(const ublas::matrix<int>& M1, const ublas::matrix<int>& M2, int c1, int c2)
{
  assert(M1.size2() == M2.size2());
  const int N = M1.size2();

  int total = 0;

  for(int i=0;i<N;i++) 
  {
    if (M1(c1,i) == alphabet::unknown or M2(c2,i) == alphabet::unknown)
      continue;

    if (M1(c1,i) == alphabet::unknown or M2(c2,i) == alphabet::gap)
      continue;

    if (M1(c1,i) == M2(c2,i))
      total++;
  }

  return total;
}
Exemplo n.º 12
0
int check_matrices(const ublas::matrix< ScalarType >& ref_mat, const ublas::matrix< ScalarType >& mat, ScalarType eps) {

  std::size_t size1, size2;
  size1 = ref_mat.size1(); size2 = ref_mat.size2();
  if( (size1 != mat.size1()) || (size2 != mat.size2()) )
    return EXIT_FAILURE;

  for (unsigned int i = 0; i < size1; i++)
    for (unsigned int j = 0; j < size2; j++)
    {
      ScalarType rel_error = std::abs(ref_mat(i,j) - mat(i,j)) / std::max(std::abs(ref_mat(i,j)), std::abs(mat(i,j)));
      if ( rel_error > eps ) {
        std::cout << "ERROR: Verification failed at (" << i <<", "<< j << "): "
                  << " Expected: " << ref_mat(i,j) << ", got: " << mat(i,j) << " (relative error: " << rel_error << ")" << std::endl;
        return EXIT_FAILURE;
      }
    }

  std::cout << "Everything went well!" << std::endl;
  return EXIT_SUCCESS;
}
Exemplo n.º 13
0
void ublas_to_vector(const ublas::matrix<float> &mat, std::vector<double> &vec)
{
  vec.clear();
  for (size_t i=0; i < mat.size1(); i++)
  {
    for (size_t j=0; j < mat.size2(); j++)
    {
      vec.push_back(mat(i,j));
    }
  }

}
Exemplo n.º 14
0
long int homologies_preserved(const ublas::matrix<int>& M1,const ublas::matrix<int>& M2,
				    const vector< vector<int> >& column_indices2)
{
  long int match=0;
  long int mismatch=0;

  for(int column=0;column<M1.size1();column++) 
    for(int i=0;i<M1.size2();i++)
      if (M1(column,i) != alphabet::gap and M1(column,i) != alphabet::unknown)
	for(int j=0;j<M1.size2();j++)
	  if (j != i) {
	    if (A_match(M1,column,i,j,M2,column_indices2))
	      match++;
	    else
	      mismatch++;
	  }
	
  assert(homologies_total(M1) == homologies_total(M2));
  assert(homologies_total(M1) == match + mismatch);

  return match;
}
Exemplo n.º 15
0
ublas::vector<double> mvnormpdf(const ublas::matrix<double>& x, const  ublas::vector<double>& mu, const ublas::matrix<double>& Omega) {
  //! Multivariate normal density
  size_t p = x.size1();
  size_t n = x.size2();
  
  double f = sqrt(det(Omega))/pow(2.0*PI, p/2.0);
  // cout << "O: " << Omega << "\n";
  // cout << "f: " << f << "\n";
  ublas::matrix<double> e(p, n);
  e.assign(x - outer_prod(mu, ublas::scalar_vector<double>(n, 1)));
  e = element_prod(e, prod(Omega, e));

  return ublas::apply_to_all<functor::exp<double> > (-(ublas::matrix_sum(e, 0))/2.0)*f;
}
Exemplo n.º 16
0
void reorderHMatrix(ublas::matrix<int> &H, ublas::matrix<int> &L, ublas::matrix<int> &U) {
  // Get matrix dimensions
  const unsigned int M = H.size1();
  const unsigned int N = H.size2();

  // Set a new matrix F for LU decomposition
  ublas::matrix<int> F(H);

  // Re-order the M x (N - M) submatrix
  for (unsigned int i = 0; i < M; i++) {
    int chosenCol = 0;

    // Create diagonally structured matrix using 'First' strategy
    for (unsigned int j = i; j < N; j++) {
      if (F(i, j) != 0) {
        chosenCol = j;
        break;
      }
    }

    //std::cout << "chosenCol=" << chosenCol << std::endl;
    //printMatrix<int>(H);

    // Re-ordering columns of F
    const ublas::vector<int> tmp1(ublas::column(F, i));
    ublas::column(F, i) = ublas::column(F, chosenCol);
    ublas::column(F, chosenCol) = tmp1;

    // Re-ordering columns of H
    const ublas::vector<int> tmp2(ublas::column(H, i));
    ublas::column(H, i) = ublas::column(H, chosenCol);
    ublas::column(H, chosenCol) = tmp2;

    // Fill the LU matrices column by column
    ublas::subrange(L, i, M, i, i + 1) = ublas::subrange(F, i, M, i, i + 1);
    ublas::subrange(U, 0, i + 1, i, i + 1) = ublas::subrange(F, 0, i + 1, i, i + 1);

    // There will be no rows operation at the last row
    if (i < M - 1) {
      // Find the later rows with non-zero elements in column i
      for (unsigned int k = i + 1; k < M; k++) {
        if (F(k, i) != 0) {
          // Add current row to the later rows which have a 1 in column i
	  ublas::row(F, k) = mod2(ublas::row(F, k) + ublas::row(F, i));
        }
      }
    }
  }  
}
unsigned int rankUsingBoost( ublas::matrix<double>& U )
{
    int numMols = U.size1();
    int numReacs = U.size2() - numMols;
    int i;
    // Start out with a nonzero entry at 0,0
    int leftCol = reorderRows( U, 0, 0 );

    for ( i = 0; i < numMols - 1; ++i )
    {
        eliminateRowsBelow( U, i, leftCol );
        leftCol = reorderRows( U, i + 1, leftCol );
        if ( leftCol == numReacs )
            break;
    }
    return i + 1;
}
Exemplo n.º 18
0
bool UniGridApprox::WriteMatrix(ublas::matrix<double> M)
{

    int row = M.size1();
    int col = M.size2();

    for (int i=0; i<row; ++i)
    {
        for (int j=0; j<col; ++j)
        {
            cout << M(i,j) << ", ";
        }
        cout << endl;
    }

    return true;
}
Exemplo n.º 19
0
void PrintMatlab( const ublas::matrix<T> & M )
{
    ofstream str( "Data/Mat.m" );

    str << "function M = Mat()\n";
    str << "% Create the matrix Mat in order to manipulate it in Matlab\n";

    str << "M = [ ... \n";

    for ( int_type i=0; i < M.size1() ; ++i )
    {
        for ( int_type j=0; j < M.size2() ; ++j )
            str << M( i,j ) << " ";

        str << "\n";
    }

    str <<"];\n";

    str <<"\n return";

}
void eliminateRowsBelow( ublas::matrix< double >& U, int start, int leftCol )
{
    int numMols = U.size1();
    double pivot = U( start, leftCol );
    assert( fabs( pivot ) > SteadyState::EPSILON );
    for ( int i = start + 1; i < numMols; ++i )
    {
        double factor = U(i, leftCol);
        if( fabs ( factor ) > SteadyState::EPSILON )
        {
            factor = factor / pivot;
            for ( size_t j = leftCol + 1; j < U.size2(); ++j )
            {
                double x = U(i,j);
                double y = U( start, j );
                x -= y * factor;
                if ( fabs( x ) < SteadyState::EPSILON )
                    x = 0.0;
                U( i, j ) = x;
            }
        }
        U(i, leftCol) = 0.0;
    }
}
Exemplo n.º 21
0
// ----------------------------------------------------------------------------
boost::numeric::ublas::vector<double>
LOGP(
	const ublas::matrix<double> &A,
	const ublas::matrix<double> &Xi_tmp,
	const boost::numeric::ublas::vector<double> &y,
	double weight,
	const int iter)
{
	ublas::matrix<double> Xi = Xi_tmp;
	boost::numeric::ublas::vector<double> yA = prod(trans(A), y );

	std::vector<unsigned int> foreach_array;
	boost::numeric::ublas::vector<double> x( A.size2() ); // ret value
	for(unsigned int i = 0; i != x.size(); ++i){
		foreach_array.push_back( i );
		x(i) = 0;
	}

	// -------------------------------------------------------
	const auto compute_m = [&](
		const unsigned int j,
		const ublas::matrix<double> &A,
		const ublas::matrix<double> &Xi,
		const boost::numeric::ublas::vector<double> &y,
		const boost::numeric::ublas::vector<double> &x
		) -> double
	{
		double Xix = 0;
		for(unsigned int i = 0; i != Xi.size2(); ++i)
			if(i != j)
				Xix += Xi(i,j) * x(i);

		return (yA(j) - Xix) / Xi(j,j);
	};

	// -------------------------------------------------------
	const auto compute_Delta = [&](
		const double m_k,
		const unsigned int k
		) -> double
	{
		double Xix = 0;
		for(unsigned int i = 0; i != Xi.size2(); ++i)
			if(i != k)
				Xix += Xi(i,k) * x(i);

		const double logp = logPenalty( m_k );

		return ((2 * yA(k) * m_k) + (Xi(k, k) * m_k * m_k) - (2 * m_k * Xix))/2 - weight * logp;
	};

	// -------------------------------------------------------
	for(int it = 0; it != iter; ++it){

		random_shuffle( foreach_array.begin(), foreach_array.end() );

		for(auto ind = foreach_array.begin(); ind != foreach_array.end(); ++ind)
		{
			unsigned int j = *ind;
			const double m = compute_m(j, A, Xi, y, x);

			const double D = m * m - 4 * weight / Xi(j,j);
			if(D < 0){
				x(j) = 0;
			}else{
				const double tmp = m - weight / Xi(j,j)/ m;
				const double delta = compute_Delta(tmp, j);
				if(delta > 0){
					x(j) = tmp;
				}else{
					x(j) = 0;
				}
			}
		}
	}
	return x;
}
Exemplo n.º 22
0
void ones(ublas::matrix<long double>& A)
{
	for (size_t i = 0; i < A.size1(); ++i)
		for (size_t j = 0; j < A.size2(); ++j)
			A(i,j) = 1;
}
Exemplo n.º 23
0
ublas::vector<int> decodeLogDomainSimple(const ublas::vector<double> &rx, const ublas::matrix<int> &H, const unsigned int iterations) {
  // Get matrix dimensions
  const unsigned int M = H.size1();
  const unsigned int N = H.size2();

  // Prior hard-decision
  ublas::vector<double> Lci(rx.size());
  for (unsigned int i = 0; i < rx.size(); i++) {
    Lci(i) = -rx(i);
  }

  // Initialization
  ublas::matrix<double> Lrji(ublas::zero_matrix<double>(M, N));
  ublas::matrix<double> Pibetaij(ublas::zero_matrix<double>(M, N));

  // Associate the L(ci) matrix with non-zero elements of H
  ublas::matrix<double> Lqij(M, N);
  for (unsigned int i = 0; i < M; i++) {
    ublas::row(Lqij, i) = ublas::element_prod(ublas::row(H, i), Lci);
  }

  ublas::vector<int> vHat(N);

  // Iteration
  for (unsigned int n = 0; n < iterations; n++) {
    //std::cout << "Iteration : " << n << std::endl;

    // Get the sign and magnitude of L(qij)
    ublas::matrix<int> alphaij(M, N);
    ublas::matrix<double> betaij(M, N);
    for (unsigned int i = 0; i < M; i++) {
      for (unsigned int j = 0; j < N; j++) {
        alphaij(i, j) = sign(Lqij(i, j));
        betaij(i, j) = std::abs(Lqij(i, j));
      }
    }

    // Horizontal step
    for (unsigned int i = 0; i < M; i++) {
      int prodOfalphaij = 1;
      for (unsigned int j = 0; j < N; j++) {
        if (H(i, j) != 0) {
          prodOfalphaij *= alphaij(i, j);
        }
      }

      // Get the minimum of betaij
      for (unsigned int j = 0; j < N; j++) {
        if (H(i, j) != 0) {
          // Minimum of betaij
          double minOfBetaij = std::numeric_limits<double>::max();
          for (unsigned int k = 0; k < N; k++) {
            if (j != k && H(i, k) != 0) {
              if (betaij(i, k) < minOfBetaij) {
                minOfBetaij = betaij(i, k);
              }
            }
          }

          // Multiplication alphaij
          // Update L(rji)
          Lrji(i, j) = prodOfalphaij * alphaij(i, j) * minOfBetaij;
        }
      }
    }

    // Vertical step
    for (unsigned int j = 0; j < N; j++) {
      double sumOfLrji = 0.0;
      for (unsigned int i = 0; i < M; i++) {
        if (H(i, j) != 0) {
          sumOfLrji += Lrji(i, j);
        }
      }

      for (unsigned int i = 0; i < M; i++) {
        if (H(i, j) != 0) {
          // Update L(qij) by summation of L(rij)
          Lqij(i, j) = Lci(j) + sumOfLrji - Lrji(i, j);
        }
      }

      // Get L(Qij)
      double LQi = Lci(j) + sumOfLrji;

      // Decode L(Qi)
      if (LQi < 0) {
        vHat(j) = 1;
      } else {
        vHat(j) = 0;
      }
    }
  }

  return vHat;
}
Exemplo n.º 24
0
ublas::vector<int> decodeBitFlipping(const ublas::vector<double> &rx, const ublas::matrix<int> &H, const unsigned int iterations) {
  // Get matrix dimensions
  const unsigned int M = H.size1();
  const unsigned int N = H.size2();

  // Prior hard-decision
  ublas::vector<int> ci(rx.size());
  for (unsigned int i = 0; i < rx.size(); i++) {
    ci(i) = 0.5 * (sign(rx(i)) + 1);
  }

  //std::cout << "ci=" << std::endl;
  //printVector<int>(ci);

  // Initialization
  ublas::matrix<int> rji(ublas::zero_matrix<int>(M, N));

  // Associate the ci matrix with non-zero elements of H
  ublas::matrix<int> qij(M, N);
  for (unsigned int i = 0; i < M; i++) {
    ublas::row(qij, i) = ublas::element_prod(ublas::row(H, i), ci);
  }

  //std::cout << "qij=" << std::endl;
  //printMatrix<int>(qij);

  ublas::vector<int> vHat(N);

  // Iteration
  for (unsigned int n = 0; n < iterations; n++) {
    //std::cout << "Iteration : " << n << std::endl;

    // Horizontal step
    for (unsigned int i = 0; i < M; i++) {
      int qijSum = 0;
      for (unsigned int j = 0; j < N; j++) {
        if (H(i, j) != 0) {
          qijSum += qij(i, j);
        }
      }

      for (unsigned int j = 0; j < N; j++) {
        if (H(i, j) != 0) {
          rji(i, j) = (qijSum + qij(i, j)) % 2;
        }
      }
    }

    // Vertical step
    for (unsigned int j = 0; j < N; j++) {
      int rjiNumberOfOnes = 0;
      for (unsigned int i = 0; i < M; i++) {
        if (rji(i, j) != 0) {
          rjiNumberOfOnes++;
        }
      }

      int hNumberOfOnes = 0;
      for (unsigned int i = 0; i < M; i++) {
        if (H(i, j) != 0) {
          hNumberOfOnes++;
        }
      }
      
      for (unsigned int i = 0; i < M; i++) {
        if (H(i, j) != 0) {
          // Update qij, set '1' for majority of 1s else '0', excluding i
          if (rjiNumberOfOnes + ci(j) >= hNumberOfOnes - rjiNumberOfOnes + rji(i, j)) {
            qij(i, j) = 1;
          } else {
            qij(i, j) = 0;
          }
        }
      }

      // Bit decoding
      if (rjiNumberOfOnes + ci(j) >= hNumberOfOnes - rjiNumberOfOnes) {
        vHat(j) = 1;
      } else {
        vHat(j) = 0;
      }
    }
  }

  return vHat;
}