Skip to content

Example Code for static extents and static strides

amitsingh19975 edited this page Jun 24, 2019 · 2 revisions

Construction of Tensor

    auto t1 = tensor<float>{1,2,3,4};
    auto t2 = tensor<float,dynamic_extents<4>>{dynamic_extents<4>{1,2,3,4}};
    auto t3 = tensor<float,dynamic_extents<>>{dynamic_extents<>{1,2,3,4}};
    auto t5 = tensor<float,dynamic_extents<>>{dynamic_extents<>{1,2,3,4},0.1}; 
    auto t4 = tensor<float,static_extents<1,2,3,4>>();

    auto t6 = tensor<float,static_extents<1,2,3,4>,first_order>();
    auto t7 = tensor<float,dynamic_extents<>,first_order>{1,2,3,4};

    auto t8 = tensor<float,dynamic_extents<>,first_order,std::vector<float>>{1,2,3,4};
    auto t9 = tensor<float,static_extents<1,2,3,4>,first_order,std::array<float,24>>();
    
    auto t10 = tensor{static_extents<1,2,3,4>{}};// using deduction guide tensor of type float
    auto t11 = tensor{dynamic_extents<>{1,2,3,4}};// using deduction guide tensor of type float
    auto t12 = tensor{dynamic_extents<4>{1,2,3,4}};// using deduction guide tensor of type float
    auto t13 = tensor{dynamic_extents<>{},1};// using deduction guide tensor of type int

Operation on Extents

using namespace boost::numeric::ublas;
{
 auto t1 = tensor<float>{1,1,1,1,1,1};
 auto t2 = tensor<float,dynamic_extents<6>>{1,1,1,1,1,1};
 auto t3 = tensor{static_extents<1,1,1,1,1,1>{},1.f};
 
 assert(valid(t1.extents()));
 assert(valid(t2.extents()));
 assert(valid(t3.extents()));

 assert(is_scalar(t1.extents()));
 assert(is_scalar(t2.extents()));
 assert(is_scalar(t3.extents()));

 assert(!is_vector(t1.extents()));
 assert(!is_vector(t2.extents()));
 assert(!is_vector(t3.extents()));

 assert(!is_matrix(t1.extents()));
 assert(!is_matrix(t2.extents()));
 assert(!is_matrix(t3.extents()));

 assert(!is_tensor(t1.extents()));
 assert(!is_tensor(t2.extents()));
 assert(!is_tensor(t3.extents()));

assert((squeeze(t1.extents()) == dynamic_extents<>{1,1}));
assert((squeeze(t2.extents()) == dynamic_extents<>{1,1}));
assert((squeeze(t3.extents()) == dynamic_extents<>{1,1}));
}

{
 auto t1 = tensor<float>{6,1,1,1,1,1};
 auto t2 = tensor<float,dynamic_extents<6>>{6,1,1,1,1,1};
 auto t3 = tensor{static_extents<6,1,1,1,1,1>{},1.f};
 
 assert(valid(t1.extents()));
 assert(valid(t2.extents()));
 assert(valid(t3.extents()));

 assert(!is_scalar(t1.extents()));
 assert(!is_scalar(t2.extents()));
 assert(!is_scalar(t3.extents()));

 assert(is_vector(t1.extents()));
 assert(is_vector(t2.extents()));
 assert(is_vector(t3.extents()));

 assert(!is_matrix(t1.extents()));
 assert(!is_matrix(t2.extents()));
 assert(!is_matrix(t3.extents()));

 assert(!is_tensor(t1.extents()));
 assert(!is_tensor(t2.extents()));
 assert(!is_tensor(t3.extents()));

assert((squeeze(t1.extents()) == dynamic_extents<>{6,1}));
assert((squeeze(t2.extents()) == dynamic_extents<>{6,1}));
assert((squeeze(t3.extents()) == dynamic_extents<>{6,1}));
}

{
 auto t1 = tensor<float>{6,6,1,1,1,1};
 auto t2 = tensor<float,dynamic_extents<6>>{6,6,1,1,1,1};
 auto t3 = tensor{static_extents<6,6,1,1,1,1>{},1.f};
 
 assert(valid(t1.extents()));
 assert(valid(t2.extents()));
 assert(valid(t3.extents()));

 assert(!is_scalar(t1.extents()));
 assert(!is_scalar(t2.extents()));
 assert(!is_scalar(t3.extents()));

 assert(!is_vector(t1.extents()));
 assert(!is_vector(t2.extents()));
 assert(!is_vector(t3.extents()));

 assert(is_matrix(t1.extents()));
 assert(is_matrix(t2.extents()));
 assert(is_matrix(t3.extents()));

 assert(!is_tensor(t1.extents()));
 assert(!is_tensor(t2.extents()));
 assert(!is_tensor(t3.extents()));

assert((squeeze(t1.extents()) == dynamic_extents<>{6,6}));
assert((squeeze(t2.extents()) == dynamic_extents<>{6,6}));
assert((squeeze(t3.extents()) == dynamic_extents<>{6,6}));
}

