void ribi::QtUblasVectorDoubleModel::SetRawData(const boost::numeric::ublas::vector<double>& data)
{
  if (!Matrix::VectorsDoubleAreEqual(m_data,data))
  {
    const int new_size = boost::numeric_cast<int>(data.size());
    const int cur_size = this->rowCount();
    if (cur_size < new_size)
    {
      this->insertRows(cur_size,new_size - cur_size,QModelIndex());
    }
    else if (cur_size > new_size)
    {
      this->removeRows(cur_size,cur_size - new_size,QModelIndex());
    }
    //Set the data before emitting signals, as the response to that signal
    //will be dependent on that data
    m_data = data;

    assert(this->rowCount() == boost::numeric_cast<int>(data.size())
      && "So emit layoutChange can work on the newest layout");

    //If you forget this line, the view displays a different number of rows than m_data has
    emit layoutChanged();

    //emit dataChanged(QModelIndex(),QModelIndex());
    const QModelIndex top_left = this->index(0,0);
    const QModelIndex bottom_right = this->index(m_data.size() - 1, 0);
    emit dataChanged(top_left,bottom_right);
  }

  assert(this->rowCount() == boost::numeric_cast<int>(this->m_data.size()));
  assert(this->rowCount() == boost::numeric_cast<int>(m_header_vertical_text.size()));
  assert(this->columnCount() == (this->rowCount() == 0 ? 0 : 1));
}
Exemple #2
0
boost::python::tuple
stft_glue(const boost::numeric::ublas::vector<double> &x,
        const int length,
        const int shift)
{
    const int n_frame = ((int)x.size() - length) / shift + 1;
    const int n_freq = length / 2 + 1;
//    std::cerr << n_frame << " " << n_freq << " " << length << " " << shift << std::endl;

    std::vector<double> y(x.size() + length * 10);
    std::cerr << x.size() << " " << y.size() << std::endl;
    for(size_t i = 0; i != y.size(); ++i)
        y[i] = i < x.size() ? x[i] : 0.0;
//    std::cerr << y.size() << std::endl;
    STFT stft(length, shift, n_frame);
    //allocation
    twoDimArray<double> abs_spec(n_frame, n_freq);
    twoDimArray<std::complex<double> > phase_spec(n_frame, n_freq);

    // exec stft
    stft.exec(&(y[0]), &abs_spec, &phase_spec);

    boost::numeric::ublas::matrix<double> abs_spec_boost(n_frame, n_freq);
    boost::numeric::ublas::matrix<double> phase_spec_boost(n_frame, n_freq);

    for(int i = 0; i != n_frame; ++i){
        for(int j = 0; j != n_freq; ++j){
            abs_spec_boost(i,j) = abs_spec[i][j];
            phase_spec_boost(i,j) = std::arg(phase_spec[i][j]);
        }
    }

    auto ret = boost::python::make_tuple(abs_spec_boost, phase_spec_boost);
    return ret;
}
  int operator()(V& v) const {
     using namespace boost::numeric::bindings::blas ;

     typedef typename V::value_type                                                        value_type ;
     typedef typename bindings::remove_imaginary<value_type>::type real_type ;

     // Copy vector from reference
     for (typename V::size_type i=0; i<v_ref_.size(); ++i)
        v[i] = v_ref_(i);

     // Test blas routines and compare with reference
     real_type nrm = nrm2( v );
     if ( std::abs(nrm - norm_2(v_ref_)) > std::numeric_limits< real_type >::epsilon() * norm_2(v_ref_)) {
       std::cout << "nrm2 : " << std::abs(nrm - norm_2(v_ref_)) << " > " << std::numeric_limits< real_type >::epsilon() * norm_2(v_ref_) << std::endl ;
       return 255 ;
     }

     nrm = asum( v );
     if ( std::abs(nrm - abs_sum(v_ref_)) > std::numeric_limits< real_type >::epsilon() * abs_sum(v_ref_)) {
       std::cout << "asum : " << std::abs(nrm - abs_sum(v_ref_)) << " > " << std::numeric_limits< real_type >::epsilon() * abs_sum(v_ref_) << std::endl ;
       return 255 ;
     }

     scal( value_type(2.0), v );
     for (typename V::size_type i=0; i<v_ref_.size(); ++i)
        if (std::abs( v[i] - real_type(2.0)*v_ref_(i) ) > real_type(2.0)*std::abs(v_ref_(i))) return 255 ;

     return 0;
  }
