tesseract  4.00.00dev
matrix.h
Go to the documentation of this file.
1 /* -*-C-*-
2  ******************************************************************************
3  * File: matrix.h (Formerly matrix.h)
4  * Description: Generic 2-d array/matrix and banded triangular matrix class.
5  * Author: Ray Smith
6  * TODO(rays) Separate from ratings matrix, which it also contains:
7  *
8  * Descrition: Ratings matrix class (specialization of banded matrix).
9  * Segmentation search matrix of lists of BLOB_CHOICE.
10  * Author: Mark Seaman, OCR Technology
11  * Created: Wed May 16 13:22:06 1990
12  * Modified: Tue Mar 19 16:00:20 1991 (Mark Seaman) marks@hpgrlt
13  * Language: C
14  * Package: N/A
15  * Status: Experimental (Do Not Distribute)
16  *
17  * (c) Copyright 1990, Hewlett-Packard Company.
18  ** Licensed under the Apache License, Version 2.0 (the "License");
19  ** you may not use this file except in compliance with the License.
20  ** You may obtain a copy of the License at
21  ** http://www.apache.org/licenses/LICENSE-2.0
22  ** Unless required by applicable law or agreed to in writing, software
23  ** distributed under the License is distributed on an "AS IS" BASIS,
24  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25  ** See the License for the specific language governing permissions and
26  ** limitations under the License.
27  *
28  *********************************************************************************/
29 #ifndef TESSERACT_CCSTRUCT_MATRIX_H_
30 #define TESSERACT_CCSTRUCT_MATRIX_H_
31 
32 #include <math.h>
33 #include "kdpair.h"
34 #include "points.h"
35 #include "serialis.h"
36 #include "unicharset.h"
37 
38 class BLOB_CHOICE;
39 class BLOB_CHOICE_LIST;
40 
41 #define NOT_CLASSIFIED static_cast<BLOB_CHOICE_LIST*>(0)
42 
43 // A generic class to hold a 2-D matrix with entries of type T, but can also
44 // act as a base class for other implementations, such as a triangular or
45 // banded matrix.
46 template <class T>
48  public:
49  // Initializes the array size, and empty element, but cannot allocate memory
50  // for the subclasses or initialize because calls to the num_elements
51  // member will be routed to the base class implementation. Subclasses can
52  // either pass the memory in, or allocate after by calling Resize().
53  GENERIC_2D_ARRAY(int dim1, int dim2, const T& empty, T* array)
54  : empty_(empty), dim1_(dim1), dim2_(dim2), array_(array) {
55  size_allocated_ = dim1 * dim2;
56  }
57  // Original constructor for a full rectangular matrix DOES allocate memory
58  // and initialize it to empty.
59  GENERIC_2D_ARRAY(int dim1, int dim2, const T& empty)
60  : empty_(empty), dim1_(dim1), dim2_(dim2) {
61  int new_size = dim1 * dim2;
62  array_ = new T[new_size];
63  size_allocated_ = new_size;
64  for (int i = 0; i < size_allocated_; ++i)
65  array_[i] = empty_;
66  }
67  // Default constructor for array allocation. Use Resize to set the size.
69  : array_(NULL), empty_(static_cast<T>(0)), dim1_(0), dim2_(0),
70  size_allocated_(0) {
71  }
73  : array_(NULL), empty_(static_cast<T>(0)), dim1_(0), dim2_(0),
74  size_allocated_(0) {
75  *this = src;
76  }
77  virtual ~GENERIC_2D_ARRAY() { delete[] array_; }
78 
79  void operator=(const GENERIC_2D_ARRAY<T>& src) {
80  ResizeNoInit(src.dim1(), src.dim2());
81  memcpy(array_, src.array_, num_elements() * sizeof(array_[0]));
82  }
83 
84  // Reallocate the array to the given size. Does not keep old data, but does
85  // not initialize the array either.
86  void ResizeNoInit(int size1, int size2) {
87  int new_size = size1 * size2;
88  if (new_size > size_allocated_) {
89  delete [] array_;
90  array_ = new T[new_size];
91  size_allocated_ = new_size;
92  }
93  dim1_ = size1;
94  dim2_ = size2;
95  }
96 
97  // Reallocate the array to the given size. Does not keep old data.
98  void Resize(int size1, int size2, const T& empty) {
99  empty_ = empty;
100  ResizeNoInit(size1, size2);
101  Clear();
102  }
103 
104  // Reallocate the array to the given size, keeping old data.
105  void ResizeWithCopy(int size1, int size2) {
106  if (size1 != dim1_ || size2 != dim2_) {
107  int new_size = size1 * size2;
108  T* new_array = new T[new_size];
109  for (int col = 0; col < size1; ++col) {
110  for (int row = 0; row < size2; ++row) {
111  int old_index = col * dim2() + row;
112  int new_index = col * size2 + row;
113  if (col < dim1_ && row < dim2_) {
114  new_array[new_index] = array_[old_index];
115  } else {
116  new_array[new_index] = empty_;
117  }
118  }
119  }
120  delete[] array_;
121  array_ = new_array;
122  dim1_ = size1;
123  dim2_ = size2;
124  size_allocated_ = new_size;
125  }
126  }
127 
128  // Sets all the elements of the array to the empty value.
129  void Clear() {
130  int total_size = num_elements();
131  for (int i = 0; i < total_size; ++i)
132  array_[i] = empty_;
133  }
134 
135  // Writes to the given file. Returns false in case of error.
136  // Only works with bitwise-serializeable types!
137  bool Serialize(FILE* fp) const {
138  if (!SerializeSize(fp)) return false;
139  if (fwrite(&empty_, sizeof(empty_), 1, fp) != 1) return false;
140  int size = num_elements();
141  if (fwrite(array_, sizeof(*array_), size, fp) != size) return false;
142  return true;
143  }
144  bool Serialize(tesseract::TFile* fp) const {
145  if (!SerializeSize(fp)) return false;
146  if (fp->FWrite(&empty_, sizeof(empty_), 1) != 1) return false;
147  int size = num_elements();
148  if (fp->FWrite(array_, sizeof(*array_), size) != size) return false;
149  return true;
150  }
151 
152  // Reads from the given file. Returns false in case of error.
153  // Only works with bitwise-serializeable types!
154  // If swap is true, assumes a big/little-endian swap is needed.
155  bool DeSerialize(bool swap, FILE* fp) {
156  if (!DeSerializeSize(swap, fp)) return false;
157  if (fread(&empty_, sizeof(empty_), 1, fp) != 1) return false;
158  if (swap) ReverseN(&empty_, sizeof(empty_));
159  int size = num_elements();
160  if (fread(array_, sizeof(*array_), size, fp) != size) return false;
161  if (swap) {
162  for (int i = 0; i < size; ++i)
163  ReverseN(&array_[i], sizeof(array_[i]));
164  }
165  return true;
166  }
168  if (!DeSerializeSize(fp)) return false;
169  if (fp->FReadEndian(&empty_, sizeof(empty_), 1) != 1) return false;
170  int size = num_elements();
171  if (fp->FReadEndian(array_, sizeof(*array_), size) != size) return false;
172  return true;
173  }
174 
175  // Writes to the given file. Returns false in case of error.
176  // Assumes a T::Serialize(FILE*) const function.
177  bool SerializeClasses(FILE* fp) const {
178  if (!SerializeSize(fp)) return false;
179  if (!empty_.Serialize(fp)) return false;
180  int size = num_elements();
181  for (int i = 0; i < size; ++i) {
182  if (!array_[i].Serialize(fp)) return false;
183  }
184  return true;
185  }
186 
187  // Reads from the given file. Returns false in case of error.
188  // Assumes a T::DeSerialize(bool swap, FILE*) function.
189  // If swap is true, assumes a big/little-endian swap is needed.
190  bool DeSerializeClasses(bool swap, FILE* fp) {
191  if (!DeSerializeSize(swap, fp)) return false;
192  if (!empty_.DeSerialize(swap, fp)) return false;
193  int size = num_elements();
194  for (int i = 0; i < size; ++i) {
195  if (!array_[i].DeSerialize(swap, fp)) return false;
196  }
197  return true;
198  }
199 
200  // Provide the dimensions of this rectangular matrix.
201  int dim1() const { return dim1_; }
202  int dim2() const { return dim2_; }
203  // Returns the number of elements in the array.
204  // Banded/triangular matrices may override.
205  virtual int num_elements() const { return dim1_ * dim2_; }
206 
207  // Expression to select a specific location in the matrix. The matrix is
208  // stored COLUMN-major, so the left-most index is the most significant.
209  // This allows [][] access to use indices in the same order as (,).
210  virtual int index(int column, int row) const {
211  return (column * dim2_ + row);
212  }
213 
214  // Put a list element into the matrix at a specific location.
215  void put(ICOORD pos, const T& thing) {
216  array_[this->index(pos.x(), pos.y())] = thing;
217  }
218  void put(int column, int row, const T& thing) {
219  array_[this->index(column, row)] = thing;
220  }
221 
222  // Get the item at a specified location from the matrix.
223  T get(ICOORD pos) const {
224  return array_[this->index(pos.x(), pos.y())];
225  }
226  T get(int column, int row) const {
227  return array_[this->index(column, row)];
228  }
229  // Return a reference to the element at the specified location.
230  const T& operator()(int column, int row) const {
231  return array_[this->index(column, row)];
232  }
233  T& operator()(int column, int row) {
234  return array_[this->index(column, row)];
235  }
236  // Allow access using array[column][row]. NOTE that the indices are
237  // in the same left-to-right order as the () indexing.
238  T* operator[](int column) {
239  return &array_[this->index(column, 0)];
240  }
241  const T* operator[](int column) const {
242  return &array_[this->index(column, 0)];
243  }
244 
245  // Adds addend to *this, element-by-element.
246  void operator+=(const GENERIC_2D_ARRAY<T>& addend) {
247  if (dim2_ == addend.dim2_) {
248  // Faster if equal size in the major dimension.
249  int size = MIN(num_elements(), addend.num_elements());
250  for (int i = 0; i < size; ++i) {
251  array_[i] += addend.array_[i];
252  }
253  } else {
254  for (int x = 0; x < dim1_; x++) {
255  for (int y = 0; y < dim2_; y++) {
256  (*this)(x, y) += addend(x, y);
257  }
258  }
259  }
260  }
261  // Subtracts minuend from *this, element-by-element.
262  void operator-=(const GENERIC_2D_ARRAY<T>& minuend) {
263  if (dim2_ == minuend.dim2_) {
264  // Faster if equal size in the major dimension.
265  int size = MIN(num_elements(), minuend.num_elements());
266  for (int i = 0; i < size; ++i) {
267  array_[i] -= minuend.array_[i];
268  }
269  } else {
270  for (int x = 0; x < dim1_; x++) {
271  for (int y = 0; y < dim2_; y++) {
272  (*this)(x, y) -= minuend(x, y);
273  }
274  }
275  }
276  }
277  // Adds addend to all elements.
278  void operator+=(const T& addend) {
279  int size = num_elements();
280  for (int i = 0; i < size; ++i) {
281  array_[i] += addend;
282  }
283  }
284  // Multiplies *this by factor, element-by-element.
285  void operator*=(const T& factor) {
286  int size = num_elements();
287  for (int i = 0; i < size; ++i) {
288  array_[i] *= factor;
289  }
290  }
291  // Clips *this to the given range.
292  void Clip(const T& rangemin, const T& rangemax) {
293  int size = num_elements();
294  for (int i = 0; i < size; ++i) {
295  array_[i] = ClipToRange(array_[i], rangemin, rangemax);
296  }
297  }
298  // Returns true if all elements of *this are within the given range.
299  // Only uses operator<
300  bool WithinBounds(const T& rangemin, const T& rangemax) const {
301  int size = num_elements();
302  for (int i = 0; i < size; ++i) {
303  const T& value = array_[i];
304  if (value < rangemin || rangemax < value)
305  return false;
306  }
307  return true;
308  }
309  // Normalize the whole array.
310  double Normalize() {
311  int size = num_elements();
312  if (size <= 0) return 0.0;
313  // Compute the mean.
314  double mean = 0.0;
315  for (int i = 0; i < size; ++i) {
316  mean += array_[i];
317  }
318  mean /= size;
319  // Subtract the mean and compute the standard deviation.
320  double sd = 0.0;
321  for (int i = 0; i < size; ++i) {
322  double normed = array_[i] - mean;
323  array_[i] = normed;
324  sd += normed * normed;
325  }
326  sd = sqrt(sd / size);
327  if (sd > 0.0) {
328  // Divide by the sd.
329  for (int i = 0; i < size; ++i) {
330  array_[i] /= sd;
331  }
332  }
333  return sd;
334  }
335 
336  // Returns the maximum value of the array.
337  T Max() const {
338  int size = num_elements();
339  if (size <= 0) return empty_;
340  // Compute the max.
341  T max_value = array_[0];
342  for (int i = 1; i < size; ++i) {
343  const T& value = array_[i];
344  if (value > max_value) max_value = value;
345  }
346  return max_value;
347  }
348 
349  // Returns the maximum absolute value of the array.
350  T MaxAbs() const {
351  int size = num_elements();
352  if (size <= 0) return empty_;
353  // Compute the max.
354  T max_abs = static_cast<T>(0);
355  for (int i = 0; i < size; ++i) {
356  T value = static_cast<T>(fabs(array_[i]));
357  if (value > max_abs) max_abs = value;
358  }
359  return max_abs;
360  }
361 
362  // Accumulates the element-wise sums of squares of src into *this.
363  void SumSquares(const GENERIC_2D_ARRAY<T>& src) {
364  int size = num_elements();
365  for (int i = 0; i < size; ++i) {
366  array_[i] += src.array_[i] * src.array_[i];
367  }
368  }
369 
370  // Scales each element using the ada-grad algorithm, ie array_[i] by
371  // sqrt(num_samples/max(1,sqsum[i])).
372  void AdaGradScaling(const GENERIC_2D_ARRAY<T>& sqsum, int num_samples) {
373  int size = num_elements();
374  for (int i = 0; i < size; ++i) {
375  array_[i] *= sqrt(num_samples / MAX(1.0, sqsum.array_[i]));
376  }
377  }
378 
379  void AssertFinite() const {
380  int size = num_elements();
381  for (int i = 0; i < size; ++i) {
382  ASSERT_HOST(isfinite(array_[i]));
383  }
384  }
385 
386  // REGARDLESS OF THE CURRENT DIMENSIONS, treats the data as a
387  // num_dims-dimensional array/tensor with dimensions given by dims, (ordered
388  // from most significant to least significant, the same as standard C arrays)
389  // and moves src_dim to dest_dim, with the initial dest_dim and any dimensions
390  // in between shifted towards the hole left by src_dim. Example:
391  // Current data content: array_=[0, 1, 2, ....119]
392  // perhaps *this may be of dim[40, 3], with values [[0, 1, 2][3, 4, 5]...
393  // but the current dimensions are irrelevant.
394  // num_dims = 4, dims=[5, 4, 3, 2]
395  // src_dim=3, dest_dim=1
396  // tensor=[[[[0, 1][2, 3][4, 5]]
397  // [[6, 7][8, 9][10, 11]]
398  // [[12, 13][14, 15][16, 17]]
399  // [[18, 19][20, 21][22, 23]]]
400  // [[[24, 25]...
401  // output dims =[5, 2, 4, 3]
402  // output tensor=[[[[0, 2, 4][6, 8, 10][12, 14, 16][18, 20, 22]]
403  // [[1, 3, 5][7, 9, 11][13, 15, 17][19, 21, 23]]]
404  // [[[24, 26, 28]...
405  // which is stored in the array_ as:
406  // [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 1, 3, 5, 7, 9, 11, 13...]
407  // NOTE: the 2 stored matrix dimensions are simply copied from *this. To
408  // change the dimensions after the transpose, use ResizeNoInit.
409  // Higher dimensions above 2 are strictly the responsibility of the caller.
410  void RotatingTranspose(const int* dims, int num_dims, int src_dim,
411  int dest_dim, GENERIC_2D_ARRAY<T>* result) const {
412  int max_d = MAX(src_dim, dest_dim);
413  int min_d = MIN(src_dim, dest_dim);
414  // In a tensor of shape [d0, d1... min_d, ... max_d, ... dn-2, dn-1], the
415  // ends outside of min_d and max_d are unaffected, with [max_d +1, dn-1]
416  // being contiguous blocks of data that will move together, and
417  // [d0, min_d -1] being replicas of the transpose operation.
418  // num_replicas represents the large dimensions unchanged by the operation.
419  // move_size represents the small dimensions unchanged by the operation.
420  // src_step represents the stride in the src between each adjacent group
421  // in the destination.
422  int num_replicas = 1, move_size = 1, src_step = 1;
423  for (int d = 0; d < min_d; ++d) num_replicas *= dims[d];
424  for (int d = max_d + 1; d < num_dims; ++d) move_size *= dims[d];
425  for (int d = src_dim + 1; d < num_dims; ++d) src_step *= dims[d];
426  if (src_dim > dest_dim) src_step *= dims[src_dim];
427  // wrap_size is the size of a single replica, being the amount that is
428  // handled num_replicas times.
429  int wrap_size = move_size;
430  for (int d = min_d; d <= max_d; ++d) wrap_size *= dims[d];
431  result->ResizeNoInit(dim1_, dim2_);
432  result->empty_ = empty_;
433  const T* src = array_;
434  T* dest = result->array_;
435  for (int replica = 0; replica < num_replicas; ++replica) {
436  for (int start = 0; start < src_step; start += move_size) {
437  for (int pos = start; pos < wrap_size; pos += src_step) {
438  memcpy(dest, src + pos, sizeof(*dest) * move_size);
439  dest += move_size;
440  }
441  }
442  src += wrap_size;
443  }
444  }
445 
446  // Delete objects pointed to by array_[i].
448  int size = num_elements();
449  for (int i = 0; i < size; ++i) {
450  T matrix_cell = array_[i];
451  if (matrix_cell != empty_)
452  delete matrix_cell;
453  }
454  }
455 
456  protected:
457  // Factored helper to serialize the size.
458  bool SerializeSize(FILE* fp) const {
459  inT32 size = dim1_;
460  if (fwrite(&size, sizeof(size), 1, fp) != 1) return false;
461  size = dim2_;
462  if (fwrite(&size, sizeof(size), 1, fp) != 1) return false;
463  return true;
464  }
465  bool SerializeSize(tesseract::TFile* fp) const {
466  inT32 size = dim1_;
467  if (fp->FWrite(&size, sizeof(size), 1) != 1) return false;
468  size = dim2_;
469  if (fp->FWrite(&size, sizeof(size), 1) != 1) return false;
470  return true;
471  }
472  // Factored helper to deserialize the size.
473  // If swap is true, assumes a big/little-endian swap is needed.
474  bool DeSerializeSize(bool swap, FILE* fp) {
475  inT32 size1, size2;
476  if (fread(&size1, sizeof(size1), 1, fp) != 1) return false;
477  if (fread(&size2, sizeof(size2), 1, fp) != 1) return false;
478  if (swap) {
479  ReverseN(&size1, sizeof(size1));
480  ReverseN(&size2, sizeof(size2));
481  }
482  Resize(size1, size2, empty_);
483  return true;
484  }
486  inT32 size1, size2;
487  if (fp->FReadEndian(&size1, sizeof(size1), 1) != 1) return false;
488  if (fp->FReadEndian(&size2, sizeof(size2), 1) != 1) return false;
489  Resize(size1, size2, empty_);
490  return true;
491  }
492 
493  T* array_;
494  T empty_; // The unused cell.
495  int dim1_; // Size of the 1st dimension in indexing functions.
496  int dim2_; // Size of the 2nd dimension in indexing functions.
497  // The total size to which the array can be expanded before a realloc is
498  // needed. If Resize is used, memory is retained so it can be re-expanded
499  // without a further alloc, and this stores the allocated size.
501 };
502 
503 // A generic class to store a banded triangular matrix with entries of type T.
504 // In this array, the nominally square matrix is dim1_ x dim1_, and dim2_ is
505 // the number of bands, INCLUDING the diagonal. The storage is thus of size
506 // dim1_ * dim2_ and index(col, row) = col * dim2_ + row - col, and an
507 // assert will fail if row < col or row - col >= dim2.
508 template <class T>
509 class BandTriMatrix : public GENERIC_2D_ARRAY<T> {
510  public:
511  // Allocate a piece of memory to hold a 2d-array of the given dimension.
512  // Initialize all the elements of the array to empty instead of assuming
513  // that a default constructor can be used.
514  BandTriMatrix(int dim1, int dim2, const T& empty)
515  : GENERIC_2D_ARRAY<T>(dim1, dim2, empty) {
516  }
517  // The default destructor will do.
518 
519  // Provide the dimensions of this matrix.
520  // dimension is the size of the nominally square matrix.
521  int dimension() const { return this->dim1_; }
522  // bandwidth is the number of bands in the matrix, INCLUDING the diagonal.
523  int bandwidth() const { return this->dim2_; }
524 
525  // Expression to select a specific location in the matrix. The matrix is
526  // stored COLUMN-major, so the left-most index is the most significant.
527  // This allows [][] access to use indices in the same order as (,).
528  virtual int index(int column, int row) const {
529  ASSERT_HOST(row >= column);
530  ASSERT_HOST(row - column < this->dim2_);
531  return column * this->dim2_ + row - column;
532  }
533 
534  // Appends array2 corner-to-corner to *this, making an array of dimension
535  // equal to the sum of the individual dimensions.
536  // array2 is not destroyed, but is left empty, as all elements are moved
537  // to *this.
539  int new_dim1 = this->dim1_ + array2->dim1_;
540  int new_dim2 = MAX(this->dim2_, array2->dim2_);
541  T* new_array = new T[new_dim1 * new_dim2];
542  for (int col = 0; col < new_dim1; ++col) {
543  for (int j = 0; j < new_dim2; ++j) {
544  int new_index = col * new_dim2 + j;
545  if (col < this->dim1_ && j < this->dim2_) {
546  new_array[new_index] = this->get(col, col + j);
547  } else if (col >= this->dim1_ && j < array2->dim2_) {
548  new_array[new_index] = array2->get(col - this->dim1_,
549  col - this->dim1_ + j);
550  array2->put(col - this->dim1_, col - this->dim1_ + j, NULL);
551  } else {
552  new_array[new_index] = this->empty_;
553  }
554  }
555  }
556  delete[] this->array_;
557  this->array_ = new_array;
558  this->dim1_ = new_dim1;
559  this->dim2_ = new_dim2;
560  }
561 };
562 
563 class MATRIX : public BandTriMatrix<BLOB_CHOICE_LIST *> {
564  public:
565  MATRIX(int dimension, int bandwidth)
566  : BandTriMatrix<BLOB_CHOICE_LIST *>(dimension, bandwidth, NOT_CLASSIFIED) {}
567 
568  // Returns true if there are any real classification results.
569  bool Classified(int col, int row, int wildcard_id) const;
570 
571  // Expands the existing matrix in-place to make the band wider, without
572  // losing any existing data.
573  void IncreaseBandSize(int bandwidth);
574 
575  // Returns a bigger MATRIX with a new column and row in the matrix in order
576  // to split the blob at the given (ind,ind) diagonal location.
577  // Entries are relocated to the new MATRIX using the transformation defined
578  // by MATRIX_COORD::MapForSplit.
579  // Transfers the pointer data to the new MATRIX and deletes *this.
580  MATRIX* ConsumeAndMakeBigger(int ind);
581 
582  // Makes and returns a deep copy of *this, including all the BLOB_CHOICEs
583  // on the lists, but not any LanguageModelState that may be attached to the
584  // BLOB_CHOICEs.
585  MATRIX* DeepCopy() const;
586 
587  // Print a shortened version of the contents of the matrix.
588  void print(const UNICHARSET &unicharset) const;
589 };
590 
591 struct MATRIX_COORD {
592  static void Delete(void *arg) {
593  MATRIX_COORD *c = static_cast<MATRIX_COORD *>(arg);
594  delete c;
595  }
596  // Default constructor required by GenericHeap.
597  MATRIX_COORD() : col(0), row(0) {}
598  MATRIX_COORD(int c, int r): col(c), row(r) {}
600 
601  bool Valid(const MATRIX &m) const {
602  return 0 <= col && col < m.dimension() &&
603  col <= row && row < col + m.bandwidth() && row < m.dimension();
604  }
605 
606  // Remaps the col,row pair to split the blob at the given (ind,ind) diagonal
607  // location.
608  // Entries at (i,j) for i in [0,ind] and j in [ind,dim) move to (i,j+1),
609  // making a new row at ind.
610  // Entries at (i,j) for i in [ind+1,dim) and j in [i,dim) move to (i+i,j+1),
611  // making a new column at ind+1.
612  void MapForSplit(int ind) {
613  ASSERT_HOST(row >= col);
614  if (col > ind) ++col;
615  if (row >= ind) ++row;
616  ASSERT_HOST(row >= col);
617  }
618 
619  int col;
620  int row;
621 };
622 
623 // The MatrixCoordPair contains a MATRIX_COORD and its priority.
625 
626 #endif // TESSERACT_CCSTRUCT_MATRIX_H_
void AdaGradScaling(const GENERIC_2D_ARRAY< T > &sqsum, int num_samples)
Definition: matrix.h:372
void ResizeWithCopy(int size1, int size2)
Definition: matrix.h:105
bool DeSerializeSize(bool swap, FILE *fp)
Definition: matrix.h:474
void Clip(const T &rangemin, const T &rangemax)
Definition: matrix.h:292
void MapForSplit(int ind)
Definition: matrix.h:612
T * operator[](int column)
Definition: matrix.h:238
void Clear()
Definition: matrix.h:129
int size_allocated_
Definition: matrix.h:500
int32_t inT32
Definition: host.h:38
bool SerializeSize(FILE *fp) const
Definition: matrix.h:458
T MaxAbs() const
Definition: matrix.h:350
void AttachOnCorner(BandTriMatrix< T > *array2)
Definition: matrix.h:538
void SumSquares(const GENERIC_2D_ARRAY< T > &src)
Definition: matrix.h:363
void operator*=(const T &factor)
Definition: matrix.h:285
bool DeSerializeClasses(bool swap, FILE *fp)
Definition: matrix.h:190
voidpf void uLong size
Definition: ioapi.h:39
bool WithinBounds(const T &rangemin, const T &rangemax) const
Definition: matrix.h:300
void operator+=(const GENERIC_2D_ARRAY< T > &addend)
Definition: matrix.h:246
GENERIC_2D_ARRAY(int dim1, int dim2, const T &empty, T *array)
Definition: matrix.h:53
T get(ICOORD pos) const
Definition: matrix.h:223
inT16 x() const
access function
Definition: points.h:52
double Normalize()
Definition: matrix.h:310
MATRIX_COORD(int c, int r)
Definition: matrix.h:598
void Resize(int size1, int size2, const T &empty)
Definition: matrix.h:98
int FReadEndian(void *buffer, int size, int count)
Definition: serialis.cpp:97
bool Serialize(tesseract::TFile *fp) const
Definition: matrix.h:144
virtual int num_elements() const
Definition: matrix.h:205
#define NOT_CLASSIFIED
Definition: matrix.h:41
const T & operator()(int column, int row) const
Definition: matrix.h:230
BandTriMatrix(int dim1, int dim2, const T &empty)
Definition: matrix.h:514
#define ASSERT_HOST(x)
Definition: errcode.h:84
GENERIC_2D_ARRAY(int dim1, int dim2, const T &empty)
Definition: matrix.h:59
void operator+=(const T &addend)
Definition: matrix.h:278
int dim1() const
Definition: matrix.h:201
static void Delete(void *arg)
Definition: matrix.h:592
bool SerializeClasses(FILE *fp) const
Definition: matrix.h:177
bool DeSerializeSize(tesseract::TFile *fp)
Definition: matrix.h:485
MATRIX_COORD()
Definition: matrix.h:597
bool Serialize(FILE *fp) const
Definition: matrix.h:137
int dim2() const
Definition: matrix.h:202
virtual ~GENERIC_2D_ARRAY()
Definition: matrix.h:77
bool DeSerialize(bool swap, FILE *fp)
Definition: matrix.h:155
inT16 y() const
access_function
Definition: points.h:56
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:122
void operator-=(const GENERIC_2D_ARRAY< T > &minuend)
Definition: matrix.h:262
int bandwidth() const
Definition: matrix.h:523
virtual int index(int column, int row) const
Definition: matrix.h:210
bool DeSerialize(tesseract::TFile *fp)
Definition: matrix.h:167
int FWrite(const void *buffer, int size, int count)
Definition: serialis.cpp:148
#define MAX(x, y)
Definition: ndminx.h:24
virtual int index(int column, int row) const
Definition: matrix.h:528
~MATRIX_COORD()
Definition: matrix.h:599
void ResizeNoInit(int size1, int size2)
Definition: matrix.h:86
void RotatingTranspose(const int *dims, int num_dims, int src_dim, int dest_dim, GENERIC_2D_ARRAY< T > *result) const
Definition: matrix.h:410
bool SerializeSize(tesseract::TFile *fp) const
Definition: matrix.h:465
int dimension() const
Definition: matrix.h:521
void put(ICOORD pos, const T &thing)
Definition: matrix.h:215
#define MIN(x, y)
Definition: ndminx.h:28
Definition: matrix.h:563
const T * operator[](int column) const
Definition: matrix.h:241
T Max() const
Definition: matrix.h:337
bool Valid(const MATRIX &m) const
Definition: matrix.h:601
MATRIX(int dimension, int bandwidth)
Definition: matrix.h:565
GENERIC_2D_ARRAY()
Definition: matrix.h:68
void delete_matrix_pointers()
Definition: matrix.h:447
T & operator()(int column, int row)
Definition: matrix.h:233
tesseract::KDPairInc< float, MATRIX_COORD > MatrixCoordPair
Definition: matrix.h:624
void operator=(const GENERIC_2D_ARRAY< T > &src)
Definition: matrix.h:79
GENERIC_2D_ARRAY(const GENERIC_2D_ARRAY< T > &src)
Definition: matrix.h:72
void AssertFinite() const
Definition: matrix.h:379
integer coordinate
Definition: points.h:30
void put(int column, int row, const T &thing)
Definition: matrix.h:218
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:184