Skip to content

FORTRAN¤

This example shows how to access a 32-bit FORTRAN library from 64-bit Python. Fortran32 is the 32-bit server and Fortran64 is the 64-bit client. The source code of the FORTRAN program is available here.

Attention

If you have issues running the example make sure that you have the prerequisites installed.

Important

By default, ctypes expects that a c_int data type is returned from the library call. If the returned value from the library is not a c_int then you must redefine the ctypes restype value to be the appropriate data type. Also, the input arguments must be passed by reference. The Fortran32 class shows various examples of passing arguments by reference and defining the restype value.

Create a Fortran64 client to communicate with the 32-bit fortran_lib32 library

>>> from msl.examples.loadlib import Fortran64
>>> f = Fortran64()

Numeric types¤

Add two int8 values, see Fortran64.sum_8bit

>>> f.sum_8bit(-50, 110)
60

Add two int16 values, see Fortran64.sum_16bit

>>> f.sum_16bit(2**15-1, -1)
32766

Add two int32 values, see Fortran64.sum_32bit

>>> f.sum_32bit(123456788, 1)
123456789

Add two int64 values, see Fortran64.sum_64bit

>>> f.sum_64bit(2**63, -2**62)
4611686018427387904

Multiply two float32 values, see Fortran64.multiply_float32

>>> f.multiply_float32(2.0, 3.0)
6.0

Multiply two float64 values, see Fortran64.multiply_float64

>>> f.multiply_float64(1e30, 2e3)
2.00000000000...e+33

Check if a value is positive, see Fortran64.is_positive

>>> f.is_positive(1.0)
True
>>> f.is_positive(-0.1)
False

Add or subtract two integers, see Fortran64.add_or_subtract

>>> f.add_or_subtract(1000, 2000, do_addition=True)
3000
>>> f.add_or_subtract(1000, 2000, do_addition=False)
-1000

Calculate the n'th factorial, see Fortran64.factorial

>>> f.factorial(0)
1.0
>>> f.factorial(127)
3.012660018457658e+213

Compute the Bessel function of the first kind of order 0, see Fortran64.besselJ0

>>> f.besselJ0(8.6)
0.0146229912787412...

Arrays¤

Calculate the standard deviation of a list of values, see Fortran64.standard_deviation

>>> f.standard_deviation([float(val) for val in range(1,10)])
2.73861278752583...

Add two 1D arrays, see Fortran64.add_1d_arrays

>>> a = [float(val) for val in range(1, 10)]
>>> b = [0.5*val for val in range(1, 10)]
>>> a
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
>>> b
[0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5]
>>> f.add_1d_arrays(a, b)
[1.5, 3.0, 4.5, 6.0, 7.5, 9.0, 10.5, 12.0, 13.5]

Multiply two matrices, see Fortran64.matrix_multiply

>>> m1 = [[1, 2, 3], [4, 5, 6]]
>>> m2 = [[1, 2], [3, 4], [5, 6]]
>>> f.matrix_multiply(m1, m2)
[[22.0, 28.0], [49.0, 64.0]]

Strings¤

Reverse a string, see Fortran64.reverse_string

>>> f.reverse_string("hello world!")
'!dlrow olleh'

You have access to the server's stdout and stderr streams when you shut down the server

>>> stdout, stderr = f.shutdown_server32()