int reorderRows( ublas::matrix< double >& U, int start, int leftCol )
{
    int leftMostRow = start;
    int numReacs = U.size2() - U.size1();
    int newLeftCol = numReacs;
    for ( size_t i = start; i < U.size1(); ++i )
    {
        for ( int j = leftCol; j < numReacs; ++j )
        {
            if ( fabs( U(i,j )) > SteadyState::EPSILON )
            {
                if ( j < newLeftCol )
                {
                    newLeftCol = j;
                    leftMostRow = i;
                }
                break;
            }
        }
    }

    if ( leftMostRow != start )   // swap them.
        swapRows( U, start, leftMostRow );

    return newLeftCol;
}
Exemple #2
0
void peel_n_mutations(const alphabet& a, const vector<int>& letters, const SequenceTree& T,
		      const ublas::matrix<B>& cost,ublas::matrix<B>& n_muts,
		      const vector<const_branchview>& branches)
{
  const int A = a.size();

  assert(letters.size() == T.n_leaves());
  assert(cost.size1() == A);
  assert(cost.size2() == A);

  // we need a scratch row in the matrix
  assert(n_muts.size1() == T.n_nodes());
  assert(n_muts.size2() == A);

  // compute the max cost -- is this approach a good idea?
  // Well... this apparently doesn't work.
  B max_cost = 0;
  for(int i=0;i<A;i++)
    for(int j=0;j<A;j++)
      max_cost = std::max(cost(i,j)+1, max_cost);
    
  // clear the length matrix.
  for(int i=0;i<n_muts.size1();i++)
    for(int j=0;j<n_muts.size2();j++)
      n_muts(i,j)=0;
  
  // set the leaf costs
  for(int s=0;s<T.n_leaves();s++)
  {
    int L = letters[s];

    if (a.is_letter_class(L))
      for(int l=0;l<A;l++)
	if (a.matches(l,L))
	  n_muts(s,l) = 0;
	else
	  n_muts(s,l) = max_cost;
  }


  // compute the costs for letters at each node
  for(int i=0;i<branches.size();i++)
  {
    int s = branches[i].source();
    int t = branches[i].target();

    // for each letter l of node target...
    for(int l=0;l<A;l++)
    {
      // compute minimum treelength for data behind source.
      B temp = n_muts(s,0)+cost(0,l);
      for(int k=1;k<A;k++)
	temp = min(temp, n_muts(s,k)+cost(k,l) );

      // add it to treelengths for data behind target
      n_muts(t,l) += temp;
    }
  }
}
Exemple #3
0
	ublas::matrix<double> normalize_affinity(ublas::matrix<double> input) {
		ublas::vector<double> ones = ublas::scalar_vector<double>(input.size1(), 1);
		ublas::vector<double> diag(input.size1());
		
		for (int i = 0; i < input.size1(); i++)
			diag[i] = input(i, i);
		
		return ublas::outer_prod(diag, ones) + ublas::outer_prod(ublas::trans(ones), ublas::trans(diag)) - input - ublas::trans(input);
	}
