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) 64 int new_size = dim1 *
dim2;
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) {
390 ASSERT_HOST(isfinite(
array_[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;
485 ReverseN(&size1,
sizeof(size1));
486 ReverseN(&size2,
sizeof(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 {
541 ASSERT_HOST(row >= column);
542 ASSERT_HOST(row - column < this->
dim2_);
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;
578 :
BandTriMatrix<BLOB_CHOICE_LIST *>(dimension, bandwidth, NOT_CLASSIFIED) {}
583 bool Classified(
int col,
int row,
int wildcard_id)
const;
587 void IncreaseBandSize(
int bandwidth);
594 MATRIX* ConsumeAndMakeBigger(
int ind);
602 void print(
const UNICHARSET &unicharset)
const;
616 return 0 <= col && col < m.
dimension() &&
627 ASSERT_HOST(row >= col);
628 if (col > ind) ++col;
629 if (row >= ind) ++row;
630 ASSERT_HOST(row >= col);
640 #endif // TESSERACT_CCSTRUCT_MATRIX_H_ void operator-=(const GENERIC_2D_ARRAY< T > &minuend)
Definition: matrix.h:267
GENERIC_2D_ARRAY(int dim1, int dim2, const T &empty, T *array)
Definition: matrix.h:56
void put(int column, int row, const T &thing)
Definition: matrix.h:223
~MATRIX_COORD()
Definition: matrix.h:613
int size_allocated_
Definition: matrix.h:512
bool SerializeSize(tesseract::TFile *fp) const
Definition: matrix.h:472
double Normalize()
Definition: matrix.h:315
BandTriMatrix(int dim1, int dim2, const T &empty)
Definition: matrix.h:526
int dim1_
Definition: matrix.h:507
void SumSquares(const GENERIC_2D_ARRAY< T > &src, const T &decay_factor)
Definition: matrix.h:368
int16_t x() const
access function
Definition: points.h:53
bool DeSerializeClasses(bool swap, FILE *fp)
Definition: matrix.h:195
bool SerializeSize(FILE *fp) const
Definition: matrix.h:466
void put(ICOORD pos, const T &thing)
Definition: matrix.h:220
T MaxAbs() const
Definition: matrix.h:355
int col
Definition: matrix.h:633
void ResizeWithCopy(int size1, int size2)
Definition: matrix.h:112
Definition: intsimdmatrix.h:25
Definition: unicharset.h:146
GENERIC_2D_ARRAY(int dim1, int dim2, const T &empty)
Definition: matrix.h:62
void AdamUpdate(const GENERIC_2D_ARRAY< T > &sum, const GENERIC_2D_ARRAY< T > &sqsum, const T &epsilon)
Definition: matrix.h:379
void operator*=(const T &factor)
Definition: matrix.h:290
void Clip(const T &rangemin, const T &rangemax)
Definition: matrix.h:297
T empty_
Definition: matrix.h:506
MATRIX_COORD()
Definition: matrix.h:611
void AssertFinite() const
Definition: matrix.h:387
Definition: serialis.h:77
int dim1() const
Definition: matrix.h:206
GENERIC_2D_ARRAY()
Definition: matrix.h:71
void RotatingTranspose(const int *dims, int num_dims, int src_dim, int dest_dim, GENERIC_2D_ARRAY< T > *result) const
Definition: matrix.h:418
bool DeSerializeSize(bool swap, FILE *fp)
Definition: matrix.h:480
T & operator()(int column, int row)
Definition: matrix.h:238
void operator=(const GENERIC_2D_ARRAY< T > &src)
Definition: matrix.h:82
int dim2() const
Definition: matrix.h:207
void MapForSplit(int ind)
Definition: matrix.h:626
void operator+=(const GENERIC_2D_ARRAY< T > &addend)
Definition: matrix.h:251
const T * operator[](int column) const
Definition: matrix.h:246
bool DeSerialize(char *data, size_t count=1)
Definition: serialis.cpp:103
bool Serialize(FILE *fp) const
Definition: matrix.h:144
bool Serialize(tesseract::TFile *fp) const
Definition: matrix.h:151
int bandwidth() const
Definition: matrix.h:535
bool DeSerialize(tesseract::TFile *fp)
Definition: matrix.h:174
bool Serialize(FILE *fp, const char *data, size_t n)
Definition: serialis.cpp:59
int16_t y() const
access_function
Definition: points.h:57
void ResizeNoInit(int size1, int size2, int pad=0)
Definition: matrix.h:91
bool Valid(const MATRIX &m) const
Definition: matrix.h:615
void operator+=(const T &addend)
Definition: matrix.h:283
T Max() const
Definition: matrix.h:342
virtual int num_elements() const
Definition: matrix.h:210
bool DeSerialize(bool swap, FILE *fp)
Definition: matrix.h:161
T get(ICOORD pos) const
Definition: matrix.h:228
integer coordinate
Definition: points.h:32
int row
Definition: matrix.h:634
MATRIX(int dimension, int bandwidth)
Definition: matrix.h:577
GENERIC_2D_ARRAY(const GENERIC_2D_ARRAY< T > &src)
Definition: matrix.h:75
virtual int index(int column, int row) const
Definition: matrix.h:215
bool SerializeClasses(FILE *fp) const
Definition: matrix.h:182
void Resize(int size1, int size2, const T &empty)
Definition: matrix.h:105
int dimension() const
Definition: matrix.h:533
virtual ~GENERIC_2D_ARRAY()
Definition: matrix.h:80
int dim2_
Definition: matrix.h:508
bool Serialize(const char *data, size_t count=1)
Definition: serialis.cpp:147
T * operator[](int column)
Definition: matrix.h:243
T * array_
Definition: matrix.h:505
bool DeSerializeSize(tesseract::TFile *fp)
Definition: matrix.h:494
bool WithinBounds(const T &rangemin, const T &rangemax) const
Definition: matrix.h:305
const T & operator()(int column, int row) const
Definition: matrix.h:235
void AttachOnCorner(BandTriMatrix< T > *array2)
Definition: matrix.h:550
MATRIX_COORD(int c, int r)
Definition: matrix.h:612
bool DeSerialize(FILE *fp, char *data, size_t n)
Definition: serialis.cpp:27
static void Delete(void *arg)
Definition: matrix.h:606
virtual int index(int column, int row) const
Definition: matrix.h:540
void Clear()
Definition: matrix.h:136
void delete_matrix_pointers()
Definition: matrix.h:455