{
 auto t1 = tensor<float>{6,6,1,1,1,6};
 auto t2 = tensor<float,dynamic_extents<6>>{6,6,1,1,1,6};
 auto t3 = tensor{static_extents<6,6,1,1,1,6>{},1.f};
 
 assert(valid(t1.extents()));
 assert(valid(t2.extents()));
 assert(valid(t3.extents()));

 assert(!is_scalar(t1.extents()));
 assert(!is_scalar(t2.extents()));
 assert(!is_scalar(t3.extents()));

 assert(!is_vector(t1.extents()));
 assert(!is_vector(t2.extents()));
 assert(!is_vector(t3.extents()));

 assert(!is_matrix(t1.extents()));
 assert(!is_matrix(t2.extents()));
 assert(!is_matrix(t3.extents()));

 assert(is_tensor(t1.extents()));
 assert(is_tensor(t2.extents()));
 assert(is_tensor(t3.extents()));

assert((squeeze(t1.extents()) == dynamic_extents<>{6,6,6}));
assert((squeeze(t2.extents()) == dynamic_extents<>{6,6,6}));
assert((squeeze(t3.extents()) == dynamic_extents<>{6,6,6}));
}

prod with updated Tensor

// prod(/tensor/,/ublas_vector/)
{
  using e1 = dynamic_extents<>;
  using e2 = dynamic_extents<4>;
  using e3 = dynamic_extents<4,2,3,5>;
  auto t1 = tensor{e1{4,2,3,5},1.f};
  auto t2 = tensor{e2{4,2,3,5},1.f};
  auto t3 = tensor{e3{},1.f};
  auto v = vector<float>(4,1.f);
  auto p1 = prod(t1,v,1);
  auto p2 = prod(t2,v,1);
  auto p3 = prod(t3,v,1);
}

// prod(/tensor/,/ublas_matrix/)
{
  using e1 = dynamic_extents<>;
  using e2 = dynamic_extents<4>;
  using e3 = dynamic_extents<4,2,3,5>;
  auto t1 = tensor{e1{4,2,3,5},1.f};
  auto t2 = tensor{e2{4,2,3,5},1.f};
  auto t3 = tensor{e3{},1.f};
  auto m = matrix<float,first_order>(4,4,1.f);
  auto p1 = prod(t1,m,1);
  auto p2 = prod(t2,m,1);
  auto p3 = prod(t3,m,1);
}

// prod(/tensor/,/tensor/,/vector/)
{
  using e1 = dynamic_extents<>;
  using e2 = dynamic_extents<4>;
  using e3 = dynamic_extents<4,2,3,5>;
  auto t1 = tensor{e1{4,2,3,5},1.f};
  auto t2 = tensor{e2{4,2,3,5},1.f};
  auto t3 = tensor{e3{},1.f};
  std::vector<size_t> phi = {/*permutation*/}; // phia one-based permutation tuple of length q for the first input tensor t1, populate it with permutation
  auto p1 = prod(t1,t2,phi);
  auto p2 = prod(t2,t1,phi);
  auto p3 = prod(t1,t3,phi);
}

// prod(/tensor/,/tensor/,/vector/,/vector/)
{
  using e1 = dynamic_extents<>;
  using e2 = dynamic_extents<4>;
  using e3 = dynamic_extents<4,2,3,5>;
  auto t1 = tensor{e1{4,2,3,5},1.f};
  auto t2 = tensor{e2{4,2,3,5},1.f};
  auto t3 = tensor{e3{},1.f};
  std::vector<size_t> phia = {/*permutation*/}; // phia one-based permutation tuple of length q for the first input tensor
  std::vector<size_t> phi = {/*permutation*/}; // phia one-based permutation tuple of length q for the first input tensor
  auto p1 = prod(t1,t2,phia,phib);
  auto p2 = prod(t2,t1,phia,phib);
  auto p3 = prod(t1,t3,phia,phib);
}


// inner_prod(/tensor/,/tensor/)
{
  using e1 = dynamic_extents<>;
  using e2 = dynamic_extents<4>;
  using e3 = dynamic_extents<4,2,3,5>;
  auto t1 = tensor{e1{4,2,3,5},1.f};
  auto t2 = tensor{e2{4,2,3,5},1.f};
  auto t3 = tensor{e3{},1.f};

  auto p1 = prod(t1,t2);
  auto p2 = prod(t2,t1);
  auto p3 = prod(t1,t3);
}

// inner_prod(/tensor/,/tensor/)
{
  using e1 = dynamic_extents<>;
  using e2 = dynamic_extents<4>;
  using e3 = dynamic_extents<4,2,3,5>;
  auto t1 = tensor{e1{4,2,3,5},1.f};
  auto t2 = tensor{e2{4,2,3,5},1.f};
  auto t3 = tensor{e3{},1.f};

  auto p1 = norm(t1);
  auto p2 = norm(t2);
  auto p3 = norm(t3);
}

// outer_prod(/tensor/,/tensor/)
{
  using e1 = dynamic_extents<>;
  using e2 = dynamic_extents<4>;
  using e3 = dynamic_extents<4,2,3,5>;
  auto t1 = tensor{e1{4,2,3,5},1.f};
  auto t2 = tensor{e2{4,2,3,5},1.f};
  auto t3 = tensor{e3{},1.f};

  auto p1 = outer_prod(t1,t2);
  auto p2 = outer_prod(t2,t1);
  auto p3 = outer_prod(t3,t1);
}

// trans(/tensor/,/vector/)
{
  using e1 = dynamic_extents<>;
  using e2 = dynamic_extents<4>;
  using e3 = dynamic_extents<4,2,3,5>;
  auto t1 = tensor{e1{4,2,3,5},1.f};
  auto t2 = tensor{e2{4,2,3,5},1.f};
  auto t3 = tensor{e3{},1.f};
  
  std::vector<size_t> v = {/*permutation*/};
  auto p1 = trans(t1,v);
  auto p2 = trans(t2,v);
  auto p3 = trans(t3,v);
}
Clone this wiki locally