forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Matrix subtraction

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
christyleomin



Joined: 08 Apr 2011
Posts: 155

PostPosted: Sat Jan 14, 2012 9:12 pm    Post subject: Matrix subtraction Reply with quote

I am using FTN 95 express downloaded from the site.

Is there anny intrinsic function to carry out matrix subtraction in FTN 95 than using the do loop?

I saw we have an intrinsic function SUM for summing arrays/matrices. Though i can negate a matrix and use SUM for subtraction, is there a direct function?
Back to top
View user's profile Send private message
JohnCampbell



Joined: 16 Feb 2006
Posts: 2556
Location: Sydney

PostPosted: Sun Jan 15, 2012 7:14 am    Post subject: Reply with quote

Array syntax can be used for addition and subtraction.
MATMUL intrinsic function can be used for multiplication.
There are probbly many matrix inversion functions available in public domain fortran 90+ libraries for division (multiply by the inverse).
The code below compares F90+ array syntax to F77 DO loop approach.
You need to make sure that the array dimensions are compatible with the operation.
Code:
!     Last change:  JDC  15 Jan 2012    5:07 pm
      integer*4, parameter :: m     = 8
      integer*4, parameter :: n     = 7
      real*8,    parameter :: two   = 2
      real*8,    parameter :: three = 3
!
      real*8     a(m,n), b(m,n), c(n,m), aa(m,n), d(m,m)
      real*10    s, e, emax
      integer*4  i,j
!
      forall (i=1:m,j=1:n)
        a(i,j) = real(i*2+j)
        b(i,j) = real(j-i)
        c(j,i) = real(j-i)
      end forall
!
!  Addition
      aa = two * a + three * b       ! array syntax
!
      emax = 0
      do i = 1,m
         do j = 1,n
            s = two * a(i,j) + three * b(i,j)
            e = abs (aa(i,j) - s)
            emax = max (emax,e)
         end do
         write (*,1001) aa(i,:)
      end do
      write (*,*) 'Max error in addition =',emax
!
!  Multiplication
      d = matmul (a, c)             ! array syntax
!
      emax = 0
      do i = 1,m
         do j = 1,m
            s = dot_product (a(i,:), c(:,j))
            e = abs (d(i,j) - s)
            emax = max (emax,e)
         end do
         write (*,1001) d(i,:)
      end do
      write (*,*) 'Max error in multiplication =',emax
!
1001  format (10f8.1)
      end
Back to top
View user's profile Send private message
christyleomin



Joined: 08 Apr 2011
Posts: 155

PostPosted: Sun Jan 15, 2012 8:48 am    Post subject: Reply with quote

Thanks a lot for the reply.

So, in FTN 95 express, for subtraction if I have:

Code:
real*8 A(5,5),B(5,5),C(5,5)
! ----- some code
!------- some code

C=A-B
IS this right?
Back to top
View user's profile Send private message
christyleomin



Joined: 08 Apr 2011
Posts: 155

PostPosted: Sun Jan 15, 2012 8:52 am    Post subject: Reply with quote

Also, what does the intrinsic function SUM do?
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7930
Location: Salford, UK

PostPosted: Sun Jan 15, 2012 9:28 am    Post subject: Reply with quote

Yes that is right. The Fortran 90 standard provides for element by element addition and subtraction etc.

The effect of the Fortran 90 intrinsic SUM can be found in the FTN95 help file (as well as in the Fortran standard and many books).
Back to top
View user's profile Send private message AIM Address
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group