Fortran Feature Description

I have this function in Fortran and I'm trying to transcode it into 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 that it DIMENSION A(N)creates a vector of values N, but we already have a variable Afrom the parameters of the function, does this mean that the values โ€‹โ€‹of the array are equal A? If so, what to use A(N). By the way, can anyone explain what this function does, I can reimplement it in C #

+3
source share
4 answers
      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 

therefore, 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;
}

A bit that does not translate is to redistribute A as an array. In C, you must pass a pointer and use it as an array, and in C ++ / C # you will probably pass a vector structure with a property of its own length.

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;
}
+5

x :

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

, Fortran 1, .


# :

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;
}

0, #. evalutates x :

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

, , , A N.

, # N; A double[], .NET- .Length.

, .

+2

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

+2
source

All Articles