Computational Physics - Department of Physics

(Axel Boer) #1

196 6 Linear Algebra


F.print("F:");
return0;
}


For more examples, please consult the online manual, seehttp://arma.sourceforgenet.


6.7.1 How to construct your own matrix-vector class


The rest of this section shows how one can build a matrix-vector class. We first give an exam-
ple of a function which use the header fileArray.h.


#include"Array.h"


#include
using namespacestd;


intmain(){


// Create an array with (default) nrows = 1, ncols = 1:
Array<double> v1;
// Redimension the array to have length n:
intn1 = 3;
v1.redim(n1);
// Extract the length of the array:
const intlength = v1.getLength();
// Create a narray of specific length:
intn2 = 5;
Array<double> v2(n2);
// Create an array as a copy of another one:
Array<double> v5(v1);
// Assign the entries in an array:
v5(0) = 3.0;
v5(1) = 2.5;
v5(2) = 1.0;
for(inti=0; i<3; i++){
cout << v5(i) << endl;
}
// Extract the ith component of an array:
inti = 2;
doublevalue = v5(1);
cout <<"value: "<< value << endl;
// Set an array equal another one:
Array<double> v6 = v5;
for(inti=0; i<3; i++){
v1(i) = 1.0;
v2(i) = 2.0;
}
// Create a two-dimensional array (matrix):
Free download pdf