Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Fortran language support #577

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
- "c_sharp or visual_basic or f_sharp"
- "r_lang"
- "elixir"
- "fortran"
- "dart or powershell"
steps:
- name: Checkout repository
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && \
dotnet-sdk-6.0 \
g++ \
gcc \
gfortran \
git \
golang-go \
haskell-platform \
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ make docker-build docker-pre-pr
- C#
- Dart
- F#
- Fortran90
- Go
- Haskell
- Java
Expand Down
29 changes: 29 additions & 0 deletions generated_code_examples/fortran/classification/decision_tree.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Model
implicit none
contains
function score(input)
implicit none
double precision, dimension(:) :: input
double precision, dimension(3) :: var0
double precision, dimension(3) :: score
if (input(3) <= 2.449999988079071d0) then
var0 = (/ 1.0d0, 0.0d0, 0.0d0 /)
else
if (input(4) <= 1.75d0) then
if (input(3) <= 4.950000047683716d0) then
if (input(4) <= 1.6500000357627869d0) then
var0 = (/ 0.0d0, 1.0d0, 0.0d0 /)
else
var0 = (/ 0.0d0, 0.0d0, 1.0d0 /)
end if
else
var0 = (/ 0.0d0, 0.3333333333333333d0, 0.6666666666666666d0 /)
end if
else
var0 = (/ 0.0d0, 0.021739130434782608d0, 0.9782608695652174d0 /)
end if
end if
score(:) = var0
return
end function score
end module Model
111 changes: 111 additions & 0 deletions generated_code_examples/fortran/classification/lightgbm.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
module Model
implicit none
contains
function score(input)
implicit none
double precision, dimension(:) :: input
double precision :: var0
double precision :: var1
double precision :: var2
double precision :: var3
double precision :: var4
double precision :: var5
double precision, dimension(3) :: score
if (input(3) > 3.1500000000000004d0) then
var0 = -1.1986122886681099d0
else
if (input(2) > 3.35d0) then
var0 = -0.8986122886681098d0
else
var0 = -0.9136122886681098d0
end if
end if
if (input(3) > 3.1500000000000004d0) then
if (input(3) > 4.450000000000001d0) then
var1 = -0.09503010837903424d0
else
var1 = -0.09563272415214283d0
end if
else
if (input(2) > 3.35d0) then
var1 = 0.16640323607832397d0
else
var1 = 0.15374604217339707d0
end if
end if
if (input(3) > 1.8d0) then
if (input(4) > 1.6500000000000001d0) then
var2 = -1.2055899476674514d0
else
var2 = -0.9500445227622534d0
end if
else
var2 = -1.2182214705715104d0
end if
if (input(4) > 0.45000000000000007d0) then
if (input(4) > 1.6500000000000001d0) then
var3 = -0.08146437273923739d0
else
var3 = 0.14244886188108738d0
end if
else
if (input(3) > 1.4500000000000002d0) then
var3 = -0.0950888159264695d0
else
var3 = -0.09438233722389686d0
end if
end if
if (input(4) > 1.6500000000000001d0) then
if (input(3) > 5.3500000000000005d0) then
var4 = -0.8824095771015287d0
else
var4 = -0.9121126703829481d0
end if
else
if (input(3) > 4.450000000000001d0) then
var4 = -1.1277829563828181d0
else
var4 = -1.1794405099157212d0
end if
end if
if (input(3) > 4.750000000000001d0) then
if (input(3) > 5.150000000000001d0) then
var5 = 0.16625543464258166d0
else
var5 = 0.09608601737074281d0
end if
else
if (input(1) > 4.950000000000001d0) then
var5 = -0.09644547407948921d0
else
var5 = -0.08181864271444342d0
end if
end if
score(:) = SOFTMAX((/ var0 + var1, var2 + var3, var4 + var5 /))
return
end function score
function softmax(x) result(res)
implicit none
double precision, dimension(:), intent(in) :: x
double precision, dimension(size(x)) :: res
double precision :: max_val, sum_val
integer :: i

! Find maximum value in x
max_val = x(1)
do i = 2, size(x)
if (x(i) > max_val) then
max_val = x(i)
end if
end do

! Compute softmax values
sum_val = 0.0d0
do i = 1, size(x)
res(i) = exp(x(i) - max_val)
sum_val = sum_val + res(i)
end do
res = res / sum_val

end function softmax
end module Model
12 changes: 12 additions & 0 deletions generated_code_examples/fortran/classification/linear.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Model
implicit none
contains
function score(input)
implicit none
double precision, dimension(:) :: input

