ProgrameR

Outer product of two vectors in Fortran

Posted in Mathmatics, Statistics by emeryyi on May 22, 2010

Unfortunately, no outer product intrinsic function available in Fortran. Outer product return a matrix from two user-provided vector a and b,

where

we have to implement our own function by

spread(a(1:n),dim=2,ncopies=n)*spread(b(1:n),dim=1,ncopies= p))
Here is some more detail about spread() function
SPREAD(SOURCE, DIM, NCOPIES)
! A is the array (/ -4.7, 6.1, 0.3 /)
       RES = SPREAD( A, DIM = 1, NCOPIES = 3 )
! The result is   | -4.7 6.1 0.3 |
!                 | -4.7 6.1 0.3 |
!                 | -4.7 6.1 0.3 |
! DIM=1 extends each column.  Each element in RES(:,1)
! becomes a copy of A(1), each element in RES(:,2) becomes
! a copy of A(2), and so on.
       RES = SPREAD( A, DIM = 2, NCOPIES = 3 )
! The result is   | -4.7 -4.7 -4.7 |
!                 |  6.1  6.1  6.1 |
!                 |  0.3  0.3  0.3 |
! DIM=2 extends each row.  Each element in RES(1,:)
! becomes a copy of A(1), each element in RES(2,:)
! becomes a copy of A(2), and so on.
Tagged with:

Leave a comment