vector.cpp 3.14 KB
Newer Older
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
/*
* KmPlot - a math. function plotter for the KDE-Desktop
*
* Copyright (C) 2006  David Saxton <david@bluehaze.org>
*               
* This file is part of the KDE Project.
* KmPlot is part of the KDE-EDU Project.
*
* 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.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/

#include "vector.h"
Peter Hedlund's avatar
Peter Hedlund committed
26
#include "function.h"
27 28

#include <assert.h>
29
#include <string.h>
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

//BEGIN class Vector
Vector Vector::operator + ( const Vector & other ) const
{
	Vector v(*this);
	v += other;
	return v;
}


Vector & Vector::operator += ( const Vector & other )
{
	assert( size() == other.size() );
	for ( int i=0; i<size(); ++i )
		(*this)[i] += other[i];
	
	return *this;
}


Vector Vector::operator - ( const Vector & other ) const
{
	Vector v(*this);
	v -= other;
	return v;
}


Vector & Vector::operator -= ( const Vector & other )
{
	assert( size() == other.size() );
	for ( int i=0; i<size(); ++i )
		(*this)[i] -= other[i];
	
	return *this;
}


Vector Vector::operator * ( double x ) const
{
	Vector v(*this);
	v *= x;
	return v;
}


Vector & Vector::operator *= ( double x )
{
	for ( int i=0; i<size(); ++i )
		(*this)[i] *= x;
	return *this;
}


84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
Vector & Vector::operator = ( const Vector & other )
{
	// I don't much like Qt's copy-on-write behaviour.
	// At least in KmPlot, it makes a small number of cases marginally faster,
	// and a lot of cases a lot slower. This is because allocating and freeing
	// memory is a lot slower than copying memory.
	// 
	// In fact, it can make drawing differential equations several times slower.
	
	resize( other.size() );
	memcpy( m_data.data(), other.m_data.data(), size() * sizeof(double) );
	
	return *this;
}


100 101 102 103 104 105 106 107
Vector & Vector::operator = ( const QVector<Value> & other )
{
	int size = other.size();
	resize( size );
	for ( int i = 0; i < size; ++i )
		(*this)[i] = other[i].value();
	return *this;
}
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


void Vector::combine( const Vector & a, double k, const Vector & b )
{
	assert( a.size() == b.size() );
	int n = a.size();
	resize( n );
	
	double *d1 = m_data.data();
	const double *d2 = a.m_data.data();
	const double *d3 = b.m_data.data();
	
	for ( int i = 0; i < n; ++i )
		d1[i] = d2[i] + k * d3[i];
}



void Vector::addRK4( double dx, const Vector & k1, const Vector & k2, const Vector & k3, const Vector & k4 )
{
	double *d = m_data.data();
	const double *d1 = k1.m_data.data();
	const double *d2 = k2.m_data.data();
	const double *d3 = k3.m_data.data();
	const double *d4 = k4.m_data.data();
	
	int n = size();
	
	for ( int i = 0; i < n; ++i )
		d[i] += (dx/6) * (d1[i] + 2*d2[i] + 2*d3[i] + d4[i]);
}
139
//END class Vector