summaryrefslogtreecommitdiffstats
path: root/src/electronics/simulation/matrix.cpp
blob: fb1248fc7e030d58175951df4fbf0e7b74c349d3 (plain)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/***************************************************************************
 *   Copyright (C) 2003-2004 by David Saxton                               *
 *   [email protected]                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "matrix.h"

#include <kdebug.h>

#include <assert.h>

#include <cmath>
#include <iostream>
#include <vector>

/// Minimum value before an entry is deemed "zero"
const double epsilon = 1e-50;

Matrix::Matrix( uint n, uint m )
{
	m_n = n;
	m_m = m;
	m_size = m_n+m_m;
	
	m_mat = new matrix(m_size);
	m_lu = new matrix(m_size);
	m_y = new double[m_size];
	m_inMap = new int[m_size];
// 	m_outMap = new int[m_size];
	m_map = new Map(m_size);
	zero();
}


Matrix::~Matrix()
{
	delete m_map;
	delete m_mat;
	delete m_lu;
	delete [] m_y;
	delete [] m_inMap;
// 	delete [] m_outMap;
}


void Matrix::zero()
{
	for ( uint i=0; i<m_size; i++ )
	{
		for ( uint j=0; j<m_size; j++ )
		{
			(*m_mat)[i][j] = 0.;
			(*m_lu)[i][j] = 0.;
		}
		m_inMap[i] = i;
// 		m_outMap[i] = i;
	}
	
	max_k = 0;
}


void Matrix::setUse( const uint i, const uint j, Map::e_type type, bool big )
{
	m_map->setUse( i, j, type, big );
}


void Matrix::createMap()
{
	int newMap[m_size];
	m_map->createMap(newMap);
	for ( uint i=0; i<m_size; i++ )
	{
		const int nu = newMap[i];
		if ( nu != m_inMap[i] )
		{
			int old = -1;
			for ( uint j=0; j<m_size && old == -1; j++ )
			{
				if ( m_inMap[j] == nu ) {
					old = j;
				}
			}
			assert( old != -1 );
			swapRows( old, i );
		}
	}
}


void Matrix::swapRows( const uint a, const uint b )
{
	if ( a == b ) return;
	m_mat->swapRows( a, b );
	
	const int old = m_inMap[a];
	m_inMap[a] = m_inMap[b];
	m_inMap[b] = old;
	
	max_k = 0;
}


/*void Matrix::genOutMap()
{
	for ( uint i=0; i<m_size; i++ )
	{
		m_outMap[ m_inMap[i] ] = i;
	}
}*/


void Matrix::operator=( Matrix *const m )
{
	for ( uint _i=0; _i<m_size; _i++ )
	{
		uint i = m_inMap[_i];
		for ( uint j=0; j<m_size; j++ )
		{
			(*m_mat)[i][j] = m->m(i,j);
		}
	}
	
	max_k = 0;
}

void Matrix::operator+=( Matrix *const m )
{
	for ( uint _i=0; _i<m_size; _i++ )
	{
		uint i = m_inMap[_i];
		for ( uint j=0; j<m_size; j++ )
		{
			(*m_mat)[i][j] += m->m(i,j);
		}
	}
	
	max_k = 0;
}

void Matrix::performLU()
{
// 	max_k = 0;
	uint n = m_size;
	if ( n == 0 ) return;
	
	// Copy the affected segment to LU
	for ( uint i=max_k; i<n; i++ )
	{
		for ( uint j=max_k; j<n; j++ )
		{
			(*m_lu)[i][j] = (*m_mat)[i][j];
		}
	}
	
	// LU decompose the matrix, and store result back in matrix
	for ( uint k=0; k<n-1; k++ )
	{
		double * const lu_K_K = &(*m_lu)[k][k];
		if ( std::abs(*lu_K_K) < 1e-10 )
		{
			if ( *lu_K_K < 0. ) *lu_K_K = -1e-10;
			else *lu_K_K = 1e-10;
		}
		for ( uint i=std::max(k,max_k)+1; i<n; i++ )
		{
			(*m_lu)[i][k] /= *lu_K_K;
		}
		for ( uint i=std::max(k,max_k)+1; i<n; i++ )
		{
			const double lu_I_K = (*m_lu)[i][k];
			if ( std::abs(lu_I_K) > 1e-12 )
			{
				for ( uint j=std::max(k,max_k)+1; j<n; j++ )
				{
					(*m_lu)[i][j] -= lu_I_K*(*m_lu)[k][j];
				}
			}
		}
	}
	
	max_k = n;
}

