我有一个C++函数,它通过引用传递的数组来更新2个数组:
double* CPPF(double array[],int size, double a1[], double a2[]){
int m = size, n = size
/* code that updates a1 and a2 arrays goes here */
return 0;
}
在我的主要职能中,我有
var bigArray = new double[size*size];
for (int i = 0; i < dimension; i++){
for (int j = 0; j <= i; j++)
bigArray[i * size + j] = bigArray [j * size+ i] = Other[i,j];
}
double[] a1 = new double[size * size];
double[] a2 = new double[size];
double* RESULT = CPPF(bigArray, size, a1, a2);
// Use updated a1 and a2
我将如何定义更新这2个数组的Fortran函数?我知道这是可能的,因为您不按值传递参数,而仅按引用传递参数。
到目前为止,我已经尝试过类似的方法:
REAL FUNCTION FF(A, size, a1,a2)
IMPLICIT NONE
INTEGER, INTENT(IN) :: size
REAL :: A(:,:), a1(:),a2(:)
!UPDATE a1 and a2 and finish
END FUNCTION FF
最佳答案
这是包含分配的翻译:
module arrays_module
implicit none
! Define a kind parameter for double precision real numbers.
integer, parameter :: rk = selected_real_kind(16)
subroutine CPPF(array, a1, a2)
! Use assumed shape arrays
real(kind=rk) :: array(:)
real(kind=rk) :: a1(:)
real(kind=rk) :: a2(:)
integer :: m, n
! Use the size of the arrays
m = size(a2)
n = size(a2)
! Do the setting of your arrays...
end subroutine CPPF
end module arrays_module
program main
use arrays_module
implicit none
integer :: dimension, n
real(kind=rk) :: bigArray(:)
real(kind=rk) :: a1(:), a2(:)
real(kind=rk) :: Other(dimension, dimension)
! Allocating the arrays dynamically
allocate(bigArray(n*n))
allocate(a1(n*n))
allocate(a2(n))
do i=1,dimension
do j=1,i
bigArray((i-1)*n + j) = Other(i,j)
bigArray((j-1)*n + i) = Other(i,j)
end do
end do
call CPPF(array = bigArray, a1 = a1, a2 = a2)
! Use the updated arrays...
end program main
如果需要,您也可以让CPPF为您分配新的阵列。