Ejemplo n.º 1
0
// m=m-m*v*v'
void sub_m_v_vT(mat &m, const vec &v)
{
  vec v2(m.rows());
  double tmp, *v2p;
  const double *vp;
  int i, j;

  it_assert(v.size() == m.cols(), "sub_m_v_vT()");

  v2p = v2._data();
  for (i = 0; i < m.rows(); i++) {
    tmp = 0.0;
    vp = v._data();
    for (j = 0; j < m.cols(); j++)
      tmp += *(vp++) * m._elem(i, j);
    *(v2p++) = tmp;
  }

  v2p = v2._data();
  for (i = 0; i < m.rows(); i++) {
    vp = v._data();
    for (j = 0; j < m.cols(); j++)
      m._elem(i, j) -= *v2p * *(vp++);
    v2p++;
  }
}
Ejemplo n.º 2
0
mat find_inliers(mat data, const parameters& params) {
  uint_fast32_t n_inliers = 0;
  double outlier_proportion;
  
  uvec best_indices;
  uvec indices = rand_indices(0, data.n_cols);
  
  uint_fast32_t iter = 0;
  uint_fast32_t iter_max = -1;
  while (iter < iter_max) {
    mat maybe_inliers = data.cols(indices.head(params.nfit_points));
    
    mat model = params.model_function(maybe_inliers);
    
    mat distances = params.distance_function(model, data);
    
    uvec inliers = find(distances < params.distance_threshold);
    
    if (inliers.n_elem > n_inliers) {
      n_inliers = inliers.n_elem;
      best_indices = inliers;
  
      outlier_proportion = 1.0 - (double) inliers.n_elem / (double) data.n_cols; 
      iter_max = lround(
        log(1.0 - 0.99) / log(1.0 - pow(1.0 - outlier_proportion, params.nfit_points))
      );
    }

    indices = shuffle(indices); // generate new random indexes
    ++iter;
  }

  return data.cols(best_indices);
}
Ejemplo n.º 3
0
mat ls_solve_od(const mat &A, const mat &B)
{
    int m=A.rows(), n=A.cols(), N=B.cols(), j;
    double beta;
    mat A2(A), B2(B), B3(n,N), submat, submat2;
    vec tmp(n), v;

//    it_assert1(m >= n, "The system is under-determined!");
    //it_assert1(m == B.rows(), "The number of rows in A must equal the number of rows in B!");

    // Perform a Householder QR factorization
    for (j=0; j<n; j++) {
	house(rvectorize(A2(j, m-1, j, j)), v, beta);
	v *= sqrt(beta);
	// 	submat.ref(A2, j,m-1, j,n-1);
 	submat = A2(j,m-1,j,n-1);
	sub_v_vT_m(submat, v);
	// 	submat.ref(B2, j,m-1, 0,N-1);
 	submat = B2(j,m-1,0,N-1);
	sub_v_vT_m(submat, v);
    }

    //    submat.ref(A2, 0,n-1,0,n-1);
    //    submat2.ref(B2, 0,n-1,0,N-1);
    submat = A2(0,n-1,0,n-1);
    submat2 = B2(0,n-1,0,N-1);
    for (j=0; j<N; j++) {
	backward_substitution(submat, submat2.get_col(j), tmp);
	B3.set_col(j, tmp);
    }
    
    return B3;
}
Ejemplo n.º 4
0
vec mix::process(bvec ce, mat x)
{
 vec y;	
 int N;
 bvec one = ("1");

	#if (DEBUG_LEVEL==3)
	cout << "***** mix::process *****" << endl;	
	cout << "ce=" << ce << endl;
	cout << "x=" << x << endl;
	sleep(1000);
	#endif

	N=ce.length();
	if (x.rows()!=N) { 
		throw sci_exception("mix::process - ce.size <> x.rows()", x.rows() );
	}
	if (x.cols()!=2) { 
		throw sci_exception("mix::process - x=[x1,x1] - x.cols()!=2", x.cols() );
	}

	y.set_length(N);
	for (int i=0; i<N; i++) {
		if ( bool(ce[i])) {
			y0 = G.process(one, to_vec(x(i,0)*x(i,1)))(0);
		}
		y[i]=y0;
	}
	#if (DEBUG_LEVEL==3)
	cout << "y=" << y << endl;
	cout << "+++++ mix::process +++++" << endl;	
	sleep(1000);
	#endif
	return (y);
}
Ejemplo n.º 5
0
    static mat qp_off_diag(const mat& Q, const mat& A) {
      mat res(Q.cols() + A.rows(), 
	      Q.cols() + A.rows());
      
      res << 
	off_diag(Q), -A.transpose(),
	A, mat::Zero(A.rows(), A.rows());
      
      return res;
    }