Exemple #4
0
      void pprint(const boost::numeric::ublas::vector<double>& v) {
	cout << "[";
	for( uint i=0; i < v.size(); i++) {
	  cout << v(i);
	  if(i != v.size()-1)
	    cout << " , ";
	}
	cout << "]\n";
      }
Exemple #5
0
double compute_distance(const ublas::vector<double>& pt1, const ublas::vector<double>& pt2)
{
	assert(pt1.size()==pt2.size());
	double sum = 0;
	for(UINT i = 0; i<pt1.size(); i++)
	{
		sum += pow((double)(pt1(i) - pt2(i)), 2.0);
	}
	return sqrt(sum);
}
double norm(const boost::numeric::ublas::vector<double>& v1, const boost::numeric::ublas::vector<double>& v2) {
    assert (v1.size() == v2.size());
    double sum = 0;
    for (size_t i=0; i<v1.size(); ++i)
    {
        double minus = v1(i) - v2(i);
        double r = minus * minus;
        sum += r;
    }

    return sqrt(sum);
}
T hotelling_t2_1test(
  boost::numeric::ublas::vector<T> mean1,
  boost::numeric::ublas::vector<T> mean2,
  boost::numeric::ublas::matrix<T> cov1,
  unsigned n1
){
  unsigned k=mean1.size();  

  boost::numeric::ublas::vector<T > mean_diff=mean1-mean2;
  boost::numeric::ublas::matrix<T>  cov_inv(cov1);
  invert_matrix(cov1  ,cov_inv);
  T t2_score=
  boost::numeric::ublas::inner_prod(
			      boost::numeric::ublas::prod( cov_inv,mean_diff)
			      ,mean_diff)*n1;
  T f_score= (t2_score*(n1 - k))/(k*(n1-1));

  std::cout << t2_score   << std::endl;
  std::cout << f_score   << std::endl;

  boost::math::fisher_f dist(k, n1-k);
  T p_score =boost::math::cdf(dist, f_score);

  std::cout << p_score << std::endl;
  T p_comp =boost::math::cdf(complement(dist, f_score));
  std::cout << p_comp << std::endl;
  //return p_comp;
  return p_score;
}
void clear(boost::numeric::ublas::vector<T> &x) {
    const ptrdiff_t n = x.size();

#pragma omp parallel for schedule(dynamic, 1024)
    for(ptrdiff_t i = 0; i < n; ++i)
        x[i] = 0;
}
Exemple #9
0
void generateCircularSpreading(boost::numeric::ublas::vector<double> &spr,
  const std::vector<double> &pars) {
  spr(0) = initiateCircularSpreading(pars);
  for (unsigned int i=1; i<spr.size(); ++i) {
    spr(i) = circularMap(spr(i-1), pars);
  }
}
T hotelling_t2_2test(
  boost::numeric::ublas::vector<T> mean1,
  boost::numeric::ublas::vector<T> mean2,
  boost::numeric::ublas::matrix<T> cov1,
  boost::numeric::ublas::matrix<T> cov2, 
  unsigned n1,
  unsigned n2){
  unsigned k=mean1.size();  
//int n = n1 + n2 -1;
  boost::numeric::ublas::vector<T > mean_diff=mean1-mean2;
  boost::numeric::ublas::matrix<T>  
  pooled_cov=(cov1*(n1-1)+cov2*(n2-1))*1.0/(n1+n2-2) ;
  pooled_cov *= (1.0/n1+1.0/n2 );
  boost::numeric::ublas::matrix<T>  pooled_cov_inv(pooled_cov);
  invert_matrix(   pooled_cov  ,pooled_cov_inv);
//std::cout << mean_diff << std::endl;  
  T t2_score=
  boost::numeric::ublas::inner_prod(
			      boost::numeric::ublas::prod( pooled_cov_inv,mean_diff)
			      ,mean_diff);
  T f_score= (t2_score*(n1 + n2 - 1- k))/(k*(  n1 + n2 - 2));

  std::cout << t2_score   << std::endl;
  std::cout << f_score   << std::endl;

  boost::math::fisher_f dist(k, n1+n2-1-k);
  T p_score =boost::math::cdf(dist, f_score);

  std::cout << p_score << std::endl;
  T p_comp =boost::math::cdf(complement(dist, f_score));
  std::cout << p_comp << std::endl;
  //return p_comp;
  return p_score;
}
void vector_print(ublas::vector<ScalarType>& v )
{

    std::cout << "vector_print!\n";
    for (unsigned int i = 0; i < v.size(); i++)
      std::cout << v(i) << "\t";
    std::cout << "\n";
}
Exemple #12
0
void generateValleySpreading(boost::numeric::ublas::vector<double> &spr,
  const std::vector<double> &pars) {
  spr(0) =
    boost::random::uniform_real_distribution<>(pars[0], pars[2])(rng);
  for (unsigned int i=1; i<spr.size(); ++i) {
    spr(i) = valleyMap(spr(i-1), pars);
  }
}
Exemple #13
0
	boost::python::object
	ublas_to_numpy(
		const boost::numeric::ublas::vector< T > & m )
	{
		//create a numpy array to put it in
		boost::python::object result(
			array_type(
				boost::python::make_tuple( m.size() ),
				dtype ) );

		//copy the elements
		for( unsigned i = 0; m.size() != i; ++i )
		{
            result[i] = m(i);
		}

		return result;
	}