Exemple #4
0
void dispMatrix(const ublas::matrix<float>& inMat) {
    cout << "Matrix [" << inMat.size1() << ", " << inMat.size2() << "]" << endl << "(";
    for (int n = 0; n < (int) inMat.size1(); n++) {
        for (int m = 0; m < (int) inMat.size2(); m++) {
            cout << inMat(n,m) << " ";
        }
        cout << endl;
    }
    cout << "\b)" << endl;
}
/**
 * @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;
    }
}
Exemple #6
0
T cond2( ublas::matrix<T> const& M )
{
    gmm::dense_matrix<T> Q( M.size1(), M.size2() );

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

    return gmm::condition_number( Q );
}
Exemple #7
0
double fourSums(const ublas::matrix<long double>& A, ublas::matrix<long double>& sums)
{
	long double total = 0.0;
	sums = ublas::zero_matrix<long double>(A.size1(), A.size2()/4);
	for (size_t i = 0; i < A.size1(); ++i)
		for (size_t j = 0; j < A.size2(); ++j)
		{
			sums(i,j/4) += A(i,j);
			total += A(i,j);
		}
	
	return total;
}
Exemple #8
0
  // ublas::matrix<T> matrix_sum(ublas::matrix<T>& m, int index) {
  ublas::vector<T> matrix_sum(const ublas::matrix<T>& m, int index) {
    using namespace boost::numeric::ublas;
    ublas::vector<T> result;
    if (index == 0) {
      result.resize(m.size2());
      for (size_t i=0; i<m.size2(); ++i)
	result(i) = sum(column(m, i));
    }
    else if (index == 1) {
      result.resize(m.size1());
      for (size_t i=0; i<m.size1(); ++i)
	result(i) = sum(row(m, i));
    }
    return result;
  }
Exemple #9
0
void dispMatrix(const ublas::matrix<float>& inMat, const int rowIndex) {
   cout << "Matrix [" << inMat.size1() << ", " << inMat.size2() << "]" << endl << "(";
   for (int m = 0; m < (int) inMat.size2(); m++) {
     cout << inMat(rowIndex,m) << " ";
   }
   cout << endl << "\b)" << endl;
}
Exemple #10
0
ublas::vector<ublas::matrix<double> > wishart_rnd(const int df, ublas::matrix<double>& S, const int mc) {
  //! inverse Wishart random matrix
  //! does not correct for poorly conditioned matrix 
  size_t p = S.size1();
  ublas::vector<double> D(p);
  ublas::matrix<double> P(p, p);
  ublas::matrix<double> F(p, p);
  F = ublas::zero_matrix<double>(p, p);

  // make copy of S
  // ublas::matrix<double> SS(S);

  lapack::gesvd('A', 'A', S, D, P, F);
  // svd0(S, P, D, F);
  
  P = prod(trans(P), diagm(ublas::apply_to_all<functor::inv_sqrt<double> >(D)));
  // rprod does not seem any faster than diagonalizing D before multiplication
  // P = rprod(P, ublas::apply_to_all<functor::inv_sqrt<double> >(D));

  // generate mc samples
  ublas::vector<ublas::matrix<double> > K(mc);
  for (int i=0; i<mc; ++i)
    K(i) = wishart_1(df, P, p, p);
  return K;
}
Exemple #11
0
long int asymmetric_pairs_distance(const ublas::matrix<int>& M1,const ublas::matrix<int>& M2,
				    const vector< vector<int> >& column_indices2)
{
  int mismatch=0;

  for(int column=0;column<M1.size1();column++) 
    for(int i=0;i<M1.size2();i++)
      for(int j=0;j<i;j++)
      {
	if (M1(column,i) == alphabet::unknown or M1(column,j) == alphabet::unknown)
	  continue;

	if (M1(column,i) != alphabet::gap or M1(column,j)!= alphabet::gap) {
	  if (not A_match(M1,column,i,j,M2,column_indices2)) 
          {
	    if (M1(column,i) != alphabet::gap)
	      mismatch++;
	    if (M1(column,j) != alphabet::gap)
	      mismatch++;
	  }
	}
      }

  return mismatch;
}
Exemple #12
0
alignment get_alignment(const ublas::matrix<int>& M, alignment& A1) 
{
  alignment A2 = A1;
  A2.changelength(M.size1());

  // get letters information
  vector<vector<int> > sequences;
  for(int i=0;i<A1.n_sequences();i++) {
    vector<int> sequence;
    for(int c=0;c<A1.length();c++) {
      if (not A1.gap(c,i))
	sequence.push_back(A1(c,i));
    }
    sequences.push_back(sequence);
  }

  for(int i=0;i<A2.n_sequences();i++) {
    for(int c=0;c<A2.length();c++) {
      int index = M(c,i);

      if (index >= 0)
	index = sequences[i][index];

      A2.set_value(c,i, index);
    }
  }

  return A2;
}
void initMatrix(ublas::matrix<T> &A, const T data[]) {
  for (unsigned int i = 0; i < A.size1(); i++) {
    for (unsigned int j = 0; j < A.size2(); j++) {
      A(i, j) = data[i * A.size2() + j];
    }
  }
}
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;
}
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;
  }
}
Exemple #16
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;
}
Exemple #17
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;
}
Exemple #18
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;
}
int checkMessage(const ublas::vector<int> &u, const ublas::matrix<int> &H) {
  int sNotZero = 0;
  for (unsigned int k = 0; k < H.size1(); k++) {
    int s = ublas::inner_prod(u, ublas::row(H, k)) % 2;
    if (s != 0) {
      sNotZero++;
    }
  }

  return sNotZero;
}
Exemple #20
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;
}
Exemple #21
0
double colSums(const ublas::matrix<long double>& A, vector<long double>& sums)
{
	long double total = 0.0;
	sums = vector<long double>(A.size2(),0.0);
	for (size_t i = 0; i < A.size1(); ++i)
		for (size_t j = 0; j < A.size2(); ++j)
		{
			sums[j] += A(i,j);
			total += A(i,j);
		}
	return total;
}
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));
    }
  }

}
Exemple #23
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;
}
Exemple #24
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;
}
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));
        }
      }
    }
  }  
}
Exemple #26
0
    double determinant(ublas::matrix<double> input) {
        ublas::permutation_matrix<std::size_t> pivots(input.size1());

        ublas::lu_factorize(input, pivots);

        double det = 1.0;

        for (std::size_t i = 0; i < pivots.size(); i++) {
            if (pivots(i) != i)
                det *= -1.0;

            det *= input(i, i);
        }

        return det;
    }
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();
}
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;
}
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;
}
Exemple #30
0
ublas::vector<ublas::matrix<double> > wishart_InvA_rnd(const int df, ublas::matrix<double>& S, const int mc) {
  // Generates wishart matrix allowing for singular wishart
  size_t p = S.size1();
  ublas::vector<double> D(p);
  ublas::matrix<double> P(p, p);
  ublas::matrix<double> F(p, p);
  F = ublas::zero_matrix<double>(p, p);

  // make copy of S
  // ublas::matrix<double> SS(S);

  lapack::gesvd('A', 'A', S, D, P, F);
  // svd0(S, P, D, F);
  // P = trans(P);

  //! correct for singular matrix
  std::vector<size_t> ii;
  for (size_t i=0; i<D.size(); ++i)
    if (D(i) > norm_inf(D)*1e-9)
      ii.push_back(i);
  
  size_t r = ii.size();
  ublas::indirect_array<> idx(r);
  for (size_t i=0; i<r; ++i)
    idx(i) = ii[i];

  ublas::indirect_array<> irow(p);
  for (size_t i=0; i<irow.size(); ++ i) 
    irow(i) = i;
  ublas::matrix<double> Q(p, r);
  // Q = prod(project(P, irow, idx), diagm(ublas::apply_to_all<functor::inv_sqrt<double> >(project(D, idx))));
  // rprod does not seem any faster than diagonalizing D before multiplication
  // Q = rprod(project(P, irow, idx), ublas::apply_to_all<functor::inv_sqrt<double> >(D));
  axpy_prod(project(trans(P), irow, idx), diagm(ublas::apply_to_all<functor::inv_sqrt<double> >(project(D, idx))), Q, true);

  // generate mc samples
  ublas::vector<ublas::matrix<double> > K(mc);
  for (int i=0; i<mc; ++i)
    K(i) = wishart_1(df, Q, p, r);
  return K;
}