Ejemplo n.º 6
0
cmat operator*(const std::complex<double> &s, const mat &m)
{
  it_assert_debug(m.rows() > 0 && m.cols() > 0, "operator*(): Matrix of zero length");

  cmat temp(m.rows(), m.cols());

  for (int i = 0;i < m._datasize();i++) {
    temp._data()[i] = s * m._data()[i];
  }
  return temp;
}
Ejemplo n.º 7
0
static
void assert_mat(const mat &ref, const mat &act)
{
  ASSERT_EQ(ref.rows(), act.rows());
  ASSERT_EQ(ref.cols(), act.cols());
  for (int n = 0; n < ref.rows(); ++n) {
    for (int k = 0; k < ref.cols(); ++k) {
      ASSERT_NEAR(ref(n,k), act(n,k), tol);
    }
  }
}
Ejemplo n.º 8
0
    mat qp_matrix(const mat& Q, const mat& A) {
      assert(A.cols() == Q.cols());
      assert(Q.rows() == Q.cols());
      
      mat M;
      M.resize(Q.rows() + A.rows(), Q.cols() + A.rows() );

      M << Q, -A.transpose(),
	A, mat::Zero(A.rows(), A.rows() );

      return std::move(M);
    }
Ejemplo n.º 9
0
cmat operator+(const mat &a, const cmat &b)
{
  it_assert_debug(a.cols() == b.cols() && a.rows() == b.rows(), "operator+(): sizes does not match");
  cmat temp(b);

  for (int i = 0;i < a.rows();i++) {
    for (int j = 0;j < a.cols();j++) {
      temp(i, j) += std::complex<double>(static_cast<double>(a(i, j)), 0.0);
    }
  }
  return temp;
}
Ejemplo n.º 10
0
mat cheb(int n, const mat &x)
{
  it_assert_debug((x.rows() > 0) && (x.cols() > 0), "cheb(): empty matrix");

  mat out(x.rows(), x.cols());
  for (int i = 0; i < x.rows(); ++i) {
    for (int j = 0; j < x.cols(); ++j) {
      out(i, j) = cheb(n, x(i, j));
    }
  }
  return out;
}
Ejemplo n.º 11
0
void network::datasetOnLineTrain(mat dataset,int TRAINNING_TYPE,int MAX_TRAINNING_TIME) {
    int time=0;
    //上一轮训练下来的误差
    double round_error;
    mat serror;
    //赋值训练最小误差为较大的数
    min_error=DBL_MAX;
    //样本的输入和输出
    rowvec sample,out;
    while(time<MAX_TRAINNING_TIME)
    {
        round_error=0;
        for(int i=0;i<dataset.n_rows;i++)
        {
            sample=dataset.row(i).cols(0,in_vec-1);
            out=dataset.row(i).cols(in_vec,dataset.n_cols-1);
            if(TRAINNING_TYPE==BP_NONE)
            {
                simplyBPTrain(sample,out);
            }
            else if(TRAINNING_TYPE==BP_WITH_SPARSE)
            {
                withSparseTrain(sample,out);
            }
            else{
                return;
            }
        }
        updateOut(dataset.cols(0,in_vec-1));
        serror=dataset.cols(in_vec,dataset.n_cols-1)-output;
        round_error=norm(sum(serror%serror/dataset.n_rows),2);
        time++;
        if(round_error<min_error)
        {
            min_error=round_error;
            for(int i=layer_num;i>=1;i--)
            {
                mw[i]=w[i];
                mo[i]=o[i];
            }
        }
        if(time%SHOW_TIME==0)
            cout<<"第"<<time<<"次训练的误差变化为"<<setprecision(50)<<round_error<<endl;
        if(round_error<tor_error)
        {
            error=round_error;
            break;
        }
    }
    cout<<"本次训练了"<<time<<"次,最小训练误差为"<<min_error<<endl<<"最终训练误差为"<<round_error<<endl;
}
Ejemplo n.º 12
0
void forward_substitution(const mat &L, int p, const vec &b, vec &x)
{
    assert( L.rows() == L.cols() && L.cols() == b.size() && b.size() == x.size() && p <= L.rows()/2 );
    int n = L.rows(), i, j;

    x=b;
  
    for (j=0;j<n;j++) {
	x(j)/=L(j,j);
	for (i=j+1;i<MIN(j+p+1,n);i++) {
	    x(i)-=L(i,j)*x(j);
	}
    }
}
Ejemplo n.º 13
0
inline void update_WtA(mat & WtA, const mat & W, const mat & W1, const mat & H2, const mat & A)
{
	// compute WtA = (W[:, 0:k-1], W1)^T (A - W[, k:end] H2^T)
	if (H2.empty())
		update_WtA(WtA, W, W1, A);
	else
	{
		int k = W.n_cols - H2.n_cols;
		//std::cout << "1.3" << std::endl;
		//A.print("A = ");
		//(W.cols(k, W.n_cols-1) * H2.t()).print("W[, k:] = ");
		update_WtA(WtA, W.cols(0, k-1), W1, A - W.cols(k, W.n_cols-1) * H2.t());
	}
}
Ejemplo n.º 14
0
void backward_substitution(const mat &U, int q, const vec &b, vec &x)
{
    assert( U.rows() == U.cols() && U.cols() == b.size() && b.size() == x.size() && q <= U.rows()/2);
    int n = U.rows(), i, j;

    x=b;
  
    for (j=n-1; j>=0; j--) {
	x(j) /= U(j,j);
	for (i=MAX(0,j-q); i<j; i++) {
	    x(i)-=U(i,j)*x(j);
	}
    }
}
Ejemplo n.º 15
0
bool schur(const mat &A, mat &U, mat &T)
{
  it_assert_debug(A.rows() == A.cols(), "schur(): Matrix is not square");

  char jobvs = 'V';
  char sort = 'N';
  int info;
  int n = A.rows();
  int lda = n;
  int ldvs = n;
  int lwork = 3 * n; // This may be choosen better!
  int sdim = 0;
  vec wr(n);
  vec wi(n);
  vec work(lwork);

  T.set_size(lda, n, false);
  U.set_size(ldvs, n, false);

  T = A; // The routine overwrites input matrix with eigenvectors

  dgees_(&jobvs, &sort, 0, &n, T._data(), &lda, &sdim, wr._data(), wi._data(),
         U._data(), &ldvs, work._data(), &lwork, 0, &info);

  return (info == 0);
}
/**
 * Compute the covariance matrix of a set of inputs
 * @param C The covariance matrix of X
 * @param X A matrix of inputs (one input per row)
 */
