Minsky
civita Namespace Reference

Namespaces

 anonymous_namespace{tensorOp.cc}
 

Classes

struct  Average
 calculates the average along an axis or whole tensor More...
 
class  BinOp
 perform a binary operation elementwise over two tensor arguments. Arguments need to be conformal: at least one must be a scalar, or both arguments have the same shape More...
 
class  CachedTensorOp
 
struct  DimensionedArgCachedOp
 
struct  ElementWiseOp
 perform an operation elementwise over a tensor valued argument More...
 
class  InterpolateHC
 Interpolates its argument to its hypercube rank must equal arg->rank(), and xvectors type must match. More...
 
class  ITensor
 
class  Max
 calculate the maximum along an axis or whole tensor More...
 
class  Min
 calculate the minimum along an axis or whole tensor More...
 
class  PermuteAxis
 
class  Pivot
 corresponds to the OLAP pivot operation More...
 
struct  Product
 calculate the product along an axis or whole tensor More...
 
struct  ReduceAllOp
 reduce all elements to a single number More...
 
class  ReduceArguments
 elementwise reduction over a vector of arguments More...
 
class  ReductionOp
 compute the reduction along the indicated dimension, ignoring any missing entry (NaNs) More...
 
class  Scan
 
class  Slice
 corresponds to OLAP slice operation More...
 
class  SortByValue
 If a rank 1 argument, sort by the value of the argument. More...
 
class  SpreadBase
 
class  SpreadFirst
 
class  SpreadLast
 
struct  StdDeviation
 calculates the standard deviation along an axis or whole tensor More...
 
struct  Sum
 calculate the sum along an axis or whole tensor More...
 

Typedefs

using TensorPtr = std::shared_ptr< ITensor >
 

Functions

vector< TensorPtrcreateRavelChain (const ravel::RavelState &, const TensorPtr &arg)
 creates a chain of tensor operations that represents a Ravel in state state, operating on arg More...
 

Typedef Documentation

◆ TensorPtr

using civita::TensorPtr = typedef std::shared_ptr<ITensor>

Definition at line 34 of file tensorInterface.h.

Function Documentation

◆ createRavelChain()

std::vector< TensorPtr > civita::createRavelChain ( const ravel::RavelState &  state,
const TensorPtr arg 
)

creates a chain of tensor operations that represents a Ravel in state state, operating on arg

Definition at line 497 of file tensorOp.cc.

References civita::anonymous_namespace{tensorOp.cc}::createReductionOp(), and minsky::str().

Referenced by MathDAG::LockDAG::addEvalOps(), minsky::TensorOpFactory::create(), minsky::RavelTensor::operator[](), and minsky::RavelTensor::setArgument().

498  {
499  set<string> outputHandles(state.outputHandles.begin(), state.outputHandles.end());
500  vector<TensorPtr> chain{arg};
501  // TODO sorts and calipers
502  for (auto& i: state.handleStates)
503  {
504  if (i.order!=ravel::HandleSort::none || i.displayFilterCaliper)
505  {
506  //apply sorting/calipers
507  auto permuteAxis=make_shared<PermuteAxis>();
508  permuteAxis->setArgument(chain.back(), {i.description,0});
509  auto& xv=chain.back()->hypercube().xvectors[permuteAxis->axis()];
510  vector<size_t> perm;
511  for (size_t i=0; i<xv.size(); ++i)
512  perm.push_back(i);
513  switch (i.order)
514  {
515  case ravel::HandleSort::none: break;
516  case ravel::HandleSort::forward:
517  case ravel::HandleSort::numForward:
518  case ravel::HandleSort::timeForward:
519  sort(perm.begin(), perm.end(),
520  [&](size_t i, size_t j) {return diff(xv[i],xv[j])<0;});
521  break;
522  case ravel::HandleSort::reverse:
523  case ravel::HandleSort::numReverse:
524  case ravel::HandleSort::timeReverse:
525  sort(perm.begin(), perm.end(),
526  [&](size_t i, size_t j) {return diff(xv[i],xv[j])>0;});
527  break;
528  case ravel::HandleSort::custom:
529  {
530  map<string, size_t> offsets;
531  for (size_t i=0; i<xv.size(); ++i)
532  offsets[str(xv[i], xv.dimension.units)]=i;
533  perm.clear();
534  for (auto& j: i.customOrder)
535  if (offsets.count(j))
536  perm.push_back(offsets[j]);
537  break;
538  }
539 // case ravel::HandleSort::numForward: case ravel::HandleSort::numReverse:
540 // case ravel::HandleSort::timeForward: case ravel::HandleSort::timeReverse:
541 // throw runtime_error("deprecated sort order used");
542  }
543  if (i.displayFilterCaliper)
544  {
545  // remove any permutation items outside calipers
546  if (!i.minLabel.empty())
547  for (auto j=perm.begin(); j!=perm.end(); ++j)
548  if (str(xv[*j],xv.dimension.units) == i.minLabel)
549  {
550  perm.erase(perm.begin(), j);
551  break;
552  }
553  if (!i.maxLabel.empty())
554  for (auto j=perm.begin(); j!=perm.end(); ++j)
555  if (str(xv[*j],xv.dimension.units) == i.maxLabel)
556  {
557  perm.erase(j+1, perm.end());
558  break;
559  }
560  }
561  permuteAxis->setPermutation(move(perm));
562  chain.push_back(permuteAxis);
563  }
564  if (!outputHandles.count(i.description))
565  {
566  auto arg=chain.back();
567  if (i.collapsed)
568  {
569  chain.emplace_back(createReductionOp(i.reductionOp));
570  chain.back()->setArgument(arg, {i.description,0});
571  }
572  else
573  {
574  chain.emplace_back(new Slice);
575  auto& xv=arg->hypercube().xvectors;
576  auto axisIt=find_if(xv.begin(), xv.end(),
577  [&](const XVector& j){return j.name==i.description;});
578  if (axisIt==xv.end()) throw runtime_error("axis "+i.description+" not found");
579  auto sliceIt=find_if(axisIt->begin(), axisIt->end(),
580  [&](const boost::any& j){return str(j,axisIt->dimension.units)==i.sliceLabel;});
581  // determine slice index
582  size_t sliceIdx=0;
583  if (sliceIt!=axisIt->end())
584  sliceIdx=sliceIt-axisIt->begin();
585  chain.back()->setArgument(arg, {i.description, double(sliceIdx)});
586  }
587  }
588  }
589 
590  if (chain.back()->rank()>1)
591  {
592  auto finalPivot=make_shared<Pivot>();
593  finalPivot->setArgument(chain.back());
594  finalPivot->setOrientation(state.outputHandles);
595  chain.push_back(finalPivot);
596  }
597  return chain;
598  }
std::string str(T x)
utility function to create a string representation of a numeric type
Definition: str.h:33
TensorPtr createReductionOp(ravel::Op::ReductionOp op)
factory method for creating reduction operations
Definition: tensorOp.cc:398
Here is the call graph for this function:
Here is the caller graph for this function: