- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I try to write a function which returns a vector like
rlogsig = 1/(1+exp(-x)), where x is a vector, and rlogsig should be a vector.
My code is like
program main
real x(2), r(2)
x(1) = 1.0; x(2) = 2.0
r = rlogsig(x)
print *, r
end program main
function rlogsig(x)
rlogsig = 1/(1+exp(-x))
end function rlogsig
But, it does not work! Anyone can help
rlogsig = 1/(1+exp(-x)), where x is a vector, and rlogsig should be a vector.
My code is like
program main
real x(2), r(2)
x(1) = 1.0; x(2) = 2.0
r = rlogsig(x)
print *, r
end program main
function rlogsig(x)
rlogsig = 1/(1+exp(-x))
end function rlogsig
But, it does not work! Anyone can help
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
! You are making 3 fundamental mistakes here,
! any one of which will cause your program
! to fail
module all_my_functions
implicit none
contains
function rlogsig(x)
! (1) x is an array and so must be declared as such
! in the called function. I use an assumed-shape
! array so that your calling program need not pass
! an additional parameter indicating the size of x.
real, intent(in) :: x(:)
! (2) Since rlogsig returns an array-valued result,
! it must be declared with the appropriate shape
real rlogsig(size(x))
rlogsig = 1/(1+exp(-x))
end function rlogsig
end module all_my_functions
program main
! (3) The interface to rlogsig must be explicit in
! all programs which call it because it has an
! assumed-shape dummy argument and also because it
! it returns an array-valued result. Modules are
! the most common way to create this explicit
! interface.
use all_my_functions
implicit none
real x(2), r(2)
x = (/1.0, 2.0/)
r = rlogsig(x)
print *, r
end program main
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Another type:
module points
implicit none
type:: point
real:: x,y
end type point
contains
function rlogsig(c) result
type (point), intent(in)::c
type (point):: r
r%x = 1/(1+exp(-c%x))
r%y = 1/(1+exp(-c%y))
end function rlogsig
end module points
program example
use points
implicit none
type (point)::c
c%x=1.0; c%y=2.0
print*, 'rlogsig', rlogsig(c)
end program example
module points
implicit none
type:: point
real:: x,y
end type point
contains
function rlogsig(c) result
type (point), intent(in)::c
type (point):: r
r%x = 1/(1+exp(-c%x))
r%y = 1/(1+exp(-c%y))
end function rlogsig
end module points
program example
use points
implicit none
type (point)::c
c%x=1.0; c%y=2.0
print*, 'rlogsig', rlogsig(c)
end program example
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
! It occurred to me that you may also want to use
! an elemental function in this context
module other_functions
implicit none
contains
elemental function rlogsig(x)
real, intent(in) :: x
real rlogsig
rlogsig = 1/(1+exp(-x))
end function rlogsig
end module other_functions
program main
! Note that the interface to an elemental function
! must be explicit in the caller
use other_functions
implicit none
real x, y(2), z(2,2)
real r, s(size(y)), t(size(z,1),size(z,2))
x = 1.0
r = rlogsig(x)
write(*,'(f9.7)') r
y = (/2.0,3.0/)
s = rlogsig(y)
write(*,'(/2(f9.7:1x))') s
z = reshape((/4.0, 5.0, &
6.0, 7.0/), &
(/2,2/),order=(/2,1/))
t = rlogsig(z)
write(*,'(/2(f9.7:1x))') transpose(t)
end program main
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
James,
Thanks a lot. Both functions are what I am looking for
Thanks a lot. Both functions are what I am looking for
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page