- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
[fortran]IF (ALL(A == B)) THEN
...
ELSE
...
END IF[/fortran]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
John,
The err_max=... needs to be adjusted such that the difference in the values is relative to the magnitude of the values.
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page