- #1
Telemachus
- 835
- 30
Hi there. I wanted to ask this question, which is about efficiency in matrix times vector multiplication in fortran. When I have some matrix ##\hat A## and vector ##\vec{x}##, and I want to compute the matrix times vector ##\hat A \vec{x}=\vec{b}## in Fortran, what I do is, I build the array ##A(i,j)## and ##x(j)##, and then I tell Fortran to do this:
And this is just fine, it works, it give the result I want. But the thing is that I have read that Fortran orders the arrays in column-major order, which means that arrays like ##A(i,j)##, and let's suppose that A is a 3x3 matrix, then these are ordered in memory like: A(1,1) A(2,1) A(3,1) A(1,2) A(2,2) A(3,2) A(1,3) A(2,3) ,A(3,3).
So, the way I am doing the matrix multiplication isn't the most efficient way of doing it. Should I fix it by using that:
##\left (\hat A \vec{x}\right)^T=\vec{b}^T=\vec{x}^T\hat A^T##, would this be the right way to do it? should I store the matrix transposed in order to make my code more efficient and then code it this way:?
Thanks in advance.
Fortran:
do i=1,msize
rsumaux=rzero
rsum=rzero
do k=1,msize
rsumaux=A(i,k)*x(k)
rsum=rsum+rsumaux
enddo
b(i)=rsum
enddo
And this is just fine, it works, it give the result I want. But the thing is that I have read that Fortran orders the arrays in column-major order, which means that arrays like ##A(i,j)##, and let's suppose that A is a 3x3 matrix, then these are ordered in memory like: A(1,1) A(2,1) A(3,1) A(1,2) A(2,2) A(3,2) A(1,3) A(2,3) ,A(3,3).
So, the way I am doing the matrix multiplication isn't the most efficient way of doing it. Should I fix it by using that:
##\left (\hat A \vec{x}\right)^T=\vec{b}^T=\vec{x}^T\hat A^T##, would this be the right way to do it? should I store the matrix transposed in order to make my code more efficient and then code it this way:?
Fortran:
do i=1,msize
rsumaux=rzero
rsum=rzero
do k=1,msize
rsumaux=A(k,i)*x(k)
rsum=rsum+rsumaux
enddo
b(i)=rsum
enddo
Thanks in advance.