bool
UZRectMollerup::converges (const ublas::vector<double>& previous,
			   const ublas::vector<double>& current) const
{
  size_t size = previous.size ();
  daisy_assert (current.size () == size);

  for (unsigned int i = 0; i < size; i++)
    {
      if (   fabs (current[i] - previous[i]) > max_absolute_difference
	  && (   iszero (previous[i])
              || iszero (current[i])
	      || (  fabs ((current[i] - previous[i]) / previous[i])
		  > max_relative_difference)))
	return false;
    }
  return true;
}
Exemple #15
0
T norm(const boost::numeric::ublas::vector<T> &x) {
    const ptrdiff_t n = x.size();
    T sum = 0;

#pragma omp parallel for schedule(dynamic, 1024) reduction(+:sum)
    for(ptrdiff_t i = 0; i < n; ++i)
        sum += x[i] * x[i];

    return sqrt(sum);
}
Exemple #16
0
void NCARPoisson1_CL::write_to_file(boost::numeric::ublas::vector<T> vec, std::string filename)
{
    std::ofstream fout;
    fout.open(filename.c_str());

    for (int i = 0; i < vec.size(); i++) {
        fout << std::scientific << vec[i] << std::endl;
    }
    fout.close();
}
Exemple #17
0
bool saveVectorToBinaryFile(const std::string & filename, const boost::numeric::ublas::vector<TYPE> & vec)
{
	std::ofstream file(filename.c_str(), std::ios_base::binary);
	if (!file) return false;

	unsigned int size = vec.size();
	file.write((char*)&size, sizeof(unsigned int));
	file.write((char*)&vec[0], sizeof(TYPE)*size);

	return true;
}
void ribi::kalman::StandardWhiteNoiseSystem::GoToNextState(const boost::numeric::ublas::vector<double>& input)
{
  //First do a perfect transition
  assert(input.size() == GetCurrentState().size());
  assert(m_parameters->GetStateTransition().size1() == GetCurrentState().size());
  assert(m_parameters->GetStateTransition().size2() == GetCurrentState().size());
  assert(m_parameters->GetControl().size1() == input.size());
  assert(m_parameters->GetControl().size2() == input.size());

  boost::numeric::ublas::vector<double> new_state
    = Matrix::Prod(m_parameters->GetStateTransition(),GetCurrentState())
    + Matrix::Prod(m_parameters->GetControl(),input);
  //Add process noise
  const auto sz = new_state.size();
  assert(new_state.size() == m_parameters->GetProcessNoise().size());
  for (std::size_t i = 0; i!=sz; ++i)
  {
    new_state(i) = GetRandomNormal(new_state(i),m_parameters->GetProcessNoise()(i));
  }
  SetNewCurrentState(new_state);
}
Exemple #19
0
T1 inner_prod(const boost::numeric::ublas::vector<T1> &x,
              const boost::numeric::ublas::vector<T2> &y
        )
{
    const ptrdiff_t n = x.size();
    T1 sum = 0;

#pragma omp parallel for schedule(dynamic, 1024) reduction(+:sum)
    for(ptrdiff_t i = 0; i < n; ++i)
        sum += x[i] * y[i];

    return sum;
}
Exemple #20
0
int spatialHash(
		const ublas::vector<int>& pt,
		const ublas::vector<int>& res){
	int n = res.size();
	int hashval = 0;
	for(int i=n-1;i>=0;i--)
	{
		int prevprod=1;
		for(int j=i-1;j>=0;j--)
			prevprod *= res(j);
		hashval += pt(i) * prevprod;
	}
	return hashval;
}
Exemple #21
0
int spatialHash(
		const ublas::vector<double>& minv, 
		const ublas::vector<double>& maxv,
		const ublas::vector<int> &   res,
		const ublas::vector<double> &pt){
	
	int n = pt.size();
	ublas::vector<int> npt(n);
	for(int i=0;i<n;i++){
		npt(i) = (int)(res(i) * (pt(i)-minv(i))/(maxv(i)-minv(i)));
	}

	return spatialHash(npt, res);
}
//threahold-Pairs impurity
//inputs - num_c: number of classes
//		 - c_tmp: weights of each class
//		 - alpha: threashold
double impurityHP(int num_c, boost::numeric::ublas::vector<int>& c_tmp, double alpha){
	vector<double> c_th(c_tmp.size(), 0.0);
	int i,j;
	double imp=0.0,imp_ij=0.0;
	for (i=0;i<num_c;i++)
		c_th[i]= (c_tmp[i]< alpha)? 0 : c_tmp[i]-alpha;
	for (i=0;i<num_c-1;i++){
		for (j=i+1;j<num_c;j++){
			imp_ij=c_th[i]*c_th[j]-alpha*alpha;
			if (imp_ij>0)
				imp+=imp_ij;
		}
	}
	return imp;
}
void ribi::kalman::SteadyStateKalmanFilter::SupplyMeasurementAndInput(
  const boost::numeric::ublas::vector<double>& measurements,
  const boost::numeric::ublas::vector<double>& input)
{
  
  assert(measurements.size() == input.size());

  //Store the calculation
  m_last_calculation->Clear();
  assert(!m_last_calculation->IsComplete());
  m_last_calculation->SetPreviousStateEstimate(this->GetStateEstimate());

  //m_last_calculation->SetMeasurement must be set before calling PredictState!
  m_last_calculation->SetMeasurement(measurements);

  // 1/1) State prediction
  const boost::numeric::ublas::vector<double> state_prediction = PredictState(input);
  m_state_estimate = state_prediction;

  //Store the calculation
  m_last_calculation->SetPredictedState(state_prediction);
  m_last_calculation->SetUpdatedState(GetStateEstimate());
  assert(m_last_calculation->IsComplete());
}
Exemple #24
0
void generatePbcs(boost::numeric::ublas::vector<double> &spr,
  const std::vector<double> &pars) {
  double r;
  int a;

  rnd::uniform_01<> uniformDist;
  rnd::bernoulli_distribution<> bernoulliDist;

  for (unsigned int i=0; i<spr.size(); ++i) {
    if (isEven(static_cast<int>(i))) {
      r = uniformDist(rng);
      spr(i) = inverseCdfPbcs(r, pars[0]);
    }
    else {
      r = bernoulliDist(rng);
      a = r==0 ? -1 : 1;
      spr(i) = pars[0]+a*sqrt(1-pow(spr(i-1)-pars[0], 2));
    }
  }
}
Exemple #25
0
bool read_vector(FILE* file, boost::numeric::ublas::vector<T>& v) {
  unsigned magic, rowCount, columnCount;
  if (fread(&magic, sizeof(unsigned), 1, file) != 1)
    return false;
  if (!check_magic<T>(magic))
    return false;

  if (fread(&rowCount, sizeof(unsigned), 1, file) != 1)
    return false;
  if (fread(&columnCount, sizeof(unsigned), 1, file) != 1)
    return false;

  const unsigned count = rowCount * columnCount;
  if (count != v.size())
    v.resize(count, false);
  T* buffer = new T[count];
  if (fread(buffer, sizeof(T), count, file) != count)
    return false;

  std::copy(buffer, buffer + count, v.begin());

  return true;
}
void FixedLagSmootherKalmanFilter::SupplyMeasurementAndInput(
  const boost::numeric::ublas::vector<double>& x,
  const boost::numeric::ublas::vector<double>& input)
{
  
  using boost::numeric::ublas::vector;
  using boost::numeric::ublas::vector_range;
  using boost::numeric::ublas::range;
  using boost::numeric::ublas::matrix;
  using boost::numeric::ublas::trans;

  //Store calculation for KalmanFilterExperiment
  m_last_fixed_lag_smoother_calculation->Clear();
  m_last_fixed_lag_smoother_calculation->SetPreviousStateEstimate(this->GetStandardKalmanFilter()->GetStateEstimate()); //1
  //m_last_fixed_lag_smoother_calculation->SetPreviousCovarianceEstimate(this->GetEstimationErrorCovariance()); //2

  const int state_size = m_standard_filter->GetStateSize();
  assert(state_size == boost::numeric_cast<int>(x.size()));
  assert(state_size == boost::numeric_cast<int>(input.size()));
  m_standard_filter->SupplyMeasurementAndInput(x,input);
  if (m_parameters->GetLag() == 0) return;
  const matrix<double> term_a = CreateTermA(m_parameters->GetLag(),state_size);
  const int lag = m_parameters->GetLag();
  assert(lag * state_size == boost::numeric_cast<int>(term_a.size1()));
  assert(  1 * state_size == boost::numeric_cast<int>(term_a.size2()));

  //Get the naive (that is, based on no time lag) prediction
  const vector<double> x_naive = m_standard_filter->PredictState(input);
  const matrix<double> term_b = CreateTermB(lag,state_size);

  //Find P(i) (yes, i can be zero and goes to lag)
  //ps has length lag
  //P(i) has size state_size x state_size
  //P(i) = P . [ [F-KH]^T ]^i (where ^T denotes a transposition, where ^i denotes an exponent to the power of i
  TRACE(m_standard_filter->GetLastStandardCalculation()->GetPreviousCovarianceEstimate());
  TRACE(m_standard_filter->GetLastStandardCalculation()->GetKalmanGain());
  vector<matrix<double> > ps_complex(lag);
  for (int i=0; i!=lag; ++i)
  {
    //
    assert(i < boost::numeric_cast<int>(ps_complex.size()));
    ps_complex[i]
      = Matrix::Prod(
          m_standard_filter->GetEstimationErrorCovariance(),
          Matrix::Power(
            trans(
              m_standard_filter->GetParameters()->GetStateTransition()
                - Matrix::Prod(
                  m_standard_filter->GetLastStandardCalculation()->GetKalmanGain(),
                  m_standard_filter->GetParameters()->GetObservation()
                )
              )
            ,
            i)
        );
  }

  //Find K(i) (yes, i can be zero and goes to lag)
  //ks has length lag
  //K(i) has size state_size x state_size
  //K(i) = P(i) . H^T . [H.P.H^T + R]^-1
  //
  vector<matrix<double> > ks_complex(lag);
  for (int i=0; i!=lag; ++i)
  {
    const boost::numeric::ublas::matrix<double> term
      = Matrix::MultiProd(
          m_standard_filter->GetParameters()->GetObservation(),
          m_standard_filter->GetEstimationErrorCovariance(),
          trans(m_standard_filter->GetParameters()->GetObservation())
        )
        + m_standard_filter->GetStandardParameters()->GetEstimatedMeasurementNoise();
    if (Matrix::CalcDeterminant(term) == 0.0)
    {
      throw std::runtime_error("Determinant of term in K(i) equals zero");
    }
    assert(i < boost::numeric_cast<int>(ks_complex.size()));
    assert(i < boost::numeric_cast<int>(ps_complex.size()));
    //
    ks_complex[i]
      = Matrix::MultiProd(
          ps_complex[i],
          trans(m_standard_filter->GetParameters()->GetObservation()),
          Matrix::Inverse(term)
        );
  }
  //
  matrix<double> ks = Matrix::SimplifyVectorOfMatrix(ks_complex);
  //
  const vector<double> innovation = m_standard_filter->GetLastStandardCalculation()->GetInnovation();
  //

  const vector<double> new_states_term_a
    = Matrix::Prod(term_a,x_naive);
  const vector<double> new_states_term_b
    = Matrix::Prod(term_b,vector_range<const vector<double> >(m_state_estimates,range(0,m_state_estimates.size()- m_standard_filter->GetStateSize())));
  //const vector<double> new_states_term_b
  //  = Matrix::Prod(term_b,vector_range<const vector<double> >(m_states,range(m_standard_filter->GetStateSize(),m_states.size())))
  const vector<double> new_states_term_c
    = Matrix::Prod(ks,innovation);

  m_state_estimates
    = new_states_term_a
    + new_states_term_b
    + new_states_term_c;

  TRACE("Store the calculation hiero");
  m_last_fixed_lag_smoother_calculation->SetStandardCalculationElement(this->m_standard_filter->GetLastStandardCalculation());
}
Exemple #27
0
 static void resize( const boost::numeric::ublas::vector< T_V , A_V > &v,
                     boost::numeric::ublas::permutation_matrix< T , A > &m )
 {
     m.resize( v.size() , v.size() );
 }
Exemple #28
0
 static bool same_size( const boost::numeric::ublas::permutation_matrix< T , A > &m ,
                        const boost::numeric::ublas::vector< T_V , A_V > &v )
 {
     return ( m.size() == v.size() ); // && ( m.size2() == v.size() ) );
 }
Exemple #29
0
 static void resize( boost::numeric::ublas::matrix< T , L , A > &m ,
                     const boost::numeric::ublas::vector< T_V , A_V > &v )
 {
     m.resize( v.size() , v.size() );
 }