Minsky
civita::ReductionOp Class Reference

compute the reduction along the indicated dimension, ignoring any missing entry (NaNs) More...

#include <tensorOp.h>

Inheritance diagram for civita::ReductionOp:
Inheritance graph
Collaboration diagram for civita::ReductionOp:
Collaboration graph

Classes

struct  SOI
 

Public Member Functions

template<class F >
 ReductionOp (F f, double init, const TensorPtr &arg={}, const std::string &dimName="")
 
void setArgument (const TensorPtr &a, const ITensor::Args &) override
 
double operator[] (std::size_t i) const override
 return or compute data at a location More...
 
- Public Member Functions inherited from civita::ReduceAllOp
template<class F >
 ReduceAllOp (F f, double init, const std::shared_ptr< ITensor > &arg={})
 
Timestamp timestamp () const override
 timestamp indicating how old the dependendent data might be. Used in CachedTensorOp to determine when to invalidate the cache More...
 
- Public Member Functions inherited from civita::ITensor
 CLASSDESC_ACCESS (ITensor)
 
 ITensor ()
 
 ITensor (const Hypercube &hc)
 
 ITensor (Hypercube &&hc)
 
 ITensor (const std::vector< unsigned > &dims)
 
 ITensor (const ITensor &)=default
 
 ITensor (ITensor &&)=default
 
ITensoroperator= (const ITensor &)=default
 
ITensoroperator= (ITensor &&)=default
 
virtual ~ITensor ()
 
virtual const Hypercube & hypercube () const
 information describing the axes, types and labels of this tensor More...
 
virtual const Hypercube & hypercube (const Hypercube &hc)
 
virtual const Hypercube & hypercube (Hypercube &&hc)
 
std::size_t rank () const
 
std::vector< unsigned > shape () const
 
void imposeDimensions (const Dimensions &dimensions)
 impose dimensions according to dimension map dimensions More...
 
virtual const Index & index () const
 the index vector - assumed to be ordered and unique More...
 
virtual std::size_t size () const
 return number of elements in tensor - maybe less than hypercube.numElements if sparse More...
 
double atHCIndex (std::size_t hcIdx) const
 returns the data value at hypercube index hcIdx, or NaN if More...
 
template<class T >
std::size_t hcIndex (const std::initializer_list< T > &indices) const
 
template<class T >
double operator() (const std::initializer_list< T > &indices) const
 
virtual void setArguments (const TensorPtr &, const TensorPtr &, const ITensor::Args &args={})
 
virtual void setArguments (const std::vector< TensorPtr > &a, const ITensor::Args &args={"", 0})
 
virtual void setArguments (const std::vector< TensorPtr > &a1, const std::vector< TensorPtr > &a2, const ITensor::Args &args={"", 0})
 

Private Attributes

std::size_t dimension
 
std::map< std::size_t, std::vector< SOI > > sumOverIndices
 

Additional Inherited Members

- Public Types inherited from civita::ITensor
using Timestamp = std::chrono::time_point< std::chrono::high_resolution_clock >
 
- Public Attributes inherited from civita::ReduceAllOp
std::function< void(double &, double, std::size_t)> f
 
double init
 
std::shared_ptr< ITensorarg
 
- Protected Member Functions inherited from civita::ITensor
void notImpl () const
 
- Protected Attributes inherited from civita::ITensor
Hypercube m_hypercube
 
Index m_index
 

Detailed Description

compute the reduction along the indicated dimension, ignoring any missing entry (NaNs)

Definition at line 113 of file tensorOp.h.

Constructor & Destructor Documentation

◆ ReductionOp()

template<class F >
civita::ReductionOp::ReductionOp ( f,
double  init,
const TensorPtr arg = {},
const std::string &  dimName = "" 
)
inline

Definition at line 121 of file tensorOp.h.

121  {}, const std::string& dimName=""):
std::shared_ptr< ITensor > arg
Definition: tensorOp.h:100
ReduceAllOp(F f, double init, const std::shared_ptr< ITensor > &arg={})
Definition: tensorOp.h:104
void setArgument(const TensorPtr &a, const ITensor::Args &) override
Definition: tensorOp.h:101
std::function< void(double &, double, std::size_t)> f
Definition: tensorOp.h:98

Member Function Documentation

◆ operator[]()

double civita::ReductionOp::operator[] ( std::size_t  ) const
overridevirtual

return or compute data at a location

Reimplemented from civita::ReduceAllOp.

