msl.examples.loadlib.fortran32 module

A wrapper around a 32-bit FORTRAN library, fortran_lib32.

Example of a server that loads a 32-bit FORTRAN library, fortran_lib32, in a 32-bit Python interpreter to host the library. The corresponding fortran64 module can be executed by a 64-bit Python interpreter and the Fortran64 class can send a request to the Fortran32 class which calls the 32-bit library to execute the request and then return the response from the library.

class msl.examples.loadlib.fortran32.Fortran32(host, port, **kwargs)

Bases: Server32

A wrapper around a 32-bit FORTRAN library, fortran_lib32.

This class demonstrates how to send/receive various data types to/from a 32-bit FORTRAN library via ctypes. For a summary of the FORTRAN data types see here.

Parameters:
  • host (str) – The IP address (or hostname) to use for the server.

  • port (int) – The port to open for the server.

  • kwargs (str) – Optional keyword arguments. The keys and values are of type str.

sum_8bit(a, b)

Add two 8-bit signed integers.

Python only has one int data type to represent integer values. The sum_8bit() method converts the data types of a and b to be ctypes.c_int8.

The corresponding FORTRAN code is

function sum_8bit(a, b) result(value)
    !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'sum_8bit' :: sum_8bit
    implicit none
    integer(1) :: a, b, value
    value = a + b
end function sum_8bit

See the corresponding 64-bit sum_8bit() method.

Parameters:
  • a (int) – First 8-bit signed integer.

  • b (int) – Second 8-bit signed integer.

Returns:

The sum of a and b.

Return type:

int

sum_16bit(a, b)

Add two 16-bit signed integers

Python only has one int data type to represent integer values. The sum_16bit() method converts the data types of a and b to be ctypes.c_int16.

The corresponding FORTRAN code is

function sum_16bit(a, b) result(value)
    !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'sum_16bit' :: sum_16bit
    implicit none
    integer(2) :: a, b, value
    value = a + b
end function sum_16bit

See the corresponding 64-bit sum_16bit() method.

Parameters:
  • a (int) – First 16-bit signed integer.

  • b (int) – Second 16-bit signed integer.

Returns:

The sum of a and b.

Return type:

int

sum_32bit(a, b)

Add two 32-bit signed integers.

Python only has one int data type to represent integer values. The sum_32bit() method converts the data types of a and b to be ctypes.c_int32.

The corresponding FORTRAN code is

function sum_32bit(a, b) result(value)
    !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'sum_32bit' :: sum_32bit
    implicit none
    integer(4) :: a, b, value
    value = a + b
end function sum_32bit

See the corresponding 64-bit sum_32bit() method.

Parameters:
  • a (int) – First 32-bit signed integer.

  • b (int) – Second 32-bit signed integer.

Returns:

The sum of a and b.

Return type:

int

sum_64bit(a, b)

Add two 64-bit signed integers.

Python only has one int data type to represent integer values. The sum_64bit() method converts the data types of a and b to be ctypes.c_int64.

The corresponding FORTRAN code is

function sum_64bit(a, b) result(value)
    !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'sum_64bit' :: sum_64bit
    implicit none
    integer(8) :: a, b, value
    value = a + b
end function sum_64bit

See the corresponding 64-bit sum_64bit() method.

Parameters:
  • a (int) – First 64-bit signed integer.

  • b (int) – Second 64-bit signed integer.

Returns:

The sum of a and b.

Return type:

int

multiply_float32(a, b)

Multiply two FORTRAN floating-point numbers.

The corresponding FORTRAN code is

function multiply_float32(a, b) result(value)
    !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'multiply_float32' :: multiply_float32
    implicit none
    real(4) :: a, b, value
    value = a * b
end function multiply_float32

See the corresponding 64-bit multiply_float32() method.

Parameters:
  • a (float) – First floating-point number.

  • b (float) – Second floating-point number.

Returns:

The product of a and b.

Return type:

float

multiply_float64(a, b)

Multiply two FORTRAN double-precision numbers.

The corresponding FORTRAN code is

function multiply_float64(a, b) result(value)
    !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'multiply_float64' :: multiply_float64
    implicit none
    real(8) :: a, b, value
    value = a * b
end function multiply_float64

See the corresponding 64-bit multiply_float64() method.

Parameters:
  • a (float) – First double-precision number.

  • b (float) – Second double-precision number.

Returns:

The product of a and b.

Return type:

float

is_positive(a)

Returns whether the value of the input argument is > 0.

The corresponding FORTRAN code is

function is_positive(a) result(value)
    !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'is_positive' :: is_positive
    implicit none
    logical :: value
    real(8) :: a
    value = a > 0.d0
end function is_positive

See the corresponding 64-bit is_positive() method.

