Fortran Quickest Way to Read Specific Place

The following Fortran code examples or sample programs bear witness different situations depending on the compiler. The first prepare of examples are for the Fortran II, Iv, and 77 compilers. The remaining examples tin can be compiled and run with any newer standard Fortran compiler (see the terminate of the principal Fortran article for lists of compilers). Past convention most contemporary Fortran compilers select the linguistic communication standard to use during compilation based on source code file proper name suffix: FORTRAN 77 for .f (or the less common .for), Fortran ninety for .f90, Fortran 95 for .f95. Other standards, if supported, may be selected manually with a control line option.

FORTRAN II, IV, and 77 compilers [edit | edit source]

NOTE: Before FORTRAN xc, nearly FORTRAN compilers enforced fixed-format source code, a carryover from IBM dial cards

  • comments must begin with a * or C or ! in cavalcade i
  • argument labels must occur in columns one-v
  • continuation lines must take a non-blank character in column six
  • statements must start in column 7
  • the line-length may exist limited to 72 characters (derived from the 80-byte width of a punch-carte du jour, with last viii characters reserved for (optional) sequence numbers)

If errors are produced when you compile your FORTRAN code, first check the column alignment. Some compilers as well offering free form source past using a compiler flag

Expanse Of a Triangle program [edit | edit source]

Elementary Fortran II program [edit | edit source]

One data card input

If one of the input values is nil, and so the programme will end with an error lawmaking of "i" in the job control card list following the execution of the program. Normal output will be one line printed with A, B, C, and Surface area. No specific units are stated.

                        C AREA OF A TRIANGLE - HERON'S FORMULA            C INPUT - Menu READER Unit 5, INTEGER INPUT            C OUTPUT -            C INTEGER VARIABLES START WITH I,J,K,Fifty,M OR N                                    READ            (            5            ,            501            )            IA            ,            IB            ,            IC                          501            FORMAT            (            three            I5            )                                    IF            (            IA            )            701            ,            777            ,            701                          701            IF            (            IB            )            702            ,            777            ,            702                          702            IF            (            IC            )            703            ,            777            ,            703                          777            Terminate                        1                          703            S            =            (            IA            +            IB            +            IC            )            /            2.0                                    Area            =            SQRT            (            S            *            (            S            -            IA            )            *            (            S            -            IB            )            *            (            South            -            IC            )            )                                    WRITE            (            vi            ,            801            )            IA            ,            IB            ,            IC            ,            Expanse                          801            FORMAT            (            iv            H            A            =            ,            I5            ,            5            H            B            =            ,            I5            ,            v            H            C            =            ,            I5            ,            8            H            Expanse            =            ,            F10            .            two            ,                                    $            13            H            Square            UNITS            )                                    Finish                                    END          

Simple Fortran IV program [edit | edit source]

Multiple information card input

This program has two input checks: ane for a blank card to betoken end-of-data, and the other for a zero value within the input data. Either condition causes a message to exist printed.

                        C Area OF A TRIANGLE - HERON'South FORMULA            C INPUT - Bill of fare READER UNIT 5, INTEGER INPUT, 1 Bare Carte FOR Cease-OF-DATA            C OUTPUT - LINE PRINTER UNIT 6, REAL OUTPUT            C INPUT ERROR DISPAY ERROR MESSAGE ON OUTPUT                          501            FORMAT            (            3            I5            )                          601            FORMAT            (            iv            H            A            =            ,            I5            ,            5            H            B            =            ,            I5            ,            five            H            C            =            ,            I5            ,            8            H            Expanse            =            ,            F10            .            2            ,                                    $            13            H            SQUARE            UNITS            )                          602            FORMAT            (            10            HNORMAL            Cease            )                          603            FORMAT            (            23            HINPUT            ERROR            ,            Nada            VALUE            )                                    INTEGER                        A            ,            B            ,            C                          10            READ            (            v            ,            501            )            A            ,            B            ,            C                                    IF            (            A            .            EQ            .            0            .            AND            .            B            .            EQ            .            0            .            AND            .            C            .            EQ            .            0            )            GO            TO            50                                    IF            (            A            .            EQ            .            0            .            OR            .            B            .            EQ            .            0            .            OR            .            C            .            EQ            .            0            )            Go            TO            xc                                    Southward            =            (            A            +            B            +            C            )            /            2.0                                    Area            =            SQRT            (            S            *            (            S            -            A            )            *            (            S            -            B            )            *            (            S            -            C            )            )                                    WRITE            (            6            ,            601            )            A            ,            B            ,            C            ,            AREA                                    GO            TO            10                          50            WRITE            (            6            ,            602            )                                    Terminate                          90            WRITE            (            six            ,            603            )                                    STOP                                    END          

Simple Fortran 77 program [edit | edit source]

Multiple data bill of fare input

