示例#1
0
  void normalizeRM(matrix_t & Q, StateMap const & staMap, float subs)
  {
    vector_t equiFreq = deriveEquiFreqForReversibleRM(Q);
    HammingDistance hammingDistance(staMap);
    number_t normConst = 0;
    for (unsigned i = 0; i < Q.size1(); ++ i)
      for (unsigned j = 0; j < Q.size2(); ++ j)
	normConst += equiFreq(i) * Q(i,j) * hammingDistance(i,j);

    Q = Q * (subs / normConst);
  }
示例#2
0
void output_matrix(ostream& out, matrix_t& M, unsigned n) {
	out << "matrix_t M[" << n << "] = {\n";
	for (unsigned i=0; i<M.size1(); ++i) {
		out << "\t{ ";
		out << M(i,0);
		for (unsigned j=1; j<M.size2() ; ++j)
			out << ',' << M(i,j);
		out << " },\n";
	}
	out << "}\n";
}
示例#3
0
  unsigned nonZeroDiagonalEntryOrDie(matrix_t const & Q)
  {
    assert( Q.size1() == Q.size1() ); // Q is quadratic

    for (unsigned i = 0; i < Q.size1(); i++)
      if (Q(i, i) != 0.0)
	return i;

    errorAbort("All zero diagonal entries. Bails out.");
    return 0; // will never reach this, only to quiet compiler
  }
示例#4
0
 color_matrix_transform(matrix_t const & matrix)
   : matrix_(matrix)
 {
   if (matrix.shape()[0] == 3 && matrix.shape()[1] == 3)
   {
     // TODO make specialization for 3x3 matrix
     matrix_.resize(boost::extents[4][5]);
     matrix_[3][3] = 1;
   }
   else
     BOOST_ASSERT(matrix.shape()[0] == 4 && matrix.shape()[1] == 5);
 }
