1

I'm writing my first numerical optimization program (Newton's method) and my first Fortran program too. I started with Python to understand the first problem and now I'm porting to Fortran to work on the other. (One thing at a time, right?)

In Python, there's a handy way of passing arguments to a function: unpacking a list, like so:

def f(x_1, x_2):
    """Just a function with two arguments."""
    return math.sqrt(x_1 ** 2 + x_2 ** 2)

...
f(*[4, 3])  # calls f with parameters 4 and 3
# output: 5

Does Fortran have something resembling this star operator? I put my 2-dimensional points into a matrix, and I'm trying to learn the most conventional way of passing one of my 2D point "vectors" to a function to be evaluated. It looks a little like this:

! "double precision", using SELECTED_REAL_KIND
REAL(KIND=dp), DIMENSION(100, 2) :: iterates = 0.0_dp

! f expects two REALs
REAL(KIND=dp) :: first_value = f(iterates(1, :))
Ian Bush
  • 6,996
  • 1
  • 21
  • 27

1 Answers1

2

No.

You can make your function accept a vector. If the function is from a dependency, you can write a wrapper:

function f_array_input(x)
  real(kind=dp), intent(in) :: x(2)
  real(kind=dp) :: f_array_input
  f_array_input = f(x(1), x(2))
end function

(

Pierre de Buyl
  • 7,074
  • 2
  • 16
  • 22
  • 1
    Thanks, this was my next guess. I suppose Python has spoiled me into hastily designing poor function interfaces. –  Mar 21 '20 at 02:30