# How could I compare two dense matrices with the same dimensions?

## How could I compare two dense matrices with the same dimensions?

I'm currently porting some Fortran codes to C/C++ and my question is: How could I compare two dense matrices with the same dimensions?

For exampe,
...
ALLOCATE ( A( n,m ) )
ALLOCATE ( B( n,m ) )
...
A = 0
B = 0
...
! Some processing...
...
Could I use:
...
IF A == B THEN
WRITE(*,"(A)"),'Matrix A equal to Matrix B'
ELSEIF THEN
WRITE(*,"(A)"),'Matrix A not equal to Matrix B'
ENDIF
...

11 Beiträge / 0 neu
Nähere Informationen zur Compiler-Optimierung finden Sie in unserem Optimierungshinweis.

```IF (ALL(A == B)) THEN
...
ELSE
...
END IF```

>>IF (ALL(A == B)) THEN
>>...
>>ELSE
>>...
>>END IF

Thank you, Ian!

PS: It is so simple :)

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

>>If you want to be careful and assuming matrices A and B are real...

Yes, they are real and in Fortran codes two different matrix multiplication methods are used. Since sizes of matricies could be up to 96Kx96K or so, ( limit due to amount of memory on my computer ) a verification that takes into account epsilon is a very good solution. Thank you.

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

Zitat:

jimdempseyatthecove schrieb:

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:

```   integer ulps
ulps = 4
if(all(abs(a-b)<ulps*spacing(max(abs(a),abs(b))))) then```

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.

Zitat:

TimP (Intel) schrieb:

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.

Thanks to everybody for all your comments, ideas and codes! I got it and will stick to first two suggestions from Ian and John.

## Kommentar hinterlassen

Bitte anmelden, um einen Kommentar hinzuzufügen. Sie sind noch nicht Mitglied?