連結已複製
Remember that if the variables are real or double precision, then your equals test is likely to fail if the only differences are due to rounding errors.
If this is what you want, then OK, but you may want to look at the magnitudes of the differences.
Regards,
David
If you want to be careful and assuming matrices A and B are real, you could try something like:
Logical function same_matrix (a,b,n,m)
real*8 a(n,m), b(n,m)
!
integer*4 i,j
real*8 err_max, max_val, tolerance
!
max_val = 0
err_max = 0
do j = 1,m
do i = 1,n
max_val = max (max_val, abs(a(i,j)) )
err_max = max (err_max, abs(a(i,j)-b(i,j)) )
end do
end do
!
tolerance = epsilon (max_val) * max_val * fudge_factor
same_matrix = (err_max < tolerance)
end function same_matrix
tolerance might still need some more work.
If a,b are integer or logical, then tolerance is much simpler, but I would still use DO, rather than array syntax.
Never addressed complex !!
John
jimdempseyatthecove wrote:
The err_max=... needs to be adjusted such that the difference in the values is relative to the magnitude of the values.
The Fortran SPACING intrinsic is great for this. An example:
[fortran] integer ulps
ulps = 4
if(all(abs(a-b)<ulps*spacing(max(abs(a),abs(b))))) then[/fortran]
TimP (Intel) wrote:
if(all(abs(a-b)<ulps*spacing(max(abs(a),abs(b))))) then
may leave something to be desired in terms of minimizing memory traffic and parallelizing a problem as large as mentioned above.
If memory traffic is an issue then the above statement has the same amount of memory traffic as the less careful
if(all(a==b)) then
and so should then execute in essentially the same time because all of the functions and operators in the more complicated argument to ALL are elemental.