double precision, dimension(3) :: score
score(:) = (/ 9.700311953536998d0 + input(1) * -0.4128360473754751d0 + input(2) * 0.9680426131053453d0 + input(3) * -2.498310603183548d0 + input(4) * -1.0723230787022542d0, 2.1575759475871163d0 + input(1) * 0.5400806228605453d0 + input(2) * -0.3245383349519669d0 + input(3) * -0.2034493200950831d0 + input(4) * -0.9338183426196143d0, -11.857887901124615d0 + input(1) * -0.12724457548509432d0 + input(2) * -0.6435042781533917d0 + input(3) * 2.7017599232786216d0 + input(4) * 2.006141421321863d0 /)
return
end function score
end module Model
71 changes: 71 additions & 0 deletions generated_code_examples/fortran/classification/random_forest.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
module Model
implicit none
contains
function score(input)
implicit none
double precision, dimension(:) :: input
double precision, dimension(3) :: var0
double precision, dimension(3) :: var1
double precision, dimension(3) :: score
if (input(4) <= 0.75d0) then
var0 = (/ 1.0d0, 0.0d0, 0.0d0 /)
else
if (input(3) <= 4.75d0) then
var0 = (/ 0.0d0, 1.0d0, 0.0d0 /)
else
if (input(3) <= 5.049999952316284d0) then
if (input(4) <= 1.75d0) then
var0 = (/ 0.0d0, 0.8333333333333334d0, 0.16666666666666666d0 /)
else
var0 = (/ 0.0d0, 0.08333333333333333d0, 0.9166666666666666d0 /)
end if
else
var0 = (/ 0.0d0, 0.0d0, 1.0d0 /)
end if
end if
end if
if (input(4) <= 0.800000011920929d0) then
var1 = (/ 1.0d0, 0.0d0, 0.0d0 /)
else
if (input(1) <= 6.25d0) then
if (input(3) <= 4.8500001430511475d0) then
var1 = (/ 0.0d0, 0.9487179487179487d0, 0.05128205128205128d0 /)
else
var1 = (/ 0.0d0, 0.0d0, 1.0d0 /)
end if
else
if (input(4) <= 1.550000011920929d0) then
var1 = (/ 0.0d0, 0.8333333333333334d0, 0.16666666666666666d0 /)
else
var1 = (/ 0.0d0, 0.02564102564102564d0, 0.9743589743589743d0 /)
end if
end if
end if
score(:) = mul_vector_number(add_vectors(var0, var1), 0.5d0)
return
end function score
function add_vectors(v1, v2) result(res)
implicit none
double precision, dimension(:), intent(in) :: v1, v2
double precision, dimension(size(v1)) :: res
integer :: i

do i = 1, size(v1)
res(i) = v1(i) + v2(i)
end do

end function add_vectors

function mul_vector_number(v1, num) result(res)
implicit none
double precision, dimension(:), intent(in) :: v1
double precision, intent(in) :: num
double precision, dimension(size(v1)) :: res
integer :: i

do i = 1, size(v1)
res(i) = v1(i) * num
end do