This program has 2 input checks in the READ statement with the END and ERR parameters, one for a blank card to indicate end-of-data; and the other for zero value along with valid data. In either status, a message volition be printed.

                        C AREA OF A TRIANGLE - HERON'S FORMULA            C INPUT - Carte du jour READER Unit five, INTEGER INPUT, NO Bare Carte du jour FOR END OF Data            C OUTPUT - LINE PRINTER Unit of measurement 6, REAL OUTPUT            C INPUT Fault DISPAYS ERROR MESSAGE ON OUTPUT                          501            FORMAT            (            3            I5            )                          601            FORMAT            (            " A= "            ,            I5            ,            "  B= "            ,            I5            ,            "  C= "            ,            I5            ,            "  Expanse= "            ,            F10            .            ii            ,                                    $            "SQUARE UNITS"            )                          602            FORMAT            (            "NORMAL END"            )                          603            FORMAT            (            "INPUT ERROR OR ZERO VALUE Fault"            )                                    INTEGER                        A            ,            B            ,            C                          10            READ            (            5            ,            501            ,            END            =            50            ,            ERR            =            90            )            A            ,            B            ,            C                                    IF            (            A            =            0            .            OR            .            B            =            0            .            OR            .            C            =            0            )            Become            TO            90                                    South            =            (            A            +            B            +            C            )            /            2.0                                    Expanse            =            SQRT            (            South            *            (            S            -            A            )            *            (            S            -            B            )            *            (            S            -            C            )            )                                    WRITE            (            6            ,            601            )            A            ,            B            ,            C            ,            AREA                                    Become            TO            ten                          fifty            WRITE            (            half dozen            ,            602            )                                    STOP                          90            WRITE            (            half dozen            ,            603            )                                    Stop                                    END          

"Retro" FORTRAN Iv [edit | edit source]

A retro example of a FORTRAN IV (after evolved into FORTRAN 66) program deck is available on the IBM 1130 page, including the IBM 1130 DM2 JCL required for compilation and execution. An IBM 1130 emulator is available at IBM 1130.org that will allow the FORTRAN 4 programme to be compiled and run on a PC.

How-do-you-do, Globe programme [edit | edit source]

In keeping with computing tradition, the showtime case presented is a simple programme to display the words "Howdy, earth" on the screen (or printer).

FORTRAN 66 (as well FORTRAN IV) [edit | edit source]

                                      C                        FORTRAN            IV            WAS            ONE            OF            THE            Showtime            PROGRAMMING                          C                        LANGUAGES            TO            SUPPORT            SOURCE            COMMENTS                                    WRITE            (            six            ,            7            )                                    seven            FORMAT            (            13            H            HELLO            ,            WORLD            )                                    Cease                                    END          

This plan prints "How-do-you-do, Earth" to Fortran unit number 6, which on most machines was the line printer or terminal. (The carte reader or keyboard was usually connected as unit 5). The number 7 in the WRITE argument refers to the statement number of the corresponding FORMAT argument. FORMAT statements may be placed anywhere in the aforementioned program or function/subroutine cake equally the WRITE statements which reference them. Typically a FORMAT statement is placed immediately following the WRITE argument which invokes information technology; alternatively, FORMAT statements are grouped together at the end of the program or subprogram block. If execution flows into a FORMAT statement, information technology is a no-op; thus, the instance in a higher place has merely 2 executable statements, WRITE and STOP.

The initial 13H in the FORMAT argument in the above example defines a Hollerith abiding, here meaning that the 13 characters immediately following are to be taken every bit a character constant (note that the Hollerith constant is non surrounded by delimiters). (Some compilers too supported character literals enclosed in unmarried quotes, a practice that came to be standard with FORTRAN 77.)

The infinite immediately following the 13H is a carriage control character, telling the I/O organization to advance to a new line on the output. A aught in this position advances 2 lines (double infinite), a ane advances to the tiptop of a new page and + character volition not advance to a new line, allowing overprinting.

FORTRAN 77 [edit | edit source]

As of FORTRAN 77, unmarried quotes are used to delimit character literals, and inline character strings may exist used instead of references to FORMAT statements. Comment lines may be indicated with either a C or an asterisk (*) in cavalcade 1.

                                                PROGRAM                        HELLO            *     The PRINT argument is like WRITE,            *     merely prints to the standard output unit of measurement                                    PRINT            '(A)'            ,            'How-do-you-do, world'                                    STOP                                    Stop          

Fortran xc [edit | edit source]

Equally of Fortran 90, double quotes are allowed in addition to single quotes. An updated version of the Hello, world example (which here makes use of listing-directed I/O, supported as of FORTRAN 77) could be written in Fortran 90 equally follows:

                        program                        HelloWorld            write            (            *            ,            *            )            'Hello, earth!'            ! This is an inline annotate            finish programme                        HelloWorld          

