File: Matrix.h

package info (click to toggle)
proda 1.0-12
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 584 kB
  • sloc: cpp: 8,549; xml: 163; makefile: 109; sh: 12
file content (121 lines) | stat: -rw-r--r-- 3,750 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//////////////////////////////////////////////////////////////////////
// Matrix.h
//
// Matrix storage class for storing a set of two-dimensional arrays.
//////////////////////////////////////////////////////////////////////

#ifndef MATRIX_H
#define MATRIX_H

#include <stdio.h>
#include "Score.h"
#include "SparseMatrix.h"
#include "ScoreMatrix.h"

//////////////////////////////////////////////////////////////////////
// Matrix object
//////////////////////////////////////////////////////////////////////

class SparseMatrix;

class Matrix {
  friend class SparseMatrix;

  int layers;
  int rows;
  int cols;

  float *data;
  
  // printing utility function
  void PrintVal (FILE *file, const float &value) const;
  void PrintRange(FILE *file, int layer, int beginy,int endy,int beginx,int endx);

 public:
	 
  // constructors and destructor
  Matrix (int layers, int rows, int cols);
  Matrix (const Matrix &m);
  Matrix (const ScoreMatrix &m);
  Matrix (const SparseMatrix &sm);
  ~Matrix ();

  // fill all entries with value
  void Fill (const float &value);

  // printing functions
  void PrintLayer (FILE *file, int layer) const;
  void Print (FILE *file) const;

  // compute sum of all entries
  float ComputeSum() const;

  //Computing sum of a row and a column
  float SumOfColumn(int layer, int column) const;
  float SumOfRow(int layer, int row) const;


  //////////////////////////////////////////////////////////////////////
  // Access matrix element
  //////////////////////////////////////////////////////////////////////
  
  float &operator() (int layer, int row, int col){
    ASSERT (0 <= layer && layer < layers, "Requested layer out-of-bounds.");
    ASSERT (0 <= row && row < rows, "Requested row out-of-bounds.");
    ASSERT (0 <= col && col < cols, "Requested column out-of-bounds.");
    return data[(row * cols + col) * layers + layer];
  }

  //////////////////////////////////////////////////////////////////////
  // Access matrix element (const version)
  //////////////////////////////////////////////////////////////////////
  
  const float &operator() (int layer, int row, int col) const {
    ASSERT (0 <= layer && layer < layers, "Requested layer out-of-bounds.");
    ASSERT (0 <= row && row < rows, "Requested row out-of-bounds.");
    ASSERT (0 <= col && col < cols, "Requested column out-of-bounds.");
    return data[(row * cols + col) * layers + layer];
  }
    
  //////////////////////////////////////////////////////////////////////
  // Access matrix element
  //////////////////////////////////////////////////////////////////////
  
  float *GetPtr (int layer, int row, int col){
    return data + (row * cols + col) * layers + layer;
  }

  //////////////////////////////////////////////////////////////////////
  // Access matrix element (const version)
  //////////////////////////////////////////////////////////////////////
  
  const float *GetPtr (int layer, int row, int col) const {
    return data + (row * cols + col) * layers + layer;
  }

  //////////////////////////////////////////////////////////////////////
  // Return number of matrix layers
  //////////////////////////////////////////////////////////////////////
  
  const int GetNumLayers() const {
    return layers;
  }
  
  //////////////////////////////////////////////////////////////////////
  // Return number of matrix rows
  //////////////////////////////////////////////////////////////////////
  
  const int GetNumRows() const {
    return rows;
  }
  
  //////////////////////////////////////////////////////////////////////
  // Return number of matrix columns
  //////////////////////////////////////////////////////////////////////
  
  const int GetNumCols() const {
    return cols;
  }
};

#endif