<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic compilation error with multivariable functions and function arr in Intel® Fortran Compiler</title>
    <link>https://community.intel.com/t5/Intel-Fortran-Compiler/compilation-error-with-multivariable-functions-and-function/m-p/746865#M4592</link>
    <description>Remove or comment out lines 7 and 8 of your main program -- func and dfunc are already defined in the USEd modules in the main program.&lt;BR /&gt;&lt;BR /&gt;Furthermore, your redefinition &lt;I&gt;dfunc(ndim)&lt;/I&gt; is inconsistent with the prior definition &lt;I&gt;dfunc(x(ndim))&lt;/I&gt;. &lt;BR /&gt;&lt;BR /&gt;Perhaps, saying that the compiler(s) reported errors in the code would be more correct than saying that they "failed", since what the compilers did (reporting the lines with syntax errors, and refusing to produce an object file when the input source code has errors) is exactly what they are expected to do!</description>
    <pubDate>Mon, 31 May 2010 14:26:22 GMT</pubDate>
    <dc:creator>mecej4</dc:creator>
    <dc:date>2010-05-31T14:26:22Z</dc:date>
    <item>
      <title>compilation error with multivariable functions and function array</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/compilation-error-with-multivariable-functions-and-function/m-p/746864#M4591</link>
      <description>Hi all, &lt;BR /&gt;I want to calculate a multivariable function &lt;I&gt;func(x)&lt;/I&gt; and its gradient &lt;I&gt;dfunc(x)&lt;/I&gt;, where &lt;I&gt;x&lt;/I&gt; is a vector. The gradient &lt;I&gt;dfunc(x)&lt;/I&gt; is calculated with finite difference method. I defined two modules and a calling program as follows:&lt;BR /&gt;1. &lt;I&gt;Mod_func&lt;/I&gt;: module to calculate the function values at &lt;I&gt;x&lt;/I&gt;&lt;BR /&gt;2. &lt;I&gt;Mod_dfunc&lt;/I&gt;: module to calculate the gradient at &lt;I&gt;x&lt;/I&gt;. Since numerical finite difference formula are used to obtain &lt;I&gt;dfunc(x)&lt;/I&gt;, &lt;I&gt;dfunc(x)&lt;/I&gt; needs to call &lt;I&gt;func(x)&lt;/I&gt;.&lt;BR /&gt;3. drive: calling program&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Problems:&lt;BR /&gt;1. Compilation error:&lt;BR /&gt;&lt;BR /&gt;$ ifort -c mod_func.f90 mod_dfunc.f90 drive.f90&lt;BR /&gt;drive.f90(7): error #6401: The attributes of this name conflict with those made accessible by a USE statement. [FUNC]&lt;BR /&gt; real ::func&lt;BR /&gt;---------^&lt;BR /&gt;drive.f90(8): error #6401: The attributes of this name conflict with those made accessible by a USE statement. [DFUNC]&lt;BR /&gt; real ::dfunc(ndim)&lt;BR /&gt;---------^&lt;BR /&gt;compilation aborted for drive.f90 (code 1)&lt;BR /&gt;&lt;BR /&gt;2. If I get rid of dfunc(x) and correspondingly omit it in the calling program (so that I only calculate the function values, not its gradient), it still shows compilation error with &lt;I&gt;ifort&lt;/I&gt;. However, if I use &lt;I&gt;gfortran&lt;/I&gt;, it was OK. But, if I tried to put the dfunc back on, both compilers fail. &lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;The codes:&lt;PRE&gt;[fortran]Module mod_func&lt;BR /&gt;! Purpose: To calculate multivariable function func(x), x is a vector&lt;BR /&gt;Contains&lt;BR /&gt;  FUNCTION func(x)&lt;BR /&gt;    IMPLICIT NONE&lt;BR /&gt;    REAL, DIMENSION(:), INTENT(IN) :: x&lt;BR /&gt;    REAL :: func&lt;BR /&gt;    integer::i,n&lt;BR /&gt;    n=size(x)&lt;BR /&gt;    func=0&lt;BR /&gt;    do i=1,n                    ! a simple multivariable function: func(x) = x1**2 + x2**2 + ... &lt;BR /&gt;       func= func+ x(i)**2&lt;BR /&gt;    end do&lt;BR /&gt;  END FUNCTION func&lt;BR /&gt;End module mod_func[/fortran]&lt;/PRE&gt; &lt;PRE&gt;[fortran]Module mod_dfunc&lt;BR /&gt;! Purpose: To calculate gradient at x by finite difference, where x is a vector&lt;BR /&gt;Contains&lt;BR /&gt;  FUNCTION dfunc(x)&lt;BR /&gt;    use mod_func ! To calculate dfunc(x), we need &lt;BR /&gt;                 ! function values func(x) and func(x+x)&lt;BR /&gt;                 !  is a small scalar&lt;BR /&gt;    implicit none&lt;BR /&gt;    REAL, DIMENSION(:), INTENT(IN) :: x&lt;BR /&gt;    REAL, DIMENSION(size(x)) :: dfunc&lt;BR /&gt;    real, dimension(:),allocatable:: e    ! e is the unit vector &lt;BR /&gt;    real :: del ! del ==&amp;gt; &lt;BR /&gt;    integer:: i,n&lt;BR /&gt;    del=sqrt(epsilon(x(1)))&lt;BR /&gt;    n=size(x)&lt;BR /&gt;    allocate(e(n))&lt;BR /&gt;    do i=1,n&lt;BR /&gt;       e=0.&lt;BR /&gt;       e(i)=1.&lt;BR /&gt;       !central difference formula&lt;BR /&gt;       dfunc(i)=(func(x+del*e) - func(x-del*e))/(2.*del)&lt;BR /&gt;    end do&lt;BR /&gt;    deallocate(e)&lt;BR /&gt;  END FUNCTION dfunc&lt;BR /&gt;End module mod_dfunc[/fortran]&lt;/PRE&gt; &lt;BR /&gt;
