02.07.2013 Views

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Program units and procedures<br />

Modules<br />

The purpose of precision is to communicate a kind type parameter to the other program<br />

units in the program, for the sake of precision portability. The second<br />

module—linear_equation_solver—contains three module procedures, the first of which,<br />

solve_linear_equations, uses the other two; solve_linear_equations is itself invoked by<br />

the main program.<br />

Stated algebraically, the equations that main.f90 provides as input for solution are:<br />

2x + 3y + 4z = 20<br />

3x + 4y + 5z = 26<br />

4x + 5y - 6z = -4<br />

Example 7-11 main.f90<br />

PROGRAM main<br />

! use the two modules defined in precision.f90 and<br />

! lin_eq_slv.f90<br />

USE precision<br />

USE linear_equation_solver<br />

IMPLICIT NONE<br />

! the matrix a contains the coefficients to solve; b holds<br />

! the constants on the right-hand side of the equation;<br />

! the solution goes in x<br />

REAL (adequate) :: a(3,3), b(3), x(3)<br />

INTEGER :: i, j<br />

! set by solve_linear_equations to indicate whether or not<br />

! a solution was possible<br />

LOGICAL :: error<br />

! initialize the matrix<br />

DO i = 1,3<br />

DO j = 1,3<br />

a(i,j) = i+j<br />

END DO<br />

END DO<br />

a(3,3) = -a(3,3)<br />

! initialize the vector of constants<br />

b = (/ 20, 26, -4 /)<br />

CALL solve_linear_equations (a, x, b, error)<br />

IF (error) THEN<br />

PRINT *, 'Cannot solve.'<br />

ELSE<br />

PRINT *, 'The solution:', x<br />

END IF<br />

END PROGRAM main<br />

162<br />

Chapter 7

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

Saved successfully!

Ooh no, something went wrong!