CompareEqual

Perform an absolute comparison of two matrices by unfolding successive rows into one-dimensional vectors.

ulong vector::Compare(
  const vector& vec    // vector to compare
   );
 
ulong matrix::CompareEqual(
  const matrix& mat     // matrix to compare
   );

Parameters

vec

[in]  Vector to compare.

mat

[in]  Matrix to compare.

Method description

Let us have two matrices: matrix A the method is called for and matrix B, which is passed as a method parameter. The comparison is performed as follows:

  1. Matrices are expanded into one-dimensional vectors by successive concatenation of rows.
  2. Vectors are compared element by element until the first mismatched element.
  3. Depending on the comparison results, one of the values described below is returned.

Return Value

-1 — if the matrix A element is less than the corresponding matrix B element.

0 — if all elements of A and B matrices are identical.

1 — if the matrix A element exceeds the corresponding matrix B element.

Note

NaN value elements are taken into account when comparing.

NaN value elements are considered equal if they are present in both matrices at corresponding positions.

The NaN sign is not taken into account in the comparison.

An element with a NaN value is considered to be less than any other numeric value.

Example

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   matrix matrix_a= {{1032}, {1812}, {654}};
   matrix matrix_i=matrix::Identity(33);
   matrix matrix_c=matrix_a.Inv();
   matrix matrix_check=matrix_a.MatMul(matrix_c);
   Print("matrix_check\n"matrix_check);
 
   ulong errors=matrix_check.Compare(matrix::Identity(33), 1e-15);
   Print("errors="errors);
   /*
   matrix_check
   [[1,0,0]
   [4.440892098500626e-16,1,8.881784197001252e-16]
   [4.440892098500626e-16,2.220446049250313e-16,0.9999999999999996]]
   errors=0
   */
 
//---  absolute comparison of matrices 
   matrix<doubleA = matrix_a;  // Matrix A initialization
   matrix<doubleB = matrix_c;  // Matrix B initialization
   int result = A.CompareEqual(B);
   switch(result)
     {
      case -1:
         Print("Matrix A is smaller than matrix B");
         break;
      case 0:
         Print("Matrices A and B are identical");
         break;
      case 1:
         Print("Matrix A is greater than matrix B");
         break;
      case -2:
         Print("Error! Matrix A is not initialized");
         break;
      case 2:
         Print("Error! Matrix B is not initialized");
         break;
      case -3:
         Print("Error! The size of matrix A is less than the size of matrix B");
         break;
      case 3:
         Print("Error! The size of matrix A is greater than the size of matrix B");
         break;
      default:
         Print("Error! Unknown error");
         break;
     }
  }