在numpy中,有一个allclose
函数其比较每个元素的两个张量。输入主要是两个张量和阈值。
如果我在Fortran中这样做,如果我使用数组A
和B
作为子例程的参数,那么在执行后续操作之前,我需要首先在子例程中声明数组的维度。这将需要我将数组的维度放入参数中。并使输入变得复杂。有没有更简单的方法可以在Fortran中实现类似allclose
的东西?
感谢Ian Bush的评论。以下代码有效。
Program compare_array_test
real :: A(2,2), B(2,2), E(2,2,2), F(2,2,2)
complex :: C(2,2), D(2,2)
real :: tol
A = 1.0
B = 1.0
A(1,1) = 1.01
tol = 0.0001
If( All( Abs( A - B ) < tol ) ) Then
write (*,*) 'within the threshold'
else
write (*,*) 'above the threshold'
end if
tol = 0.1
If( All( Abs( A - B ) < tol ) ) Then
write (*,*) 'within the threshold'
else
write (*,*) 'above the threshold'
end if
C = (1.0, 1.0)
D = (1.0, 1.0)
C(1,1) = (1.01, 1.00)
tol = 0.0001
If( All( cabs( C - D ) < tol ) ) Then
write (*,*) 'within the threshold'
else
write (*,*) 'above the threshold'
end if
tol = 0.1
If( All( cabs( C - D ) < tol ) ) Then
write (*,*) 'within the threshold'
else
write (*,*) 'above the threshold'
end if
E = 1.0
F = 1.0
E(1,1,1) = 1.01
tol = 0.0001
If( All( Abs( E - F ) < tol ) ) Then
write (*,*) 'within the threshold'
else
write (*,*) 'above the threshold'
end if
tol = 0.1
If( All( Abs( E - F ) < tol ) ) Then
write (*,*) 'within the threshold'
else
write (*,*) 'above the threshold'
end if
end program