diff options
author | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-02-24 01:49:02 +0000 |
---|---|---|
committer | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-02-24 01:49:02 +0000 |
commit | 5de3dd4762ca33a0f92e79ffa4fe2ff67069d531 (patch) | |
tree | bad482b7afa4cdf47422d60a5dd2c61c7e333b09 /src/electronics/simulation/vec.cpp | |
download | ktechlab-5de3dd4762ca33a0f92e79ffa4fe2ff67069d531.tar.gz ktechlab-5de3dd4762ca33a0f92e79ffa4fe2ff67069d531.zip |
Added KDE3 version of ktechlab
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/ktechlab@1095338 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'src/electronics/simulation/vec.cpp')
-rw-r--r-- | src/electronics/simulation/vec.cpp | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/src/electronics/simulation/vec.cpp b/src/electronics/simulation/vec.cpp new file mode 100644 index 0000000..d45f505 --- /dev/null +++ b/src/electronics/simulation/vec.cpp @@ -0,0 +1,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; +} + + + |