Exemple #1
0
inline void for_each(boost::numeric::ublas::matrix<T> & mat, Fun f){
    for(auto it1 = mat.begin1(); it1 < mat.end1(); ++it1){
        for(auto it = it1.begin(); it < it1.end(); ++it){
            f(*it);
        }
    }
}
void fill_boost_matrix_from_munkres_matrix (boost::numeric::ublas::matrix <T> & boost_matrix, const Matrix <T> & matrix)
{
    const int dimention = std::min (boost_matrix.size1 (), boost_matrix.size2 () );
    for (int i = 0; i < dimention; ++i) {
        for (int j = 0; j < dimention; ++j) {
            boost_matrix (i, j) = matrix (i, j);
        }
    }
};
Exemple #3
0
    double det_chol(const ublas::matrix<double,F,A> &m)
	// Compute determinant of Cholesky matrix. 
    {
        assert(m.size1() == m.size2());
        double d = 1.;
        for (size_t i=0; i < m.size1(); ++i)
            d *= m(i,i);
        return d;
    }
double summOffDiagonal2(boost::numeric::ublas::matrix<double> &S) {
	double sum = 0;
	for (int i = 0; i < S.size1(); i++) {
		for (int j = i + 1; j < S.size2(); j++)
		{
			sum += abs(S(i, j));
		}
	}
	return sum;
}
Exemple #5
0
double diameter(const ublas::matrix<double>& D)
{
  double total = 0;
  for(int i=0;i<D.size1();i++)
    for(int j=0;j<i;j++)
      total += D(i,j);
  
  int N = D.size1() * (D.size1() - 1) /2;

  return total/N;
}
Exemple #6
0
void printMatrix(boost::numeric::ublas::matrix<int> matrix) {
    for (unsigned int i=0; i < matrix.size1(); i++) {
        for (unsigned int j=0; j < matrix.size2(); j++) {
            cout << matrix(i, j);
            if(j+1 != matrix.size2()) {
                cout << "\t";
            }    
        }
        cout << endl;
    }
}
Matrix <T> convert_boost_matrix_to_munkres_matrix (const boost::numeric::ublas::matrix <T> & boost_matrix)
{
    const int dimention = std::min (boost_matrix.size1 (), boost_matrix.size2 () );
    Matrix <T> matrix (dimention, dimention);
    for (int i = 0; i < dimention; ++i) {
        for (int j = 0; j < dimention; ++j) {
            matrix (i, j) = boost_matrix (i, j);
        }
    }

    return matrix;
};
Exemple #8
0
double determinant( bnu::matrix<double>& m ) {
    bnu::permutation_matrix<std ::size_t> pm(m.size1());
    double det = 1.0;
    if( bnu::lu_factorize(m,pm) ) {
        det = 0.0;
    } else {
        for(int i = 0; i < m.size1(); i++)
            det *= m(i,i); // multiply by elements on diagonal
        det = det * determinant_sign( pm );
    }
    return det;
}
void setTrajectoryKeyPosition(double * trajectoryKeyPositions, int * size1, int * size2)
{
  if (TRAJECTORYKEYPOSITIONS.size1()*TRAJECTORYKEYPOSITIONS.size2() != 0)
  {
    std::cout << "You are reinitializing TrajectoryKeyPositions...\n" ;
    TRAJECTORYKEYPOSITIONS.resize(0, 0);
  }

  TRAJECTORYKEYPOSITIONS.resize(*size1, *size2);
  for (int i = 0; i < *size1 * (*size2); i++)
    TRAJECTORYKEYPOSITIONS(i % (*size1), i / (*size1)) = trajectoryKeyPositions[i];
}
Exemple #10
0
	bool is_symmetric(const ublas::matrix<double, F, A> &m)
	{
		if (m.size1() != m.size2())
			return false;

		for (size_t i = 0; i < m.size1(); ++i)
			for (size_t j = i+1; j < m.size2(); ++j)
				if (m(i,j) != m(j,i))
					return false;

		return true;
	}
