Fortran Function explanation
c#, fortran, function
Solution
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;
}
Problem
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#