diff m-toolbox/classes/+utils/@math/isequal.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/m-toolbox/classes/+utils/@math/isequal.m	Wed Nov 23 19:22:13 2011 +0100
@@ -0,0 +1,61 @@
+% ISEQUAL test if two matrices are equal to within the given tolerance.
+%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+% DESCRIPTION: ISEQUAL test if two matrices are equal to within the given
+%              tolerance.
+%
+% CALL:       result = isequal(m1, m2)
+%             result = isequal(m1, m2, tol)
+%
+% INPUTS:     m1  - a matrix of real or complex values
+%             m2  - a matrix of real or complex values
+%             tol - the tolerance to test against [default: 1e-14]
+%
+% OUTPUTS:    result - true or false
+%
+% VERSION: $Id: isequal.m,v 1.8 2010/11/19 12:40:13 ingo Exp $
+%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+function res = isequal(varargin)
+  
+  % Check size of the input numbers
+  if  ~all(size(varargin{1}) == size(varargin{2}))        || ...
+      ~(isnumeric(varargin{1}) || islogical(varargin{1})) || ...
+      ~(isnumeric(varargin{2}) || islogical(varargin{2}))
+    res = false;
+    return
+  end
+  
+  m1 = double(varargin{1});
+  m2 = double(varargin{2});
+  
+  r = m1./m2;
+  
+  if nargin > 2 && ~isempty(varargin{3})
+    tol = varargin{3};
+  else
+    tol = 1e-14;
+  end
+  
+  maxAbsDiff = abs(max(1-abs(r)));
+  minAbsDiff = abs(min(1-abs(r)));
+  maxAngleDiff = max(abs(angle(r)));
+  minAngleDiff = min(abs(angle(r)));
+  
+  maxAbsDiff(isnan(maxAbsDiff))=0;
+  minAbsDiff(isnan(minAbsDiff))=0;
+  maxAngleDiff(isnan(maxAngleDiff))=0;
+  minAngleDiff(isnan(minAngleDiff))=0;
+  
+  if  all([maxAbsDiff < tol,...
+      minAbsDiff < tol, ...
+      maxAngleDiff < tol, ...
+      minAngleDiff < tol])
+    
+    res = true;
+  else
+    res = false;
+  end
+  
+end