Computational Physics - Department of Physics

(Axel Boer) #1

6.7 A vector and matrix class 197


Array<double> matrix(2, 2);
// Fill the array:
matrix(0,0) = 1;
matrix(0,1) = 2;
matrix(1,0) = 3;
matrix(1,1) = 4;
// Get the entries in the array:
cout <<"\nMatrix: "<< endl;
for(inti=0; i<2; i++){
for(intj=0; j<2; j++){
cout << matrix(i,j) <<" ";
}
cout << endl;
}
// Assign an entry of the matrix to a variable:
doublescalar = matrix(0,0);
const doubleb = matrix(1,1);

Array vector(2);
vector(0) = 1.0;
vector(1) = 2.0;
Array v = vector;
Array A = matrix;
Array u(2);
cout <<"\nMatrix: "<< endl;
for(inti=0; i<2; i++){
for(intj=0; j<2; j++){
cout << matrix(i,j) <<" ";
}
cout << endl;
}
Array a(2,2);
a(1,1) = 5.0;
// Arithmetic operations with arrays using a
// syntax close to the mathematical language
Array w = v1 + 2.0*v2;
// Create multidimensional matrices and assign values to them:
intN = 3;
Array multiD; multiD.redim(N,N,N);
for(inti=0; i<N; i++){
for(intj=0; j<N; j++){
for(intk=0; k<N; k++){
cout <<"multD(i,j,k) = "<< multiD(i,j,k) << endl;
}
}
}
multiD(1,2,3) = 4.0;
cout <<"multiD(1,2,3) = "<< multiD(1,2,3) << endl;
}


The header file follows here

Free download pdf