void CovarianceFunction::covariance(mat& C, const mat& X) const
{
	// ensure that data dimensions match supplied covariance matrix
	assert(C.rows() == X.rows());
	assert(C.cols() == X.rows());

	if (X.rows() == 1)
	{
	    C.set(0, 0, computeDiagonalElement(X.get_row(0)));
	    return;
	}
	
	// calculate the lower and upper triangles
	double d;
	
	for(int i=0; i<X.rows() ; i++)
	{
		for(int j=0; j<i; j++)
		{
		    d = computeElement(X.get_row(i), X.get_row(j));
		    C.set(i, j, d);
			C.set(j, i, d);
		}
	}

	// calculate the diagonal part
	for(int i=0; i<X.rows() ; i++)
	{
		C.set(i, i, computeDiagonalElement(X.get_row(i)));
	}
}
Ejemplo n.º 17
0
//used for both spheric and hybrid multilateration
static
bool generate_meas(vec &meas, const bvec &method, const mat &bs_pos, const vec &ms_pos)
{
  unsigned int method_len = length(method);
  unsigned int nb_bs = bs_pos.cols();
  bool column = true;
  if(3 == nb_bs) {
    nb_bs = bs_pos.rows();
    column = false;
  }
  if((nb_bs < method_len) || (3 != length(ms_pos))) {
    return false;
  }
  meas.set_size(method_len);
  vec pos(3);
  vec pos_ref(3);
  pos_ref = column ? bs_pos.get_col(0) : bs_pos.get_row(0);
  for(unsigned int k = 0; k < method_len; ++k) {
    if(bin(1) == method[k]) {  /* hyperbolic */
      pos = column ? bs_pos.get_col(k + 1) : bs_pos.get_row(k + 1);
      meas[k] = get_dist(pos, ms_pos) - get_dist(pos_ref, ms_pos);
    }
    else { /* spherical */
      pos = column ? bs_pos.get_col(k) : bs_pos.get_row(k);
      meas[k] = get_dist(pos, ms_pos);
    }
  }
  return true;
}
Ejemplo n.º 18
0
arma::mat lin_pred_matF_svft(const field<vec>& Xbetas, const field<mat>& Z,
                             const mat& b, const field<mat>& U,
                             const field<uvec>& RE_inds, const field<uvec>& id,
                             const field<uvec>& col_inds, const uvec& row_inds,
                             const int& nrows, const int& ncols,
                             const CharacterVector& trans_Funs) {
    int n_field = Xbetas.size();
    mat out = mat(nrows, ncols, fill::zeros);
    for (int i = 0; i < n_field; ++i) {
        mat bb = b.cols(RE_inds.at(i));
        vec Zb = sum(Z.at(i) % bb.rows(id.at(i)), 1);
        if (trans_Funs[i] == "identity") {
            out.submat(row_inds, col_inds.at(i)) = U.at(i).each_col() % (Xbetas.at(i) + Zb);
        } else if (trans_Funs[i] == "expit") {
            vec exp_eta = exp(Xbetas.at(i) + Zb);
            out.submat(row_inds, col_inds.at(i)) = U.at(i).each_col() % (exp_eta / (1 + exp_eta));
        } else if (trans_Funs[i] == "exp") {
            out.submat(row_inds, col_inds.at(i)) = U.at(i).each_col() % exp(Xbetas.at(i) + Zb);
        } else if (trans_Funs[i] == "log") {
            out.submat(row_inds, col_inds.at(i)) = U.at(i).each_col() % log(Xbetas.at(i) + Zb);
        } else if (trans_Funs[i] == "log2") {
            out.submat(row_inds, col_inds.at(i)) = U.at(i).each_col() % log2(Xbetas.at(i) + Zb);
        } else if (trans_Funs[i] == "log10") {
            out.submat(row_inds, col_inds.at(i)) = U.at(i).each_col() % log10(Xbetas.at(i) + Zb);
        } else if (trans_Funs[i] == "sqrt") {
            out.submat(row_inds, col_inds.at(i)) = U.at(i).each_col() % sqrt(Xbetas.at(i) + Zb);
        }
    }
    return(out);
}
Ejemplo n.º 19
0
Archivo: qr.cpp Proyecto: nvmd/itpp
bool qr(const mat &A, mat &R)
{
  int info;
  int m = A.rows();
  int n = A.cols();
  int lwork = n;
  int k = std::min(m, n);
  vec tau(k);
  vec work(lwork);

  R = A;

  // perform workspace query for optimum lwork value
  int lwork_tmp = -1;
  dgeqrf_(&m, &n, R._data(), &m, tau._data(), work._data(), &lwork_tmp,
          &info);
  if (info == 0) {
    lwork = static_cast<int>(work(0));
    work.set_size(lwork, false);
  }
  dgeqrf_(&m, &n, R._data(), &m, tau._data(), work._data(), &lwork, &info);

  // construct R
  for (int i = 0; i < m; i++)
    for (int j = 0; j < std::min(i, n); j++)
      R(i, j) = 0;

  return (info == 0);
}
Ejemplo n.º 20
0
    static vec qp_diag(const mat& Q, const mat& A) {
      vec res(Q.cols() + A.rows() );
      
      res << Q.diagonal(), vec::Ones(A.rows());

      return res;
    }