matrix<double> BoostMatrixFacade::invert(const boost::numeric::ublas::matrix<double> matrix)
{
    boost::numeric::ublas::matrix<double> inverted(matrix.size1(), matrix.size2());
    cpu_invert(matrix, inverted);

    for( int i = 0; i < inverted.size1(); i++ )
        for( int j = 0; j < inverted.size2(); j++ )
            if( isnan(inverted(i, j)) )
                inverted(i, j) = 0;

    return inverted;
}
Exemple #12
0
cv::Mat Bridge::createImgMap(const boost::numeric::ublas::matrix<STATE> &groundMap) {
    cv::Mat ground(groundMap.size1(), groundMap.size2(), CV_8UC3, cv::Scalar(0, 0, 0));
    for (unsigned int i = 0; i < groundMap.size1(); ++i) {
        for (unsigned int j = 0; j < groundMap.size2(); ++j) {
            if (groundMap(i, j) == 0) {
                ground.at<cv::Vec3b>(i, j) = cv::Vec3b(0, 0, 0);
            } else {
                ground.at<cv::Vec3b>(i, j) = cv::Vec3b(255, 255, 255);
            }
        }
    }
    return ground;
}
const boost::numeric::ublas::matrix<double>
  FixedLagSmootherKalmanFilter::CreateTermA(
  const int lag,
  const int state_size)
{
  //
  assert(lag > 0 && "Term A is not needed for a lag of zero");
  const boost::numeric::ublas::matrix<double> v
    = Matrix::SimplifyVectorOfMatrix(CreateComplexTermA(lag,state_size));
  assert(lag * state_size == boost::numeric_cast<int>(v.size1()));
  assert(  1 * state_size == boost::numeric_cast<int>(v.size2()));
  return v;
}
Exemple #14
0
      void pprint(const boost::numeric::ublas::matrix<double>& m) {
	cout << "[";
	for( uint r=0; r < m.size1(); r++) {
	  for( uint c=0; c < m.size2(); c++) {
	    cout << m(r,c);
	    if(c == m.size2()-1 && r != m.size1()-1) 
	      cout << "\n ";
	    else if(c != m.size2()-1)
	      cout << " , ";
	  }
	}
	cout << "]\n";
      }
inline void serialize(json_output_handler& os, const boost::numeric::ublas::matrix<double>& A)
{
    os.begin_array();
    for (size_t i = 0; i < A.size1(); ++i)
    {
        os.begin_array();
        for (size_t j = 0; j < A.size2(); ++j)
        {
            os.value(A(i, j));
        }
        os.end_array();
    }
    os.end_array();
}
Exemple #16
0
void resize(ublas::matrix<int>& M1,int s1,int s2,int clear=0)
{
  ublas::matrix<int> M2(s1,s2);

  for(int i=0;i<M2.size1();i++)
    for(int j=0;j<M2.size2();j++)
      M2(i,j) = clear;

  for(int i=0;i<M1.size1() and i<M2.size1();i++)
    for(int j=0;j< M1.size2() and j<M2.size2();j++)
      M2(i,j) = M1(i,j);

  M1.swap(M2);
}
Exemple #17
0
	FixedMatrix( const boost::numeric::ublas::matrix<TYPE> &boost_matrix ) throw ( std::logic_error & ) {
		if( boost_matrix.size1() == ROWS && boost_matrix.size2() == COLS ) {
			for( size_t m = 0; m < ROWS; m++ ) {
				for( size_t n = 0; n < COLS; n++ ) {
					this->elem( n, m ) = boost_matrix( m, n );
				}
			}
		} else {
			LOG( Runtime, error ) << "The size of the boost matrix ("
								  << boost_matrix.size1() << ", " << boost_matrix.size2()
								  << ") does not coincide with the size of the isis matrix (" << ROWS << ", " << COLS << ").";
			throw( std::logic_error( "Size mismatch" ) );
		}
	};
int jacobiSync(
	boost::numeric::ublas::matrix<double> &S,
	boost::numeric::ublas::vector<double> &e,
	boost::numeric::ublas::matrix<double>  &U,
	int &iter,
	bool isOptimized)
{
	iter = 0;
	int col, row;
	bool iterating = true;
	int n = S.size1();
	if (S.size2() != n)
	{
		return -1;
	}
	boost::numeric::ublas::matrix<double> M(n, n);
	e = boost::numeric::ublas::zero_vector<double>(n);
	U = boost::numeric::ublas::identity_matrix<double>(n, n);
	while (iterating)
	{
		M = S;
		abs(M);
		iter++;
		for (int k = 0; k < n; k++)
		{
			M(k, k) = 0;
		}
		findMax(M, row, col);
		if (row == col)
		{
			for (int i = 0; i < n; i++) e(i) = S(i, i);
			return 0;
		}
		double Smax = S(row, col);
		if (isOptimized) {
			rotateColRowJacobi(S, row, col);
		}
		else {
			rotateRowCol(S, U, row, col);
		}
		//cout<<row<<" row|col "<<col <<" sum: "<< sumOffDiagonal(S) << endl;
		//if (Smax < _EPS * norm_frobenius(S)) iterating = false;
		if (sumOffDiagonal(S) < _EPS) iterating = false;
	}

	for (int i = 0; i < n; i++) e(i) = S(i, i);

	return 0;
}
    boost::array<WorkingType, ValDim>
    interpolate(const boost::array<WorkingType, PosDim> &position) const
    {
      boost::array<WorkingType, ValDim> result;
      // Init result
      for(int j(0); j < ValDim; ++j)
        result[j] = 0;

      unsigned int i(0);
      for(; i < Wa.size1() - (PosDim+1); ++i)
      {
        for(int j(0); j < ValDim; ++j)
          result[j] += Wa(i,j) * radialbasis<WorkingType, WorkingType, PosDim>(refPositions[i], position);
      }

      for(int j(0); j < ValDim; ++j)
        result[j] += Wa(i,j);
      ++i;

      for(int k(0); k < PosDim; ++k, ++i)
      {
        for(int j(0); j < ValDim; ++j)
          result[j] += Wa(i,j) * position[k];
      }
      return result;
    }
