summaryrefslogtreecommitdiffstats
path: root/src/electronics/simulation/vec.cpp
blob: d45f5050307c4f75bf83ae617aa25f88a99b458e (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
/***************************************************************************
 *   Copyright (C) 2003-2004 by David Saxton                               *
 *   david@bluehaze.org                                                    *
 *                                                                         *
 *   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 "vec.h"

#include <assert.h>
#include <cmath>
#include <string.h>
using namespace std;

Vector::Vector( const int size )
{
	m_size = size;
	m_vec = new double[m_size];
	reset();
	b_changed = true;
}


Vector::~Vector()
{
	// Hmm...this looks like it's the correct format, although valgrind complains
	// about improper memory use. Interesting. "delete m_vec" definitely leaks
	// memory, so this seems like the lesser of two evils. I miss C memory allocation
	// somtimes, with a nice simple free :p
	delete [] m_vec;
}


void Vector::reset()
{
	for ( int i=0; i<m_size; i++ )
	{
		m_vec[i] = 0.;
	}
	b_changed = true;
}


void Vector::limitTo( double scaleMax, Vector * limitVector )
{
	assert( limitVector );
	assert( limitVector->size() == size() );
	
	for ( int i = 0; i < m_size; ++i )
	{
		double limitAbs = std::abs( limitVector->m_vec[i] );
		if ( limitAbs < 1e-6 )
			limitAbs = 1e-6;
		
		double thisAbs = std::abs( m_vec[i] );
		if ( thisAbs < 1e-6 )
			thisAbs = 1e-6;
		
		if ( (thisAbs / limitAbs) > scaleMax )
			m_vec[i] /= (thisAbs / limitAbs);
		
		else if ( (limitAbs / thisAbs) > scaleMax )
			m_vec[i] /= (limitAbs / thisAbs);
	}
	b_changed = true;
}


void Vector::operator += ( Vector *rhs )
{
	if (!rhs) return;
	for ( int i=0; i<m_size; i++ )
	{
		m_vec[i] += (*rhs)[i];
	}
	b_changed = true;
}


void Vector::operator -= ( Vector *rhs )
{
	if (!rhs) return;
	for ( int i=0; i<m_size; i++ )
	{
		m_vec[i] -= (*rhs)[i];
	}
	b_changed = true;
}


void Vector::operator *=( double s )
{
	for ( int i=0; i<m_size; i++ )
	{
		m_vec[i] *= s;
	}
	b_changed = true;
}

	
void Vector::operator = ( Vector& v )
{
	assert( size() == v.size() );
	memcpy( m_vec, v.m_vec, m_size * sizeof( double ) );
	b_changed = true;
}


void Vector::negative( Vector *rhs )
{
	if (!rhs) return;
	for ( int i=0; i<m_size; i++ )
	{
		m_vec[i] = -(*rhs)[i];
	}
	b_changed = true;
}


double Vector::abs() const
{
	double s=0;
	for ( int i=0; i<m_size; i++ )
	{
		s += m_vec[i]*m_vec[i];
	}
	return sqrt(s);
}



// matrix stuff...

matrix::matrix( const uint size )
{
	m_size = size;
	m_mat = new Vector*[m_size];
	for ( uint i=0; i<m_size; ++i )
	{
		m_mat[i] = new Vector(m_size);
	}
}

matrix::~matrix()
{
	for ( uint i=0; i<m_size; ++i )
	{
		delete m_mat[i];
	}
	delete [] m_mat;
}


void matrix::swapRows( const uint a, const uint b )
{
	if ( a == b ) return;
	Vector *v = m_mat[a];
	m_mat[a] = m_mat[b];
	m_mat[b] = v;
}