Fortran compiler - Utilities

Skip to content
Back to top
View this page
Edit this page

# Fortran compiler

class sage.misc.inline_fortran.InlineFortran(globals=None)

  Bases: `object`

  add_library(s)
  add_library_path(s)
  eval(x, globals=None, locals=None)

    Compile fortran code `x` and adds the functions in it to `globals`.

    INPUT:

    - `x` – fortran code

    - `globals` – dictionary to which to add the functions from the fortran module

    - `locals` – ignored

    EXAMPLES:

    Sage

        sage: # needs numpy
        sage: code = '''
        ....: C FILE: FIB1.F
        ....:       SUBROUTINE FIB(A,N)
        ....: C
        ....: C     CALCULATE FIRST N FIBONACCI NUMBERS
        ....: C
        ....:       INTEGER N
        ....:       REAL*8 A(N)
        ....:       DO I=1,N
        ....:          IF (I.EQ.1) THEN
        ....:             A(I) = 0.0D0
        ....:          ELSEIF (I.EQ.2) THEN
        ....:             A(I) = 1.0D0
        ....:          ELSE
        ....:             A(I) = A(I-1) + A(I-2)
        ....:          ENDIF
        ....:       ENDDO
        ....:       END
        ....: C END FILE FIB1.F
        ....: '''
        sage: fortran(code, globals())
        sage: import numpy
        sage: a = numpy.array(range(10), dtype=float)
        sage: fib(a, 10)
        sage: a
        array([  0.,   1.,   1.,   2.,   3.,   5.,   8.,  13.,  21.,  34.])

    Python

        >>> from sage.all import *
        >>> # needs numpy
        >>> code = '''
        ... C FILE: FIB1.F
        ...       SUBROUTINE FIB(A,N)
        ... C
        ... C     CALCULATE FIRST N FIBONACCI NUMBERS
        ... C
        ...       INTEGER N
        ...       REAL*8 A(N)
        ...       DO I=1,N
        ...          IF (I.EQ.1) THEN
        ...             A(I) = 0.0D0
        ...          ELSEIF (I.EQ.2) THEN
        ...             A(I) = 1.0D0
        ...          ELSE
        ...             A(I) = A(I-1) + A(I-2)
        ...          ENDIF
        ...       ENDDO
        ...       END
        ... C END FILE FIB1.F
        ... '''
        >>> fortran(code, globals())
        >>> import numpy
        >>> a = numpy.array(range(Integer(10)), dtype=float)
        >>> fib(a, Integer(10))
        >>> a
        array([  0.,   1.,   1.,   2.,   3.,   5.,   8.,  13.,  21.,  34.])