Ejemplo n.º 21
0
//used only for hyperbolic multilateration
static
bool generate_meas(mat &meas, const bvec &method, const mat &bs_pos, const vec &ms_pos)
{
  unsigned int k;
  unsigned int i;
  unsigned int method_len = length(method);
  unsigned int nb_bs = bs_pos.cols();
  bool column = true;
  if(3 == nb_bs) {
    nb_bs = bs_pos.rows();
    column = false;
  }
  if((nb_bs < method_len) || (3 != length(ms_pos))) {
    return false;
  }
  meas.set_size(nb_bs, nb_bs);
  vec pos_i(3);
  vec pos_k(3);
  for(k = 0; k < nb_bs; ++k) {
    pos_k = column ? bs_pos.get_col(k) : bs_pos.get_row(k);
    for(i = 0; i < nb_bs; ++i) {
      pos_i = column ? bs_pos.get_col(i) : bs_pos.get_row(i);
      meas(i, k) = get_dist(pos_i, ms_pos) - get_dist(pos_k, ms_pos);
    }
  }
  return true;
}
Ejemplo n.º 22
0
// [[Rcpp::export]]
mat lassocore(const mat Y,const mat Z, mat B,mat BOLD, const double gam,const double lassothresh,const colvec ZN, uvec m, const int k2,const int n2){
double thresh=10*lassothresh;

while(thresh>lassothresh)
{
 int j;
  for(j = 0; j < k2; ++j)
	{
	
	  
// Required critical region due to the use of an Rcpp object.         
      	#pragma omp critical
       	  {

     	    m=ind(k2,j);	       
	  }
	  B.col(j)=ST3a((Y-B.cols(m)*Z.rows(m))*trans(Z.row(j)),gam)/ZN(j);
	}
mat thresh1=abs((B-BOLD)/(ones(n2,k2)+abs(BOLD)));
 thresh=norm(thresh1,"inf");
 BOLD=B;
  
}

return(B);
}
Ejemplo n.º 23
0
 void set_cols(int start_col, int end_col, const mat& pmat){
   assert(start_col >= 0);
   assert(end_col <= end_offset - start_offset);
   assert(pmat.rows() == end-start);
   assert(pmat.cols() >= end_col - start_col);
   for (int i=start_col; i< end_col; i++)
     this->operator[](i) = get_col(pmat, i-start_col);
 }