Fortran 77 examples [edit | edit source]

Greatest mutual divisor [edit | edit source]

The following introductory instance in FORTRAN 77 finds the greatest common divisor for two numbers A {\displaystyle A} and B {\displaystyle B} using a verbatim implementation of Euclid'due south algorithm.

                        *     euclid.f (FORTRAN 77)            *     Find greatest common divisor using the Euclidean algorithm                                    PROGRAM                        EUCLID                                    PRINT            *            ,            'A?'                                    READ            *            ,            NA                                    IF            (            NA            .            LE            .            0            )            Then                                    PRINT            *            ,            'A must be a positive integer.'                                    STOP                                    END IF                                    PRINT            *            ,            'B?'                                    READ            *            ,            NB                                    IF            (            NB            .            LE            .            0            )            THEN                                    PRINT            *            ,            'B must be a positive integer.'                                    STOP                                    End IF                                    Print            *            ,            'The GCD of'            ,            NA            ,            ' and'            ,            NB            ,            ' is'            ,            NGCD            (            NA            ,            NB            ),            '.'                                    End                                    Finish                                    Office                        NGCD            (            NA            ,            NB            )                                    IA            =            NA                                    IB            =            NB                          i            IF            (            IB            .            NE            .            0            )            So                                    ITEMP            =            IA                                    IA            =            IB                                    IB            =            Modern            (            ITEMP            ,            IB            )                                    GOTO                        1                                    Finish IF                                    NGCD            =            IA                                    Render                                    END          

The above instance is intended to illustrate the post-obit:

  • The Impress and READ statements in the above employ '*' equally a format, specifying list-directed formatting. List-directed formatting instructs the compiler to make an educated guess virtually the required input or output format based on the following arguments.
  • As the earliest machines running Fortran had restricted graphic symbol sets, FORTRAN 77 uses abbreviations such every bit .EQ., .NE., .LT., .GT., .LE., and .GE. to represent the relational operators =, ≠, <, >, ≤, and ≥, respectively.
  • This example relies on the implicit typing mechanism to specify the INTEGER types of NA, NB, IA, IB, and ITEMP.
  • In the function NGCD(NA, NB), the values of the function arguments NA and NB are copied into the local variables IA and IB respectively. This is necessary as the values of IA and IB are altered within the part. Considering argument passing in Fortran functions and subroutines utilize phone call by reference by default (rather than call past value, as is the default in languages such equally C), modifying NA and NB from within the function would effectively have modified the corresponding actual arguments in the main PROGRAM unit which called the office.

The following shows the results of compiling and running the plan.

                        $            g77 -o euclid euclid.f            $            euclid                          A?            24                          B?            36                          The GCD of 24 and 36 is 12.          

Complex numbers [edit | edit source]

The following FORTRAN 77 instance prints out the values of east j i π / 4 {\displaystyle east^{ji\pi /4}} (where j = 1 {\displaystyle j={\sqrt {-1}}} ) for values of i = 0 , 1 , , 7 {\displaystyle i=0,1,\ldots ,vii} .

                        *     cmplxd.f (FORTRAN 77)            *     Demonstration of Complex numbers            *            *     Prints the values of due east ** (j * i * pi / four) for i = 0, 1, 2, ..., 7            *         where j is the imaginary number sqrt(-1)                                    Program                        CMPLXD                                    IMPLICIT                        Circuitous            (            Ten            )                                    PARAMETER            (            PI            =            3.141592653589793            ,            XJ            =            (            0            ,            ane            ))                                    Exercise                        1            ,            I            =            0            ,            seven                                    X            =            EXP            (            XJ            *            I            *            PI            /            4            )                                    IF            (            AIMAG            (            10            ).            LT            .            0            )            And then                                    Print                        ii            ,            'eastward**(j*'            ,            I            ,            '*pi/iv) = '            ,            Real            (            X            ),            ' - j'            ,            -            AIMAG            (            Ten            )                                    ELSE                                    PRINT                        ii            ,            'due east**(j*'            ,            I            ,            '*pi/4) = '            ,            REAL            (            10            ),            ' + j'            ,            AIMAG            (            10            )                                    Stop IF                          ii            FORMAT            (            A            ,            I1            ,            A            ,            F10            .            seven            ,            A            ,            F9            .            7            )                          ane            Go along                                    STOP                                    Terminate          

