27 #ifndef TESSERACT_CCSTRUCT_MATRIX_H_ 28 #define TESSERACT_CCSTRUCT_MATRIX_H_ 41 class BLOB_CHOICE_LIST;
44 #define NOT_CLASSIFIED static_cast<BLOB_CHOICE_LIST*>(nullptr) 92 int new_size = size1 * size2 + pad;
101 for (
int i = size1 * size2; i < new_size; ++i)
array_[i] =
empty_;
105 void Resize(
int size1,
int size2,
const T& empty) {
114 int new_size = size1 * size2;
115 T* new_array =
new T[new_size];
116 for (
int col = 0; col < size1; ++col) {
117 for (
int row = 0; row < size2; ++row) {
118 int old_index = col *
dim2() + row;
119 int new_index = col * size2 + row;
121 new_array[new_index] =
array_[old_index];
123 new_array[new_index] =
empty_;
138 for (
int i = 0; i < total_size; ++i)
168 for (
int i = 0; i < size; ++i)
184 if (!
empty_.Serialize(fp))
return false;
186 for (
int i = 0; i < size; ++i) {
197 if (!
empty_.DeSerialize(swap, fp))
return false;
199 for (
int i = 0; i < size; ++i) {
215 virtual int index(
int column,
int row)
const {
216 return (column *
dim2_ + row);
223 void put(
int column,
int row,
const T& thing) {
231 T
get(
int column,
int row)
const {
255 for (
int i = 0; i < size; ++i) {
259 for (
int x = 0; x <
dim1_; x++) {
260 for (
int y = 0; y <
dim2_; y++) {
261 (*this)(x, y) += addend(x, y);
271 for (
int i = 0; i < size; ++i) {
275 for (
int x = 0; x <
dim1_; x++) {
276 for (
int y = 0; y <
dim2_; y++) {
277 (*this)(x, y) -= minuend(x, y);
285 for (
int i = 0; i < size; ++i) {
292 for (
int i = 0; i < size; ++i) {
297 void Clip(
const T& rangemin,
const T& rangemax) {
299 for (
int i = 0; i < size; ++i) {
307 for (
int i = 0; i < size; ++i) {
308 const T& value =
array_[i];
309 if (value < rangemin || rangemax < value)
317 if (size <= 0)
return 0.0;
320 for (
int i = 0; i < size; ++i) {
326 for (
int i = 0; i < size; ++i) {
327 double normed =
array_[i] - mean;
329 sd += normed * normed;
331 sd = sqrt(sd / size);
334 for (
int i = 0; i < size; ++i) {
344 if (size <= 0)
return empty_;
347 for (
int i = 1; i < size; ++i) {
348 const T& value =
array_[i];
349 if (value > max_value) max_value = value;
357 if (size <= 0)
return empty_;
359 T max_abs =
static_cast<T
>(0);
360 for (
int i = 0; i < size; ++i) {
361 T value =
static_cast<T
>(fabs(
array_[i]));
362 if (value > max_abs) max_abs = value;
369 T update_factor = 1.0 - decay_factor;
371 for (
int i = 0; i < size; ++i) {
382 for (
int i = 0; i < size; ++i) {
389 for (
int i = 0; i < size; ++i) {
420 int max_d = std::max(src_dim, dest_dim);
421 int min_d = std::min(src_dim, dest_dim);
430 int num_replicas = 1, move_size = 1, src_step = 1;
431 for (
int d = 0; d < min_d; ++d) num_replicas *= dims[d];
432 for (
int d = max_d + 1; d < num_dims; ++d) move_size *= dims[d];
433 for (
int d = src_dim + 1; d < num_dims; ++d) src_step *= dims[d];
434 if (src_dim > dest_dim) src_step *= dims[src_dim];
437 int wrap_size = move_size;
438 for (
int d = min_d; d <= max_d; ++d) wrap_size *= dims[d];
443 for (
int replica = 0; replica < num_replicas; ++replica) {
444 for (
int start = 0; start < src_step; start += move_size) {
445 for (
int pos = start; pos < wrap_size; pos += src_step) {
446 memcpy(dest, src + pos,
sizeof(*dest) * move_size);
457 for (
int i = 0; i < size; ++i) {
458 T matrix_cell =
array_[i];
459 if (matrix_cell !=
empty_)
467 uint32_t size =
dim1_;
473 uint32_t size =
dim1_;
481 uint32_t size1, size2;
489 if (size1 > UINT16_MAX)
return false;
490 if (size2 > UINT16_MAX)
return false;
495 int32_t size1, size2;
499 if (size1 > UINT16_MAX)
return false;
500 if (size2 > UINT16_MAX)
return false;
540 virtual int index(
int column,
int row)
const {
543 return column * this->dim2_ + row - column;
552 int new_dim2 = std::max(this->
dim2_, array2->
dim2_);
553 T* new_array =
new T[new_dim1 * new_dim2];
554 for (
int col = 0; col < new_dim1; ++col) {
555 for (
int j = 0; j < new_dim2; ++j) {
556 int new_index = col * new_dim2 + j;
558 new_array[new_index] = this->
get(col, col + j);
559 }
else if (col >= this->
dim1_ && j < array2->
dim2_) {
560 new_array[new_index] = array2->
get(col - this->
dim1_,
561 col - this->
dim1_ + j);
562 array2->
put(col - this->
dim1_, col - this->
dim1_ + j,
nullptr);
564 new_array[new_index] = this->
empty_;
570 this->
dim1_ = new_dim1;
571 this->
dim2_ = new_dim2;
583 bool Classified(
int col,
int row,
int wildcard_id)
const;
640 #endif // TESSERACT_CCSTRUCT_MATRIX_H_ void print(const UNICHARSET &unicharset) const
void MapForSplit(int ind)
void delete_matrix_pointers()
T * operator[](int column)
void SumSquares(const GENERIC_2D_ARRAY< T > &src, const T &decay_factor)
virtual ~GENERIC_2D_ARRAY()
MATRIX * ConsumeAndMakeBigger(int ind)
GENERIC_2D_ARRAY(int dim1, int dim2, const T &empty)
int16_t y() const
access_function
bool DeSerialize(char *data, size_t count=1)
void Clip(const T &rangemin, const T &rangemax)
void RotatingTranspose(const int *dims, int num_dims, int src_dim, int dest_dim, GENERIC_2D_ARRAY< T > *result) const
virtual int index(int column, int row) const
const T * operator[](int column) const
void operator+=(const T &addend)
GENERIC_2D_ARRAY(const GENERIC_2D_ARRAY< T > &src)
bool Serialize(FILE *fp, const char *data, size_t n)
void operator*=(const T &factor)
bool Valid(const MATRIX &m) const
bool WithinBounds(const T &rangemin, const T &rangemax) const
void ResizeNoInit(int size1, int size2, int pad=0)
void ReverseN(void *ptr, int num_bytes)
void IncreaseBandSize(int bandwidth)
const T & operator()(int column, int row) const
void AssertFinite() const
MATRIX_COORD(int c, int r)
bool SerializeSize(tesseract::TFile *fp) const
virtual int num_elements() const
void AdamUpdate(const GENERIC_2D_ARRAY< T > &sum, const GENERIC_2D_ARRAY< T > &sqsum, const T &epsilon)
GENERIC_2D_ARRAY(int dim1, int dim2, const T &empty, T *array)
int16_t x() const
access function
virtual int index(int column, int row) const
MATRIX(int dimension, int bandwidth)
bool DeSerializeSize(bool swap, FILE *fp)
static void Delete(void *arg)
T & operator()(int column, int row)
void put(int column, int row, const T &thing)
bool Serialize(tesseract::TFile *fp) const
bool Serialize(const char *data, size_t count=1)
void put(ICOORD pos, const T &thing)
bool Serialize(FILE *fp) const
bool DeSerialize(tesseract::TFile *fp)
bool DeSerialize(bool swap, FILE *fp)
MATRIX * DeepCopy() const
bool Classified(int col, int row, int wildcard_id) const
BandTriMatrix(int dim1, int dim2, const T &empty)
bool DeSerializeSize(tesseract::TFile *fp)
bool SerializeClasses(FILE *fp) const
bool DeSerializeClasses(bool swap, FILE *fp)
bool SerializeSize(FILE *fp) const
void operator+=(const GENERIC_2D_ARRAY< T > &addend)
void Resize(int size1, int size2, const T &empty)
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
void ResizeWithCopy(int size1, int size2)
bool DeSerialize(FILE *fp, char *data, size_t n)
void AttachOnCorner(BandTriMatrix< T > *array2)
void operator-=(const GENERIC_2D_ARRAY< T > &minuend)
void operator=(const GENERIC_2D_ARRAY< T > &src)