13.07.2015 Views

Fortran 90/95 Programming Manual

Fortran 90/95 Programming Manual

Fortran 90/95 Programming Manual

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>Programming</strong> <strong>Manual</strong>The same can be achieved by using a do loop:do i = 1, 3do j = 1, 2c(j,i) = a(j+1,i) + b(j,i)end doend doBut the expression c = a(2:3,1:3) + b is clearly more concise.The operator + in the above example can be replaced by any of the other arithmeticoperators. The following expressions are all valid:c = a(2:3,1:3) * bc = a(2:3,1:3) / bc = a(2:3,1:3) – bc = a(2:3,1:3) ** b! c(1,1) = a(2,1) * b(1,1), etc.! c(1,1) = a(2,1) / b(1,1), etc.! c(1,1) = a(2,1) - b(1,1), etc.! c(1,1) = a(2,1) ** b(1,1), etc.Note that the operation is done element by element. Thus, the result of the multiplicationa(2:3,1:3) * b is not a matrix product!Scalars can be used in an array expression as well. Thus, a / 2.0 has the effect ofdividing all elements of the array a by 2.0, and a ** 2 raises all the elements of a to thepower 2.Array expressions can be used to avoid do-loops. For example, the expressiona(1:4) = a(2:5)Is equivalent todo i = 1, 4a(i) = a(i+1)end doHowever, if the do loop iterations are interdependent, then the do loop and the arrayexpression are not equivalent. This is because in the do loop the elements of the array areupdated after each iteration, and in the array expression the updating is done only after allthe elements have been processed.For example, if the array a contains the numbers 1, 2, 3, 4, 5, thendo i = 1,4a(i+1) = a(i)end doyields 1, 1, 1, 1, 1(After the first iteration, the array contains 1, 1, 3, 4, 5, after the second 1, 1, 1, 4, 5, afterthe third 1, 1, 1, 1, 5 and after the fourth 1, 1, 1, 1, 1).However,a(2:5) = a(1:4)yields 1, 1, 2, 3, 4.38

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!