Minsky
civita::BinOp Class Reference

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...

#include <tensorOp.h>

Inheritance diagram for civita::BinOp:
Inheritance graph
Collaboration diagram for civita::BinOp:
Collaboration graph

Public Member Functions

template<class F >
 BinOp (F f, const TensorPtr &arg1={}, const TensorPtr &arg2={})
 
void setArguments (const TensorPtr &a1, const TensorPtr &a2, const ITensor::Args &) override
 
double operator[] (std::size_t i) const override
 return or compute data at a location More...
 
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 setArgument (const TensorPtr &, const ITensor::Args &args={"", 0})
 
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})
 

Protected Attributes

std::function< double(double, double)> f
 
TensorPtr arg1
 
TensorPtr arg2
 
- Protected Attributes inherited from civita::ITensor
Hypercube m_hypercube
 
Index m_index
 

Additional Inherited Members

- Public Types inherited from civita::ITensor
using Timestamp = std::chrono::time_point< std::chrono::high_resolution_clock >
 
- Protected Member Functions inherited from civita::ITensor
void notImpl () const
 

Detailed Description

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

Definition at line 49 of file tensorOp.h.

Constructor & Destructor Documentation

◆ BinOp()

template<class F >
civita::BinOp::BinOp ( f,
const TensorPtr arg1 = {},
const TensorPtr arg2 = {} 
)
inline

Definition at line 56 of file tensorOp.h.

56  {},const TensorPtr& arg2={}):
57  f(f) {BinOp::setArguments(arg1,arg2,{"",0});}
TensorPtr arg1
Definition: tensorOp.h:53
void setArguments(const TensorPtr &a1, const TensorPtr &a2, const ITensor::Args &) override
Definition: tensorOp.cc:28
TensorPtr arg2
Definition: tensorOp.h:53
std::shared_ptr< ITensor > TensorPtr
std::function< double(double, double)> f
Definition: tensorOp.h:52

Member Function Documentation

◆ operator[]()

double civita::BinOp::operator[] ( std::size_t  ) const
inlineoverridevirtual

return or compute data at a location

Implements civita::ITensor.

Definition at line 61 of file tensorOp.h.

References arg1, arg2, f, civita::ITensor::hcIndex(), civita::ITensor::index(), and civita::ITensor::size().

61  {
62  // missing arguments treated as group identity
63  if (!arg1)
64  {
65  if (arg2)
66  return (*arg2)[i];
67  else
68  throw std::runtime_error("inputs undefined");
69  }
70  if (!arg2) return (*arg1)[i];
71  assert(index().size()==0 || i<index().size());
72  auto hcIndex=index().size()? index()[i]: i;
73  // scalars are broadcast
74  return f(arg1->rank()? arg1->atHCIndex(hcIndex): (*arg1)[0],
75  arg2->rank()? arg2->atHCIndex(hcIndex): (*arg2)[0]);
76  }
TensorPtr arg1
Definition: tensorOp.h:53
virtual const Index & index() const
the index vector - assumed to be ordered and unique
virtual std::size_t size() const
return number of elements in tensor - maybe less than hypercube.numElements if sparse ...
std::size_t hcIndex(const std::initializer_list< T > &indices) const
TensorPtr arg2
Definition: tensorOp.h:53
std::function< double(double, double)> f
Definition: tensorOp.h:52
Here is the call graph for this function:

◆ setArguments()

void civita::BinOp::setArguments ( const TensorPtr a1,
const TensorPtr a2,
const ITensor::Args  
)
overridevirtual

Reimplemented from civita::ITensor.

Reimplemented in minsky::TensorBinOp< OperationType::add >.

Definition at line 28 of file tensorOp.cc.

29  {
30  arg1=a1; arg2=a2;
31  if (arg1 && arg1->rank()!=0)
32  {
33  hypercube(arg1->hypercube());
34  if (arg2 && arg2->rank()!=0 && arg1->hypercube().dims()!=arg2->hypercube().dims())
35  throw std::runtime_error("arguments not conformal");
36 
37  }
38  else if (arg2)
39  hypercube(arg2->hypercube());
40  else
41  hypercube(Hypercube());
42  set<size_t> indices;
43  if (arg1) indices.insert(arg1->index().begin(), arg1->index().end());
44  if (arg2) indices.insert(arg2->index().begin(), arg2->index().end());
45  m_index=indices;
46  }
TensorPtr arg1
Definition: tensorOp.h:53
virtual const Hypercube & hypercube() const
information describing the axes, types and labels of this tensor
TensorPtr arg2
Definition: tensorOp.h:53

◆ timestamp()

Timestamp civita::BinOp::timestamp ( ) const
inlineoverridevirtual

timestamp indicating how old the dependendent data might be. Used in CachedTensorOp to determine when to invalidate the cache

Implements civita::ITensor.

Definition at line 77 of file tensorOp.h.

References arg1, and arg2.

78  {return max(arg1->timestamp(), arg2->timestamp());}
TensorPtr arg1
Definition: tensorOp.h:53
TensorPtr arg2
Definition: tensorOp.h:53

Member Data Documentation

◆ arg1

TensorPtr civita::BinOp::arg1
protected

Definition at line 53 of file tensorOp.h.

Referenced by operator[](), and timestamp().

◆ arg2

TensorPtr civita::BinOp::arg2
protected

Definition at line 53 of file tensorOp.h.

Referenced by operator[](), and timestamp().

◆ f

std::function<double(double,double)> civita::BinOp::f
protected

Definition at line 52 of file tensorOp.h.

Referenced by operator[]().


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