Exemple #1
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;
        }
Exemple #2
0
    void inv_chol_inplace(ublas::matrix<double,F,A> &m, const bool upper=true)
	// Compute inverse (in-place) of a pos. def. matrix that has previously been 
	// Cholesky decomposed (ie, "m" is already a Cholesky matrix). 
	// WARNINGS: 
	//	(1) The value of "upper" must match the actual form of "m". This function does not check.
	//	(2) This function does *not* return the inverse of a Cholesky matrix.
    {
        // Call LAPACK routine
        int info;
        char uplo = detail::uplo_flag(m, upper);
        int size = static_cast<int>(m.size1());
        detail::dpotri_( &uplo, &size, m.data().begin(), &size, &info );

        // Check validity of result
        if (info < 0) 
            throw LogicalError(ERROR_INFO("Invalid argument"), info);
        else if (info > 0) 
            throw NumericalError(ERROR_INFO("Inverse does not exist"), info);

        // Copy result to other triangular part (ie, make a symmetric inverse matrix)
        force_symmetry(m, upper);
    }
Exemple #3
0
bool read_matrix(FILE* file, boost::numeric::ublas::matrix<T, boost::numeric::ublas::column_major>& m) {
  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 (rowCount != m.size1() || columnCount != m.size2())
    m.resize(rowCount, columnCount, false);
  T* buffer = new T[count];
  if (fread(buffer, sizeof(T), count, file) != count)
    return false;

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

  return true;
}
 BOOST_UBLAS_INLINE
 typename ublas::matrix<T,F,A>::pointer 
 matrix_storage (ublas::matrix<T,F,A> &m) {
   return &m.data().begin()[0];
 }
Exemple #5
0
template <typename T> boost::const_multi_array_ref<T, 2> make_view(boost::numeric::ublas::matrix<T> const& m) {
    return  boost::const_multi_array_ref<T,2> (
            &*m.data().begin(),
            boost::extents[m.size1()][m.size2()]
        );
}