Computational Physics - Department of Physics

(Axel Boer) #1

78 3 Numerical differentiation and interpolation


v5(1) = 2.5;// or alternatively v5[1] = 2.5;
v5(2) = 1.0;// or alternatively v5[2] = 1.0;


// Extract the ith component of a vector:
inti = 2;
doublevalue = v5(1);
cout <<"value: "<< value << endl;


// Set a vector equal another one:
Vector v6 = v5;


cout <<"try redim.v6: "<< v6.redim(1) << endl;
cout <<"v6.getLength: "<< v6.getLength() << endl;


// Take the inner product between two vectors:
doubledot = v6.inner(v5);// alternatively: double dot = inner(v6,v5);
cout <<"dot(v6,v5): "<< dot << endl;


// Get the euclidean norm to a vector:
doublenorm = v6.l2norm();
cout <<"norm of v6: "<< norm << endl;


// Normalize a vector:
v5.normalize();


// Dump a vector to the screen:
v5.print(std::cout <<"v5: "<< endl);


// Arithmetic operations with vectors using a
// syntax close to the mathematical language
Vector w = v1 + a*v2;


We list here the header file first.


http://folk.uio.no/mhjensen/compphys/programs/chapter03/cpp/Vector.h
#ifndefVECTOR_H
#defineVECTOR_H


#include
#include


//
/
VECTOR CLASS /
/
/


/*
@file Vector.h
@class Vector
@brief Class used for manipulating one-dimensional arrays.



  • Contains user-defined operators to do computations with arrays in a style
    close to mathematical equations.


  • **/




classVector{
private:
intlength;// Number of entries.
double*vec;// Entries.
public:

Free download pdf