Ejemplo n.º 24
0
double mse(const mat & A, const mat & W, const mat & H, const mat & W1, const mat & H2)
{
	// compute mean square error of A and fixed A
	const int k = W.n_cols - H2.n_cols;

	mat Adiff = A;
	Adiff -= W.cols(0, k-1) * H.cols(0, k-1).t();
	if (!W1.empty())
		Adiff -= W1*H.cols(k, H.n_cols-1).t();
	if (!H2.empty())
		Adiff -= W.cols(k, W.n_cols-1)*H2.t();

	if (A.is_finite())
		return mean(mean(square(Adiff)));
	else
		return mean(square(Adiff.elem(find_finite(Adiff))));
}
Ejemplo n.º 25
0
//Eigen does not sort eigenvalues, as done in matlab
inline bool eig_sym(const mat & T, vec & eigenvalues, mat & eigenvectors){
  //
  //Column  of the returned matrix is an eigenvector corresponding to eigenvalue number  as returned by eigenvalues(). The eigenvectors are normalized to have (Euclidean) norm equal to one.
  SelfAdjointEigenSolver<mat> solver(T);
  eigenvectors = solver.eigenvectors();
  eigenvalues = solver.eigenvalues(); 
  ivec index = sort_index(eigenvalues);
  sort(eigenvalues);
  vec eigenvalues2 = eigenvalues.reverse();
  mat T2 = zeros(eigenvectors.rows(), eigenvectors.cols());
  for (int i=0; i< eigenvectors.cols(); i++){
    set_col(T2, index[i], get_col(eigenvectors, i));
  }   
  eigenvectors = T2;
  eigenvalues = eigenvalues2;
  return true;
}
Ejemplo n.º 26
0
    static mat qp_mod(const mat& A) {
      mat res(A.rows() + A.cols(),
	      A.rows());
      
      res << A.transpose(), mat::Identity(A.rows(), A.rows()) ;
      
      return res;
    }
Ejemplo n.º 27
0
vec ls_solve_chol(const mat &A, int p, const vec &b)
{
vec ls_solve(const mat &L, int p, const mat &U, int q, const vec &b);
    mat U(A.rows(),A.cols());

//    it_error_if(!chol(A, p, U), "ls_solve_chol: Linear system not positive definite");
    return ls_solve(transpose(U), p, U, p, b);
}
Ejemplo n.º 28
0
inline mat get_cols(const mat&A, int start_col, int end_col){
  assert(end_col > start_col);
  assert(end_col <= A.cols());
  assert(start_col >= 0);
  mat a(A.rows(), end_col-start_col);
  for (int i=0; i< end_col-start_col; i++)
    set_col(a, i, get_col(A, i));
  return a;
}
Ejemplo n.º 29
0
mat ls_solve(const mat &A, const mat &B)
{
	vec ls_solve(const mat &L, const mat &U, const vec &b);
    mat L, U;
    ivec p;
    vec btemp;
  
    lu(A, L, U, p);

    mat X(B.rows(), B.cols());
  
    for (int i=0; i<B.cols(); i++) {
	btemp=B.get_col(i);
	interchange_permutations(btemp, p);
	X.set_col(i, ls_solve(L, U, btemp));
    }

    return X;
}
Ejemplo n.º 30
0
void backward_substitution(const mat &U, const vec &b, vec &x)
{
    assert( U.rows() == U.cols() && U.cols() == b.size() && b.size() == x.size() );
    int n = U.rows(), i, j;
    double temp;

    x(n-1)=b(n-1)/U(n-1,n-1);
    for (i=n-2; i>=0; i--) {
	// Should be: x(i)=((b(i)-U(i,i,i+1,n-1)*x(i+1,n-1))/U(i,i))(0); but this is too slow.
	temp=0;
	//i_pos=i*U._row_offset();
	for (j=i+1; j<n; j++) {
	    temp += U._elem(i,j) * x(j);
	    //temp+=U._data()[i_pos+j]*x(j);
	}
	x(i) = (b(i)-temp)/U._elem(i,i);
	//x(i)=(b(i)-temp)/U._data()[i_pos+i];
    }
}