Reimplemented in civita::StdDeviation, and civita::Average.

Definition at line 137 of file tensorOp.cc.

References f, TCLcmd::trap::init, and minsky::anonymous_namespace{userFunction.cc}::isnan().

Referenced by civita::Average::operator[](), civita::StdDeviation::operator[](), minsky::GeneralTensorOp< OperationType::median >::operator[](), and minsky::GeneralTensorOp< OperationType::moment >::operator[]().

138  {
139  assert(i<size());
140  if (!arg) return init;
141  if (dimension>arg->rank())
142  return ReduceAllOp::operator[](i);
143 
144  double r=init;
145  if (index().empty())
146  {
147  auto argDims=arg->shape();
148  size_t stride=1;
149  for (size_t j=0; j<dimension; ++j)
150  stride*=argDims[j];
151  auto quotRem=ldiv(i, stride); // quotient and remainder calc in one hit
152  auto start=quotRem.quot*stride*argDims[dimension] + quotRem.rem;
153  assert(stride*argDims[dimension]>0);
154  for (size_t j=0; j<argDims[dimension]; ++j)
155  {
156  double x=arg->atHCIndex(j*stride+start);
157  if (!isnan(x)) f(r,x,j);
158  }
159  }
160  else
161  {
162  auto soi=sumOverIndices.find(index()[i]);
163  assert(soi!=sumOverIndices.end());
164  if (soi!=sumOverIndices.end())
165  for (auto j: soi->second)
166  {
167  double x=(*arg)[j.index];
168  if (!isnan(x)) f(r,x,j.dimIndex);
169  }
170  }
171  return r;
172  }
std::size_t dimension
Definition: tensorOp.h:115
virtual const Index & index() const
the index vector - assumed to be ordered and unique
std::map< std::size_t, std::vector< SOI > > sumOverIndices
Definition: tensorOp.h:117
std::shared_ptr< ITensor > arg
Definition: tensorOp.h:100
virtual std::size_t size() const
return number of elements in tensor - maybe less than hypercube.numElements if sparse ...
std::function< void(double &, double, std::size_t)> f
Definition: tensorOp.h:98
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setArgument()

void civita::ReductionOp::setArgument ( const TensorPtr a,
const ITensor::Args args 
)
overridevirtual

Reimplemented from civita::ReduceAllOp.

Definition at line 101 of file tensorOp.cc.

References civita::ITensor::Args::dimension.

Referenced by minsky::GeneralTensorOp< OperationType::moment >::setArgument(), and minsky::GeneralTensorOp< OperationType::linearRegression >::setArguments().

102  {
103  arg=a;
104  dimension=std::numeric_limits<size_t>::max();
105  if (arg)
106  {
107  const auto& ahc=arg->hypercube();
108  m_hypercube=ahc;
109  auto& xv=m_hypercube.xvectors;
110  for (auto i=xv.begin(); i!=xv.end(); ++i)
111  if (i->name==args.dimension)
112  dimension=i-xv.begin();
113  if (dimension<arg->rank())
114  {
115  xv.erase(xv.begin()+dimension);
116  // compute index - enter index elements that have any in the argument
117  set<size_t> indices;
118  for (size_t i=0; i<arg->size(); ++i)
119  {
120  auto splitIdx=ahc.splitIndex(arg->index()[i]);
121  SOI soi{i,splitIdx[dimension]};
122  splitIdx.erase(splitIdx.begin()+dimension);
123  auto idx=m_hypercube.linealIndex(splitIdx);
124  sumOverIndices[idx].emplace_back(soi);
125  indices.insert(idx);
126  }
127  m_index=indices;
128  }
129  else
130  m_hypercube.xvectors.clear(); //reduce all, return scalar
131  }
132  else
133  m_hypercube.xvectors.clear();
134  }
std::size_t dimension
Definition: tensorOp.h:115
std::size_t rank() const
std::map< std::size_t, std::vector< SOI > > sumOverIndices
Definition: tensorOp.h:117
std::shared_ptr< ITensor > arg
Definition: tensorOp.h:100
Hypercube m_hypercube
Here is the caller graph for this function:

Member Data Documentation

◆ dimension

std::size_t civita::ReductionOp::dimension
private

Definition at line 115 of file tensorOp.h.

◆ sumOverIndices

std::map<std::size_t, std::vector<SOI> > civita::ReductionOp::sumOverIndices
private

Definition at line 117 of file tensorOp.h.


The documentation for this class was generated from the following files: