我是个Fortran新手。我试图为矩阵运算编写fortran代码,但在设计我的函数的原型(接口)时却陷入了困境。
我的C/C++编程经验告诉我,要编写适用于所有维数的矩阵的代码,需要将数组和维数分别传递给函数。示例(可能不是最好的):
int * matx_op(int *mat_a, int arows, int acols, int *mat_b, int brows, int bcols);
但在fortran中,matmul()函数似乎是自动执行此操作的。想要了解它是如何做到的。
print *, 'Enter 16 elements of matrix A
在fortran中处理fortran参数时,我认为使用present()内部的分支是典型的,即:
subroutine foo(ii,jj)
implicit none
integer, intent(in) :: ii
integer, optional :: jj
if (present(jj)) then
! do something
else
! do something else
end if
end subroutine foo
我的假设(来自C++世界)是希望present()是一个编译时构造,并且不会有任何相关的运行时性能
我可以在Fortran类型中存储对过程的引用吗?
我的目标是将重复的参数分组为一个类型,从而将它们减少到Fortran子例程中。然而,Fortran不允许我在外部过程中这样做。
下面是我尝试做的一个简单的例子:
module my_functions
type mytype
external :: f
end type
contains
subroutine fa()
WRITE(*,*) "yiha"
end subroutine
subroutine fb(t)
type(mytype
我是Fortran的新手。请看下面的代码:
c main program
call foo(2)
print*, 2
stop
end
subroutine foo(x)
x = x + 1
return
end
在Fortran的某些实现中,上面的代码会打印一个3,为什么呢?你能给我解释一下吗?
您认为最近的Fortran实现是如何解决这个问题的?
我们非常感谢你的帮助。谢谢。
我正在编写一些fortran调用-C代码,并且不清楚iso_c_binding模块的使用情况。
我让fortran和C接口在没有iso_c_binding的情况下成功地工作,问题是我是否仍然应该显式地绑定函数和变量。例如,这起作用是:
program testprog
...
interface
subroutine c_parser(param)
integer, intent(in) :: param
end subroutine
end interface
integer :: a
call c_parser(a)
..
end program
/**********
我正在尝试在两个Fortran子例程之间插入一个C++层。我还需要传递一个派生类型作为参数,我不想在C++中声明它,因为我不需要访问它数据,而且在我的原始程序中,它是一个复杂类型,使用了许多其他类型。
我做了一个简单的Fortran程序:
module my_module
implicit none
type :: my_type
real :: x
end type my_type
end module my_module
subroutine fortran_subroutine(b)
use my_module
implicit none
ty
我有一个程序,它调用子例程,然后子例程再调用一个函数。我被Fortran对函数类型声明的要求弄糊涂了。我已经在函数中声明了类型(即实函数foo(...)),无论我是否在子例程声明部分中声明函数,程序都会正常工作。
我的具体问题是,在子例程中声明函数不会在将来导致意外的行为吗?我也看到了接口块,想知道这是否也是必要的。
更广泛地说,我也感兴趣的是Fortran在“幕后”做了什么,以及为什么声明函数或使用接口块或多或少很重要。
编辑:一些示例代码:
program foo
real :: a,b,c
call bar(a,b,c)
end program foo
subroutine
我试图编译一个简单的C++/Fortran混合程序,但有链接问题。我使用的是Visual 2013终极版和Intel Visual Fortran编译器XE 14。这个程序非常简单,是从网上复制的。它有一个C++文件和一个Fortran文件。
C++文件是:
// Illustrate passing integer and floating point arrays
// from C++ to Fortran
#include <iostream>
using namespace std;
extern "C"
{
int __stdcall SUM
我正在学习如何将Fortran与C接口,我编写了简单的C代码,将所有数组值设置为1:
#include <stdlib.h>
void cset( int *array, int size ) {
for (size_t i = 0; i < size; i++) {
array[i] = 1;
}
return;
}
我想要编写一个Fortran接口来处理数组(任意维度)和标量。
我的fortran密码:
program main
use iso_c_binding
implicit none
interface cset
! In
我从2008年的Fortran开始,我和OOP做了很大的斗争。看来,在2008年的语言标准中,解释一个非常基本的OOP概念的材料非常少。
我已经找到了关于继承的信息,但是我找不到任何关于多态性的信息。
因此,如果我想在C++中重载一个函数,我可以这样做(例如维基百科):
// volume of a cube
int volume(const int s)
{
return s*s*s;
}
// volume of a cylinder
double volume(const double r, const int h)
{
return 3.1415926*r*r*sta
我想知道Fortran中是否有类似的东西。当然,这个示例不编译,但我认为您可以理解。
program test
character(1):: sub
sub='A'
call sub
sub='B'
call sub
end program
subroutine A
print*,'OK! A'
end subroutine A
subroutine B
print*,'OK! B'
end subroutine B
我试图使用iso_c_bindings模块编写Fortran 2003绑定到CUFFT库,但是cufftPlanMany子程序(类似于FFTW库中的sfftw_plan_many_dft )有问题。
绑定本身看起来如下所示:
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! cufftResult cufftPlanMany(cufftHandle *plan, int rank, int *n,
! int *inembed, int is
我刚刚开始在Fortran 2003中重载操作符(包括赋值),我想重载用户定义类型的箭头操作符(=>)。我知道大多数运营商,比如(+),我会说
interface operator(+)
! What I want this to mean instead
end interface operator
但是,这并不适用于(=>)。我知道任务,我会说
interface assignment(=)
! What I want this to mean instead
end interface assignment
它仍然不适用于(=>)。
具体
我正在尝试将一段代码从Fortran 77移植到Fortran 90,并且我有一个关于在Fortran 77的争论中捕获级别错配的问题。
这是Fortran 90中的代码
program test
use my_module
real ml_time
call gettimes(cdfid,ml_time,ml_ntimes)
在调用子例程中,这是定义传递变量的方式。
module my_module
use netcdf
subroutine gettimes(cdfid,times,ntimes)
real times(*)
call check(nf90_in
问题是在其派生类型中重命名基类型的成员。我认为这是可以做到的,因为FORTRAN提供了指针和关联函数。例如:
type :: base1
integer :: old
endtype base1
type, extends(base1) :: derived1
!here I want old to be renamed as new
!Something like new=> old
!So I can use derived1%new elsewhere
endtype derived1
但问题是基数1%的人不能成为目标。所以我想我不能使用指针来做这
我正在学习如何在fortran中使用函数,我遇到了几个例子,这使我相信fortran函数通过const引用传递了这个论点。当我说“通过康斯特参考”时,我是从C++的意义上说的。我在网上搜索,没有找到相关的文件。使我相信fortran函数通过const引用传递参数的代码如下所示。
program try
implicit none
real sq
real a,b
write(*,*) sq(2)
a=2
write(*,*) sq(a)
end program
real function sq(x)
real x
sq=x**2
return
我正在考虑使用Fortran中的子模块特性,并设置了一个小的测试示例。我有两个与它们的使用有关的问题。下面是示例代码:
module points
type :: point
real :: x, y
end type point
interface
module function point_dist(a, b) result(distance)
type(point), intent(in) :: a, b
real :: distance
end function p