end function mul_vector_number
end module Model
67 changes: 67 additions & 0 deletions generated_code_examples/fortran/classification/svm.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module Model
implicit none
contains
function score(input)
implicit none
double precision, dimension(:) :: input
double precision :: var0
double precision :: var1
double precision :: var2
double precision :: var3
double precision :: var4
double precision :: var5
double precision :: var6
double precision :: var7
double precision :: var8
double precision :: var9
double precision :: var10
double precision :: var11
double precision :: var12
double precision :: var13
double precision :: var14
double precision :: var15
double precision :: var16
double precision :: var17
double precision :: var18
double precision :: var19
double precision :: var20
double precision :: var21
double precision :: var22
double precision :: var23
double precision :: var24
double precision :: var25
double precision :: var26
double precision :: var27
double precision, dimension(3) :: score
var0 = EXP(-0.06389634699048878d0 * ((5.1d0 - input(1)) ** 2.0d0 + (2.5d0 - input(2)) ** 2.0d0 + (3.0d0 - input(3)) ** 2.0d0 + (1.1d0 - input(4)) ** 2.0d0))
var1 = EXP(-0.06389634699048878d0 * ((4.9d0 - input(1)) ** 2.0d0 + (2.4d0 - input(2)) ** 2.0d0 + (3.3d0 - input(3)) ** 2.0d0 + (1.0d0 - input(4)) ** 2.0d0))
var2 = EXP(-0.06389634699048878d0 * ((6.3d0 - input(1)) ** 2.0d0 + (2.5d0 - input(2)) ** 2.0d0 + (4.9d0 - input(3)) ** 2.0d0 + (1.5d0 - input(4)) ** 2.0d0))
var3 = EXP(-0.06389634699048878d0 * ((5.4d0 - input(1)) ** 2.0d0 + (3.0d0 - input(2)) ** 2.0d0 + (4.5d0 - input(3)) ** 2.0d0 + (1.5d0 - input(4)) ** 2.0d0))
var4 = EXP(-0.06389634699048878d0 * ((6.2d0 - input(1)) ** 2.0d0 + (2.2d0 - input(2)) ** 2.0d0 + (4.5d0 - input(3)) ** 2.0d0 + (1.5d0 - input(4)) ** 2.0d0))
var5 = EXP(-0.06389634699048878d0 * ((5.6d0 - input(1)) ** 2.0d0 + (2.9d0 - input(2)) ** 2.0d0 + (3.6d0 - input(3)) ** 2.0d0 + (1.3d0 - input(4)) ** 2.0d0))
var6 = EXP(-0.06389634699048878d0 * ((6.7d0 - input(1)) ** 2.0d0 + (3.0d0 - input(2)) ** 2.0d0 + (5.0d0 - input(3)) ** 2.0d0 + (1.7d0 - input(4)) ** 2.0d0))
var7 = EXP(-0.06389634699048878d0 * ((5.0d0 - input(1)) ** 2.0d0 + (2.3d0 - input(2)) ** 2.0d0 + (3.3d0 - input(3)) ** 2.0d0 + (1.0d0 - input(4)) ** 2.0d0))
var8 = EXP(-0.06389634699048878d0 * ((6.0d0 - input(1)) ** 2.0d0 + (2.7d0 - input(2)) ** 2.0d0 + (5.1d0 - input(3)) ** 2.0d0 + (1.6d0 - input(4)) ** 2.0d0))
var9 = EXP(-0.06389634699048878d0 * ((5.9d0 - input(1)) ** 2.0d0 + (3.2d0 - input(2)) ** 2.0d0 + (4.8d0 - input(3)) ** 2.0d0 + (1.8d0 - input(4)) ** 2.0d0))
var10 = EXP(-0.06389634699048878d0 * ((5.7d0 - input(1)) ** 2.0d0 + (2.6d0 - input(2)) ** 2.0d0 + (3.5d0 - input(3)) ** 2.0d0 + (1.0d0 - input(4)) ** 2.0d0))
var11 = EXP(-0.06389634699048878d0 * ((5.0d0 - input(1)) ** 2.0d0 + (3.0d0 - input(2)) ** 2.0d0 + (1.6d0 - input(3)) ** 2.0d0 + (0.2d0 - input(4)) ** 2.0d0))
var12 = EXP(-0.06389634699048878d0 * ((5.4d0 - input(1)) ** 2.0d0 + (3.4d0 - input(2)) ** 2.0d0 + (1.7d0 - input(3)) ** 2.0d0 + (0.2d0 - input(4)) ** 2.0d0))
var13 = EXP(-0.06389634699048878d0 * ((5.7d0 - input(1)) ** 2.0d0 + (3.8d0 - input(2)) ** 2.0d0 + (1.7d0 - input(3)) ** 2.0d0 + (0.3d0 - input(4)) ** 2.0d0))
var14 = EXP(-0.06389634699048878d0 * ((4.8d0 - input(1)) ** 2.0d0 + (3.4d0 - input(2)) ** 2.0d0 + (1.9d0 - input(3)) ** 2.0d0 + (0.2d0 - input(4)) ** 2.0d0))
var15 = EXP(-0.06389634699048878d0 * ((4.5d0 - input(1)) ** 2.0d0 + (2.3d0 - input(2)) ** 2.0d0 + (1.3d0 - input(3)) ** 2.0d0 + (0.3d0 - input(4)) ** 2.0d0))
var16 = EXP(-0.06389634699048878d0 * ((5.7d0 - input(1)) ** 2.0d0 + (4.4d0 - input(2)) ** 2.0d0 + (1.5d0 - input(3)) ** 2.0d0 + (0.4d0 - input(4)) ** 2.0d0))
var17 = EXP(-0.06389634699048878d0 * ((5.1d0 - input(1)) ** 2.0d0 + (3.8d0 - input(2)) ** 2.0d0 + (1.9d0 - input(3)) ** 2.0d0 + (0.4d0 - input(4)) ** 2.0d0))
var18 = EXP(-0.06389634699048878d0 * ((5.1d0 - input(1)) ** 2.0d0 + (3.3d0 - input(2)) ** 2.0d0 + (1.7d0 - input(3)) ** 2.0d0 + (0.5d0 - input(4)) ** 2.0d0))
var19 = EXP(-0.06389634699048878d0 * ((6.2d0 - input(1)) ** 2.0d0 + (2.8d0 - input(2)) ** 2.0d0 + (4.8d0 - input(3)) ** 2.0d0 + (1.8d0 - input(4)) ** 2.0d0))
var20 = EXP(-0.06389634699048878d0 * ((7.2d0 - input(1)) ** 2.0d0 + (3.0d0 - input(2)) ** 2.0d0 + (5.8d0 - input(3)) ** 2.0d0 + (1.6d0 - input(4)) ** 2.0d0))
var21 = EXP(-0.06389634699048878d0 * ((6.1d0 - input(1)) ** 2.0d0 + (3.0d0 - input(2)) ** 2.0d0 + (4.9d0 - input(3)) ** 2.0d0 + (1.8d0 - input(4)) ** 2.0d0))
var22 = EXP(-0.06389634699048878d0 * ((6.0d0 - input(1)) ** 2.0d0 + (3.0d0 - input(2)) ** 2.0d0 + (4.8d0 - input(3)) ** 2.0d0 + (1.8d0 - input(4)) ** 2.0d0))
var23 = EXP(-0.06389634699048878d0 * ((4.9d0 - input(1)) ** 2.0d0 + (2.5d0 - input(2)) ** 2.0d0 + (4.5d0 - input(3)) ** 2.0d0 + (1.7d0 - input(4)) ** 2.0d0))
var24 = EXP(-0.06389634699048878d0 * ((7.9d0 - input(1)) ** 2.0d0 + (3.8d0 - input(2)) ** 2.0d0 + (6.4d0 - input(3)) ** 2.0d0 + (2.0d0 - input(4)) ** 2.0d0))
var25 = EXP(-0.06389634699048878d0 * ((5.6d0 - input(1)) ** 2.0d0 + (2.8d0 - input(2)) ** 2.0d0 + (4.9d0 - input(3)) ** 2.0d0 + (2.0d0 - input(4)) ** 2.0d0))
var26 = EXP(-0.06389634699048878d0 * ((6.0d0 - input(1)) ** 2.0d0 + (2.2d0 - input(2)) ** 2.0d0 + (5.0d0 - input(3)) ** 2.0d0 + (1.5d0 - input(4)) ** 2.0d0))
var27 = EXP(-0.06389634699048878d0 * ((6.3d0 - input(1)) ** 2.0d0 + (2.8d0 - input(2)) ** 2.0d0 + (5.1d0 - input(3)) ** 2.0d0 + (1.5d0 - input(4)) ** 2.0d0))
score(:) = (/ 0.11172510039290856d0 + var0 * -0.8898986041811555d0 + var1 * -0.8898986041811555d0 + var2 * -0.0d0 + var3 * -0.0d0 + var4 * -0.0d0 + var5 * -0.756413813553974d0 + var6 * -0.0d0 + var7 * -0.8898986041811555d0 + var8 * -0.0d0 + var9 * -0.0d0 + var10 * -0.8898986041811555d0 + var11 * 0.04218875216876044d0 + var12 * 0.7142250613852136d0 + var13 * 0.0d0 + var14 * 0.8898986041811555d0 + var15 * 0.8898986041811555d0 + var16 * 0.0d0 + var17 * 0.8898986041811555d0 + var18 * 0.8898986041811555d0, -0.04261957451303831d0 + var19 * -0.37953658977037247d0 + var20 * -0.0d0 + var21 * -0.0d0 + var22 * -0.37953658977037247d0 + var23 * -0.37953658977037247d0 + var24 * -0.26472396872040066d0 + var25 * -0.3745962010653211d0 + var26 * -0.10077618026650095d0 + var27 * -0.0d0 + var11 * 0.0d0 + var12 * 0.0d0 + var13 * 0.37953658977037247d0 + var14 * 0.37953658977037247d0 + var15 * 0.3044555865539922d0 + var16 * 0.05610417372785803d0 + var17 * 0.37953658977037247d0 + var18 * 0.37953658977037247d0, 1.8136162062461285d0 + var19 * -110.34516826676301d0 + var20 * -13.999391039896215d0 + var21 * -108.44329471899991d0 + var22 * -110.34516826676301d0 + var23 * -22.21095753342801d0 + var24 * -0.0d0 + var25 * -0.0d0 + var26 * -65.00217641452454d0 + var27 * -110.34516826676301d0 + var0 * 0.0d0 + var1 * 0.0d0 + var2 * 110.34516826676301d0 + var3 * 62.115561183470184d0 + var4 * 37.19509025661546d0 + var5 * 0.0d0 + var6 * 110.34516826676301d0 + var7 * 0.0d0 + var8 * 110.34516826676301d0 + var9 * 110.34516826676301d0 + var10 * 0.0d0 /)
return
end function score
end module Model
Loading