inline void prepare_waypoints(O const &o, const X& x, W& wpts) { typedef typename X::value_type itype; BOOST_ASSERT_MSG(isempty(o.waypoints) || (numel(x) == 2), "Choose x or waypoints, not both"); if (isempty(o.waypoints)) { wpts = nt2::rowvect(x); } else if (numel(x) == 2) { // input_t a = x(begin_), b = x(end_); // BOOST_AUTO_TPL(w, nt2::cath(nt2::cath(x(begin_), nt2::rowvect(o.waypoints)), x(end_))); // BOOST_AUTO_TPL(d, nt2::is_nez(nt2::cath(nt2::One<input_t>(), nt2::diff(w)))); // wpts = wpts(d); itype a = x(begin_), b = x(end_); if(a != o.waypoints(begin_) && b!=o.waypoints(end_) ) wpts = nt2::cath(nt2::cath(a, nt2::rowvect(o.waypoints)), b); else if (a != o.waypoints(begin_)) wpts = nt2::cath(a, nt2::rowvect(o.waypoints)); else if (b != o.waypoints(end_)) wpts = nt2::cath(nt2::rowvect(o.waypoints), b); else wpts = nt2::rowvect(o.waypoints); } else { wpts = nt2::rowvect(o.waypoints); } }
/** * @brief Construct FFTW plans for forward and backward FT with credentials * * @param sl Matrix of side length of the FT range * @param mask K-Space mask (if left empty no mask is applied) * @param pc Phase correction (or target phase) * @param b0 Field distortion */ explicit DFT (const Matrix<size_t>& sl, const Matrix<T>& mask = Matrix<T>(1), const Matrix<CT>& pc = Matrix<CT>(1), const Matrix<T>& b0 = Matrix<T>(1)) : m_N(1), m_have_mask (false), m_have_pc (false) { size_t rank = numel(sl); container<int> n (rank); if (numel(mask) > 1) { m_have_mask = true; m_mask = mask; } if (numel(pc) > 1) { m_have_pc = true; m_pc = pc; m_cpc = conj(pc); } for (int i = 0; i < rank; i++) n[i] = (int) sl [rank-1-i]; m_N = std::accumulate(n.begin(), n.end(), 1, std::multiplies<int>()); Matrix<size_t> tmp = resize(sl,3,1); for (size_t i = 0; i < 3; ++i) tmp[i] = (tmp[i] > 0) ? tmp[i] : 1; d = tmp.Container(); // data side lengths c = (floor(tmp/2)).Container(); // center coords Allocate (rank, &n[0]); m_initialised = true; }
BOOST_FORCEINLINE result_type operator()(Expr& e) const { result_type sizee; sizee[0] = numel(boost::proto::child_c<0>(e)); sizee[1] = numel(boost::proto::child_c<1>(e)); return sizee; }
BOOST_FORCEINLINE void inverse(T1& ip) const { T1 inv(of_size(1, numel(ip))); inv(ip) = nt2::_(nt2_la_int(1), nt2_la_int(numel(ip))); assign_swap(ip, inv); }
void DataChannelMPI::allGather(std::vector<at::Tensor>& output, at::Tensor& input, THDGroup group_id) { const auto& group_pair = _groups.at(group_id); const auto& comm = group_pair.first; if (comm == MPI_COMM_NULL) return; if (output.size() != group_pair.second.size()) throw std::logic_error("allGather: number of output tensors and group size does not match"); for (auto out_tensor : output) assertSameSizeAndType(out_tensor, input, "allGather"); auto recv_buffer = _newLikeFlat(output); auto contig_input = input.contiguous(); MPI_Allgather( contig_input.data_ptr(), contig_input.numel(), mpi_datatype.at(contig_input.type().scalarType()), recv_buffer.data_ptr(), contig_input.numel(), mpi_datatype.at(recv_buffer.type().scalarType()), comm ); for (size_t i = 0; i < output.size(); ++i) output[i].copy_(recv_buffer[i]); }
/** * @brief MATLAB-like meshgrid. x and y vectors must be specified z may be specified optionally. * * @param x X-Vector * @param y Y-Vector * @param z Z-Vector (default: unused) * @return Mesh grid O (Ny x Nx x Nz x 3) (if z specified) else O (Ny x Nx x 2)<br/> */ template <class T> inline static Matrix<T> meshgrid (const Vector<T>& x, const Vector<T>& y, const Vector<T>& z = Vector<T>(1)) { size_t nx = numel(x); size_t ny = numel(y); size_t nz = numel(z); assert (nx > 1); assert (ny > 1); // Column vectors assert (size(x,0) == nx); assert (size(y,0) == ny); assert (size(z,0) == nz); Matrix<T> res (ny, nx, (nz > 1) ? nz : 2, (nz > 1) ? 3 : 1); for (size_t i = 0; i < ny * nz; i++) Row (res, i , x); for (size_t i = 0; i < nx * nz; i++) Column (res, i + nx * nz, y); if (nz > 1) for (size_t i = 0; i < nz; i++) Slice (res, i + 2 * nz, z[i]); return res; }
inline void prepare_waypoints(O const &o, const X& x, W& wpts) { typedef typename X::value_type itype; BOOST_ASSERT_MSG( isempty(o.waypoints) || (numel(x) == 2) , "Choose x or waypoints, not both" ); if (isempty(o.waypoints)) { wpts = nt2::rowvect(x); } else if (numel(x) == 2) { itype a = x(begin_), b = x(end_); if(a != o.waypoints(begin_) && b!=o.waypoints(end_) ) wpts = nt2::cath(nt2::cath(a, nt2::rowvect(o.waypoints)), b); else if (a != o.waypoints(begin_)) wpts = nt2::cath(a, nt2::rowvect(o.waypoints)); else if (b != o.waypoints(end_)) wpts = nt2::cath(nt2::rowvect(o.waypoints), b); else wpts = nt2::rowvect(o.waypoints); } else { wpts = nt2::rowvect(o.waypoints); } }
result_type operator()(A0& yi, A1& inputs) const { yi.resize(inputs.extent()); const child0 & x = boost::proto::child_c<0>(inputs); if (numel(x) <= 1) BOOST_ASSERT_MSG(numel(x) > 1, "Interpolation requires at least two sample points in each dimension."); else { BOOST_ASSERT_MSG(issorted(x, 'a'), "for 'nearest' interpolation x values must be sorted in ascending order"); const child1 & y = boost::proto::child_c<1>(inputs); BOOST_ASSERT_MSG(numel(x) == numel(y), "The grid vectors do not define a grid of points that match the given values."); const child2 & xi = boost::proto::child_c<2>(inputs); bool extrap = false; value_type extrapval = Nan<value_type>(); choices(inputs, extrap, extrapval, N1()); table<index_type> index = bsearch (x, xi); table<value_type> dx = xi-x(index); table<index_type> indexp1 = oneplus(index); yi = y(nt2::if_else(lt(nt2::abs(xi-x(index)), nt2::abs(xi-x(indexp1))), index, indexp1)); value_type b = value_type(x(begin_)); value_type e = value_type(x(end_)); if (!extrap) yi = nt2::if_else(nt2::logical_or(boost::simd::is_nge(xi, b), boost::simd::is_nle(xi, e)), extrapval, yi); } return yi; }
static void _check_inputs(std::vector<at::Tensor> &inputs, std::vector<at::Tensor> &outputs, int input_multiplier, int output_multiplier) { // len(inputs) == len(outputs) size_t len = inputs.size(); if (len <= 0) { throw std::runtime_error("input sequence can't be empty"); } if (len != outputs.size()) { std::stringstream err; err << "inputs and outputs sequences have to be of the same length, but got input of length " << len << " and output of length " << outputs.size(); throw std::runtime_error(err.str()); } std::unordered_set<int> devices; devices.reserve(len); int64_t numel = inputs[0].numel(); auto type = inputs[0].type().ID(); for (size_t i = 0; i < len; i++) { auto input = inputs[i]; auto output = outputs[i]; if (!(input.type().is_cuda() && !input.type().is_sparse() && output.type().is_cuda() && !output.type().is_sparse())) { throw std::runtime_error("input and output elements have to be cuda dense Tensors"); } if (type != input.type().ID() || type != output.type().ID()) { throw std::runtime_error("all inputs and outputs must be of the same Tensor type"); } if (!input.is_contiguous() || !output.is_contiguous()) { throw std::runtime_error("all inputs and outputs have to be contiguous"); } auto input_device = input.get_device(); // inputs must be on unique devices if (devices.find(input_device) != devices.end()) { throw std::runtime_error("inputs must be on unique devices"); } devices.insert(input_device); // inputs and outputs must be on same device respectively if (input_device != output.get_device()) { throw std::runtime_error("input and output must be on the same device"); } // all inputs must be same size if (input.numel() != numel) { throw std::runtime_error("all inputs must have the same number of elements"); } if (output.numel() * output_multiplier != numel * input_multiplier) { throw std::runtime_error("output must be of size input_size * size_multiplier"); } } }
BOOST_FORCEINLINE result_type eval(A0& a0, A1& a1, boost::mpl::false_ const&, Same const&) const { BOOST_ASSERT_MSG( numel(a0) == numel(a1) , "Swapping expression with incompatible sizes." ); nt2::tie(a1,a0) = nt2::tie(a0,a1); }
template<class T, class S> inline unsigned short issame (const Matrix<T>& A, const Matrix<S>& B) { if (numel(A) != numel(B)) return 0; for (auto i = 0; i < numel(A); ++i) if (A[i]!=B[i]) return 0; if (size(A)==size(B)) return 2; return 1; }
static void compute(A0& out,const C& c, const R& r) { size_t p = numel(r); size_t m = numel(c); BOOST_AUTO_TPL(idx, nt2::_(ptrdiff_t(p), ptrdiff_t(-1), ptrdiff_t(2))); BOOST_AUTO_TPL(ridx, nt2::colvect(r(idx))); BOOST_AUTO_TPL(x, catv(ridx, c)); //build vector of user data BOOST_AUTO_TPL(v1, nt2::fliplr(nt2::cif(m, p, meta::as_<ptrdiff_t>()))); BOOST_AUTO_TPL(v2, nt2::ric(m, p, meta::as_<ptrdiff_t>())); out(nt2::_) = x(v1+v2); }
template<class T> T rmse (const Matrix<T>& A, const Matrix<T>& B) { assert (numel(A) == numel(B)); Matrix<T> res = (A - B) ^ 2; res = resize (res,numel(res),1); res = sum(res,0); return (sqrt(res[0])); }
result_type operator()(Expr& e) const { const choice1_t & v1 = boost::proto::child_c<N-2>(e); const choice2_t & v2 = boost::proto::child_c<N-1>(e); typedef typename nt2::meta::is_integral<choice1_t>::type c_t; std::size_t dim1 = 2, dim2 = 1; getdims(boost::proto::child_c<1>(e), v1, v2, dim1, dim2, c_t()); result_type sizee = boost::proto::child_c<0>(e).extent(); sizee[dim1-1] = numel(boost::proto::child_c<1>(e)); sizee[dim2-1] = numel(boost::proto::child_c<2>(e)); return sizee; }
BOOST_FORCEINLINE result_type operator()(A0 const& a0) const { BOOST_ASSERT_MSG( numel(a0) <= 2, "randi: invalid range size"); std::size_t i = first_index<1>(a0); result_type imin = numel(a0) == 2 ? a0(i) : 1; result_type imax = numel(a0) == 2 ? a0(i+1) : a0(i); result_type that; current_prng_.generator_->randi(&that,0,1,imin,imax); return that; }
/** * @brief Complex dot product (A'*B) on data vector * * Usage: * @code{.cpp} * Matrix<cxdb> a = rand<cxdb> (20,1); * Matrix<cxdb> b = rand<cxdb> (20,1); * cxdb dotpr = dotc (a, b); * @endcode * * @param A Left factor (is conjugated) * @param B Right factor * @return A'*B */ template <class T> inline T dotc (const Matrix<T>& A, const Matrix<T>& B) { int n = (int) numel(A), one = 1; T res = (T)0.; assert (n == (int) numel(B)); LapackTraits<T>::dotc (n, A.Ptr(), one, B.Ptr(), one, &res); return res; }
Solution VDSpiral (SpiralParams& sp) { GradientParams gp; Matrix<double>& fov = sp.fov; Matrix<double>& rad = sp.rad; double k_max, fov_max, dr; Matrix<double> r, theta; long n = 0; assert (numel(rad) >= 2); assert (isvec(rad) == isvec(fov)); assert (numel(rad) == numel(fov)); k_max = 5.0 / sp.res; fov_max = m_max(fov); dr = sp.shots / (fov_max); n = size(fov,1)*100; r = linspace<double> (0.0, k_max, n); Matrix<double> x = k_max*rad; fov = interp1 (x, fov, r, INTERP::LINEAR); dr = sp.shots / (1500.0 * fov_max); n = ceil (k_max/dr); x = r; r = linspace<double> (0.0, k_max, n); fov = interp1 (x, fov, r, INTERP::AKIMA); theta = cumsum ((2 * PI * dr / sp.shots) * fov); gp.k = Matrix<double> (numel(r), 3); for (size_t i = 0; i < numel(r); i++) { gp.k(i,0) = r[i] * cos (theta[i]); gp.k(i,1) = r[i] * sin (theta[i]); } gp.mgr = sp.mgr; gp.msr = sp.msr; gp.dt = sp.dt; gp.gunits = sp.gunits; gp.lunits = sp.lunits; Solution s = ComputeGradient (gp); return s; }
/** * @brief Create new matrix with the new dimensions * and copy the data into the new matrix. If the target * is bigger, the remaining space is set 0. If it is * smaller data is truncted. * * @param M The matrix to resize * @param sz New dimension vector * @return Resized copy */ template <class T> inline static Matrix<T> resize (const Matrix<T>& M, const Vector<size_t>& sz) { Matrix<T> res (sz); size_t copysz = std::min(numel(M), numel(res)); typename Vector<T>:: iterator rb = res.Begin (); typename Vector<T>::const_iterator mb = M.Begin (); std::copy (mb, mb+copysz, rb); return res; }
FBNN::FBNN(const Arch_t & arch, std::string activeStr, int learningRate, double zeroMaskedFraction, bool testing, std::string outputStr) : m_oArch(arch) , m_iN(numel(arch)) , m_strActivationFunction(activeStr) , m_iLearningRate(learningRate) , m_fInputZeroMaskedFraction(zeroMaskedFraction) , m_fTesting(testing) , m_strOutput(outputStr) { for(int i = 1; i < m_iN; ++i) { FMatrix f = (rand(m_oArch[i], m_oArch[i-1] + 1) - 0.5) * (2 * 4 * sqrt(6.0/(m_oArch[i] + m_oArch[i-1])));//based on nnsetup.m m_oWs.push_back(std::make_shared<FMatrix>(f)); if(m_fMomentum > 0) { FMatrix z = zeros(f.rows(), f.columns()); m_oVWs.push_back(std::make_shared<FMatrix>(z)); } if(m_fNonSparsityPenalty > 0) { FMatrix p = zeros(1, m_oArch[i]); m_oPs.push_back(std::make_shared<FMatrix>(p)); } } }
/** * @brief Round down * * @param M Matrix * @return Rounded down matrix */ template<class T> inline static Matrix<T> floor (const Matrix<T>& M) { Matrix<T> res = M; for (size_t i = 0; i < numel(M); ++i) res[i] = floor ((float)res[i]); return res; }
void maxpool3d_cpu::bprop() { // create dX at input port check_dY_ind(); create_dX(); // dX at input port float* const dxx = (float*)dX.getDataBeg(); // dY and index at output port float* const dyy = (float*)dY.getDataBeg(); int* const ii = (int*)ind.getDataBeg(); // iterate over dY, set dX int num = static_cast<int>( numel(dY) ); #pragma omp parallel for for (int n = 0; n < num; ++n) { int ix = ii[n]; ix -= 1; // matlab 1-base -> C++ 0-base // accumulate! there can be overlapping ix! #pragma omp atomic dxx[ix] += dyy[n]; } }
template<class TAG, class F, class X> BOOST_FORCEINLINE typename details::integration<F, X, TAG>::result_type primitive(F f, X x) { typedef details::integration<F, X, TAG> i_t; typedef typename i_t::input_t input_t; typedef typename i_t::result_t result_t; typedef typename i_t::real_t real_t; typedef container::table<input_t> itab_t; typedef container::table<result_t> rtab_t; typedef typename details::h2_t<input_t>::ab_t ab_t; typedef typename i_t::result_type result_type; size_t nbres = nt2::numel(x); rtab_t res(of_size(1, nbres)); real_t err = Zero<real_t>(); size_t warn = 0; size_t fcnt = 0; if (numel(x) == 0) { result_type r = { nt2::zeros(nt2::of_size(1, 0)), err, 0, true, 0}; return r; } res(1) = nt2::Zero<result_t>(); for(size_t i=2; i <= nbres; i++) { BOOST_AUTO_TPL(r, (details::integration<F, ab_t, TAG>::call(f, nt2::cath(x(i-1), x(i))))); res(i) = res(i-1)+r.integrals(end_); err = nt2::max(err, r.errors); warn = nt2::max(warn, r.warning); fcnt += r.eval_count; } result_type r = { res, err, fcnt, warn == 0, warn}; return r; }
/** * @brief MATLAB-like round * * @param M Matrix * @return Rounded matrix */ template<class T> inline static Matrix<T> round (const Matrix<T>& M) { Matrix<T> res = M; for (size_t i = 0; i < numel(M); ++i) res[i] = ROUND (res[i]); return res; }
template <class T> inline static T mmin (const View<T, true>& V) { T mx = 1e-20; for (size_t i = 0; i < numel(V); ++i) if (V[i] <= mx) mx = V[i]; return mx; }
/* * @brief Create new vector * and copy the data into the new vector. If the target * is bigger, the remaining space is set 0. If it is * smaller data is truncted. * * @param M The matrix to resize * @param sz New length * @return Resized vector */ template <class T> inline static Matrix<T> resize (const Matrix<T>& M, const size_t& s0, const size_t& s1, const size_t& s2, const size_t& s3, const size_t& s4) { assert (numel(M)==s0*s1*s2*s3*s4); Matrix<T> res (s0,s1,s2,s3,s4); res.Container() = M.Container(); return res; }
/** * @brief Swap a ranges order */ void SwapRange (const Matrix<size_t>& sol, Matrix<size_t>& nsol) { size_t i, x, y, pos, nr = numel(sol); // copy the current solution nsol = sol; // pick two places do not touch start x = gsl_rng_uniform_int (m_rng, nr-1) + 1; y = gsl_rng_uniform_int (m_rng, nr-1) + 1; // Swap if (y < x) { pos = x; x = y; y = pos; } else if (x == y) return; // Create a temporary array that holds all the values between place1 and place2 size_t len = y - x + 1; container<size_t> cut (len); i = len; while (i--) nsol[x+len-1-i] = sol[x+i]; }
/** * @brief Calculate the trajectory length * * @param k K-space points * @param order Order of positions * @return Trajectory length */ double Lengthiness (const Matrix<double>& k, const Matrix<size_t>& order) { double length = 0, x[3], y[3]; size_t j, nr = numel(order), nx, ny; // calculate the total length of the traj for (j = 0; j < nr; j++) { nx = order[j]; ny = order[(j == nr-1) ? 0 : j + 1]; x[0] = k (nx,0); x[1] = k (nx,1); x[2] = k (nx,2); y[0] = k (ny,0); y[1] = k (ny,1); y[2] = k (ny,2); length += sqrt(pow(x[0]-y[0],2) + pow(x[1]-y[1],2) + pow(x[2]-y[2],2)); } return length; }
void compute( const FUNC& f, const X & x, const o_t & o) { real_t tmptol = tol_; init(o, x); BOOST_AUTO_TPL(dif, nt2::abs(nt2::diff(nt2::rowvect(wpts_)))); real_t tol1= tol_/nt2::globalasum1(dif); size_t l = numel(dif); res_.resize(extent(wpts_)); res_(1) = nt2::Zero<value_t>(); tol_ = tol1*dif(1); res_(2) = compute<true>(f, wpts_(1), wpts_(2)); for(size_t i=2; i < l; ++i) { tol_ = tol1*dif(i); res_(i+1) = res_(i)+compute<false>(f, wpts_(i), wpts_(i+1)); } if (l >= 2) { tol_ = tol1*dif(l); res_(l+1) = res_(l)+compute<true>(f, wpts_(l), wpts_(l+1)); } tol_ = tmptol; if (!o.return_waypoints) { res_(begin_) = res_(end_); res_.resize(nt2::of_size(1, 1)); } }
static std::vector<Tensor> expandByteTensors(const Tensor & self, TensorList indices) { // Expands byte tensors (masks) into the equivalent indexing by LongTensors std::vector<Tensor> result; for (auto & index : indices) { if (index.type().scalarType() == kByte) { // The sizes of the ByteTensor mask must match the sizes of the // corresponding dimensions in self for (int64_t j = 0; j < index.dim(); j++) { int64_t srcIdx = result.size() + j; if (index.size(j) != self.size(srcIdx)) { invalid_mask(self, srcIdx, index, j); } } // Replace with nonzeros auto nonzero = index.nonzero(); auto is_empty = nonzero.numel() == 0; for (int64_t j = 0; j < index.dim(); j++) { if (is_empty) { // We can't call select on an empty tensor so we just create an empty // tensor. result.emplace_back(nonzero.type().tensor()); } else { result.emplace_back(nonzero.select(1, j)); } } } else { result.emplace_back(index); } } return result; }
/* @brief 1D Interpolation * * @param x Original base * @param y Original values * @param xi Interpolation base * @param intm Interpolation method @see INTERP::Method * @return Interpolation values */ template <class T> inline static Matrix<T> interp_1 (const Vector<double>& x, const Matrix<T>& y, const Matrix<double>& xi, const INTERP::Method intm = INTERP::CSPLINE) { size_t nx = x.size(); assert (nx > 0); assert (nx == size(y,0)); size_t nd = numel(y)/nx; size_t nxi = size(xi,0); Matrix<double> xx(nx,1); xx.Container() = x; Matrix<T> yi (nxi,nd); for (size_t j = 0; j < nd; j++) { PolyVal<T> pv (xx, (T*) y.Ptr(j*nx), intm); for (size_t i = 0; i < nxi; i++) yi [j * nxi + i] = pv.Lookup (xi[i]); } return yi; }