template <> bool Raster<ushort>::write_pgmfile(const std::string& filename,Progress* target) const { ProgressScope progress(height(),"Writing PGM image:\n"+filename,target); std::ofstream out(filename.c_str(),std::ios::binary); out << "P5" << std::endl; out << width() << " " << height() << std::endl; const ushort m=maximum_scalar_pixel_value(); out << m << std::endl; for (ConstRowIterator row=row_begin();row!=row_end();++row) { progress.step(); for (const ushort* it=row->begin();it!=row->end();++it) { const uchar p[2]={uchar((*it)>>8),uchar(*it)}; if (m>=256) { // PGM spec is most significant byte first out.write(reinterpret_cast<const char*>(p),2); } else { assert(p[0]==0); out.write(reinterpret_cast<const char*>(p+1),1); } } }
void print ( std::ostream& o = std::cout ) { const int space = 16; string buffer(space,' '); o << buffer; std::vector< entry_type > entries; for ( typename Types::iterator ii = types.begin(); ii!=types.end(); ++ii ) { o << setw(space) << *ii; entries.push_back( *ii ); } o << '\n'; for ( row_iterator ii=row_begin(); ii!=row_end(); ++ii ) { o << setw(space) << ii->first; unsigned ctr = 0; for ( column_iterator jj=ii->second.begin(); jj!=ii->second.end(); ++jj ) { AGAIN: if ( entries[ctr++] == jj->first ) { o << setw(space) << jj->second; } else { if ( ctr >= entries.size() ) break; else { o << setw(space) << "X"; goto AGAIN; } } } while( ctr++ < entries.size() ) { o << setw(space) << "X"; } o << '\n'; } }
template <typename T> const typename Raster<T>::ScalarType Raster<T>::maximum_scalar_pixel_value() const { ScalarType m(scalar(*_data)); for (ConstRowIterator row=row_begin();row!=row_end();++row) for (const T* it=row->begin();it!=row->end();++it) { const ScalarType v(scalar(*it)); if (v>m) m=v; } return m; }
template <typename T> void Raster<T>::fill(const T& v) { if (contiguous()) { std::fill(contiguous_begin(),contiguous_end(),v); } else { for (RowIterator row=row_begin();row!=row_end();++row) std::fill((*row).begin(),(*row).end(),v); } }
Matrix<rows, cols, T> Matrix<rows, cols, T>::operator*(const Matrix<rows, cols, T> &rhs) const { Matrix<rows, cols, T> to_return; for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { to_return(i, j) = std::inner_product(row_begin(i), row_end(i), rhs.col_begin(j), T(0)); } } return(to_return); }
template <> bool Raster<uchar>::write_pgmfile(const std::string& filename,Progress* target) const { ProgressScope progress(height(),"Writing PGM image:\n"+filename,target); std::ofstream out(filename.c_str(),std::ios::binary); out << "P5" << std::endl; out << width() << " " << height() << std::endl; out << "255" << std::endl; for (ConstRowIterator row=row_begin();row!=row_end();++row) { progress.step(); out.write(reinterpret_cast<const char*>(&(*(row->begin()))),row->size()); } out.close(); return out; }
Matrix<rows, rhs_cols, T> Matrix<rows, cols, T>::operator*(const Matrix<rhs_rows, rhs_cols, T> &rhs) const { if (cols != rhs_rows) throw(std::domain_error("Matrix sizes do not match up for multiplication.")); Matrix<rows, rhs_cols, T> to_return; for (int i = 0; i < rows; ++i) { for (int j = 0; j < rhs_cols; ++j) { to_return(i, j) = std::inner_product(row_begin(i), row_end(i), rhs.col_begin(j), T(0)); } } return(to_return); }
bool exists( entry_type e1 , entry_type e2 ) { row_iterator ri = m.find( e1 ); if ( ri == row_end() ) { return false; } return ri->second.find( e2 ) != ri->second.end(); }
const complex_matrix_type make_ug( const matrix_type& G, const matrix_type& A, const matrix_type& D ) const { assert( G.col() == 3 ); assert( A.col() == 3 ); assert( D.col() == 1 ); assert( A.row() == D.row() ); auto const M = make_matrix(); auto const S = G * ( M.inverse() ); matrix_type s( 1, S.row() ); for ( size_type i = 0; i < S.row(); ++ i ) { s[0][i] = value_type( 0.5 ) * std::sqrt( std::inner_product( S.row_begin( i ), S.row_end( i ), S.row_begin( i ), value_type( 0 ) ) ); } auto const piomega = 3.141592553590 * feng::inner_product( array_type( M[0][0], M[1][0], M[2][0] ), feng::cross_product( array_type( M[0][1], M[1][1], M[2][1] ), array_type( M[0][2], M[1][2], M[2][2] ) ) ); auto const atomcellfacte = make_gaussian_electron( s, v0 ); const complex_matrix_type dwss = D * feng::pow( s, value_type( 2 ) ); const complex_matrix_type piag = A * G.transpose(); auto fact = feng::exp( - dwss - piag * complex_type( 0, 6.2831853071796 ) ); std::transform( fact.begin(), fact.end(), atomcellfacte.begin(), fact.begin(), [piomega]( const complex_type f, const value_type a ) { return f * a / piomega; } ); complex_matrix_type Ug( fact.col(), 1 ); for ( size_type i = 0; i < fact.col(); ++i ) { Ug[i][0] = std::accumulate( fact.col_begin( i ), fact.col_end( i ), complex_type() ); //if ( std::abs(Ug[i][0].real()) < 1.0e-8 ) Ug[i][0].real(0); //if ( std::abs(Ug[i][0].imag()) < 1.0e-8 ) Ug[i][0].imag(0); } return Ug; }