The to a higher place example is intended to illustrate the following:

  • The IMPLICIT argument can exist used to specify the implicit type of variables based on their initial letter if dissimilar from the default implicit typing scheme described in a higher place. In this example, this argument specifies that the implicit blazon of variables commencement with the letter X shall be Circuitous.
  • The PARAMETER statement may exist used to specify constants. The second abiding in this case (XJ) is given the circuitous-valued value 0 + j 1 {\displaystyle 0+j1} , where j {\displaystyle j} is the imaginary unit 1 {\displaystyle {\sqrt {-one}}} .
  • The first number in the Do argument specifies the number of the last statement considered to be inside the trunk of the DO loop. In this example, every bit neither the End IF nor the FORMAT is a single executable statement, the Go along statement (which does nothing) is used simply in order for in that location to be some statement to denote as the final statement of the loop.
  • EXP() corresponds to the exponential part e ten {\displaystyle e^{x}} . In FORTRAN 77, this is a generic office, pregnant that it accepts arguments of multiple types (such every bit REAL and, in this example, COMPLEX). In FORTRAN 66, a specific part would take to be called past name depending on the type of the function arguments (for this example, CEXP() for a Complex-valued argument).
  • When practical to a COMPLEX-valued argument, REAL() and AIMAG() render the values of the argument'southward existent and imaginary components, respectively.

