File: Matrix.cc

package info (click to toggle)
proda 1.0-12
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 584 kB
  • sloc: cpp: 8,549; xml: 163; makefile: 109; sh: 12
file content (216 lines) | stat: -rw-r--r-- 6,571 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//////////////////////////////////////////////////////////////////////
// Matrix.cc
//////////////////////////////////////////////////////////////////////

#include <string.h>
#include "Assert.h"
#include "Matrix.h"

//////////////////////////////////////////////////////////////////////
// Constructor
//////////////////////////////////////////////////////////////////////

Matrix::Matrix (int layers, int rows, int cols) : 
  layers(layers), rows(rows), cols(cols) {
  
  ASSERT (layers >= 0, "Number of layers in matrix must be positive.");
  ASSERT (rows >= 0, "Number of rows in matrix must be positive.");
  ASSERT (cols >= 0, "Number of columns in matrix must be positive.");
    
  data = new float[layers * rows * cols];
  ASSERT (data, "Out of memory.");
}

//////////////////////////////////////////////////////////////////////
// Copy constructor
//////////////////////////////////////////////////////////////////////

Matrix::Matrix (const Matrix &m) :
  layers (m.layers), rows(m.rows), cols(m.cols) {
  
  ASSERT (layers >= 0, "Number of layers in matrix must be positive.");
  ASSERT (rows >= 0, "Number of rows in matrix must be positive.");
  ASSERT (cols >= 0, "Number of columns in matrix must be positive.");
    
  data = new float[layers * rows * cols];
  ASSERT (data, "Out of memory.");

  memcpy (data, m.data, sizeof(float) * (layers * rows * cols));
}

//////////////////////////////////////////////////////////////////////
// Copy constructor (using ScoreMatrix)
//////////////////////////////////////////////////////////////////////

Matrix::Matrix (const ScoreMatrix &m) :
  layers (m.layers), rows(m.rows), cols(m.cols) {
  
  ASSERT (layers >= 0, "Number of layers in matrix must be positive.");
  ASSERT (rows >= 0, "Number of rows in matrix must be positive.");
  ASSERT (cols >= 0, "Number of columns in matrix must be positive.");
    
  data = new float[layers * rows * cols];
  ASSERT (data, "Out of memory.");

  for (int i = 0; i < layers * rows * cols; i++)
    data[i] = EXP_FLOAT(TO_FLOAT(m.data[i]));
}

//////////////////////////////////////////////////////////////////////
// Constructor (using SparseMatrix)
//////////////////////////////////////////////////////////////////////

Matrix::Matrix (const SparseMatrix &sm) : 
  layers (sm.layers), rows (sm.rows), cols (sm.cols){

  ASSERT (layers >= 0, "Number of layers in matrix must be positive.");
  ASSERT (rows >= 0, "Number of rows in matrix must be positive.");
  ASSERT (cols >= 0, "Number of columns in matrix must be positive.");
    
  data = new float[layers * rows * cols];
  ASSERT (data, "Out of memory.");

  for (int i = 0; i < layers * rows * cols; i++)
    data[i] = sm.missing;

  {for (int i = 0; i < layers; i++){
    float *dataPtr = data + i;
    for (int j = 0; j < rows; j++){
      for (int k = 0; k < sm.rowSize[i * rows + j]; k++){
	SparseMatrix::SparseMatrixEntry *smCell = &sm.rowPtrs[i * rows + j][k];
	dataPtr[layers * smCell->column] = smCell->value;
      }
      dataPtr += cols * layers;
    }
  }}
}

//////////////////////////////////////////////////////////////////////
// Destructor
//////////////////////////////////////////////////////////////////////

Matrix::~Matrix (){
  delete[] data;
}

//////////////////////////////////////////////////////////////////////
// Fill all matrix values
//////////////////////////////////////////////////////////////////////

void Matrix::Fill (const float &value){
  for (int i = 0; i < layers * rows * cols; i++)
    data[i] = value;
}

//////////////////////////////////////////////////////////////////////
// Printing utility function
//////////////////////////////////////////////////////////////////////

void Matrix::PrintVal (FILE *file, const float &value) const {
  if (value == LOG_ZERO_FLOAT)
    fprintf (file, "\t-inf");
  else
    fprintf (file, "\t%d", (int)value);
}

//////////////////////////////////////////////////////////////////////
// Print a single matrix layer
//////////////////////////////////////////////////////////////////////

void Matrix::PrintLayer (FILE *file, int layer) const {
	int i, j;
	for (j = 0; j < cols; j++)
		fprintf (file,"eee\t");
	fprintf(file, "\n");
  for (i = 0; i < rows; i++){
    //fprintf (file, "%s[", (i == 0 ? "[" : " "));
	  fprintf (file, "first");
    for (j = 0; j < cols; j++){
      //if (j > 0) fprintf (file, ", ");
		if (j > 0) fprintf (file, "\t");
      PrintVal (file, operator()(layer,i,j));
    }
    //fprintf (file, "]%s\n", (i == rows-1 ? "]" : ""));
	fprintf(file, "\n");
  }
}

//////////////////////////////////////////////////////////////////////
// Print all matrix layers
//////////////////////////////////////////////////////////////////////

void Matrix::Print (FILE *file) const {
  for (int i = 0; i < layers; i++)
    PrintLayer (file, i);
}

//////////////////////////////////////////////////////////////////////
// Compute sum of all entries in matrix
//////////////////////////////////////////////////////////////////////

float Matrix::ComputeSum() const {
  float total = 0;
  for (int i = 0; i < layers * rows * cols; i++)
    total += data[i];
  return total;
}

//////////////////////////////////////////////////////////////////////
// Compute sum of a row in matrix
//////////////////////////////////////////////////////////////////////

float Matrix::SumOfRow(int layer, int row) const
{
	float max = 0;
	int pos = 0;
	float *ptr;

	ptr = (float *)GetPtr(layer, row, 0);
	for(int i = 0; i < cols; i++){
		if(max < operator()(layer,row,i)){
			max = operator()(layer,row,i);
			pos = i;
		}
	
	}
	return max;
}

//////////////////////////////////////////////////////////////////////
// Compute sum of a column in matrix
//////////////////////////////////////////////////////////////////////

float Matrix::SumOfColumn(int layer, int column) const
{
	float sum = 0;
	int pos = 0;
	for (int i = 0; i < rows; i++){
		
		if(sum < operator()(layer, i, column)){
			sum = operator()(layer, i, column);
			pos = i;
		}
	}

	return sum;
}


void Matrix::PrintRange(FILE *file, int layer, int beginy, int endy, int beginx, int endx)
{
	ASSERT(beginx > 0 && endx < cols && beginy > 0 && endy < rows, "Out of range in PrintRange");
	fprintf(file, " \t");
	for (int k = beginx; k <= endx; k++)
		fprintf(file, "\t%d", k);
	fprintf (file, "\n");
	for (int i = beginy; i <= endy; i++){
		fprintf(file, "\t%d ", i);
    for (int j = beginx; j <= endx; j++){
      //if (j > beginx) fprintf (file, ", ");
      PrintVal (file, operator()(layer,i,j));
    }
    fprintf (file, "\n");
  }
  fprintf (file, "\n");

}