void Matrix::fbSub( Vector* b )
{
	if ( m_size == 0 ) return;
	
	for ( uint i=0; i<m_size; i++ )
	{
		m_y[m_inMap[i]] = (*b)[i];
	}
	
	// Forward substitution
	for ( uint i=1; i<m_size; i++ )
	{
		double sum = 0;
		for ( uint j=0; j<i; j++ )
		{
			sum += (*m_lu)[i][j]*m_y[j];
		}
		m_y[i] -= sum;
	}
	
	// Back substitution
	m_y[m_size-1] /= (*m_lu)[m_size-1][m_size-1];
	for ( int i=m_size-2; i>=0; i-- )
	{
		double sum = 0;
		for ( uint j=i+1; j<m_size; j++ )
		{
			sum += (*m_lu)[i][j]*m_y[j];
		}
		m_y[i] -= sum;
		m_y[i] /= (*m_lu)[i][i];
	}
	
	for ( uint i=0; i<m_size; i++ )
		(*b)[i] = m_y[i];
}


void Matrix::multiply( Vector *rhs, Vector *result )
{
	if ( !rhs || !result ) return;
	result->reset();
	for ( uint _i=0; _i<m_size; _i++ )
	{
		uint i = m_inMap[_i];
		for ( uint j=0; j<m_size; j++ )
		{
			(*result)[_i] += (*m_mat)[i][j] * (*rhs)[j];
		}
	}
}


void Matrix::displayMatrix()
{
	uint n = m_size;
	for ( uint _i=0; _i<n; _i++ )
	{
		uint i = m_inMap[_i];
		for ( uint j=0; j<n; j++ )
		{
			if ( j > 0 && (*m_mat)[i][j] >= 0 ) kdDebug() << "+";
			kdDebug() << (*m_mat)[i][j] << "("<<j<<")";
		}
		kdDebug()  << endl;
	}
}

void Matrix::displayLU()
{
	uint n = m_size;
	for ( uint _i=0; _i<n; _i++ )
	{
		uint i = m_inMap[_i];
// 		uint i = _i;
		for ( uint j=0; j<n; j++ )
		{
			if ( j > 0 && (*m_lu)[i][j] >= 0 ) std::cout << "+";
			std::cout << (*m_lu)[i][j] << "("<<j<<")";
		}
		std::cout << std::endl;
	}
	std::cout << "m_inMap:    ";
	for ( uint i=0; i<n; i++ )
	{
		std::cout << i<<"->"<<m_inMap[i]<<"  ";
	}
	std::cout << std::endl;
	/*cout << "m_outMap:   ";
	for ( uint i=0; i<n; i++ )
	{
		cout << i<<"->"<<m_outMap[i]<<"  ";
	}
	cout << endl;*/
}


Map::Map( const uint size )
{
	m_size = size;
	m_map = new ETMap( m_size );
	reset();
}


Map::~Map()
{
	delete m_map;
}


void Map::reset()
{
	for ( uint i=0; i<m_size; i++ )
	{
		for ( uint j=0; j<m_size; j++ )
		{
			(*m_map)[i][j] = 0;
		}
	}
}


void Map::setUse( const uint i, const uint j, Map::e_type type, bool big )
{
	if ( type == Map::et_none ) {
		(*m_map)[i][j] = Map::et_none;
	} else {
		(*m_map)[i][j] = type | (big)?Map::et_big:0;
	}
}