Parameters:

a (float) – Double-precision number.

Returns:

Whether the value of a is > 0.

Return type:

bool

add_or_subtract(a, b, do_addition)

Add or subtract two integers.

The corresponding FORTRAN code is

function add_or_subtract(a, b, do_addition) result(value)
    !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'add_or_subtract' :: add_or_subtract
    implicit none
    logical :: do_addition
    integer(4) :: a, b, value
    if (do_addition) then
        value = a + b
    else
        value = a - b
    endif
end function add_or_subtract

See the corresponding 64-bit add_or_subtract() method.

Parameters:
  • a (int) – First integer.

  • b (int) – Second integer.

  • do_addition (bool) – Whether to add or subtract the numbers.

Returns:

a+b if do_addition is True else a-b.

Return type:

int

factorial(n)

Compute the n’th factorial.

The corresponding FORTRAN code is

function factorial(n) result(value)
    !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'factorial' :: factorial
    implicit none
    integer(1) :: n
    integer(4) :: i
    double precision value
    if (n < 0) then
        value = 0.d0
        print *, "Cannot compute the factorial of a negative number", n
    else
        value = 1.d0
        do i = 2, n
            value = value * i
        enddo
    endif
end function factorial

See the corresponding 64-bit factorial() method.

Parameters:

n (int) – The integer to computer the factorial of. The maximum allowed value is 127.

Returns:

The factorial of n.

Return type:

float

standard_deviation(data)

Compute the standard deviation.

The corresponding FORTRAN code is

function standard_deviation(a, n) result(var)
    !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'standard_deviation' :: standard_deviation
    integer :: n ! the length of the array
    double precision :: var, a(n)
    var = SUM(a)/SIZE(a) ! SUM is a built-in fortran function
    var = SQRT(SUM((a-var)**2)/(SIZE(a)-1.0))
end function standard_deviation

See the corresponding 64-bit standard_deviation() method.

Parameters:

data (Sequence[float]) – The values to compute the standard deviation of.

Returns:

The standard deviation of data.

Return type:

float

besselJ0(x)

Compute the Bessel function of the first kind of order 0 of x.

The corresponding FORTRAN code is

function besselj0(x) result(val)
    !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'besselj0' :: besselj0
    double precision :: x, val
    val = BESSEL_J0(x)
end function besselJ0

See the corresponding 64-bit besselJ0() method.

Parameters:

x (float) – The value to compute BESSEL_J0 of.

Returns:

The value of BESSEL_J0(x).

Return type:

float

reverse_string(original)

Reverse a string.

The corresponding FORTRAN code is

subroutine reverse_string(original, n, reversed)
    !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'reverse_string' :: reverse_string
    !DEC$ ATTRIBUTES REFERENCE :: original, reversed
    implicit none
    integer :: i, n
    character(len=n) :: original, reversed
    do i = 1, n
        reversed(i:i) = original(n-i+1:n-i+1)
    end do
end subroutine reverse_string

See the corresponding 64-bit reverse_string() method.

Parameters:

original (str) – The original string.

Returns:

The string reversed.

Return type:

str

add_1d_arrays(a1, a2)

Perform an element-wise addition of two 1D double-precision arrays.

The corresponding FORTRAN code is

subroutine add_1d_arrays(a, in1, in2, n)
    !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'add_1d_arrays' :: add_1d_arrays
    implicit none
    integer(4) :: n ! the length of the input arrays
    double precision :: in1(n), in2(n) ! the arrays to add (element-wise)
    double precision :: a(n) ! the array that will contain the element-wise sum
    a(:) = in1(:) + in2(:)
end subroutine add_1d_arrays

See the corresponding 64-bit add_1d_arrays() method.

Parameters:
Returns:

The element-wise addition of a1 + a2.

Return type:

list[float]

matrix_multiply(a1, a2)

Multiply two matrices.

The corresponding FORTRAN code is

subroutine matrix_multiply(a, a1, r1, c1, a2, r2, c2)
    !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'matrix_multiply' :: matrix_multiply
    implicit none
    integer(4) :: r1, c1, r2, c2 ! the dimensions of the input arrays
    double precision :: a1(r1,c1), a2(r2,c2) ! the arrays to multiply
    double precision :: a(r1,c2) ! resultant array
    a = MATMUL(a1, a2)
end subroutine matrix_multiply

Note

FORTRAN stores multidimensional arrays in column-major order, as opposed to row-major order for C (Python) arrays. Therefore, the input matrices need to be transposed before sending the matrices to FORTRAN and also the result needs to be transposed before returned.

See the corresponding 64-bit matrix_multiply() method.

Parameters:
Returns:

The product of a1 * a2.

Return type:

list[list[float]]