&lt;PRE&gt;[fortran]program drive&lt;BR /&gt;  use mod_func&lt;BR /&gt;  use mod_dfunc&lt;BR /&gt;  implicit none&lt;BR /&gt;  integer,parameter::ndim=2&lt;BR /&gt;  real :: x(ndim) ! 2 variables, (x1,x2)&lt;BR /&gt;  real ::func&lt;BR /&gt;  real ::dfunc(ndim)&lt;BR /&gt;  x=(/1.0,3./) &lt;BR /&gt;  write(*,*) 'x:',x&lt;BR /&gt;  write(*,*) 'function value:',func(x)&lt;BR /&gt;  write(*,*) 'function gradiet:',dfunc(x)&lt;BR /&gt;end program drive[/fortran]&lt;/PRE&gt;</description>
      <pubDate>Mon, 31 May 2010 13:37:26 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/compilation-error-with-multivariable-functions-and-function/m-p/746864#M4591</guid>
      <dc:creator>yyxt11a</dc:creator>
      <dc:date>2010-05-31T13:37:26Z</dc:date>
    </item>
    <item>
      <title>compilation error with multivariable functions and function arr</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/compilation-error-with-multivariable-functions-and-function/m-p/746865#M4592</link>
      <description>Remove or comment out lines 7 and 8 of your main program -- func and dfunc are already defined in the USEd modules in the main program.&lt;BR /&gt;&lt;BR /&gt;Furthermore, your redefinition &lt;I&gt;dfunc(ndim)&lt;/I&gt; is inconsistent with the prior definition &lt;I&gt;dfunc(x(ndim))&lt;/I&gt;. &lt;BR /&gt;&lt;BR /&gt;Perhaps, saying that the compiler(s) reported errors in the code would be more correct than saying that they "failed", since what the compilers did (reporting the lines with syntax errors, and refusing to produce an object file when the input source code has errors) is exactly what they are expected to do!</description>
      <pubDate>Mon, 31 May 2010 14:26:22 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/compilation-error-with-multivariable-functions-and-function/m-p/746865#M4592</guid>
      <dc:creator>mecej4</dc:creator>
      <dc:date>2010-05-31T14:26:22Z</dc:date>
    </item>
    <item>
      <title>compilation error with multivariable functions and function arr</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/compilation-error-with-multivariable-functions-and-function/m-p/746866#M4593</link>
      <description>Many thanks to mecej4. Spot on! I did as you said, and the problem was resolved. However, my real intention is to create a subroutine that does some calculations with function values &lt;I&gt;func(x)&lt;/I&gt; and its gradient &lt;I&gt;dfunc(x)&lt;/I&gt;. The subroutine should look like this:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE&gt;[fortran]SUBROUTINE func_sum(x,func,dfunc,result)&lt;BR /&gt;    IMPLICIT NONE&lt;BR /&gt;    REAL, DIMENSION(:), INTENT(IN) :: x&lt;BR /&gt;    REAL, INTENTION(OUT) :: result&lt;BR /&gt;    real,external::func&lt;BR /&gt;    real,external::dfunc(size(x))&lt;BR /&gt;    result=func(x) + dfunc(x)&lt;BR /&gt;end SUBROUTINE func_sum [/fortran]&lt;/PRE&gt; &lt;BR /&gt;Where should I define the subroutine? Should I define it in a module or define it as a stand alone subroutine? How do I call it from the main program? Basically, I need a subroutine that needs functions (func and dfunc), one of which needs the other (dfunc needs func). I am not sure how to pass the arrays.</description>
      <pubDate>Mon, 31 May 2010 22:28:17 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/compilation-error-with-multivariable-functions-and-function/m-p/746866#M4593</guid>
      <dc:creator>yyxt11a</dc:creator>
      <dc:date>2010-05-31T22:28:17Z</dc:date>
    </item>
    <item>
      <title>compilation error with multivariable functions and function arr</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/compilation-error-with-multivariable-functions-and-function/m-p/746867#M4594</link>
      <description>Is it your intention to pass different procedure arguments &lt;I&gt;func&lt;/I&gt; and &lt;I&gt;dfunc&lt;/I&gt; in multiple calls to &lt;I&gt;func_sum&lt;/I&gt; in a single run? If so, you may consult, for example, Section 5.12 of Metcalf, Reid and Cohen, "Fortran 95/2003 explained". You will also benefit from reading about explicit and implicit interfaces.&lt;BR /&gt;&lt;BR /&gt;If, on the other hand, you will only employ one function &lt;I&gt;func &lt;/I&gt;in a single run, the coding can be much simpler. For example:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE&gt;[fortran]Module mod_func
