Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fortran LUT

Status
Not open for further replies.

anust

Technical User
Jul 23, 2012
3
CA
I need to make look up tables for some of my functions to reduce run time for my fortran77 program. Can somebody tell me how to make a LUT and then how to call it in program. For example if I need a LUT of factorials up to 10! how to do that. Couldn't find anything searching the web.
Thanks
 
For function with integer argument I would precompute some values and store it in a global array. When necessary then make a wrapper function which get the values from the array.
You say, that you want it written in Fortran 77 - I only have g77 but I coded in free-form, because I don't want to bother with fixed-form.
Below is an example for factorial up to 12, however it uses things like COMMON, DATA, BLOCK DATA which are now obsolete.
fac_example.f
Code:
[COLOR=#a020f0]program[/color] lookup
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
  [COLOR=#2e8b57][b]integer[/b][/color] nmax
  [COLOR=#2e8b57][b]parameter[/b][/color](nmax [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]12[/color])
  [COLOR=#2e8b57][b]integer[/b][/color] factorial_array(nmax), factorial, fac_res
  [COLOR=#2e8b57][b]common[/b][/color] [COLOR=#804040][b]/[/b][/color]globals[COLOR=#804040][b]/[/b][/color]factorial_array

  [COLOR=#2e8b57][b]integer[/b][/color] j 
  [COLOR=#804040][b]do[/b][/color] j[COLOR=#804040][b]=-[/b][/color][COLOR=#ff00ff]3[/color],[COLOR=#ff00ff]15[/color]
    fac_res [COLOR=#804040][b]=[/b][/color] factorial(j)
    [COLOR=#804040][b]if[/b][/color] (fac_res [COLOR=#804040][b]>[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
      [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"factorial("[/color], j, [COLOR=#ff00ff]") = "[/color], fac_res
    [COLOR=#804040][b]else[/b][/color] [COLOR=#804040][b]if[/b][/color] (fac_res [COLOR=#804040][b].eq.[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
      [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"factorial("[/color], j, [COLOR=#ff00ff]") is undefined for this value !"[/color]
    [COLOR=#804040][b]else[/b][/color]
      [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"factorial("[/color], j, [COLOR=#ff00ff]") is out of lookup table range !"[/color]
    [COLOR=#804040][b]end if[/b][/color]
  [COLOR=#804040][b]end do[/b][/color]
[COLOR=#a020f0]end program[/color] lookup

[COLOR=#a020f0]block data[/color] init_factorial_array
  [COLOR=#2e8b57][b]integer[/b][/color] nmax
  [COLOR=#2e8b57][b]parameter[/b][/color](nmax [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]12[/color])
  [COLOR=#2e8b57][b]integer[/b][/color] factorial_array(nmax)
  [COLOR=#2e8b57][b]common[/b][/color] [COLOR=#804040][b]/[/b][/color]globals[COLOR=#804040][b]/[/b][/color]factorial_array
  [COLOR=#2e8b57][b]data[/b][/color] factorial_array[COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]1[/color],[COLOR=#ff00ff]2[/color],[COLOR=#ff00ff]6[/color],[COLOR=#ff00ff]24[/color],[COLOR=#ff00ff]120[/color],[COLOR=#ff00ff]720[/color],[COLOR=#ff00ff]5040[/color],[COLOR=#ff00ff]40320[/color],[COLOR=#804040][b]&[/b][/color]
        [COLOR=#ff00ff]362880[/color],[COLOR=#ff00ff]3628800[/color],[COLOR=#ff00ff]39916800[/color],[COLOR=#ff00ff]479001600[/color][COLOR=#804040][b]/[/b][/color]
[COLOR=#a020f0]end[/color] [COLOR=#a020f0]block data[/color] init_factorial_array

[COLOR=#2e8b57][b]integer[/b][/color] [COLOR=#a020f0]function[/color] factorial(n)
  [COLOR=#0000ff]! lookup table wrapper function[/color]
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
  [COLOR=#2e8b57][b]integer[/b][/color] nmax
  [COLOR=#2e8b57][b]parameter[/b][/color](nmax [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]12[/color])
  [COLOR=#2e8b57][b]integer[/b][/color] factorial_array(nmax)
  [COLOR=#2e8b57][b]common[/b][/color] [COLOR=#804040][b]/[/b][/color]globals[COLOR=#804040][b]/[/b][/color]factorial_array

  [COLOR=#2e8b57][b]integer[/b][/color] n, res
  [COLOR=#804040][b]if[/b][/color] (n [COLOR=#804040][b]<[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
     [COLOR=#0000ff]! Error undefined for this value[/color]
     factorial [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color]
  [COLOR=#804040][b]else[/b][/color] [COLOR=#804040][b]if[/b][/color] (n [COLOR=#804040][b]>[/b][/color] nmax) [COLOR=#804040][b]then[/b][/color]
     [COLOR=#0000ff]! Error out of lookup table range[/color]
     factorial [COLOR=#804040][b]=[/b][/color] [COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]100[/color] 
  [COLOR=#804040][b]else[/b][/color] [COLOR=#804040][b]if[/b][/color] (n [COLOR=#804040][b].eq.[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
     [COLOR=#0000ff]! factorial(0) = 1 [/color]
     factorial [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color]
  [COLOR=#804040][b]else[/b][/color]
     factorial [COLOR=#804040][b]=[/b][/color] factorial_array(n)
  [COLOR=#804040][b]end if[/b][/color]
[COLOR=#a020f0]end function[/color] factorial
Output:
Code:
$ g77 fac_example.f -o fac_example -ffree-form
$ fac_example   
 factorial( -3) is undefined for this value !
 factorial( -2) is undefined for this value !
 factorial( -1) is undefined for this value !
 factorial( 0) =  1
 factorial( 1) =  1
 factorial( 2) =  2
 factorial( 3) =  6
 factorial( 4) =  24
 factorial( 5) =  120
 factorial( 6) =  720
 factorial( 7) =  5040
 factorial( 8) =  40320
 factorial( 9) =  362880
 factorial( 10) =  3628800
 factorial( 11) =  39916800
 factorial( 12) =  479001600
 factorial( 13) is out of lookup table range !
 factorial( 14) is out of lookup table range !
 factorial( 15) is out of lookup table range !
 
Many thanks for your detailed reply and the program. So the values have to be stored as data to be called in later. What if we need to compute the values and make an array of them and store them as table. For example I have made function for Binomial(n,k) and now I don't want it to be computed every time when I call that function but to be picked up from a table from nth column and kth row. Is it possible? First to make such a table and then how to call it or is there other way of doing it?. I highly appreciate your help. My routine for Binomial is

************************************************************************
Integer Function Binomial(n,k)
implicit none
Integer Binom
integer n,k

If (k.lt.0) then
if (n.lt.0) then
Binomial=(-1)**(k-n)*Binom(-k-1,-n-1)
else
Binomial=0
end if
else
Binomial=Binom(n,k)
endif


return
end

*****************************************************
Integer Function Binom(n,k)
implicit none
Integer n,k,i

Binom=1

if (k.eq.0) then
Binom=1
goto 20
endif

if(n.eq.0) then
Binom=0
goto 20
endif

do i= 1,k
Binom=Binom*(n-(k-i))/i
enddo

20 return
end
 
Declare a lookup table as global array Bin_coeff(n_max,k_max) and create a wrapper function Binomial(n,k) which looks first for the array value Bin_coeff(n,k). If the value greater zero exists, then the function returns it and when not then it first computes the value and stores it in the lookup table using the formula
Bin_coeff(n,k) = Binomial(n-1,k-1) + Bin1omial(n-1,k)
and then finally returns it.

Here I found a similar example in C++
It sholdn't be very difficult to transform it into fortran
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top