示例#5
0
// LU factorization of a general matrix A.  
//    Computes an LU factorization of a general M-by-N matrix A using
//    partial pivoting with row interchanges. Factorization has the form
//    A = P*L*U.
//    a       (IN/OUT - matrix(M,N)) On entry, the coefficient matrix A to be factored. On exit, the factors L and U from the factorization A = P*L*U.
//    ipivot  (OUT - vector(min(M,N))) Integer vector. The row i of A was interchanged with row IPIV(i).
//    info    (OUT - int)
//   0   :  successful exit
//   < 0 :  If INFO = -i, then the i-th argument had an illegal value.
//   > 0 :  If INFO = i, then U(i,i) is exactly zero. The  factorization has been completed, but the factor U is exactly singular, and division by zero will occur if it is used to solve a system of equations.
int getrf (matrix_t& a, pivot_t& ipivot)
{
	matrix_t::value_type* _a = a.data().begin();
	int _m = int(a.size1());
	int _n = int(a.size2());
	int _lda = _m;	// minor size
	int _info;

	rawLAPACK::getrf (_m, _n,	_a, _lda, ipivot.data().begin(), _info);

	return _info;
}
示例#6
0
vector_t
svd_hint_from_most_similar(
    index_t  const user, 
    matrix_t const & sim, 
    matrix_t const & P, 
    matrix_t const & Q)
{
    stack::fe_asserter dummy{};
    vector_ll_t neighbors{std::min<index_t>(10,sim.get_rows())};
    vector_t    weights{std::min<index_t>(10,sim.get_rows())};
    most_similar(user, neighbors, weights, sim);
    return svd_hint(neighbors, weights, P, Q);
}
示例#7
0
vector_t /* user_factor */
est_factor(
    vector_ll_t const & neighbors,
    vector_t    const & weights, 
    matrix_t    const & P)
{
    stack::fe_asserter dummy1{};
    scoped_timer dummy(std::string(__func__));

    vector_t user_factor{P.get_cols()};

    for(int i = 0; i < neighbors.get_len(); i++)
        user_factor += weights[i] * P.get_row_clone(neighbors[i]);
    return user_factor.normalize_2();
}
void matrix_t::filtra(matrix_t& M, matrix_item_t it, double precision){
    matrix_inx_t m = get_m();
    matrix_inx_t n = get_n();
    M.redimensiona(m, n);
    for (int i = 1; i < m; i++) {
        for (int j = 1; j < n ; j++) {
            if (M.igual(get_matrix_item(i, j),it,precision) == true) {
                M.set_matrix_item(i, j, get_matrix_item(i, j));
            } else {
                matrix_item_t x = 0.00000;
                M.set_matrix_item(i, j, x);
            }
        }
    }
}
示例#9
0
static void applyGaussian(matrix_t& m) {
	matrix_t::array_type& arr = m.data();
	double sig = *std::max_element(arr.begin(), arr.end());

	BOOST_FOREACH(double& v, arr) {
		v = std::exp(-v * v / (2 * sig * sig));
	}
示例#10
0
// mult X * D
matrix_t diag_t::left_mult(matrix_t const &X) const
{
    stack::fe_asserter fe{};
    stack_assert(diagonal.get_len() == X.get_cols());

    matrix_t ret = X.clone();

    if(ret.get_rows() < ret.get_cols())
        for(size_t r = 0; r < ret.get_rows(); r++)
            ret[r] *= diagonal;
    else
        for(size_t c = 0; c < ret.get_cols(); c++)
            ret.get_col(c) *= diagonal[c];

    return ret;
}
示例#11
0
void multi2(const matrix_t &A, const matrix_t &B, matrix_t &C)
{
    size_t n = A.size(); 
    float **__restrict__ const da = A.data;
    float **__restrict__ const db = B.data;
    float **__restrict__ dc = C.data;

    const size_t chunk_size = 8;
    const size_t chunks = n / chunk_size;

#pragma omp parallel for num_threads(8)
    for(size_t i = 0; i < n; ++i)
    {
        __m256 a_line, b_line, c_line, r_line;
        for(size_t k = 0; k < n; ++k)
        {
            float c = da[i][k];
            a_line = _mm256_set_ps(c, c, c, c, c, c, c, c);
            for(size_t j = 0; j < chunks; ++j)
            {
                float mc[32] __attribute__((aligned(32))); 
                b_line = _mm256_load_ps(&db[k][j * chunk_size]);
                c_line = _mm256_load_ps(&dc[i][j * chunk_size]);
                r_line = _mm256_mul_ps(a_line, b_line);  
                r_line = _mm256_add_ps(r_line, c_line);
                _mm256_store_ps(&dc[i][j * chunk_size], r_line);
            } 

            for(size_t j = chunk_size * chunks; j < n; ++j)
            {
                dc[i][j] += c * db[k][j];
            }
        }
    }
}
示例#12
0
matrix_t operator+(matrix_t const & X, diag_t const & D)
{
    stack::fe_asserter fe{};
    stack_assert(X.get_rows() == D.get_rows());
    stack_assert(X.get_cols() == D.get_cols());
    matrix_t ret = X.clone();

    vector_t Xdiag = vector_t{
        ret.get_data(),
        ret.get_rows()/*it's a square matrix*/,
        ret.get_rows() + 1, // diagonal entries of a square matrix
    };
    Xdiag += diag_clone(D);

    return ret;
}
示例#13
0
bool search(matrix_t &matrix, int x, int y, string needle, int start_index, bool scan) {
  int x_max = matrix.size() - 1;
  int y_max = matrix[0].size() - 1;

  if (x < 0 || x > x_max) return false;
  if (y < 0 || y > y_max) return false;

  if (needle.size() == start_index) {
    return true;
  }

  if (matrix[x][y] == needle[start_index]) {
    int original_char = matrix[x][y];
    matrix[x][y] = '.';
    for (int i = 0; i < 8; ++i) {
      bool found = search(matrix, x + search_x[i], y + search_y[i], needle, start_index + 1, false);
      if (found) return true;
    }
    matrix[x][y] = original_char;
  }

  if (scan) {
    if (y < y_max) {
      return search(matrix, x, y + 1, needle, start_index, true);
    } else if (x < x_max) {
      return search(matrix, x + 1, 0, needle, start_index, true);
    }
  }

  return false;
}
示例#14
0
// QR Factorization of a MxN General Matrix A.
//    a       (IN/OUT - matrix(M,N)) On entry, the coefficient matrix A. On exit , the upper triangle and diagonal is the min(M,N) by N upper triangular matrix R.  The lower triangle, together with the tau vector, is the orthogonal matrix Q as a product of min(M,N) elementary reflectors.
//    tau     (OUT - vector (min(M,N))) Vector of the same numerical type as A. The scalar factors of the elementary reflectors.
//    info    (OUT - int)
//   0   : function completed normally
//   < 0 : The ith argument, where i = abs(return value) had an illegal value.
int geqrf (matrix_t& a, vector_t& tau)
{
	int              _m = int(a.size1());
	int              _n = int(a.size2());
	int              _lda = int(a.size1());
	int              _info;

	// make_sure tau's size is greater than or equal to min(m,n)
	if (int(tau.size()) < (_n<_m ? _n : _m) )
		return -104;

	int ldwork = _n*_n;
	vector_t dwork(ldwork);
	rawLAPACK::geqrf (_m, _n, a.data().begin(), _lda, tau.data().begin(), dwork.data().begin(), ldwork, _info);

	return _info;
}
示例#15
0
文件: main.cpp 项目: CCJY/coliru
matrix_t matrixMul(const matrix_t & matrixA, const matrix_t & matrixB) {
    auto dimension = matrixA.size();

	assert(matrixA.size() == dimension);
	assert(matrixA[0].size() == dimension);
	assert(matrixB.size() == dimension);
	assert(matrixB[0].size() == dimension);

	matrix_t matrixC(dimension, typename matrix_t::value_type(dimension, 0));//0ed matrix

	for(int x{0}; x < dimension; ++x)
	for(int i{0}; i < dimension; ++i)
	for(int y{0}; y < dimension; ++y)
		matrixC[x][y] += matrixA[x][i] * matrixB[i][y];

	return matrixC;//move semantics ftw
}
示例#16
0
  void NormalMeanPostFactor::mkFactor(matrix_t &m) const{
    //Remember to convert to index space
    boost::math::normal dist( (postmean_-minv_)*bins_/(maxv_-minv_), 
			      std::sqrt(postvar_)*bins_/(maxv_-minv_) );
    for(int i = 0; i < m.size2(); ++i){
      m(0,i) = cdf(dist, i+1)-cdf(dist,i);
    }
  }
示例#17
0
文件: main.cpp 项目: CCJY/coliru
matrix_t matrixMulTiled(const matrix_t & matrixA, const matrix_t & matrixB) {
	auto dimension = matrixA.size();

	assert(matrixA.size() == dimension);
	assert(matrixA[0].size() == dimension);
	assert(matrixB.size() == dimension);
	assert(matrixB[0].size() == dimension);

	matrix_t matrixC(dimension, typename matrix_t::value_type(dimension, 0));//0ed matrix

	const int m{8};//256bit
	const size_t n{dimension - dimension % m};
	int start{0};

	if(n >= m) {
		for (int i{0}; i < n; i+=m)
		for (int j{0}; j < n; j+=m)
		for (int k{0}; k < n; k+=m)
		for (int s{0}; s < m; s++)
		for (int t{0}; t < m; t++)
		for (int u{0}; u < m; u++)
			matrixC[i + s][j + t] += matrixA[i + s][k + u] * matrixB[k + u][j + t];
		start = n;
	}

	//finalize calculations within tiles
	for(int x{0}; x < n; ++x)
	for(int i{start}; i < dimension; ++i)
	for(int y{0}; y < n; ++y)
		matrixC[x][y] += matrixA[x][i] * matrixB[i][y];

	//calculate remaining rows
	for(int x{start}; x < dimension; ++x)
	for(int i{0}; i < dimension; ++i)
	for(int y{0}; y < dimension; ++y)
		matrixC[x][y] += matrixA[x][i] * matrixB[i][y];

	//calculate remaining elements (remaining columns without already calculated rows)
	for(int x{0}; x < n; ++x)
	for(int i{0}; i < dimension; ++i)
	for(int y{start}; y < dimension; ++y)
		matrixC[x][y] += matrixA[x][i] * matrixB[i][y];

	return matrixC;//move semantics ftw
}
示例#18
0
文件: mul.cpp 项目: dragosht/courses
void Print(const matrix_t& m)
{
    for (unsigned i = 0; i < m.size(); i++) {
        for (unsigned j = 0; j < m[i].size(); j++) {
            cout << m[i][j] << " ";
        }
        cout << endl;
    }
}
示例#19
0
  void BinomialFactor::mkFactor(matrix_t &m) const{
    boost::math::binomial binom(N_, prob_);
    //Return vector with probabilities over x
    for(int j = 0; j < m.size2(); ++j){
      if( j+minv_ > N_)
	m(0,j) = 0;
      else
	m(0,j) = pdf( binom, j+minv_);
    }
  }
示例#20
0
 AbstractFullyParameterizedFactor::AbstractFullyParameterizedFactor(string const & type, string const & id, matrix_t const & m, matrix_t const & pseudoCounts, matrix_t const & funBMat) 
   : AbstractBaseFactor(type, id, m.size1(), m.size2()), m_(m), pseudoCounts_(pseudoCounts), funBMat_(funBMat)
 {
   if (pseudoCounts.size1() != 0) {
     assert(pseudoCounts.size1() == size1_); 
     assert(pseudoCounts.size2() == size2_); 
   }
   if (funBMat.size1() != 0) {
     assert(funBMat.size1() == size1_); 
     assert(funBMat.size2() == size2_); 
   }
 }
示例#21
0
  void deriveEquiFreqs(matrix_t const & Q, vector_t & equiFreq)
  {
    unsigned size = Q.size1();

    for (unsigned int j = 0; j < size; j++)
      if (equiFreq(j) == 0 && Q(j, j) != 0.0 )
	for (unsigned int i = 0; i < size; i++) 
	  if (equiFreq(i) != 0.0 && Q(j, i) != 0.0 )
	    equiFreq(j) = equiFreq(i) * Q(i, j) / Q(j, i);
  }
示例#22
0
文件: main.cpp 项目: CCJY/coliru
matrix_t gaussSeidelIteration(const matrix_t & grid) {
	auto dimension = grid.size();

	assert(grid[0].size() == dimension);

	auto gridCopy = grid;

	for(int x{1}; x < dimension - 1; ++x)
	for(int y{1}; y < dimension - 1; ++y)
		gridCopy[x][y] += 0.25 * (grid[x-1][y] + grid[x][y-1] + grid[x][y+1] + grid[x+1][y]);

	return gridCopy;
}
sparse_matrix_t::sparse_matrix_t(matrix_t& M, double precision):
matval_(NULL),
matind_(NULL),
matbeg_(NULL),
matcnt_(NULL),
nz_(0),
m_(M.get_m()),
n_(M.get_n())
{	
	for(int i=1;i<=m_;i++)
		for(int j=1;j<=n_;j++)
			if (!zero(M.get_matrix_item(i,j),precision))
				nz_++;

	matval_ = new matrix_item_t [nz_];
	matind_ = new vector_inx_t  [nz_];
	matbeg_ = new vector_inx_t  [n_];
	matcnt_ = new vector_inx_t  [n_];

	int nz_i=0;
	
	for(int j=1;j<=n_;j++){

		matcnt_[j-1]=0;
		matbeg_[j-1]=nz_i;

		for(int i=1;i<=m_;i++)	
			if (!zero(M.get_matrix_item(i,j),precision)){

				matval_[nz_i]=M.get_matrix_item(i,j);
				matind_[nz_i]=i-1;

				matcnt_[j-1]++;
		
				nz_i++;						
			}
	}
}	
示例#24
0
// the weights are (this user, everyone else) vector.
vector_t 
svd_hint(
    vector_ll_t const & neighbors,
    vector_t    const & weights,
    matrix_t    const & P,
    matrix_t    const & Q)
{
    stack::fe_asserter dummy1{};
    vector_t user_factor = est_factor(neighbors, weights, P);
 
// get hint
    
    scoped_timer dummy(std::string(__func__) + " factor X Q");
    return user_factor * Q.tr_shallow();
}
示例#25
0
// Transpose of a matrix
void transposeMatrix(matrix_t & M)
{
    int rM = M.size();
    int cM = M[1].size();
    matrix_t tM;
    sizeMatrix(tM,cM,rM);
    for (int r=0; r<cM; r++)
    {
	for (int c=0; c<rM; c++)
	{
	    tM[r][c] = M[c][r];
	}
    }
    M = tM;
}
示例#26
0
// Solution to a system using LU factorization 
//   Solves a system of linear equations A*X = B with a general NxN
//   matrix A using the LU factorization computed by GETRF.
//   transa  (IN - char)  'T' for the transpose of A, 'N' otherwise.
//   a       (IN - matrix(M,N)) The factors L and U from the factorization A = P*L*U as computed by GETRF.
//   ipivot  (IN - vector(min(M,N))) Integer vector. The pivot indices from GETRF; row i of A was interchanged with row IPIV(i).
//   b       (IN/OUT - matrix(ldb,NRHS)) Matrix of same numerical type as A. On entry, the right hand side matrix B. On exit, the solution matrix X.
//
//   info    (OUT - int)
//   0   : function completed normally
//   < 0 : The ith argument, where i = abs(return value) had an illegal value.
//   > 0 : if INFO =  i,  U(i,i)  is  exactly  zero;  the  matrix is singular and its inverse could not be computed.
int getrs (char transa, matrix_t& a,
	    pivot_t& ipivot, matrix_t& b)
{
	matrix_t::value_type* _a = a.data().begin();
	int a_n = int(a.size1());
	int _lda = a_n;
	int p_n = int(ipivot.size());

	matrix_t::value_type* _b = b.data().begin();
	int b_n = int(b.size1());
	int _ldb = b_n;
	int _nrhs = int(b.size2()); /* B's size2 is the # of vectors on rhs */

	if (a_n != b_n) /*Test to see if AX=B has correct dimensions */
		return -101;
	if (p_n < a_n)     /*Check to see if ipivot is big enough */
		return -102;

	int _info;
	rawLAPACK::getrs (transa, a_n, _nrhs, _a,	_lda, ipivot.data().begin(), 
				_b, _ldb, _info);

	return _info;
} 
示例#27
0
void gaussj(matrix_t & a, matrix_t & b)
{
  int i,icol,irow,j,k,l,ll;
  double big,dum,pivinv;
  
  int n=a.size();
  int m=b[0].size();
  vector_t indxc(n),indxr(n),ipiv(n);
  for (j=0;j<n;j++) ipiv[j]=0;
  for (i=0;i<n;i++) {
    big=0.0;
    for (j=0;j<n;j++)
      if (ipiv[j] != 1)
	for (k=0;k<n;k++) {
	  if (ipiv[k] == 0) {
	    if (fabs(a[j][k]) >= big) {
	      big=fabs(a[j][k]);
	      irow=j;
	      icol=k;
	    }
	  }
	}
    ++(ipiv[icol]);
    if (irow != icol) {
      for (l=0;l<n;l++) SWAP(a[irow][l],a[icol][l]);
      for (l=0;l<m;l++) SWAP(b[irow][l],b[icol][l]);
    }
    indxr[i]=irow;
    indxc[i]=icol;
    if (a[icol][icol] == 0.0) error("gaussj: Singular Matrix");
    pivinv=1.0/a[icol][icol];
    a[icol][icol]=1.0;
    for (l=0;l<n;l++) a[icol][l] *= pivinv;
    for (l=0;l<m;l++) b[icol][l] *= pivinv;
    for (ll=0;ll<n;ll++)
      if (ll != icol) {
	dum=a[ll][icol];
	a[ll][icol]=0.0;
	for (l=0;l<n;l++) a[ll][l] -= a[icol][l]*dum;
	for (l=0;l<m;l++) b[ll][l] -= b[icol][l]*dum;
      }
  }
  for (l=n-1;l>=0;l--) {
    if (indxr[l] != indxc[l])
      for (k=0;k<n;k++)
	SWAP(a[k][(int)indxr[l]],a[k][(int)indxc[l]]);
  }
}
示例#28
0
bool HMM::check_enrichment(const Data::Contrast &contrast,
                           const matrix_t &counts, size_t group_idx) const {
  string motif = groups[group_idx].name;
  double signal = 0, control = 0;
  double total_signal = 0, total_control = 0;
  for (size_t i = 0; i < counts.size1(); i++) {
    if (contrast.sets[i].motifs.find(motif) != contrast.sets[i].motifs.end()) {
      signal += counts(i, 0);
      total_signal += counts(i, 0) + counts(i, 1);
    } else {
      control += counts(i, 0);
      total_control += counts(i, 0) + counts(i, 1);
    }
  }
  bool ok = signal / total_signal > control / total_control;
  return ok;
}
示例#29
0
文件: io-mix.cpp 项目: ybouret/yocto4
        void __lua:: load(lua_State     *L,
                          matrix_t      &solutions,
                          const string  &id,
                          const library &lib,
                          equilibria    &eqs,
                          const double   t)
        {
            // get the array of names
            assert(L);
            const char *name = id.c_str();
            lua_settop(L,0);
            lua_getglobal(L,name);

            if(!lua_istable(L, -1))
            {
                throw exception("'%s' is not a LUA_TABLE of initial conditions",name);
            }

            // parse all strings to use other function
            const size_t   ns = lua_rawlen(L, -1);
            solutions.make(ns,eqs.M);

            vector<string> vs(ns,as_capacity);
            for(size_t i=1;i<=ns;++i)
            {
                lua_rawgeti(L, -1, i);
                if( !lua_isstring(L, -1))
                {
                    throw exception("%s[%u] is not a string", name, unsigned(i));
                }
                const string tmp = lua_tostring(L, -1);
                vs.push_back(tmp);
                lua_pop(L,1);
            }

            // now loading
            for(size_t i=1;i<=ns;++i)
            {
                const string ini_name = vs[i];
                boot         loader;
                load(L,loader,ini_name,lib);
                eqs.create(solutions[i], loader, t);
            }
        }
示例#30
0
void multi1(const matrix_t &A, const matrix_t &B, matrix_t &C)
{
    size_t n = A.size(); 
    float **__restrict__ const da = A.data;
    float **__restrict__ const db = B.data;
    float **__restrict__ dc = C.data;

#pragma omp parallel for num_threads(8)
    for(size_t i = 0; i < n; ++i)
    {
        for(size_t k = 0; k < n; ++k)
        {
            float c = da[i][k];
            for(size_t j = 0; j < n; ++j)
                dc[i][j] += c * db[k][j];
        }
    }

}