Exemple #20
0
 bool invert(const boost::numeric::ublas::matrix<T>& input, boost::numeric::ublas::matrix<T>& inverse) {

   // create a working copy of the input
   boost::numeric::ublas::matrix<T> A(input);

   // create a permutation matrix for the LU-factorization
   boost::numeric::ublas::permutation_matrix<std::size_t> pm(A.size1());

   // perform LU-factorization
   typename boost::numeric::ublas::matrix<T>::size_type res = boost::numeric::ublas::lu_factorize(A, pm);
   if( res != 0 ){
     LOG_FREE(Info, "boost.ublas", "boost::numeric::ublas::lu_factorize returned res = " << res <<
                    ", A = " << A << ", pm = " << pm << " for input = " << input);
     return false;
   }

   // create identity matrix of "inverse"
   inverse.assign(boost::numeric::ublas::identity_matrix<T>(A.size1()));

   // backsubstitute to get the inverse
   try {
     boost::numeric::ublas::lu_substitute(A, pm, inverse);
   }catch (std::exception& e){
     LOG_FREE(Info, "boost.ublas", "boost::numeric::ublas::lu_substitute threw exception '" << e.what() <<
                    "' for A = " << A << ", pm = " << pm);
     return false;
   }

   return true;
 }
Exemple #21
0
int SAFForwardingTable::determineRowOfFace(int face_id, boost::numeric::ublas::matrix<double> tab, std::vector<int> faces)
{
  // check if table fits to faces
  if(tab.size1 () != faces.size ())
  {
    fprintf(stderr, "Error the number of faces dont correspond to the table!\n");
    return FACE_NOT_FOUND;
  }

  if(std::find(faces.begin (), faces.end (), face_id) == faces.end ())
  {
    fprintf(stderr, "Face Not Found!!!\n");
    return FACE_NOT_FOUND;
  }

  //determine row of face
  int faceRow = FACE_NOT_FOUND;
  std::sort(faces.begin(), faces.end());//order

  int rowCounter = 0;
  for(std::vector<int>::iterator i = faces.begin (); i != faces.end() ; ++i)
  {
    //fprintf(stderr, "*i=%d ; face_id=%d\n",*i,face_id);
    if(*i == face_id)
    {
      faceRow = rowCounter;
      break;
    }
    rowCounter++;
  }
  return faceRow;
}
ScalarType matrix_compare(ublas::matrix<ScalarType>& res,
                            ublas::matrix<ScalarType>& ref)
{
    ScalarType diff = 0.0;
    ScalarType mx = 0.0;

    for(std::size_t i = 0; i < res.size1(); i++)
    {
        for(std::size_t j = 0; j < res.size2(); j++)
        {
            diff = std::max(diff, std::abs(res(i, j) - ref(i, j)));
            mx = std::max(mx, res(i, j));
        }
    }

    return diff / mx;
}
void parallel_jacob_musictest(boost::numeric::ublas::matrix<float> M, std::string isWriteToConsole, std::ofstream fp_outs[1], int i) {
	int iter;
	auto begin = std::chrono::high_resolution_clock::now();
	auto end = std::chrono::high_resolution_clock::now();

	double duration = 0;
	matrix* A = 0; 
	init_matrix(&A, M.size1());
	make_matrix(*A, M);
	std::vector<float> e;
	
	// BEGIN TEST
	begin = std::chrono::high_resolution_clock::now();
	find_eigenvalues_parallel_jacob_music(A, e, iter);
	end = std::chrono::high_resolution_clock::now();
	// END TEST
	std::sort(e.begin(), e.end());
	duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count() / 1000000.0;
	boost::numeric::ublas::vector<float> eigs(M.size1());
	for (size_t i = 0; i < e.size(); i++)
	{
		eigs(i) = e[i];
	}
	
	// INFO
	if (isWriteToConsole == "true") {
		std::string eig = "[";
		eig += std::to_string(M.size1());
		eig += "](";
		for (int i = 0; i < M.size1() - 1; i++)
		{
			eig += std::to_string(e[i]);
			eig += ",";
		}
		eig += std::to_string(e[M.size1() - 1]);
		eig += ")";

		writeToAllStreams((boost::format("#%1%: \n") % i).str(), fp_outs);

		writeToAllStreams((boost::format("Name: %1% \nEigenvalues: %2% \nElapsed(ms): %3% \nIter: %4%")
			% "parallel_jacob_music"% eig%duration%iter).str(), fp_outs);

		writeToAllStreams("============================", fp_outs);
	}
}
void setTrajectoryKeyPosition(double * trajectoryKeyPositions, int * size1, int * size2)
{
  if (TRAJECTORYKEYPOSITIONS.size() != 0)
  {
    std::cout << "You are reinitializing TrajectoryKeyPositions...\n" ;
    for (unsigned int i = 0; i < TRAJECTORYKEYPOSITIONS.size(); i++)
      TRAJECTORYKEYPOSITIONS[i].resize(0);
    TRAJECTORYKEYPOSITIONS.resize(0);
  }

  TRAJECTORYKEYPOSITIONS.resize(*size1);
  for (int i = 0; i < *size1; i++)
  {
    TRAJECTORYKEYPOSITIONS[i].resize(*size2);
    for (int j = 0; j < *size2; j++)
      TRAJECTORYKEYPOSITIONS[i][j] = trajectoryKeyPositions[i * (*size2) + j];
  }
}
const std::vector<std::vector<double> >
  ribi::FilterOperationerMainDialog::MatrixToVector(
  const boost::numeric::ublas::matrix<double>& m)
{
  const int height = m.size1();
  const int width = m.size2();
  std::vector<std::vector<double> > v(height,std::vector<double>(width));
  for (int y=0; y!=height; ++y)
  {
    assert(y < static_cast<int>(v.size()));
    for (int x=0; x!=width; ++x)
    {
      assert(x < static_cast<int>(v[y].size()));
      v[y][x] = m(y,x);
    }
  }
  return v;
}
Exemple #26
0
ublas::matrix<double> remove_duplicates(const ublas::matrix<double>& D)
{
  assert(D.size1() == D.size2());
  int N = D.size1();

  // find duplicates
  vector<int> remove;
  for(int i=0;i<N;i++) {
    bool found=false;
    for(int j=0;j<i and not found;j++)
      if (D(i,j) == 0.0)
	found=true;
    if (found)
      remove.push_back(i);
  }

  if (not remove.size()) return D;

  cerr<<"removing "<<remove.size()<<" duplicates."<<endl;

  // compute mapping of old to new indices
  vector<int> indices(N-remove.size());
  int k=0;
  int p=0;
  for(int i=0;i<indices.size();i++)
  {
    while (p<remove.size() and k==remove[p]) {
      k++;
      p++;
    }
    indices[i] = k++;
  }

  // construct the new matrix
  ublas::matrix<double> D2(indices.size(),indices.size());

  for(int i=0;i<D2.size1();i++)
    for(int j=0;j<D2.size2();j++)
      D2(i,j) = D(indices[i],indices[j]);

  return D2;
}
Exemple #27
0
        int cholesky_basic_checked(ublas::matrix<double,F,A> &m, const bool upper)
            // Perform Cholesky decomposition, but do not zero out other-triangular part.
            // Returns 0 if matrix was actual positive-definite, otherwise it returns a
            // LAPACK info value.
        {
            if (m.size1() != m.size2())
                throw LogicalError(ERROR_INFO("Matrix is not square"));
            assert(is_symmetric(m)); 

            // Call LAPACK routine
            int info;
            char uplo = detail::uplo_flag(m, upper);
            int size = static_cast<int>(m.size1());
            detail::dpotrf_( &uplo, &size, &m.data()[0], &size, &info );

            // Check validity of result
            if (info < 0) 
                throw LogicalError(ERROR_INFO("Invalid argument"), info);

            return info;
        }
void abs(boost::numeric::ublas::matrix<double> &M)
{
	int n = M.size1();

	for (int k = 0; k < n; k++)
	{
		for (int i = 0; i < n; i++)
		{
			M(k, i) = abs(M(k, i));
		}
	}
}
Exemple #29
0
	boost::python::object
	ublas_to_numpy(
		const boost::numeric::ublas::matrix< T > & m )
	{
		//create a numpy array to put it in
		boost::python::object result(
			array_type(
				boost::python::make_tuple( m.size1(), m.size2() ),
				dtype ) );

		//copy the elements
		for( unsigned i = 0; m.size1() != i; ++i )
		{
			for( unsigned j = 0; m.size2() != j; ++j )
			{
				result[ boost::python::make_tuple( i, j ) ] = m( i, j );
			}
		}

		return result;
	}