小编Alf*_*ago的帖子

在`function`语句中定义结果数据类型

好吧,我先说一下我为什么要这样做.我经常用C/C++编写代码,所以对我来说定义函数非常自然:

vector<int> TestFunct (int a, int b){
<some code here>

return <result>;}
Run Code Online (Sandbox Code Playgroud)

现在我正在学习Fortran,所以我声明这样的函数:

function TestFunc(a,b)
        integer, dimension(:)    :: TestFunc
        integer                  :: a
        integer                  :: b
        <some code here>
endfunction TestFunc
Run Code Online (Sandbox Code Playgroud)

但我最近了解到结果的数据类型可以在函数语句中定义,例如:<data_type> function TestFunc(a,b),这对我来说更自然,因为我习惯了类似的C++声明.

问题是,当我'尝试定义一个向量(实际上是一个integer, dimension(:)严格说话)作为结果数据类型时,我有ifort错误#5082(我将在下一行详述).

在一个例子中,对于代码:

real, dimension(:) function TestFunc(a,b)
         integer, intent(in)    :: a
         integer, intent(in)    :: b

         <more code here>

endfunction Testfunc
Run Code Online (Sandbox Code Playgroud)

我得到输出:

main.f90(23): error #5082: Syntax error, found ',' when expecting one of: ( * ) ( :: %FILL , …
Run Code Online (Sandbox Code Playgroud)

fortran function variable-declaration fortran90 intel-fortran

5
推荐指数
1
解决办法
953
查看次数

内部函数作为函数参数

嗯,这就是我今天的问题......

我正在编写一个模块过程,它有一个函数作为参数。这个模块看起来像这样:

module Integ
    implicit none
     <variables declaration>
contains
    function Integral(a,b,f) result(res)
        real, intent(in)     ::a, b
        real                 ::res

        interface
            pure function f(x)
                real, intent(in) :: x
                real             :: f
            endfunction
        endinterface


     <more code of function Integral>

    endfunction Integral

endmodule Integ
Run Code Online (Sandbox Code Playgroud)

那么,到这里为止,一切都很好。当我尝试将此函数与Fortran 内在函数一起使用时,问题就会出现。即,在这段代码中:

program main

use Integ

implicit none

real   ::res,a,b

a=3.0; b=4.0

res=Integral(a,b,sin)  !<- This line does not work

!res=Integral(a,b,sen) !<- This line does work

contains
    function sen(x)
        real, intent(in)   :: x
        real               :: sen

        sen=sin(x)
    endfunction

endprogram …
Run Code Online (Sandbox Code Playgroud)

fortran argument-passing fortran90 fortran95

4
推荐指数
1
解决办法
929
查看次数

fortran中的多重定义链接错误(ifort - gfortran)

好吧,我有这个问题(描述很长,但我觉得很容易解决).我有三个档案:

nrtype.f90,它有一些愚蠢的定义,但它被以下文件使用:

module nrtype
    integer, parameter :: I4B = SELECTED_INT_KIND(9)
    integer, parameter :: I2B = SELECTED_INT_KIND(4)
    integer, parameter :: I1B = SELECTED_INT_KIND(2)
    integer, parameter :: SP = KIND(1.0)
    integer, parameter :: DP = KIND(1.0D0)
endmodule nrtype
Run Code Online (Sandbox Code Playgroud)

LUd.f90,这是部分工作:

module descomposicionLU
    use nrtype

    implicit none

contains

subroutine LUd(A, LU, bk)
    implicit none

    real(DP), intent (in), dimension(:,:)                 :: A
    real(DP), intent (out), dimension(:,:)                :: LU
    integer(I2B), dimension(size(A,1),2)                  :: bk

        <more code that doesn't worth to mention>

endsubroutine LUd

<more code that …
Run Code Online (Sandbox Code Playgroud)

linker fortran gfortran intel-fortran

2
推荐指数
1
解决办法
2796
查看次数

在fortran中定义长参数向量的简洁方法

好吧,我现在有这个问题.我有一个(巨大的)参数集,我想在矢量中组织.

当然,我可以这样做:

real, dimension(64)          :: CONST

CONST(1) = 2.4
CONST(2) = 1.4
...
CONST(n) = CONST(1)*CONST(14)**CONST(7)
...
CONST(64) = ABS(CONST(18))
Run Code Online (Sandbox Code Playgroud)

(注意,一些常量与其他常量有关).

但在那种情况下,我不会parameter在变量中有属性,我想拥有它.

我能想到的另一个选择是使用属性parameter,在这种情况下,我要在变量定义期间将值赋给向量.就像是:

real, parameter, dimension(64) :: CONST =[2.4 , 1.4       , &
                                                ...       , &
                                                1.5412356 , &
                                                ...       , &
                                                342.5]
Run Code Online (Sandbox Code Playgroud)

但是,与一些巨大的同行:

  • 由于在变量定义期间存在大量行,因此代码变得难以阅读.
  • 我不能将常量定义为其他常量的函数.

所以,

  • 有一种巧妙的方法(如在第一个代码中)用parameterFortran中的属性定义长向量?
  • (正如我想的那样)有一种方法可以在我定义变量值后更改变量的属性吗?

感谢您的时间.

variables fortran vector definition

2
推荐指数
1
解决办法
173
查看次数

Julia 中具有未指定维度数组的定义类型

我是朱莉娅的新人。我一直在查看一些文档,例如[1][2][3]。一般来说,我发现它很有用,但我发现它们缺乏组织性。它们都采用“通过示例学习”的方法,但它们都采用结构化的方式来处理语言的显着特征。

好吧,我的问题是我试图用一些变量构建一个定义的类型。其中,至少一个变量是一个 n 维数组,其中维度是该定义类型内的变量。首先,我什至无法在“结构”中定义固定维度张量。

Type Geometry
     dimension::UInt
     coordinates::Array{Float64}(10,2)
end
Run Code Online (Sandbox Code Playgroud)

说:

expected Type, got Array.
Run Code Online (Sandbox Code Playgroud)

并且,例如:

Type Geometry
     dimension::UInt
     coordinates=Array{Float64}(10,2)
end
Run Code Online (Sandbox Code Playgroud)

说:

"assignation" in Type definition is reserved.
Run Code Online (Sandbox Code Playgroud)

另一种方法是在 中定义一个“指针”,Type然后重塑该指针,如下所示:

Type Geometry
     dimension::UInt
     coordinates::Float64
end

mesh=Geometry(10,0)
reshape(mesh.coordinates,(10,3))
Run Code Online (Sandbox Code Playgroud)

说你不能是reshape一个标量。

所以,我的两个问题是:

有什么方法可以在 julia 中构建这个动态维度定义类型吗?

更重要的是:您是否有任何推荐的、有组织的和结构化的朱莉娅参考书目,就像我们的梅特卡夫一样。Fortran 95/2003对 fortran 的解释?

谢谢你们。

dynamic multidimensional-array julia

2
推荐指数
1
解决办法
876
查看次数