Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" Fortran compiler """
# globals=None means: use user globals from REPL
return "Interface to Fortran compiler"
""" Compile fortran code ``x`` and adds the functions in it to ``globals``.
INPUT:
- ``x`` -- Fortran code
- ``globals`` -- a dict to which to add the functions from the fortran module
- ``locals`` -- ignored
EXAMPLES::
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.])
TESTS::
sage: os.chdir(SAGE_ROOT) sage: fortran.eval("SYNTAX ERROR !@#$") Traceback (most recent call last): ... RuntimeError: failed to compile Fortran code:... sage: os.getcwd() == SAGE_ROOT True """
# Create everything in a temporary directory
# if the first line has !f90 as a comment, gfortran will # treat it as Fortran 90 code fortran_file = name + '.f90' else:
s_lib_path = s_lib_path + "-L%s "
s_lib = s_lib + "-l%s "%s
s_lib_path, s_lib, log)
# Note that f2py() doesn't raise an exception if it fails. # In that case, the import below will fail. finally:
print(log_string) finally:
# Do not delete temporary DLLs on Cygwin; this will cause # future forks of this process to fail. Instead temporary DLLs # will be cleaned up upon process exit except OSError: # This can fail for example over NFS pass
self.libraries.append(s)
self.library_paths.append(s)
# An instance |