Fortran中如何实现numpy.allclose?

How to realize numpy.allclose in Fortran?

在numpy中,有一个allclose function 它比较每个元素的两个张量。输入主要是两个张量和阈值。

如果我在Fortran中做,如果我使用数组AB作为子程序的参数,我需要先在子程序中声明数组的维度,然后再做后续操作。这将需要我将数组的维度放入参数中。并使输入复杂化。有没有更简单的方法可以在 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