0% found this document useful (0 votes)
3 views1 page

Fortran Array

This document contains a Fortran program that demonstrates various matrix operations including initialization, addition, multiplication, and applying trigonometric functions. The program declares three 3x3 matrices, assigns initial values, and performs operations like summing, multiplying, and taking the sine of the matrices before outputting the results.

Uploaded by

Marcos A Santos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Fortran Array

This document contains a Fortran program that demonstrates various matrix operations including initialization, addition, multiplication, and applying trigonometric functions. The program declares three 3x3 matrices, assigns initial values, and performs operations like summing, multiplying, and taking the sine of the matrices before outputting the results.

Uploaded by

Marcos A Santos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

program matrix

implicit none
real, parameter :: PI = 4.0 * atan( 1.0 ) ! The number pi
real, dimension(3,3) :: a, b, c ! Declare size of a, b and c
! real a(3,3), b(3,3), c(3,3) ! Alternative dimension statement
integer i, j ! Counters
character(len=*), parameter :: fmt = "( a, 3(/, 3(1x, f8.3)), / )"
! Format string for output

! Basic initialisation of matrices by assigning all values (very inefficient!)


a(1,1) = 1.0; a(1,2) = 2.0; a(1,3) = 3.0
a(2,1) = 4.0; a(2,2) = 5.0; a(2,3) = 6.0
a(3,1) = 7.0; a(3,2) = 8.0; a(3,3) = 9.0

b(1,1) = 10.0; b(1,2) = 20.0; b(1,3) = 30.0


b(2,1) = 40.0; b(2,2) = 50.0; b(2,3) = 60.0
b(3,1) = 70.0; b(3,2) = 80.0; b(3,3) = 90.0

! Alternative initialisation using data statements (note order)


! data a / 1.0, 4.0, 7.0, 2.0, 5.0, 8.0, 3.0, 6.0, 9.0 /
! data b / 10.0, 40.0, 70.0, 20.0, 50.0, 80.0, 30.0, 60.0, 90.0 /

! Alternative initialisation computing each element of a


! do j = 1, 3
! do i = 1, 3
! a(i,j) = ( i - 1 ) * 3 + j
! end do
! end do
! Then whole-array operation for b
! b = 10.0 * a

! Alternative initialisation using array constructor, implied do-loop and


reshape
! a = reshape( [ ( i, i = 1, 9 ) ], [ 3, 3 ] )
! a = transpose( a ) ! To get intended order!
! b = 10 * a

! Write out matrices (using implied do loops)


write( *, fmt ) "a", ( ( a(i,j), j = 1, 3 ), i = 1, 3 )
write( *, fmt ) "b", ( ( b(i,j), j = 1, 3 ), i = 1, 3 )

! Matrix sum
c = a + b
write( *, fmt ) "a+b", ( ( c(i,j), j = 1, 3 ), i = 1, 3 )

! "element-by-element" multiplication
c = a * b
write( *, fmt ) "a*b", ( ( c(i,j), j = 1, 3 ), i = 1, 3 )

! "Proper" matrix multiplication


c = matmul( a, b )
write( *, fmt ) "matmul(a,b)", ( ( c(i,j), j = 1, 3 ), i = 1, 3 )

! Some operation applied to all elements of a matrix


c = sin( b * PI / 180.0 )
write( *, fmt ) "sin(b)", ( ( c(i,j), j = 1, 3 ), i = 1, 3 )

end program matrix

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy