/** * @brief FFT shift * * @param m TO be shifted * @return Shifted */ template <class T> inline Matrix<T> fftshift (const Matrix<T>& m, const bool& fw = true) { assert (isvec(m) || is2d(m) || is3d(m)); Matrix<size_t> tmp = resize(size(m),ndims(m),1); for (size_t i = 0; i<ndims(m); i++) if (tmp[i] == 0) tmp[i] = 1; container<size_t> d = tmp.Container(); // data side lengths container<size_t> c = floor(tmp/2).Container(); // center coords Matrix<T> res (vsize(m)); size_t oi[3]; size_t si[3]; for (oi[0] = 0; oi[0] < d[0]; oi[0]++) { si[0] = (oi[0] + c[0]) % d[0]; for (oi[1] = 0; oi[1] < d[1]; oi[1]++) { si[1] = (oi[1] + c[1]) % d[1]; for (oi[2] = 0; oi[2] < d[2]; oi[2]++) { si[2] = (oi[2] + c[2]) % d[2]; if (fw) res(si[0],si[1],si[2]) = m(oi[0],oi[1],oi[2]); else res(oi[0],oi[1],oi[2]) = m(si[0],si[1],si[2]); } } } return res; }
mxArray* KDTree::to_matlab_matrix(){ /// Create an empty 1x1 struct const char* fieldnames[] = {"points", "nodes"}; mxArray* matstruct = mxCreateStructMatrix(1,1,2,fieldnames); /// Sticks datapoints into a mxArray { /// Create memory mxArray* points_mex = mxCreateDoubleMatrix(npoints, ndims(), mxREAL); double* points_data = mxGetPr(points_mex); /// Fill data for( int i=0; i<npoints; i++ ) for( int j=0; j<ndims(); j++ ) points_data[ i + j*npoints ] = points[i][j]; /// Add it to struct mxSetField(matstruct, 0, "points", points_mex); } /// Sticks tree nodes into an { /// Create memory mxArray* nodes_mex = mxCreateNumericMatrix(nodesPtrs.size(), 4, mxDOUBLE_CLASS, mxREAL); double* nodes_data = (double*) mxGetData(nodes_mex); /// Fill data for( int i=0,off=0; i<nodesPtrs.size(); i++,off+=4 ){ nodes_data[off+0] = (double) nodesPtrs[i]->LIdx; nodes_data[off+1] = (double) nodesPtrs[i]->RIdx; nodes_data[off+2] = (double) nodesPtrs[i]->pIdx; nodes_data[off+3] = (double) nodesPtrs[i]->key; } mxSetField(matstruct, 0, "nodes", nodes_mex); } return matstruct; }
/** * @brief Assign k-space trajectory * * @param k K-space trajectory */ void KSpace (const Matrix<RT>& k) { m_k = k; if (size(k,1) == KSpaceSize() && m_nmany == 1) { #pragma omp parallel num_threads (m_fts.size()) { m_fts[omp_get_thread_num()].KSpace(k); } } else if (size(m_k,2) == m_nmany) { #pragma omp parallel num_threads (m_fts.size()) { size_t i = omp_get_thread_num(); if (ndims(k)==3) m_fts[i].KSpace(k(CR(),CR(),CR(i))); else if (ndims(k) == 4) m_fts[i].KSpace(k(CR(),CR(),CR(),CR(i))); else throw NCSENSE_KSPACE_DIMENSIONS; } } else if (size(m_k,2)*size(m_k,3) == m_nmany) { #pragma omp parallel num_threads (m_fts.size()) { size_t i = omp_get_thread_num(), l=i%size(m_k,2), n = i/size(m_k,2); if (ndims(k)==4) m_fts[i].KSpace(k(CR(),CR(),CR(l),CR(n))); else if (ndims(k) == 5) m_fts[i].KSpace(k(CR(),CR(),CR(),CR(l),CR(n))); else throw NCSENSE_KSPACE_DIMENSIONS; } } else { throw NCSENSE_KSPACE_DIMENSIONS; } }
/** * @brief Sum of squares over a dimension * * Usage: * @code * Matrix<cxfl> m = rand<double> (8,7,6); * m = sos (M,1); // dims (8,6); * @endcode * * @param M Matrix * @param d Dimension * @return Sum of squares */ template <class T> inline static Matrix<typename TypeTraits<T>::RT> sos (const Matrix<T>& M, long d = -1) { typedef typename TypeTraits<T>::RT real_type; if (d == -1) d = ndims(M)-1; assert (d <= ndims(M)-1); const real_type* rt = (real_type*)&M[0]; Matrix<real_type> res(size(M)); for (size_t i = 0; i < numel(M); ++i) res[i] = rt[2*i]*rt[2*i]+rt[2*i+1]*rt[2*i+1]; return sum (res, d); }
cv::MatND MxArray::toMatND(int depth, bool transpose) const { // Create cv::Mat object. std::vector<int> d(dims(), dims()+ndims()); std::swap(d[0], d[1]); cv::MatND m(ndims(), &d[0], CV_MAKETYPE(DepthOf[classID()], 1), mxGetData(p_)); // Copy. cv::MatND mat; depth = (depth==CV_USRTYPE1) ? CV_MAKETYPE(DepthOf[classID()], 1) : depth; m.convertTo(mat, CV_MAKETYPE(depth, 1)); return (mat.dims==2 && transpose) ? cv::Mat(mat.t()) : mat; }
void ZArray::printInfo() { if (m_data == NULL) { std::cout << "Empty array: null data" << std::endl; } else { if (ndims() == 0) { std::cout << "Empty array: 0-dimentional" << std::endl; } else { std::cout << "Array(#): " << mylib::Array_Refcount(m_data) << std::endl; std::cout << " size: " << dim(0); for (int i = 1; i < ndims(); i++) { std::cout << " x " << dim(i); } std::cout << std::endl; switch (valueType()) { case mylib::UINT8_TYPE: std::cout << " uint8" << std::endl; break; case mylib::UINT16_TYPE: std::cout << " uint16" << std::endl; break; case mylib::UINT32_TYPE: std::cout << " uint32" << std::endl; break; case mylib::UINT64_TYPE: std::cout << " uint64" << std::endl; break; case mylib::INT8_TYPE: std::cout << " int8" << std::endl; break; case mylib::INT16_TYPE: std::cout << " int16" << std::endl; break; case mylib::INT32_TYPE: std::cout << " int32" << std::endl; break; case mylib::INT64_TYPE: std::cout << " int64" << std::endl; break; case mylib::FLOAT32_TYPE: std::cout << " single float" << std::endl; case mylib::FLOAT64_TYPE: std::cout << " double float" << std::endl; default: std::cout << " unknown type" << std::endl; } } } }
bool validposition(const position& pos) const { if (ndims() != pos.nd_) return false; for (int i=0; i != pos.nd_; ++i) { if (pos.position_[i] < 0 || pos.position_[i] >= this->dim(i)) return false; } return true; }
cv::Mat MxArray::toMat(int depth, bool transpose) const { // Create cv::Mat object. std::vector<int> d(dims(), dims()+ndims()); int ndims = (d.size()>2) ? d.size()-1 : d.size(); int nchannels = (d.size()>2) ? *(d.end()-1) : 1; depth = (depth==CV_USRTYPE1) ? DepthOf[classID()] : depth; std::swap(d[0], d[1]); cv::Mat mat(ndims, &d[0], CV_MAKETYPE(depth, nchannels)); // Copy each channel. std::vector<cv::Mat> channels(nchannels); std::vector<mwSize> si(d.size(), 0); // subscript index int type = CV_MAKETYPE(DepthOf[classID()], 1); // Source type for (int i = 0; i<nchannels; ++i) { si[d.size()-1] = i; void *pd = reinterpret_cast<void*>( reinterpret_cast<size_t>(mxGetData(p_))+ mxGetElementSize(p_)*subs(si)); cv::Mat m(ndims, &d[0], type, pd); // Read from mxArray through m m.convertTo(channels[i], CV_MAKETYPE(depth, 1)); } cv::merge(channels, mat); return (mat.dims==2 && transpose) ? cv::Mat(mat.t()) : mat; }
std::vector<int> cartesian_communicator::coordinates(int rk) const { std::vector<int> cbuf(ndims()); BOOST_MPI_CHECK_RESULT(MPI_Cart_coords, (MPI_Comm(*this), rk, cbuf.size(), c_data(cbuf) )); return cbuf; }
cv::Mat MxArray::toMat(int depth, bool transpose) const { // Create cv::Mat object (of the specified depth), equivalent to mxArray std::vector<int> d(dims(), dims()+ndims()); const mwSize ndims = (d.size()>2) ? d.size()-1 : d.size(); const mwSize nchannels = (d.size()>2) ? d.back() : 1; depth = (depth == CV_USRTYPE1) ? DepthOf[classID()] : depth; std::swap(d[0], d[1]); cv::Mat mat(ndims, &d[0], CV_MAKETYPE(depth, nchannels)); // Copy each channel from mxArray to Mat (converting to specified depth), // as in: channels[i] <- cast_to_mat_depth(p_(:,:,i)) std::vector<cv::Mat> channels(nchannels); std::vector<mwSize> si(d.size(), 0); // subscript index const int type = CV_MAKETYPE(DepthOf[classID()], 1); // Source type for (mwIndex i = 0; i<nchannels; ++i) { si[si.size() - 1] = i; // last dim is a channel idx void *pd = reinterpret_cast<void*>( reinterpret_cast<size_t>(mxGetData(p_)) + mxGetElementSize(p_)*subs(si)); // ptr to i-th channel data const cv::Mat m(ndims, &d[0], type, pd); // only creates Mat headers // Read from mxArray through m, writing into channels[i] m.convertTo(channels[i], CV_MAKETYPE(depth, 1)); } // Merge channels back into one cv::Mat array cv::merge(channels, mat); // transpose cv::Mat if needed if (mat.dims==2 && transpose) mat = mat.t(); return mat; }
cartesian_topology cartesian_communicator::topology() const { cartesian_topology topo(ndims()); std::vector<int> coords; topology(topo, coords); return topo; }
std::pair<int, int> cartesian_communicator::shifted_ranks(int dim, int disp) const { std::pair<int, int> r(-1,-1); assert(0 <= dim && dim < ndims()); BOOST_MPI_CHECK_RESULT(MPI_Cart_shift, (MPI_Comm(*this), dim, disp, &(r.first), &(r.second))); return r; }
result_type operator()(Expr& e) const { sz0 const& size0 = boost::proto::child_c<0>(e).extent(); sz1 const& size1 = boost::proto::child_c<1>(e).extent(); BOOST_ASSERT_MSG( ndims(size0) <= 2 && ndims(size1) <= 2 , "Inputs must be 2-D, or at least one input must be scalar" ); BOOST_ASSERT_MSG( boost::fusion::at_c<1>(size0) == boost::fusion::at_c<0>(size1) , "Inner dimensions must agree" ); return result_type( boost::fusion::at_c<0>(size0) , boost::fusion::at_c<1>(size1) ); }
int cartesian_communicator::rank(const std::vector<int>& coords ) const { int r = -1; assert(int(coords.size()) == ndims()); BOOST_MPI_CHECK_RESULT(MPI_Cart_rank, (MPI_Comm(*this), c_data(const_cast<std::vector<int>&>(coords)), &r)); return r; }
cv::MatND MxArray::toMatND(int depth, bool transpose) const { // Create cv::MatND object (of the specified depth), equivalent to mxArray std::vector<int> d(dims(), dims()+ndims()); std::swap(d[0], d[1]); depth = (depth == CV_USRTYPE1) ? DepthOf[classID()] : depth; cv::MatND mat(d.size(), &d[0], CV_MAKETYPE(depth, 1)); // Copy from mxArray to cv::MatND (converting to specified depth) const int type = CV_MAKETYPE(DepthOf[classID()], 1); // source type const cv::MatND m(d.size(), &d[0], type, mxGetData(p_)); // only Mat header // Read from mxArray through m, writing into mat m.convertTo(mat, CV_MAKETYPE(depth, 1)); // transpose cv::MatND if needed if (mat.dims==2 && transpose) mat = mat.t(); return mat; }
//#include "Print.hpp" template <class T> inline static Matrix<T> permute (const Matrix<T>& M, const Vector<size_t>& perm) { // Check that perm only includes one number between 0 and INVALID_DIM once size_t ndnew = perm.size(), i = 0; size_t ndold = ndims (M); // Must have same number of dimensions assert (ndnew == ndold); // Every number between 0 and ndnew must appear exactly once Vector<cbool> occupied; occupied.resize(ndnew); for (i = 0; i < ndnew; ++i) { assert (!occupied[perm[i]]); occupied [perm[i]] = true; } // Old and new sizes Vector<size_t> so = size (M); Vector<size_t> sn (16,1); for (i = 0; i < ndnew; ++i) sn[i] = so[perm[i]]; // Allocate new matrix with permuted dimensions Matrix<T> res(sn); // Relation of old to new indices Vector<size_t> d(16), od(16); for (i = 0; i < ndnew; ++i) od[i] = perm[i]; for ( ; i < 16; ++i) od[i] = i; // Copy data accordingly for (d[15] = 0; d[15] < size(M,15); d[15]++) for (d[14] = 0; d[14] < size(M,14); d[14]++) for (d[13] = 0; d[13] < size(M,13); d[13]++) for (d[12] = 0; d[12] < size(M,12); d[12]++) for (d[11] = 0; d[11] < size(M,11); d[11]++) for (d[10] = 0; d[10] < size(M,10); d[10]++) for (d[ 9] = 0; d[ 9] < size(M, 9); d[ 9]++) for (d[ 8] = 0; d[ 8] < size(M, 8); d[ 8]++) for (d[ 7] = 0; d[ 7] < size(M, 7); d[ 7]++) for (d[ 6] = 0; d[ 6] < size(M, 6); d[ 6]++) for (d[ 5] = 0; d[ 5] < size(M, 5); d[ 5]++) for (d[ 4] = 0; d[ 4] < size(M, 4); d[ 4]++) for (d[ 3] = 0; d[ 3] < size(M, 3); d[ 3]++) for (d[ 2] = 0; d[ 2] < size(M, 2); d[ 2]++) for (d[ 1] = 0; d[ 1] < size(M, 1); d[ 1]++) for (d[ 0] = 0; d[ 0] < size(M, 0); d[ 0]++) res (d[od[ 0]],d[od[ 1]],d[od[ 2]],d[od[ 3]], d[od[ 4]],d[od[ 5]],d[od[ 6]],d[od[ 7]], d[od[ 8]],d[od[ 9]],d[od[10]],d[od[11]], d[od[12]],d[od[13]],d[od[14]],d[od[15]]) = M (d[ 0],d[ 1],d[ 2],d[ 3],d[ 4],d[ 5], d[ 6],d[ 7],d[ 8],d[ 9],d[10],d[11], d[12],d[13],d[14],d[15]); // Remove trailing singelton dimensions sn.erase(sn.begin()+ndnew, sn.begin()+16); return resize(res, sn); }
template<class T> bool Write (const Matrix<T>& M, const std::string& uri) { T t; Group group, *tmp; std::string path; boost::tokenizer<> tok(uri); std::vector<std::string> sv (Split (uri, "/")); std::string name = sv[sv.size() - 1]; sv.pop_back(); // data name not part of path if (sv.size() == 0) path = "/"; else for (size_t i = 0; i < sv.size(); i++) { if (sv[i].compare("")) path += "/"; path += sv[i]; } if (this->m_verb) printf ("Creating dataset %s at path (%s)\n", name.c_str(), path.c_str()); try { group = m_file.openGroup(path); if (this->m_verb) printf ("Group %s opened for writing\n", path.c_str()) ; } catch (const Exception& e) { for (size_t i = 0, depth = 0; i < sv.size(); i++) { if (sv[i].compare("")) { try { group = (depth) ? (*tmp).openGroup(sv[i]) : m_file.openGroup(sv[i]); } catch (const Exception& e) { group = (depth) ? (*tmp).createGroup(sv[i]) : m_file.createGroup(sv[i]); } tmp = &group; depth++; } } } // One more field for complex numbers size_t tmpdim = ndims(M); std::vector<hsize_t> dims (tmpdim); for (size_t i = 0; i < tmpdim; i++) dims[i] = M.Dim(tmpdim-1-i); if (is_complex(t)) { dims.push_back(2); tmpdim++; } DataSpace space (tmpdim, &dims[0]); PredType* type = HDF5Traits<T>::PType(); DataSet set = group.createDataSet(name, (*type), space); set.write (M.Ptr(), (*type)); set.close (); space.close (); return true; }