ACT-CV - Machine Vision for Cognitive Modeling
Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
general
DMatrix.h
Go to the documentation of this file.
1
// -*- mode: c++; indent-tabs-mode: nil; c-basic-offset: 4; coding: iso-8859-1; -*-
2
3
/*
4
ACT-CV - Machine Vision for Cognitive Modeling
5
Copyright (c) 2008 Marc Halbruegge
6
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
11
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
16
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
*/
21
22
30
#ifndef __DMATRIX_H
31
#define __DMATRIX_H
32
33
#include <iostream>
34
#include <cmath>
35
36
template
<
unsigned
int
zeilen,
unsigned
int
spalten>
37
class
DMatrix
;
38
template
<
unsigned
int
dims>
39
double
Len
(
const
DMatrix<dims, 1>
&p);
40
41
43
template
<
unsigned
int
zeilen,
unsigned
int
spalten>
44
class
DMatrix
{
45
public
:
46
double
x
[zeilen][spalten];
47
48
DMatrix
() {}
49
50
explicit
DMatrix
(
double
d) {
51
x
[0][0]=d;
52
}
53
54
explicit
DMatrix
(
double
d0,
double
d1) {
55
x
[0][0]=d0;
56
x
[1][0]=d1;
57
}
58
60
void
Zero
() {
61
for
(
unsigned
int
i=0;i<zeilen;i++)
62
for
(
unsigned
int
j=0;j<spalten;j++)
63
x
[i][j]=0.0;
64
}
65
66
void
Normalize
() {
67
double
len=
Len
(*
this
);
68
if
(len>0.0) *
this
/= len;
69
}
70
71
DMatrix<zeilen,spalten>
&
operator =
(
const
DMatrix<zeilen,spalten>
&m) {
72
for
(
unsigned
int
i=0;i<zeilen;i++)
73
for
(
unsigned
int
j=0;j<spalten;j++)
74
x
[i][j]=m.
x
[i][j];
75
return
*
this
;
76
}
77
78
DMatrix<zeilen,spalten>
&
operator +=
(
const
DMatrix<zeilen,spalten>
&m) {
79
for
(
unsigned
int
i=0;i<zeilen;i++)
80
for
(
unsigned
int
j=0;j<spalten;j++)
81
x
[i][j]+=m.
x
[i][j];
82
return
*
this
;
83
}
84
85
DMatrix<zeilen,spalten>
&
operator -=
(
const
DMatrix<zeilen,spalten>
&m) {
86
for
(
unsigned
int
i=0;i<zeilen;i++)
87
for
(
unsigned
int
j=0;j<spalten;j++)
88
x
[i][j]-=m.
x
[i][j];
89
return
*
this
;
90
}
91
92
DMatrix<zeilen,spalten>
operator *
(
double
d)
const
{
93
DMatrix<zeilen,spalten>
result;
94
for
(
unsigned
int
i=0;i<zeilen;i++)
95
for
(
unsigned
int
j=0;j<spalten;j++)
96
result.
x
[i][j]=
x
[i][j]*d;
97
return
result;
98
}
99
100
101
DMatrix<zeilen,spalten>
&
operator *=
(
double
d) {
102
for
(
unsigned
int
i=0;i<zeilen;i++)
103
for
(
unsigned
int
j=0;j<spalten;j++)
104
x
[i][j]*=d;
105
return
*
this
;
106
}
107
108
DMatrix<zeilen,spalten>
operator /
(
double
d)
const
{
109
DMatrix<zeilen,spalten>
result;
110
for
(
unsigned
int
i=0;i<zeilen;i++)
111
for
(
unsigned
int
j=0;j<spalten;j++)
112
result.
x
[i][j]=
x
[i][j]/d;
113
return
result;
114
}
115
116
DMatrix<zeilen,spalten>
&
operator /=
(
double
d) {
117
for
(
unsigned
int
i=0;i<zeilen;i++)
118
for
(
unsigned
int
j=0;j<spalten;j++)
119
x
[i][j]/=d;
120
return
*
this
;
121
}
122
123
124
DMatrix<zeilen,spalten>
operator +
(
const
DMatrix<zeilen,spalten>
&m)
const
{
125
DMatrix<zeilen,spalten>
result;
126
for
(
unsigned
int
i=0;i<zeilen;i++)
127
for
(
unsigned
int
j=0;j<spalten;j++)
128
result.
x
[i][j]=
x
[i][j]+m.
x
[i][j];
129
return
result;
130
}
131
132
DMatrix<zeilen,spalten>
operator -
(
const
DMatrix<zeilen,spalten>
&m)
const
{
133
DMatrix<zeilen,spalten>
result;
134
for
(
unsigned
int
i=0;i<zeilen;i++)
135
for
(
unsigned
int
j=0;j<spalten;j++)
136
result.
x
[i][j]=
x
[i][j]-m.
x
[i][j];
137
return
result;
138
}
139
140
double
*
operator[]
(
unsigned
int
row) {
141
return
x
[row];
142
}
143
144
#if 1
145
const
double
*
operator[]
(
unsigned
int
row)
const
{
146
return
x
[row];
147
}
148
#endif
149
151
DMatrix<spalten,zeilen>
T
()
const
{
152
DMatrix<spalten,zeilen>
result;
153
for
(
unsigned
int
i=0;i<zeilen;i++)
154
for
(
unsigned
int
j=0;j<spalten;j++)
155
result[j][i] =
x
[i][j];
156
return
result;
157
}
158
};
159
160
// unary minus
161
template
<
unsigned
int
zeilen,
unsigned
int
spalten>
162
DMatrix<zeilen,spalten>
operator -
(
const
DMatrix<zeilen,spalten>
&m) {
163
DMatrix<zeilen,spalten>
result;
164
for
(
unsigned
int
i=0;i<zeilen;i++)
165
for
(
unsigned
int
j=0;j<spalten;j++)
166
result[i][j]=-m[i][j];
167
return
result;
168
}
169
170
template
<
unsigned
int
zeilen,
unsigned
int
spalten>
171
std::ostream& operator << (std::ostream &o, const DMatrix<zeilen,spalten> &m) {
172
for
(
unsigned
int
i=0;i<zeilen;i++) {
173
for
(
unsigned
int
j=0;j<spalten;j++) {
174
o << m[i][j];
175
if
(i+1<zeilen || j+1<spalten) o <<
" "
;
176
}
177
//o << endl;
178
}
179
return
o;
180
}
181
182
template
<
unsigned
int
zeilen,
unsigned
int
spalten>
183
std::istream&
operator >>
(std::istream &in,
DMatrix<zeilen,spalten>
&m) {
184
for
(
unsigned
int
i=0;i<zeilen;i++) {
185
for
(
unsigned
int
j=0;j<spalten;j++) {
186
in >> m.
x
[i][j];
187
}
188
}
189
return
in;
190
}
191
192
template
<
unsigned
int
l,
unsigned
int
m,
unsigned
int
n>
193
DMatrix<l,n>
operator *
(
const
DMatrix<l,m>
&a,
const
DMatrix<m,n>
&b) {
194
DMatrix<l,n>
result;
195
for
(
unsigned
int
i=0;i<l;i++) {
196
for
(
unsigned
int
j=0;j<n;j++) {
197
result.
x
[i][j]=0;
198
for
(
unsigned
int
k=0;k<m;k++){
199
result.
x
[i][j]+=a.
x
[i][k]*b.
x
[k][j];
200
}
201
}
202
}
203
return
result;
204
}
205
206
// vc6 - Workaround
207
template
<
unsigned
int
l,
unsigned
int
m,
unsigned
int
n>
208
DMatrix<l,n>
Multiply
(
const
DMatrix<l,m>
&a,
const
DMatrix<m,n>
&b) {
209
DMatrix<l,n>
result;
210
for
(
unsigned
int
i=0;i<l;i++) {
211
for
(
unsigned
int
j=0;j<n;j++) {
212
result.
x
[i][j]=0;
213
for
(
unsigned
int
k=0;k<m;k++){
214
result.
x
[i][j]+=a.
x
[i][k]*b.
x
[k][j];
215
}
216
}
217
}
218
return
result;
219
}
220
221
222
template
<
unsigned
int
dims>
223
double
Len
(
const
DMatrix<dims, 1>
&p) {
224
double
result=0;
225
for
(
unsigned
int
i=0;i<dims;i++) {
226
result+=p[i][0]*p[i][0];
227
}
228
return
std::sqrt(result);
229
}
230
231
232
template
<
unsigned
int
dims>
233
double
ScalProd
(
const
DMatrix<dims, 1>
&p1,
const
DMatrix<dims, 1>
&p2) {
234
double
result=0;
235
for
(
unsigned
int
i=0;i<dims;i++) {
236
result+=p1[i][0]*p2[i][0];
237
}
238
return
result;
239
}
240
241
242
// for statistiken.h
243
template
<
unsigned
int
zeilen,
unsigned
int
spalten>
244
void
InitZero
(
DMatrix<zeilen,spalten>
&x) {
245
x.
Zero
();
246
};
247
248
249
template
<
class
_t>
class
SquareOperation
;
250
256
template
<
unsigned
int
zeilen>
257
class
SquareOperation
<
DMatrix
<zeilen, 1> > {
258
public
:
259
typedef
DMatrix<zeilen, zeilen>
sq_t
;
260
261
static
sq_t
square
(
const
DMatrix<zeilen, 1>
&x) {
262
return
x * x.
T
();
263
}
264
};
265
266
#endif
ACT-CV - Machine Vision for Cognitive Modeling
© 2015
Marc Halbruegge
(actcvlibrary@googlemail.com)