Beispiel #1
0
    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;
    }
Beispiel #2
0
 BOOST_FORCEINLINE void
 combine_eigens(const T& wr,  const T& wi, A0& w)
 {
   typedef typename A0::value_type type_t;
   int n = numel(wr);
   w.resize(of_size(n, 1));
   nt2::container::table<type_t, nt2::shared_> sw(of_size(n, 1), share(w.data(), w.data()+n));
   sw = tocomplex(wr, wi);
 }
Beispiel #3
0
 result_type operator()(A0& out, const A1& in) const
 {
   size_t n =  boost::proto::child_c<0>(in);
   out.resize(nt2::of_size(n, n));
   nt2::table<value_type, _2D> l = nt2::abs(nt2::pascal(n, 1, nt2::meta::as_<value_type>()));
   nt2::table<value_type, _2D> u = nt2::fliplr(nt2::flipud(l));
   nt2::table<value_type, _2D> d = nt2::from_diag(nt2::pow(value_type(-2), nt2::_(value_type(0), value_type(n-1))));
   out =  nt2::round2even(nt2::mtimes(l, nt2::mtimes(d, u)));
   return out;
 }
Beispiel #4
0
 result_type operator()(A0& out, const A1& in) const
 {
   BOOST_AUTO_TPL(x,boost::proto::child_c<0>(in));
   _2D siz =  boost::proto::value(boost::proto::child_c<1>(in));
   size_t n = siz[0];
   size_t m = siz[1];
   tab_t rnd = nt2::rand(n*m, 1, nt2::meta::as_<value_t>());
   value_t p = nt2::numel(x);
   out.resize(siz);
   out(nt2::_) = x(nt2::iceil(rnd*p));
   return out;
 }
Beispiel #5
0
    result_type operator()(A0& out, const A1& in) const
    {
      size_t n =  boost::proto::child_c<0>(in);
      value_type fn =  value_type(n);
      table<value_type> ns =  nt2::cons(of_size(1, 3), fn, fn/12, fn/20);
      table<i_type> ee(nt2::of_size(1, 3));
      table<value_type> ff(nt2::of_size(1, 3));
//      nt2::frexp(ns, ff, ee);
      nt2::frexp(ns(1), ff(1), ee(1));
      nt2::frexp(ns(2), ff(2), ee(2));
      nt2::frexp(ns(3), ff(3), ee(3));
      BOOST_AUTO_TPL(kk, nt2::find(nt2::logical_and(eq(ff, nt2::Half<value_type>()), is_gtz(ee))));
      BOOST_ASSERT_MSG(!nt2::isempty(kk), "n must be of form 2^m or  12*2^m or 20*2^m");
      size_t k =  kk(1);
      i_type e = nt2::minusone(ee(k));
      out.resize(nt2::of_size(n, n));
      table<value_type> h;
      if (k == 1) //    n = 1 * 2^e;
      {
        h = nt2::One<value_type>();
      }
      else if ( k == 2) // N = 12 * 2^e;
      {
        h = nt2::vertcat(nt2::ones(1,12,meta::as_<value_type>()),
                         nt2::horzcat(nt2::ones(11,1,meta::as_<value_type>()),
                                      nt2::toeplitz(cons<value_type>(of_size(1, 11), -1, -1,  1, -1, -1, -1,  1,  1,  1, -1,  1),
                                                    cons<value_type>(of_size(1, 11), -1,  1, -1,  1,  1,  1, -1, -1, -1,  1, -1)
                                        )
                           )
          );
      }
      else if ( k == 3) // N = 20 * 2^e;
      {
          h = vertcat(nt2::ones(1,20,meta::as_<value_type>()),
                      nt2::horzcat(nt2::ones(19,1,meta::as_<value_type>()),
                              nt2::hankel(cons<value_type>(of_size(1, 19),-1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1),
                                          cons<value_type>(of_size(1, 19),1, -1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1)
                                )
                        )
            );
      }
      //  Kronecker product construction.
      for (i_type i = 1; i <= e; ++i)
      {
        table<value_type> h1 =  nt2::vertcat(nt2::horzcat(h, h), nt2::horzcat(h, -h));
        h =  h1;  //ALIASING
      }
      out =  h;
      return out;
    }
Beispiel #6
0
 result_type operator()(A0& out, const A1& in) const
 {
   size_t n = boost::proto::child_c<0>(in);
   BOOST_ASSERT_MSG(n!= 2, "There is no 2x2 magic matrix");
   out.resize(nt2::of_size(n, n));
   if(n%2 == 1) //Odd order
   {
     oddOrderMagicSquare(out,n);
   }
   else if(n%4 == 0)
   {
     evenx2(out,n);
   }
   else
   {
     evenx1(out,n);
   }
   return out;
 }
Beispiel #7
0
 BOOST_FORCEINLINE void
 combine_vects(const T1& rv, const T2& wi, A0& v)
 {
   typedef typename A0::value_type type_t;
   int n = height(rv);
   v.resize(of_size(n, n));
   nt2::container::table<type_t, nt2::shared_> sv(of_size(n, n), share(v.data(), v.data()+numel(v)));
   for(int j=1; j <= n; ++j)
   {
     if(wi(j))
     {
       sv(nt2::_, j  ) = tocomplex(rv(nt2::_, j), rv(nt2::_, j+1));
       sv(nt2::_, j+1) = conj(sv(nt2::_, j));
       ++j;
     }
     else
       sv(nt2::_, j) = rv(nt2::_, j);
   }
 }
Beispiel #8
0
    BOOST_FORCEINLINE result_type
    operator()(A0& a0, A1& a1) const
    {
      a0.resize(a1.extent());

      input_type input = boost::proto::child_c<0>(a1);
      extent_type ext = input.extent();
      std::size_t dim = nt2::ndims(ext);
      std::size_t red = reduction_dim(a1, boost::mpl::bool_<!(boost::proto::arity_of<A1>::value <= 1)>());

#if 0
      if((red - 1 < ext.size() && ext[red-1] == 1) || ext.size() < red)
        return nt2::run_assign(a0, input);
#endif

      if(dim == 1 && red == 1)
      {
        nt2::run( a0, 0u
                , nt2::fold( input
                           , typename nt2::make_functor<Neutral1, A0>::type()
                           , typename nt2::make_functor<O1, A0>::type()
                           , typename nt2::make_functor<T1, A0>::type()
                           )
                );

      }
      else if(red == 1)
      {
        nt2::inner_fold( a0
                       , input
                       , typename nt2::make_functor<Neutral1, A0>::type()
                       , typename nt2::make_functor<O1, A0>::type()
                       , typename nt2::make_functor<T1, A0>::type()
                       );
      }
#if 0
      else if(red == ext.size())
      {
        nt2::outer_fold( a0
                       , input
                       , typename nt2::make_functor<Neutral1, A0>::type()
                       , typename nt2::make_functor<O1, A0>::type()
                       , typename nt2::make_functor<T1, A0>::type()
                       );
      }
#endif
      else
      {
        std::size_t inner = red-1 < ext.size() ? ext[red-1] : 1;

        std::size_t lo = std::accumulate( ext.begin()
                                        , ext.begin()+std::min(red-1, dim)
                                        , std::size_t(1)
                                        , std::multiplies<std::size_t>()
                                        );

        std::size_t hi = std::accumulate( ext.begin()+std::min(red, dim)
                                        , ext.begin()+dim
                                        , std::size_t(1)
                                        , std::multiplies<std::size_t>()
                                        );

        nt2::partial_fold( reshape(a0, of_size(lo, hi))
                         , reshape(input, of_size(lo, inner, hi))
                         , typename nt2::make_functor<Neutral1, A0>::type()
                         , typename nt2::make_functor<O1, A0>::type()
                         , typename nt2::make_functor<T1, A0>::type()
                         );
      }

      return a0;
    }