workoverflow
workoverflow

Reputation: 586

Fortran Function explanation

I have this function in Fortran and i'm trying to recode it in C#

C **************************************************************** 
C    FUNCTION   POLY 
C***************************************************************** 
      FUNCTION POLY(N,A,X) 
      DIMENSION A(N) 
C
      POLY    = 0. 
      L       = N 
      DO 1 K  = 1,N 
      POLY    = POLY*X + A(L) 
1     L       = L-1 
      RETURN 
      END 
C***************************************************************** 

I found out that DIMENSION A(N) creates a vector of N values, but we already have a variable A from the function parameters, does this means that the array values all equal to A? If so then what is the use of A(N). By the way can anyone just explain what does this function do so i can re-implement it in C#

Upvotes: 3

Views: 261

Answers (4)

Phil H
Phil H

Reputation: 20141

      FUNCTION POLY(N,A,X)      ! implicitly real (float) function poly(int n,int a,real x)
      DIMENSION A(N)            ! shape A as 1d array of n values in this scope
C                               ! say nothing (blank comment)
      POLY    = 0.              ! initialise return variable to float value 0
      L       = N               ! set L (implicitly integer) to n
      DO 1 K  = 1,N             ! for(int k=1; k<=n; ++k)
      POLY    = POLY*X + A(L)   !    update return variable
1     L       = L-1             !    decrement L
      RETURN                    ! return current value for poly
      END 

so in c-like syntax:

float poly(int n, int a, float x) {
    // redim a(n)
    float result = 0;
    int l = n;
    for(int k=1; k <= n; ++k) {
        result = result*x + a(l);
        --l;
    }
    return result;
}

The bit that doesn't translate is redimensioning A as an array. In C you would pass a pointer and use it as an array, and in C++/C# you'd probably pass a vector-like structure with its own length property.

In C#, using a list:

float poly(List<float> coeffs, float x) {
    float result = 0;
    for(int i=coeffs.Count-1; i >= 0; --i) {
        result = result*x + coeff[i];
    }
    return result;
}

Upvotes: 5

The line DIMENSION A(N) just declares details of the A dummy argument (PARAMETERs are something very different in Fortran), i.e. it says it is an array from 1 to N. The other ones are not declared this way, because the function uses implicit typing.

Upvotes: 2

AakashM
AakashM

Reputation: 63338

guessing slightly I think this is specifying that the parameter A itself is an array of N elements.

So for the C# equivalent you wouldn't need a separate N parameter; you would only need to pass A as a double[], as in .NET arrays can tell you their .Length.

The function evaluates polynomials using Horner's method.

Upvotes: 2

David Heffernan
David Heffernan

Reputation: 612993

It evaluates a polynomial in x of the form:

a[1] + a[2]x + a[3]x^2 + ... a[N]x^(N-1)

Remember that Fortran uses 1-based array indices and I have followed that convention in this equation.


You could write it in C# like this:

double EvaluatePolynomial(double[] a, double x)
{
    double result = 0.0;
    int i = a.Length;
    while (i>0)
    {
        i--;
        result = result*x + a[i];
    }
    return result;
}

Here we are using 0-based array indexing appropriate to C#. And so this function evalutates a polynomial in x of the form:

a[0] + a[1]x + a[2]x^2 + ... a[N-1]x^(N-1)

Upvotes: 3

Related Questions