forked from jpcsousa/codejam
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.cpp
More file actions
250 lines (223 loc) · 5.93 KB
/
Matrix.cpp
File metadata and controls
250 lines (223 loc) · 5.93 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
/**
* Matrix class
* by igor
*
* Requires the template parameter to support all of these:
* - constructor T( int )
* - constructor T( double )
* - operator=( T, const T& )
* - bool operator<( const T&, const T& )
* - T operator-( const T& )
* - T operator-=( const T&, const T& )
* - T operator*( const T&, const T& )
* - T operator/( const T&, const T& )
* - operator>>( istream, T )
* - operator<<( ostream, const T& )
*
* Has
* - LU-decomposition: Matrix::lu()
* - Determinant: Matrix::det()
*
* This file is part of my library of algorithms found here:
* http://shygypsy.com/tools/
* LICENSE:
* http://shygypsy.com/tools/LICENSE.html
* Copyright (c) 2003
* Contact author:
* abednego at gmail.com
**/
#include <iostream>
template< class T >
class Matrix
{
private:
int m, n;
T *data;
T *LU;
int *P, Psgn;
// What is considered a zero
static const T EPS = T( 0.0000001 );
inline T &at( T *v, int i, int j ) { return v[i * n + j]; }
inline T &at( int i, int j ) { return data[i * n + j]; }
inline T myabs( const T &t ) { return( t < T( 0 ) ? -t : t ); }
public:
Matrix( int m, int n );
Matrix( int m, int n, T deflt );
Matrix( const Matrix< T > &mtx );
const Matrix< T > &operator=( const Matrix< T > &mtx );
~Matrix();
T *operator[]( int i );
/**
* The PLU-decomposition is stored in two companion arrays
* P and LU. They are allocated by lu() and become obsolete
* whenever the original matrix is modified. It is the user's
* responsibility to call lu() again when that happens. The
* () operator is used to access the LU matrix, just like []
* is used for the matrix data. p() accesses the permutation
* vector P.
**/
void lu();
T &operator()( int i, int j );
T &p( int i );
// I/O Friends
friend istream &operator>><>( istream &in, Matrix< T > &mtx );
friend ostream &operator<<<>( ostream &out, const Matrix< T > &mtx );
// Other friends
friend T det<>( Matrix< T > &mtx );
// DEBUG
void printLU()
{
for( int i = 0; i < m; i++ )
{
for( int j = 0; j < n; j++ )
{
if( j ) cout << " ";
cout << at( LU, i, j );
}
cout << endl;
}
}
};
template< class T > istream &operator>>( istream &in, Matrix< T > &mtx );
template< class T > ostream &operator<<( ostream &out, const Matrix< T > &mtx );
template< class T > T det( Matrix< T > &mtx );
//------------------ Implementation ---------------------//
template< class T >
Matrix< T >::Matrix( int m, int n )
{
this->m = m;
this->n = n;
data = new T[m * n];
LU = NULL;
P = NULL;
}
template< class T >
Matrix< T >::Matrix( int m, int n, T deflt ) : Matrix( m, n )
{
for( int i = 0; i < m * n; i++ )
data[i] = deflt;
}
template< class T > Matrix< T >::Matrix( const Matrix< T > &mtx ) : Matrix( mtx.m, mtx.n )
{
for( int i = 0; i < m * n; i++ )
data[i] = mtx.data[i];
}
template< class T >
const Matrix< T > &Matrix< T >::operator=( const Matrix< T > &mtx )
{
if( &mtx != this )
{
delete [] data;
m = mtx.m;
n = mtx.n;
data = new T[m * n];
for( int i = 0; i < m * n; i++ )
data[i] = mtx.data[i];
}
return *this;
}
template< class T >
Matrix< T >::~Matrix()
{
delete [] data;
}
template< class T >
T *Matrix< T >::operator[]( int i )
{
return data + i * n;
}
template< class T >
void Matrix< T >::lu()
{
if( LU ) delete [] LU;
if( P ) delete [] P;
LU = new T[m * n];
P = new int[m];
memcpy( LU, data, m * n * sizeof( T ) );
for( int i = 0; i < m; i++ ) P[i] = i;
Psgn = 1;
for( int r = 0, c = 0; r < m && c < n; r++, c++ ) // For each row
{
// Find largest pivot in this column
int pr = r;
for( int i = r + 1; i < m; i++ )
if( myabs( at( LU, i, c ) ) > myabs( at( LU, pr, c ) ) )
pr = i;
if( myabs( at( LU, pr, c ) ) <= EPS )
{
// Singular matrix; skip column
r--;
continue;
}
if( pr != r )
{
// Swap rows r and pr
P[r] ^= P[pr] ^= P[r] ^= P[pr];
Psgn = -Psgn;
for( int i = 0; i < n; i++ )
{
T tmp = at( LU, r, i );
at( LU, r, i ) = at( LU, pr, i );
at( LU, pr, i ) = tmp;
}
}
// Subtract row r from rows below it
for( int s = r + 1; s < m; s++ )
{
at( LU, s, c ) = at( LU, s, c ) / at( LU, r, c );
for( int d = c + 1; d < n; d++ )
at( LU, s, d ) -= at( LU, s, c ) * at( LU, r, d );
}
}
}
template< class T >
T &Matrix< T >::operator()( int i, int j )
{
return LU[i * n + j];
}
template< class T >
T &Matrix< T >::p( int i )
{
return P[i];
}
template< class T >
istream &operator>>( istream &in, Matrix< T > &mtx )
{
for( int i = 0; i < mtx.m * mtx.n; i++ )
in >> mtx.data[i];
return in;
}
template< class T >
ostream &operator<<( ostream &out, const Matrix< T > &mtx )
{
for( int i = 0; i < mtx.m; i++ )
{
for( int j = 0; j < mtx.n; j++ )
{
if( j ) out << " ";
out << mtx.at( i, j );
}
out << endl;
}
return out;
}
template< class T >
T det( Matrix< T > &mtx )
{
if( mtx.m != mtx.n ) throw "Not a square matrix";
mtx.lu();
T ans = 1;
for( int i = 0; i < mtx.n; i++ )
ans *= mtx.at( mtx.LU, i, i );
return( mtx.Psgn > 0 ? ans : -ans );
}
//------------------- DEBUG and Testing ------------------//
int main()
{
int x, y;
cin >> x >> y;
Matrix< double > m( x, y );
cin >> m;
cout << "det(M) = " << det( m ) << endl;
return 0;
}