CONTAINS
FUNCTION func(x)
  IMPLICIT NONE
  REAL, DIMENSION(:), INTENT(IN) :: x
  REAL :: func
  integer::i,n
  n=size(x)
  func=0
  do i=1,n
     func= func+ x(i)**2
  end do
END FUNCTION func

FUNCTION dfunc(x)
  implicit none
  REAL, DIMENSION(:), INTENT(IN) :: x
  REAL, DIMENSION(size(x)) :: dfunc
  real, dimension(size(x)):: e    ! e is the unit vector
  real :: del ! del ==&amp;gt; 
  integer:: i
  del=epsilon(x(1))**(1.0/3.0) ! for Central-Diff.
  e=0.
  do i=1,size(x)
     e(i)=1.
     !central difference formula
     dfunc(i)=(func(x+del*e) - func(x-del*e))/(2.*del)
     e(i)=0.
  end do
END FUNCTION dfunc

SUBROUTINE func_sum(x,result)
  IMPLICIT NONE
  real, dimension(:) :: x
  real, dimension(size(x)) :: df
  real :: result
  df=1e-3
  result=func(x) + dot_product(dfunc(x),df)
end SUBROUTINE func_sum

end module mod_func

program usemod
use mod_func
  real :: x(3)=(/1.0,2.0,3.0/), res
  call func_sum(x,res)
  write(*,'(2x,"Res = ",G12.3)')res
end program usemod[/fortran]&lt;/PRE&gt; &lt;BR /&gt;Note that a step proportional to sqrt(machine_eps) is appropriate only for one-sided difference approximations.</description>
      <pubDate>Mon, 31 May 2010 23:38:47 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/compilation-error-with-multivariable-functions-and-function/m-p/746867#M4594</guid>
      <dc:creator>mecej4</dc:creator>
      <dc:date>2010-05-31T23:38:47Z</dc:date>
    </item>
  </channel>
</rss>

