Matrix

template<typename RegType, template<typename> class MergeOp, std::size_t RowCount, std::size_t ColCount>
class Matrix

General Matrix Sketch Container.

good reference for operator declarations: https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading

Implements the container handling infrastructure of any sketch whose atomic elements include a fixed size set of register values organized into a matrix, and supporting a merge operation consisting of the application of an element-wise operator on pairs of register matrices.

Note

MergeOp template parameter is currently unused and may be removed in a future update.

Template Parameters:
  • RegType – The type held by each register.

  • MergeOp – An template merge operator to combine two sketches.

  • RowCount – The maximum number of rows. Assumed equal to ColCount.

  • ColCount – The maximum number of columns. Assumed equal to RowCount.

Public Functions

inline Matrix()

Construct a new Matrix container object. Currently assuming that Matrix objects are always square.

inline Matrix(const self_type &rhs)

Copy constructor.

Parameters:

rhs – The base Matrix container to copy.

inline void compactify()

A no-op for dense containers.

inline void clear()

Set all registers to 0.

inline bool empty() const

Check if all registers are 0.

inline constexpr void merge(const self_type &rhs)

Merge other Matrix registers into this.

Parameters:

rhs – the other Matrix.

inline self_type &operator+=(const self_type &rhs)

Operator overload for convenience for embeddings without additional consistency checks.

Parameters:

rhs – the other Matrix.

Returns:

self_type& this Matrix, having been merged with rhs.

inline constexpr registers_type::iterator begin()

Mutable begin iterator.

inline constexpr registers_type::const_iterator begin() const

Const begin iterator.

inline constexpr registers_type::const_iterator cbegin() const

Const begin iterator.

inline constexpr registers_type::iterator end()

Mutable end iterator.

inline constexpr registers_type::const_iterator end() const

Const end iterator.

inline constexpr registers_type::const_iterator cend()

Const end iterator.

inline constexpr const register_type &operator[](const std::pair<std::uint64_t, std::uint64_t> &indices) const

Const access Matrix at indices pair.

Parameters:

indices – The row and column indices of the underlying matrix to index. Must be less than row_count and col_count, respectively.

Returns:

constexpr const register_type& A const reference to the object at the indicated register.

inline register_type &operator[](const std::pair<std::uint64_t, std::uint64_t> &indices)

Access Matrix at indices pair.

Parameters:

indices – The row and column indices of the underlying matrix to index. Must be less than row_count and col_count, respectively.

Returns:

register_type& A reference to the object at the indicated register.

inline constexpr const register_type &operator()(const std::uint64_t &row_idx, const std::uint64_t &col_idx) const

Const access Matrix at (row_idx, col_idx).

Parameters:
  • row_idx – The row index of the underlying matrix. Must be less than row_count.

  • col_idx – The column index of the underlying matrix. Must be less than col_count.

Returns:

constexpr const register_type& A const reference to the object at the indicated register.

inline register_type &operator()(const std::uint64_t &row_idx, const std::uint64_t &col_idx)

Access Matrix at (row_idx, col_idx).

Parameters:
  • row_idx – The row index of the underlying matrix. Must be less than row_count.

  • col_idx – The column index of the underlying matrix. Must be less than col_count.

Returns:

register_type& A reference to the object at the indicated register.

inline constexpr bool is_sparse() const

Matrix is also not Sparse.

inline constexpr std::size_t reg_size() const

The number of bytes used by each register.

inline const registers_type &registers() const

Get a copy of the raw registers vector.

Returns:

const registers_type The register vector.

inline registers_type scaled_registers(double scaling_factor) const

Get a copy of the scaled registers vector.

Parameters:

scaling_factor – the scalar scaling factor.

Returns:

const registers_type The register vector.

inline constexpr bool same_registers(const self_type &rhs) const

Checks whether another Dense container has the same register state.

Note

allows for small disagreements in floating point representations that may arise from merging, so will return true even in cases where register sets are not exactly equivalent.

Parameters:

rhs – The other container.

Returns:

true The registers agree.

Returns:

false At least one register disagrees.

inline self_type &operator=(self_type rhs)

Copy-and-swap assignment operator.

Parameters:

rhs – The other container.

Returns:

self_type& this, having been swapped with rhs.

Public Static Functions

static inline constexpr std::string name()

Returns a description of the type of container.

Returns:

std::string “Matrix”

static inline constexpr std::string full_name()

Returns a description of the fully-qualified type of container.

Returns:

std::string “Matrix”

static inline constexpr std::size_t size()

The size of the registers vector.

static inline constexpr std::size_t row_count()

The number of rows in the registers matrix.

static inline constexpr std::size_t col_count()

The number of columns in the registers matrix.

static inline constexpr std::size_t max_size()

The size of the registers vector.

static inline constexpr std::size_t embedding_size()

The size of the embedding. Equal to RowCount and ColCount. Assuming that the matrix is square.

Friends

inline friend void swap(self_type &lhs, self_type &rhs)

Swap two Matrix containers.

Parameters:
  • lhs – The left-hand container.

  • rhs – The right-hand container.

inline friend constexpr friend self_type operator+ (self_type lhs, const self_type &rhs)

Merge two Matrix containers.

Parameters:
  • lhs – The left-hand container.

  • rhs – The right-hand container.

Returns:

self_type The merge of the two container objects.

inline friend constexpr bool operator==(const self_type &lhs, const self_type &rhs)

Checks equality between two Dense containers.

Parameters:
  • lhs – The left-hand container.

  • rhs – The right-hand container.

Returns:

true The registers agree.

Returns:

false At least one register disagrees.

inline friend constexpr bool operator!=(const self_type &lhs, const self_type &rhs)

CHecks inequality between two Dense containers.

Parameters:
  • lhs – The left-hand container.

  • rhs – The right-hand container.

Returns:

true At least one register disagrees.

Returns:

false The registers agree.

inline friend std::ostream &operator<<(std::ostream &os, const self_type &sk)

Serialize a Dense container to human-readable output stream.

Output format is a space-delmined list of (key, value) pairs.

Note

Intended for debugging only. Now using Eigen’s default print style.

Parameters:
  • os – The output stream.

  • sk – The Dense object.

Returns:

std::ostream& The new stream state.

template<typename RetType>
inline friend RetType accumulate(const self_type &sk, const RetType init)

Accumulate sum of register values.

Template Parameters:

RetType – The value type to return.

Parameters:
  • sk – The Dense to accumulate.

  • init – Initial value of return.

Returns:

RetType The sum over all register values + init.

template<typename Func>
inline friend void for_each(const self_type &sk, const Func &func)

Apply a given function to all register values.

Template Parameters:

Func – The (probably lambda) function type.

Parameters:
  • sk – The Dense object.

  • func – The particular function to apply.