void Map::createMap( int *map )
{
	assert(map);
	
	// In this function, the passes through that we make want to be done from
	// top left to bottom right, to minimise fill-in
	
	// available[i] is true if an external-row can be mapped to internal-row "i"
	// map[i] gives the internal-row for external-row i
	bool available[m_size];
	for ( uint i=0; i<m_size; i++ )
	{
		available[i] = true;
		map[i] = -1;
	}
	
	// This loop looks through columns and rows to find any swaps that are necessary
	// (e.g. only one matrix-element in that row/column), and if no necessary swaps
	// were found, then it will swap two rows according to criteria given below
	bool badMap = false;
	bool changed;
	do
	{
		changed = false;
		
		// Pass through columns
		int E,N;
		uint highest = 0;
		for ( uint j=0; j<m_size; j++ )
		{
			if ( map[j] == -1 ) // If we haven't mapped this column yet
			{
				int count = 0; // Number of "spare" elements
				int element; // Last element that is "spare", only applicable if count=1
				for ( uint i=0; i<m_size; i++ )
				{
					if ( available[i] && (*m_map)[i][j] )
					{
						count++;
						element = i;
					}
				}
				if ( count == 0 ) {
					badMap = true;
				}
				else if ( count == 1 )
				{
					const uint newType = (*m_map)[element][j];
					if ( typeCmp( newType, highest) )
					{
						E=element;
						N=j;
						highest=newType;
					}
				}
			}
		}
		// Pass through rows
		for ( uint i=0; i<m_size; i++ )
		{
			if ( map[i] == -1 ) // If we haven't mapped this row yet
			{
				int count = 0; // Number of "spare" elements
				int element; // Last element that is "spare", only applicable if count=1
				for ( uint j=0; j<m_size; j++ )
				{
					if ( available[j] && (*m_map)[i][j] )
					{
						count++;
						element = j;
					}
				}
				if ( count == 0 ) {
					badMap = true;
				}
				else if ( count == 1 )
				{
					const uint newType = (*m_map)[i][element];
					if ( typeCmp( newType, highest) )
					{
						E=element;
						N=i;
						highest=newType;
					}
				}
			}
		}
		if (highest)
		{
			available[E] = false;
			map[N] = E;
			changed = true;
		}
		if (!changed)
		{
			int next = -1; // next is the row to mapped to (interally)
			uint j=0;
			
			/// TODO We want to change this search to one that finds a swap, taking into acocunt the priorities given below
			while ( next == -1 && j<m_size )
			{
				if ( available[j] ) next=j;
				j++;
			}
			uint i=0;
			while ( i<m_size && map[i] != -1 ) i++;
			if ( next != -1 && i < m_size )
			{
				available[next] = false;
				map[i] = next;
				changed = true;
			}
		}
	} while (changed);
	
	if (badMap)
	{
// 		cerr << "Map::createMap: unable to create decent mapping; do not trust the matrix, Neo!"<<endl;
	}
	
	for ( int i = 0; i < int(m_size); ++i )
	{
		assert( map[i] >= 0 && map[i] < int(m_size) );
	}
	
	// Ignore this, for now:
	
	// Now, we want to order the matrix, with the following priorities:
	//	(1) How often values change
	//	(2) How few values there are
	//	(3) How large the values are
	// For each value in the column, 
}


bool Map::typeCmp( const uint t1, const uint t2 )
{
	if (!t2) return true;
	if (!t1) return false;
	
	int t1_score = 1;
	if		( t1 | Map::et_constant )	t1_score += 64;
	else if ( t1 | Map::et_stable )		t1_score += 16;
	else if ( t1 | Map::et_variable )	t1_score += 4;
	
	int t2_score = 1;
	if		( t2 | Map::et_constant )	t2_score += 64;
	else if ( t2 | Map::et_stable )		t2_score += 16;
	else if ( t2 | Map::et_variable )	t2_score += 4;
	
	if ( t1 | Map::et_big ) t1_score *= 2;
	if ( t2 | Map::et_big ) t2_score *= 2;
	
	return ( t1_score >= t2_score );
}


Matrix22::Matrix22()
{
	reset();
}

bool Matrix22::solve()
{
	const double old_x1 = m_x1;
	const double old_x2 = m_x2;
	
	const bool e11 = std::abs((m_a11))<epsilon;
	const bool e12 = std::abs((m_a12))<epsilon;
	const bool e21 = std::abs((m_a21))<epsilon;
	const bool e22 = std::abs((m_a22))<epsilon;
	
	if (e11)
	{
		if ( e12||e21 )
			return false;
		m_x2 = m_b1/m_a12;
		m_x1 = (m_b2-(m_a22*m_x2))/m_a21;
	}
	else if (e12)
	{
		if ( e11||e22 )
			return false;
		m_x1 = m_b1/m_a11;
		m_x2 = (m_b2-(m_a21*m_x1))/m_a22;
	}
	else if (e21)
	{
		if ( e11||e22 )
			return false;
		m_x2 = m_b2/m_a22;
		m_x1 = (m_b1-(m_a12*m_x2))/m_a11;
	}
	else if (e22)
	{
		if ( e12||e21 )
			return false;
		m_x1 = m_b2/m_a21;
		m_x2 = (m_b1-(m_a11*m_x1))/m_a12;
	}
	else
	{
		m_x2 = (m_b2-(m_a21*m_b1/m_a11))/(m_a22-(m_a21*m_a12/m_a11));
		m_x1 = (m_b1-(m_a12*m_x2))/m_a11;
	}
	if ( !std::isfinite(m_x1) || !std::isfinite(m_x2) )
	{
		m_x1 = old_x1;
		m_x2 = old_x2;
		return false;
	}
	return true;
}

void Matrix22::reset()
{
	m_a11=m_a12=m_a21=m_a22=0.;
	m_b1=m_b2=0.;
	m_x1=m_x2=0.;
}