Incidentally, the output of the in a higher place program is as follows (come across the article on Euler'southward formula for the geometric estimation of these values as eight points spaced evenly virtually a unit circumvolve in the circuitous airplane).

                        $            cmplxd            e**(j*0*pi/4) =  1.0000000 + j0.0000000            e**(j*1*pi/4) =  0.7071068 + j0.7071068            e**(j*2*pi/iv) =  0.0000000 + j1.0000000            e**(j*3*pi/iv) = -0.7071068 + j0.7071068            eastward**(j*iv*pi/4) = -i.0000000 - j0.0000001            eastward**(j*v*pi/4) = -0.7071066 - j0.7071069            e**(j*6*pi/iv) =  0.0000000 - j1.0000000            e**(j*7*pi/four) =  0.7071070 - j0.7071065          

Error can be seen occurring in the concluding decimal place in some of the numbers above, a outcome of the Complex information type representing its existent and imaginary components in single precision. Incidentally, Fortran ninety also fabricated standard a double-precision complex-number data type (although several compilers provided such a type even earlier).

FORTRAN 90 plan to find the surface area of a triangle [edit | edit source]

                        programme                        area            implicit none                                    real            ::            A            ,            B            ,            C            ,            S            ! surface area of a triangle            read            *            ,            A            ,            B            ,            C            S            =            (            A            +            B            +            C            )            /            2            A            =            sqrt            (            S            *            (            S            -            A            )            *            (            S            -            B            )            *            (            S            -            C            ))            print            *            ,            "area ="            ,            A            finish            stop program                        area          

Fortran 90/95 examples [edit | edit source]

Summations with a DO loop [edit | edit source]

In this example of Fortran 90 code, the programmer has written the bulk of the lawmaking within of a DO loop. Upon execution, instructions are printed to the screen and a SUM variable is initialized to zero outside the loop. In one case the loop begins, it asks the user to input any number. This number is added to the variable SUM every time the loop repeats. If the user inputs 0, the Leave argument terminates the loop, and the value of SUM is displayed on screen.

Also credible in this program is a information file. Before the loop begins, the program creates (or opens, if information technology has already been run before) a text file chosen "SumData.DAT". During the loop, the WRITE statement stores whatever user-inputted number in this file, and upon termination of the loop, also saves the answer.

                        ! sum.f90            ! Performs summations using in a loop using EXIT statement            ! Saves input information and the summation in a data file            program                        summation            implicit none                                    integer            ::            sum            ,            a            print            *            ,            "This programme performs summations. Enter 0 to cease."            open            (            unit            =            10            ,            file            =            "SumData.DAT"            )            sum            =            0            do                          print            *            ,            "Add:"            read            *            ,            a            if            (            a            ==            0            )            then                          exit                          else                                    sum            =            sum            +            a            finish if                          write            (            10            ,            *            )            a            end practice                          impress            *            ,            "Summation ="            ,            sum                                    write            (            10            ,            *            )            "Summation ="            ,            sum                                    close            (            10            )            end          

When executed, the console would display the post-obit:

                        This program performs summations.  Enter 0 to cease.  Add: i  Add: 2  Add together:  3  Add together: 0  Summation = vi          

And the file SumData.DAT would contain:

Calculating cylinder surface area [edit | edit source]

The post-obit program, which calculates the surface area of a cylinder, illustrates gratis-form source input and other features introduced by Fortran 90.

                        program                        cylinder            ! Calculate the surface area of a cylinder.            !            ! Declare variables and constants.            ! constants=pi            ! variables=radius squared and acme            implicit none            ! Require all variables to be explicitly alleged            integer            ::            ierr            character            (            1            )            ::            yn            existent            ::            radius            ,            height            ,            area            real            ,            parameter            ::            pi            =            3.141592653589793            interactive_loop            :            do            !   Prompt the user for radius and height            !   and read them.            write            (            *            ,            *            )            'Enter radius and height.'            read            (            *            ,            *            ,            iostat            =            ierr            )            radius            ,            height            !   If radius and acme could non be read from input,            !   so cycle through the loop.            if            (            ierr            /=            0            )            so                          write            (            *            ,            *            )            'Error, invalid input.'            cycle                        interactive_loop            end if            !   Compute area.  The ** ways "heighten to a power."            expanse            =            2            *            pi            *            (            radius            **            2            +            radius            *            superlative            )            !   Write the input variables (radius, height)            !   and output (expanse) to the screen.            write            (            *            ,            '(1x,a7,f6.two,5x,a7,f6.2,5x,a5,f6.ii)'            )            &            'radius='            ,            radius            ,            'elevation='            ,            height            ,            'area='            ,            area            yn            =            ' '            yn_loop            :            do                          write            (            *            ,            *            )            'Perform another adding? y[n]'            read            (            *            ,            '(a1)'            )            yn            if            (            yn            ==            'y'            .            or            .            yn            ==            'Y'            )            get out                        yn_loop            if            (            yn            ==            'due north'            .            or            .            yn            ==            'N'            .            or            .            yn            ==            ' '            )            get out                        interactive_loop            stop do                        yn_loop            finish do                        interactive_loop            end program                        cylinder          

Dynamic memory resource allotment and arrays [edit | edit source]

The post-obit programme illustrates dynamic memory allocation and assortment-based operations, two features introduced with Fortran 90. Specially noteworthy is the absence of DO loops and IF/And then statements in manipulating the array; mathematical operations are applied to the array as a whole. Also credible is the use of descriptive variable names and general code formatting that comport with gimmicky programming style. This example computes an average over information entered interactively.

                        programme                        average            ! Read in some numbers and take the average            ! As written, if there are no data points, an average of zippo is returned            ! While this may not be desired beliefs, it keeps this example simple            implicit none                                    integer            ::            number_of_points            real            ,            dimension            (:),            allocatable            ::            points            existent            ::            average_points            =            0.            ,            positive_average            =            0.            ,            negative_average            =            0.            write            (            *            ,            *            )            "Input number of points to boilerplate:"            read            (            *            ,            *            )            number_of_points            classify            (            points            (            number_of_points            ))            write            (            *            ,            *            )            "Enter the points to average:"            read            (            *            ,            *            )            points            ! Take the average by summing points and dividing by number_of_points            if            (            number_of_points            >            0            )            average_points            =            sum            (            points            )            /            number_of_points            ! Now form average over positive and negative points merely            if            (            count            (            points            >            0.            )            >            0            )            positive_average            =            sum            (            points            ,            points            >            0.            )            &            /            count            (            points            >            0.            )            if            (            count            (            points            <            0.            )            >            0            )            negative_average            =            sum            (            points            ,            points            <            0.            )            &            /            count            (            points            <            0.            )            deallocate            (            points            )            ! Print result to terminal            write            (            *            ,            '(''Average = '', 1g12.4)'            )            average_points            write            (            *            ,            '(''Average of positive points = '', 1g12.4)'            )            positive_average            write            (            *            ,            '(''Boilerplate of negative points = '', 1g12.4)'            )            negative_average            end program                        average          

Writing functions [edit | edit source]

Modern Fortran features available for apply with procedures, including deferred-shape, protected, and optional arguments, are illustrated in the following case, a function to solve a organization of linear equations.

                        function                        gauss_sparse            (            num_iter            ,            tol            ,            b            ,            A            ,            x            ,            actual_iter            )            result            (            tol_max            )            !  This function solves a system of equations (Ax = b) by using the Gauss-Seidel Method            implicit none                                    real            ::            tol_max            !  Input: its value cannot be modified from inside the part            integer            ,            intent            (            in            )            ::            num_iter            real            ,            intent            (            in            )            ::            tol            existent            ,            intent            (            in            ),            dimension            (:)            ::            b            ,            A            (:,:)            !  Input/Output: its input value is used within the function, and tin be modified            real            ,            intent            (            inout            )            ::            x            (:)            !  Output: its value is modified from within the part, only if the argument is required            integer            ,            optional            ,            intent            (            out            )            ::            actual_iter            !  Locals            integer            ::            i            ,            n            ,            iter            real            ::            xk            !  Initialize values            n            =            size            (            b            )            ! Size of array, obtained using size intrinsic function            tol_max            =            2.            *            tol            iter            =            0            !  Compute solution until convergence            convergence_loop            :            practise while            (            tol_max            >=            tol            .            and            .            iter            <            num_iter            );            iter            =            iter            +            1            tol_max            =            -            i.            ! Reset the tolerance value            !     Compute solution for the chiliad-th iteration            iteration_loop            :            do                        i            =            1            ,            n            !        Compute the current x-value            xk            =            (            b            (            i            )            -            dot_product            (            A            (            i            ,:            i            -            i            ),            10            (:            i            -            1            ))            -            dot_product            (            A            (            i            ,            i            +            1            :            n            ),            x            (            i            +            one            :            n            )))            /            A            (            i            ,            i            )            !        Compute the error of the solution            !        dot_product(a,v)=a'b            tol_max            =            max            ((            abs            (            ten            (            i            )            -            xk            )            /            (            1.            +            abs            (            xk            )))            **            two            ,            abs            (            A            (            i            ,            i            )            *            (            x            (            i            )            -            xk            )),            tol_max            )            10            (            i            )            =            xk            enddo                        iteration_loop            enddo                        convergence_loop            if            (            present            (            actual_iter            ))            actual_iter            =            iter            stop function                        gauss_sparse          

Note that an explicit interface to this routine must exist available to its caller so that the blazon signature is known. This is preferably done by placing the role in a MODULE and so USEing the module in the calling routine. An culling is to utilise an INTERFACE block, as shown past the following case:

                        plan                        test_gauss_sparse            implicit none            !   explicit interface to the gauss_sparse role            interface                          function                        gauss_sparse            (            num_iter            ,            tol            ,            b            ,            A            ,            x            ,            actual_iter            )            result            (            tol_max            )            real            ::            tol_max            integer            ,            intent            (            in            )            ::            num_iter            real            ,            intent            (            in            )            ::            tol            real            ,            intent            (            in            ),            dimension            (:)            ::            b            ,            A            (:,:)            real            ,            intent            (            inout            )            ::            x            (:)            integer            ,            optional            ,            intent            (            out            )            ::            actual_iter            cease function                          end interface            !   declare variables            integer            ::            i            ,            Due north            =            3            ,            actual_iter            existent            ::            residue            existent            ,            allocatable            ::            A            (:,:),            10            (:),            b            (:)            !   allocate arrays            allocate            (            A            (            N            ,            North            ),            b            (            N            ),            x            (            N            ))            !   Initialize matrix            A            =            reshape            ([(            real            (            i            ),            i            =            1            ,            size            (            A            ))],            shape            (            A            ))            !   Make matrix diagonally ascendant            do                        i            =            i            ,            size            (            A            ,            one            )            A            (            i            ,            i            )            =            sum            (            A            (            i            ,:))            +            ane            enddo            !   Initialize b            b            =            [(            i            ,            i            =            1            ,            size            (            b            ))]            !   Initial (guess) solution            x            =            b            !   invoke the gauss_sparse office                        residue            =            gauss_sparse            (            num_iter            =            100            ,            &            tol            =            1E-v            ,            &            b            =            b            ,            &            A            =            a            ,            &            ten            =            x            ,            &            actual_iter            =            actual_iter            )            !   Output            print            '(/ "A = ")'            practice                        i            =            i            ,            size            (            A            ,            1            )            print            '(100f6.ane)'            ,            A            (            i            ,:)            enddo                          print            '(/ "b = " / (f6.1))'            ,            b            impress            '(/ "residue = ", g10.3 / "iterations = ", i0 / "solution = "/ (11x, g10.3))'            ,            &            residue            ,            actual_iter            ,            x            end plan                        test_gauss_sparse          

Writing subroutines [edit | edit source]

In those cases where information technology is desired to render values via a procedure's arguments, a subroutine is preferred over a office; this is illustrated by the post-obit subroutine to swap the contents of two arrays:

                        subroutine                        swap_real            (            a1            ,            a2            )            implicit none            !  Input/Output            real            ,            intent            (            inout            )            ::            a1            (:),            a2            (:)            !  Locals            integer            ::            i            real            ::            a            !  Bandy            practice                        i            =            one            ,            min            (            size            (            a1            ),            size            (            a2            ))            a            =            a1            (            i            )            a1            (            i            )            =            a2            (            i            )            a2            (            i            )            =            a            enddo            terminate subroutine                        swap_real          

As in the previous example, an explicit interface to this routine must be bachelor to its caller so that the type signature is known. As before, this is preferably washed by placing the function in a MODULE and and then USEing the module in the calling routine. An alternative is to use a INTERFACE block.

Internal and Elemental Procedures [edit | edit source]

An culling way to write the swap_real subroutine from the previous case, is:

                        subroutine                        swap_real            (            a1            ,            a2            )            implicit none            !  Input/Output            real            ,            intent            (            inout            )            ::            a1            (:),            a2            (:)            !  Locals            integer            ::            N            !  Bandy, using the internal subroutine            N            =            min            (            size            (            a1            ),            size            (            a2            ))            call                        swap_e            (            a1            (:            N            ),            a2            (:            Due north            ))            contains                          elemental subroutine                        swap_e            (            a1            ,            a2            )            existent            ,            intent            (            inout            )            ::            a1            ,            a2            real            ::            a            a            =            a1            a1            =            a2            a2            =            a            end subroutine                        swap_e            end subroutine                        swap_real          

In the example, the swap_e subroutine is elemental, i.east., it acts upon its array arguments, on an element-by-element ground. Elemental procedures must be pure (i.due east., they must have no side furnishings and can invoke but pure procedures), and all the arguments must be scalar. Since swap_e is internal to the swap_real subroutine, no other program unit can invoke it.

The following program serves every bit a test for whatever of the ii swap_real subroutines presented:

                        program                        test_swap_real            implicit none            !   explicit interface to the swap_real subroutine            interface                          subroutine                        swap_real            (            a1            ,            a2            )            existent            ,            intent            (            inout            )            ::            a1            (:),            a2            (:)            end subroutine                        swap_real            end interface            !   Declare variables            integer            ::            i            real            ::            a            (            10            ),            b            (            10            )            !   Initialize a, b            a            =            [(            existent            (            i            ),            i            =            1            ,            xx            ,            2            )]            b            =            a            +            i            !   Output before swap            print            '(/"before swap:")'            impress            '("a = [", 10f6.ane, "]")'            ,            a            print            '("b = [", 10f6.i, "]")'            ,            b            !   Call the swap_real subroutine            call                        swap_real            (            a            ,            b            )            !   Output after bandy            print            '(// "after bandy:")'            print            '("a = [", 10f6.1, "]")'            ,            a            print            '("b = [", 10f6.1, "]")'            ,            b            end program                        test_swap_real          

Pointers and targets methods [edit | edit source]

In Fortran, the concept of pointers differs from that in C-like languages. A Fortran 90 arrow does non merely store the retentivity address of a target variable; information technology also contains boosted descriptive information such as the target's rank, the upper and lower bounds of each dimension, and even strides through memory. This allows a Fortran ninety pointer to point at submatrices.

Fortran xc pointers are "associated" with well-defined "target" variables, via either the pointer consignment operator (=>) or an Allocate statement. When appearing in expressions, pointers are always dereferenced; no "pointer arithmetic" is possible.

The following example illustrates the concept:

                        module                        SomeModule            implicit none                          contains                          elemental function                        A            (            x            )            result            (            res            )            integer            ::            res            integer            ,            intent            (            IN            )            ::            x            res            =            x            +            1            terminate part            stop module                        SomeModule            program                        Test            use                        SomeModule            ,            DoSomething            =>            A            implicit none            !Declare variables            integer            ,            parameter            ::            m            =            3            ,            n            =            three            integer            ,            pointer            ::            p            (:)            =>            goose egg            (),            q            (:,:)            =>            null            ()            integer            ,            allocatable            ,            target            ::            A            (:,:)            integer            ::            istat            =            0            ,            i            ,            j            character            (            lxxx            )            ::            fmt            !  Write format string for matrices            !  (/ A / A, " = [", 3( "[",3(i2, 1x), "]" / 5x), "]" )            write            (            fmt            ,            '("(/ A / A, "" = ["", ", i0, "( ""["",", i0, "(i2, 1x), ""]"" / 5x), ""]"" )")'            )            1000            ,            n            allocate            (            A            (            m            ,            north            ),            q            (            chiliad            ,            n            ),            stat            =            istat            )            if            (            istat            /=            0            )            finish            'Fault during allocation of A and q'            !  Matrix A is:            !  A = [[ 1  4  7 ]            !       [ 2  v  8 ]            !       [ 3  half-dozen  9 ]            !       ]            A            =            reshape            ([(            i            ,            i            =            ane            ,            size            (            A            ))],            shape            (            A            ))            q            =            A            write            (            *            ,            fmt            )            "Matrix A is:"            ,            "A"            ,            ((            A            (            i            ,            j            ),            j            =            one            ,            size            (            A            ,            2            )),            i            =            1            ,            size            (            A            ,            one            ))            !  p will be associated with the first column of A            p            =>            A            (:,            1            )            !  This functioning on p has a direct effect on matrix A            p            =            p            **            two            !  This will end the association between p and the beginning column of A            nullify            (            p            )            !  Matrix A becomes:            !  A = [[ i  4  7 ]            !       [ four  5  8 ]            !       [ 9  6  9 ]            !       ]            write            (            *            ,            fmt            )            "Matrix A becomes:"            ,            "A"            ,            ((            A            (            i            ,            j            ),            j            =            1            ,            size            (            A            ,            2            )),            i            =            1            ,            size            (            A            ,            ane            ))            !  Perform some array operation            q            =            q            +            A            !  Matrix q becomes:            !  q = [[ 2  8 14 ]            !       [ 6 10 16 ]            !       [12 12 18 ]            !       ]            write            (            *            ,            fmt            )            "Matrix q becomes:"            ,            "q"            ,            ((            q            (            i            ,            j            ),            j            =            ane            ,            size            (            A            ,            2            )),            i            =            i            ,            size            (            A            ,            one            ))            !  Use p equally an ordinary assortment            allocate            (            p            (            1            :            m            *            due north            ),            stat            =            istat            )            if            (            istat            /=            0            )            stop            'Error during allocation of p'            !  Perform some assortment performance            p            =            reshape            (            DoSomething            (            A            +            A            **            2            ),            shape            (            p            ))            !  Array functioning:            !      p(1) = 3            !      p(2) = 21            !      p(three) = 91            !      p(4) = 21            !      p(5) = 31            !      p(half-dozen) = 43            !      p(vii) = 57            !      p(8) = 73            !      p(nine) = 91            write            (            *            ,            '("Array operation:" / (4x,"p(",i0,") = ",i0))'            )            (            i            ,            p            (            i            ),            i            =            1            ,            size            (            p            ))            deallocate            (            A            ,            p            ,            q            ,            stat            =            istat            )            if            (            istat            /=            0            )            end            'Error during deallocation'            finish programme                        Test          

Module programming [edit | edit source]

A module is a programme unit of measurement which contains information definitions, global information, and CONTAINed procedures. Unlike a simple INCLUDE file, a module is an independent program unit of measurement that can be compiled separately and linked in its binary course. Once compiled, a module's public contents tin can be made visible to a calling routine via the Apply statement.

The module machinery makes the explicit interface of procedures easily available to calling routines. In fact, modern Fortran encourages every SUBROUTINE and Office to exist Contained in a MODULE. This allows the programmer to utilize the newer argument passing options and allows the compiler to perform full type checking on the interface.

The following example as well illustrates derived types, overloading of operators and generic procedures.

                        module                        GlobalModule            !  Reference to a pair of procedures included in a previously compiled            !  module named PortabilityLibrary            utilise                        PortabilityLibrary            ,            only            :            GetLastError            ,            &            ! Generic procedure            Date            ! Specific procedure            !  Constants            integer            ,            parameter            ::            dp_k            =            kind            (            ane.0d0            )            ! Double precision kind            real            ,            parameter            ::            null            =            (            0.            )            real            (            dp_k            ),            parameter            ::            pi            =            three.141592653589793_dp_k            !  Variables            integer            ::            n            ,            1000            ,            retint            logical            ::            status            ,            retlog            character            (            50            )            ::            AppName            !  Arrays            existent            ,            allocatable            ,            dimension            (:,:,:)            ::            a            ,            b            ,            c            ,            d            complex            (            dp_k            ),            allocatable            ,            dimension            (:)            ::            z            !  Derived type definitions            blazon                        ijk            integer            ::            i            integer            ::            j            integer            ::            g            end type                        ijk            type                        matrix            integer                        m            ,            n            real            ,            allocatable            ::            a            (:,:)            ! Fortran 2003 feature. For Fortran 95, use the pointer aspect instead            end type                        matrix            !  All the variables and procedures from this module can be accessed            !  by other program units, except for AppName            public                          private            ::            AppName            !  Generic procedure swap            interface                        swap            module process                        swap_integer            ,            swap_real            end interface                        swap            interface                        GetLastError            ! This adds a new, additional procedure to the            ! generic procedure GetLastError            module process                        GetLastError_GlobalModule            stop interface                        GetLastError            !  Operator overloading            interface                        operator            (            +            )            module procedure                        add_ijk            end interface            !  Paradigm for external procedure            interface                          part                        gauss_sparse            (            num_iter            ,            tol            ,            b            ,            A            ,            x            ,            actual_iter            )            upshot            (            tol_max            )            real            ::            tol_max            integer            ,            intent            (            in            )            ::            num_iter            real            ,            intent            (            in            )            ::            tol            existent            ,            intent            (            in            ),            dimension            (:)            ::            b            ,            A            (:,:)            real            ,            intent            (            inout            )            ::            x            (:)            integer            ,            optional            ,            intent            (            out            )            ::            actual_iter            end office                        gauss_sparse            end interface            !  Procedures included in the module            contains            !  Internal role            office                        add_ijk            (            ijk_1            ,            ijk_2            )            type            (            ijk            )            add_ijk            ,            ijk_1            ,            ijk_2            intent            (            in            )            ::            ijk_1            ,            ijk_2            add_ijk            =            ijk            (            ijk_1            %            i            +            ijk_2            %            i            ,            ijk_1            %            j            +            ijk_2            %            j            ,            ijk_1            %            k            +            ijk_2            %            chiliad            )            end function                        add_ijk            !  Include external files            include            'swap_integer.f90'            ! Comments SHOULDN'T be added on include lines            include            'swap_real.f90'            end module                        GlobalModule          

parkeryoulthalater1997.blogspot.com

Source: https://en.wikibooks.org/wiki/Fortran/Fortran_examples

0 Response to "Fortran Quickest Way to Read Specific Place"

ارسال یک نظر

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel