[1442bb]: SimpleMatrix.c  Maximize  Restore  History

Download this file

289 lines (236 with data), 6.8 kB

  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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* autopano-sift, Automatic panorama image creation
* Copyright (C) 2004 -- Sebastian Nowozin
*
* This program is free software released under the GNU General Public
* License, which is included in this software package (doc/LICENSE).
*/
/* SimpleMatrix.cs
*
* Minimal two-dimensional matrix class implementation.
*
* (C) Copyright 2004 -- Sebastian Nowozin (nowozin@cs.tu-berlin.de)
*/
#include "AutoPanoSift.h"
SimpleMatrix* SimpleMatrix_new0 ()
{
SimpleMatrix* self = (SimpleMatrix*)malloc(sizeof(SimpleMatrix));
self->values = NULL;
return self;
}
void SimpleMatrix_delete(SimpleMatrix* self)
{
if (self) {
DoubleMap_delete(self->values);
self->values = NULL;
free(self);
}
}
void SimpleMatrix_init (SimpleMatrix* self, int yDim, int xDim)
{
self->xDim = xDim;
self->yDim = yDim;
self->values = DoubleMap_new(yDim, xDim);
}
SimpleMatrix* SimpleMatrix_new (int yDim, int xDim)
{
SimpleMatrix* self = SimpleMatrix_new0();
self->xDim = xDim;
self->yDim = yDim;
self->values = DoubleMap_new(yDim, xDim);
return self;
}
SimpleMatrix* SimpleMatrix_clone (SimpleMatrix* self)
{
SimpleMatrix* cp = SimpleMatrix_new (self->yDim, self->xDim);
int x, y;
for ( y = 0 ; y < self->yDim ; ++y) {
for ( x = 0 ; x < self->xDim ; ++x) {
cp->values[y][x] = self->values[y][x];
}
}
return (cp);
}
double SimpleMatrix_GetValue(SimpleMatrix* self, int y, int x)
{
return (self->values[y][x]);
}
void SimpleMatrix_SetValue(SimpleMatrix* self, int y, int x, double value )
{
self->values[y][x] = value;
}
SimpleMatrix* SimpleMatrix_Mul(SimpleMatrix* m1, SimpleMatrix* m2)
{
if (m1->xDim != m2->yDim)
FatalError ("Matrixes cannot be multiplied, dimension mismatch");
// vanilla!
SimpleMatrix* res = SimpleMatrix_new (m1->yDim, m2->xDim);
int y;
for ( y = 0 ; y < m1->yDim ; ++y) {
int x;
for ( x = 0 ; x < m2->xDim ; ++x) {
int k;
for ( k = 0 ; k < m2->yDim ; ++k)
res->values[y][x] += m1->values[y][k] * m2->values[k][x];
}
}
return (res);
}
double SimpleMatrix_Dot (SimpleMatrix* self, SimpleMatrix* m)
{
if (self->yDim != m->yDim || self->xDim != 1 || m->xDim != 1)
FatalError
("Dotproduct only possible for two equal n x 1 matrices");
double sum = 0.0;
int y;
for ( y = 0 ; y < self->yDim ; ++y)
sum += self->values[y][0] * m->values[y][0];
return (sum);
}
void SimpleMatrix_Negate (SimpleMatrix* self)
{
int y;
for (y = 0 ; y < self->yDim ; ++y) {
int x;
for ( x = 0 ; x < self->xDim ; ++x) {
self->values[y][x] = -self->values[y][x];
}
}
}
void SimpleMatrix_Inverse (SimpleMatrix* self)
{
if (self->xDim != self->yDim)
FatalError("Matrix x dimension != y dimension");
// Shipley-Coleman inversion, from
// http://www.geocities.com/SiliconValley/Lab/4223/fault/ach03.html
int dim = self->xDim;
int i,j,k;
for ( k = 0 ; k < dim ; ++k) {
self->values[k][k] = - 1.0 / self->values[k][k];
for ( i = 0 ; i < dim ; ++i) {
if (i != k)
self->values[i][k] *= self->values[k][k];
}
for ( i = 0 ; i < dim ; ++i) {
if (i != k) {
for ( j = 0 ; j < dim ; ++j) {
if (j != k)
self->values[i][j] += self->values[i][k] * self->values[k][j];
}
}
}
for ( i = 0 ; i < dim ; ++i) {
if (i != k)
self->values[k][i] *= self->values[k][k];
}
}
for ( i = 0 ; i < dim ; ++i) {
for ( j = 0 ; j < dim ; ++j)
self->values[i][j] = -self->values[i][j];
}
}
// The vector 'vec' is used both for input/output purposes. As input, it
// contains the vector v, and after this method finishes it contains x,
// the solution in the formula
// self * x = v
// This matrix might get row-swapped, too.
void SimpleMatrix_SolveLinear (SimpleMatrix* self, SimpleMatrix* vec)
{
if (self->xDim != self->yDim || self->yDim != vec->yDim)
FatalError ("Matrix not quadratic or vector dimension mismatch");
// Gaussian Elimination Algorithm, as described by
// "Numerical Methods - A Software Approach", R.L. Johnston
// Forward elimination with partial pivoting
int x, y;
for (y = 0 ; y < (self->yDim - 1) ; ++y) {
// Searching for the largest pivot (to get "multipliers < 1.0 to
// minimize round-off errors")
int yMaxIndex = y;
double yMaxValue = abs (self->values[y][y]);
int py;
for (py = y ; py < self->yDim ; ++py) {
if (abs (self->values[py][y]) > yMaxValue) {
yMaxValue = abs (self->values[py][y]);
yMaxIndex = py;
}
}
// if a larger row has been found, swap with the current one
SimpleMatrix_SwapRow (self, y, yMaxIndex);
SimpleMatrix_SwapRow (vec, y, yMaxIndex);
// Now do the elimination left of the diagonal
for ( py = y + 1 ; py < self->yDim ; ++py) {
// always <= 1.0
double elimMul = self->values[py][y] / self->values[y][y];
for ( x = 0 ; x < self->xDim ; ++x)
self->values[py][x] -= elimMul * self->values[y][x];
// FIXME: do we really need this?
vec->values[py][0] -= elimMul * vec->values[y][0];
}
}
// Back substitution
for ( y = self->yDim - 1 ; y >= 0 ; --y) {
double solY = vec->values[y][0];
for ( x = self->xDim - 1 ; x > y ; --x)
solY -= self->values[y][x] * vec->values[x][0];
vec->values[y][0] = solY / self->values[y][y];
}
}
// Swap two rows r1, r2
void SimpleMatrix_SwapRow (SimpleMatrix* self, int r1, int r2)
{
if (r1 == r2)
return;
int x;
for ( x = 0 ; x < self->xDim ; ++x) {
double temp = self->values[r1][x];
self->values[r1][x] = self->values[r2][x];
self->values[r2][x] = temp;
}
}
char* SimpleMatrix_ToString (SimpleMatrix* self)
{
int len = 5 + self->yDim * (3 + self->xDim*15);
char* str = (char*)malloc(len);
char* p = str;
p += sprintf(p, "( ");
int x,y;
for ( y = 0 ; y < self->yDim ; ++y) {
if (y > 0)
p += sprintf(p, "\n ");
for ( x = 0 ; x < self->xDim ; ++x) {
if (x > 0)
p += sprintf(p, " ");
p += sprintf(p, "%3.015g", self->values[y][x]);
}
}
p+= sprintf(p, " )");
if (p > str + len) {
FatalError("SimpleMatrix_toString overflow: len=%d, strlen=%d\n", len, p-str);
}
return str;
}
#ifdef TEST_MAIN
int main(int argc, char* argv[]) {
SimpleMatrix * A = SimpleMatrix_new(3,3);
A->values[0][0] = 1;
A->values[0][1] = 2;
A->values[0][2] = 3;
A->values[1][0] = 4;
A->values[1][1] = 5;
A->values[1][2] = 6;
A->values[2][0] = 7;
A->values[2][1] = 9;
A->values[2][2] = 8;
WriteLine("A=%s", SimpleMatrix_ToString(A));
SimpleMatrix * B = SimpleMatrix_new(3,1);
B->values[0][0] = 1;
B->values[1][0] = 2;
B->values[2][0] = 3;
WriteLine("B=%s", SimpleMatrix_ToString(B));
SimpleMatrix * C = SimpleMatrix_Mul(A,B);
WriteLine("A*B=%s", SimpleMatrix_ToString(C));
SimpleMatrix_delete(A);
SimpleMatrix_delete(B);
SimpleMatrix_delete(C);
return 0;
}
#endif

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks