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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is this intrinsic function?

Status
Not open for further replies.

milenko76

Technical User
Mar 26, 2010
100
PT
I have subroutine like this:
Subroutine FuncSub(N, PP, F)

Implicit None
integer :: N
real :: PP(N), F, func
F = func(PP)
end Subroutine FuncSub

I do not understand F = func(PP)?
 
Possible usage:
Code:
[COLOR=#a020f0]module[/color] my_foo
[COLOR=#a020f0]contains[/color]
[COLOR=#2e8b57][b]  real[/b][/color] [COLOR=#a020f0]function[/color] func(vec)
    [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
[COLOR=#2e8b57][b]    real[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color](:), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: vec
    [COLOR=#2e8b57][b]integer[/b][/color] :: i

    func[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]0[/color]
    [COLOR=#804040][b]do[/b][/color] i[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color], [COLOR=#008080]size[/color](vec)
      func [COLOR=#804040][b]=[/b][/color] func [COLOR=#804040][b]+[/b][/color] vec(i)
    [COLOR=#804040][b]end do[/b][/color]
  [COLOR=#a020f0]end function[/color] func
[COLOR=#a020f0]end module[/color] my_foo

[COLOR=#a020f0]program[/color] main
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
  [COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]parameter[/b][/color] :: input_array_size [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]2[/color]
[COLOR=#2e8b57][b]  real[/b][/color] :: input_array(input_array_size), output_result

  [COLOR=#0000ff]! set input vector[/color]
  input_array [COLOR=#804040][b]=[/b][/color] ([COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]1[/color]., [COLOR=#ff00ff]2[/color].[COLOR=#804040][b]/[/b][/color])
  [COLOR=#0000ff]! call subroutine[/color]
  [COLOR=#008080]call[/color] func_sub(input_array_size, input_array, output_result)
  [COLOR=#0000ff]! print result[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'output_result = '[/color], output_result   
[COLOR=#a020f0]end program[/color] main

[COLOR=#a020f0]subroutine[/color] func_sub(N, PP, F)
  [COLOR=#0000ff]! use module which contains foo[/color]
  [COLOR=#a020f0]use[/color] my_foo
  [COLOR=#2e8b57][b]Implicit[/b][/color] [COLOR=#2e8b57][b]None[/b][/color]
  [COLOR=#2e8b57][b]integer[/b][/color] :: N
[COLOR=#2e8b57][b]  real[/b][/color] :: PP(N), F , func
  
  F [COLOR=#804040][b]=[/b][/color] func(PP)
[COLOR=#a020f0]end Subroutine[/color] func_sub
Result:
Code:
$ gfortran subr_func.f95 -o subr_func

$ subr_func
 output_result =    3.0000000
However, if the function is contained in the module, you don't have to declare it in the subroutine, for example gfortran accept it (see above), but g95 doesn't like it:
Code:
$ g95 subr_func.f95 -o subr_func
In file subr_func.f95:33

  real :: PP(N), F , func
                     1
Error: Symbol 'func' at (1) already has basic type of REAL
So you have to correct the code to
Code:
[COLOR=#2e8b57][b]  real[/b][/color] :: PP(N), F [COLOR=#0000ff]!, func[/color]
Then g95 compiles it fine
Code:
$ g95 subr_func.f95 -o subr_func

$ subr_func
 output_result =  3.
 
I found your post very usefull.I have found function.
function func(PP)
! Objective function
Use OptV
Implicit None
Real :: Func, PP(c*s) ! v:),:) reshaped
Integer :: i, k
real, pointer :: dist_ik:))
real(kind(1d0)) row_sum, total
integer, save :: count=0

vv = Reshape(PP, (/ c, s /))
dist_ik => work_s

total = 0d0
do k = 1, n_data
row_sum = 0d0
do i = 1, c
dist_ik = vv(i,:)-x(k,:)
row_sum = 1.0/dot_product(dist_ik, dist_ik) + row_sum
end do
total = 1d0/row_sum + total
end do
Func = total
End function Func
It should calculate the square norm.Still I do not get it why is he doing inverse two times?
 
milenko76 said:
It should calculate the square norm. Still I do not get it why is he doing inverse two times?
It doesn't seem to simply calculate the norm of vector PP, reshaped as matrix vv.

In inner loop:
First it substracts from every i-th row of matrix vv the k-th row of a matrix x getting the difference vector dist_ik. Then it computes dot produkt of dist_ik (i.e. the norm of dist_ik).
In other words: For each row in vv, it calculates the norm of the difference of that row with the each row of x.
Maybe the computation of dot_product delivers small value, so the reciprocal 1.0/dot_product enlarges the result.
In outer loop:
the sum of reciprocals will be computed.

IMHO, what the function computes isn't simply a Matrix norm, I would say it's a pseudo norm.
 
I agree with you it is pseudo norm.The physical problem is to calculate distance from cluster centers,dist_ik,which should be later weighted(other subroutine).
D=(Yk-Vi)T(Yk-Vi) like this
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top