pyyeti.ytools.compmat

pyyeti.ytools.compmat(a, b, filterval=0.0, method='abs', pdiff_tol=0, verbose=5)[source]

Compare two matrices term-by-term.

Parameters:
  • a (2d array_like) – Matrix to compare to b

  • b (2d array_like) – Matrix to compare to a

  • filterval (scalar; optional) – Used to filter out small numbers; the exact usage depends on method. In all cases however, both the a and b values (absolute value wise) have to be below the filter value for the comparison to be ignored. Must be >= 0.0.

  • method (string; optional) – Specifies how to use filter:

    method

    ‘abs’

    only numbers in a and b that are greater than filterval are compared

    ‘row’

    for each row, only the numbers in a and b that are greater than filterval * Max_in_row(a or b) are compared

    ‘col’

    for each column, only the numbers in a and b that are greater than filterval * Max_in_col(a or b) are compared

    ‘max’

    only numbers in a and b that are greater than filterval * Max_overall_value(a or b) are compared

  • pdiff_tol (scalar; optional) – Percent differences (absolute value wise) that are equal or less than pdiff_tol are considered a match.

  • verbose (int; optional) – If > 0, write messages to the screen showing comparison results. The value specifies the limit to how many specific comparisons are printed. If <= 0, no messages are printed.

Returns:

  • max_pdiff (scalar) – Maximum absolute percent difference after applying filters; will be 0.0 if the two matrices match within the criteria

  • stats (1d or 2d ndarray) –

    4 values of statistics on the percent differences:

    [min(perc), max(perc), mean(perc), std(perc)]
    

    If comparing complex matrices, stats will have two rows of those four values: the first row for the real part and the second row for the imaginary part.

Raises:

Notes

The percent differences are computed by: (b - a)/a * 100

If a and/or b are complex, the real and imaginary parts are compared separately. In this case, max_pdiff will be the abs-max over both parts (scalar), but stats will have 2 rows in this case, first for the real part, second for the imaginary part.

Examples

Compare matrices A and B row-wise, ignoring values less than 10% of the max in the row (over both A and B), and print only percent differences if one is at least 5% different:

>>> import numpy as np
>>> # from pyyeti.ytools import compmat
>>> A = np.array([[1,   4, 5, 40], [15, 16, 17, 80]])
>>> B = np.array([[2, 4.2, 5, 43], [20, 14, 17, 82]])
>>>
>>> # compare matching matrices:
>>> compmat(A, A)
Matrices match (within compare criteria).
(0.0, [[0.0, 0.0, 0.0, 0.0]])
>>> compmat(A, A, verbose=0)
(0.0, [[0.0, 0.0, 0.0, 0.0]])
>>>
>>> # and non-matching:
>>> mx, stats = compmat(A, B, 0.1, method='row', pdiff_tol=5)
Maximum Negative Percent Differences (`b` relative to `a`):
   1:      -12.5% at [1, 1]:  14 vs. 16
Maximum Positive Percent Differences (`b` relative to `a`):
   1:      33.33% at [1, 0]:  20 vs. 15
   2:        7.5% at [0, 3]:  43 vs. 40
   3:        2.5% at [1, 3]:  82 vs. 80

Statistics on Max and Min Percent Differences:
    [Min, Max, Mean, Std] = [-12.5%, 33.33%, 5.139%, 15.31%]
>>> mx
33.3333...
>>>
>>> # demo a comparison with complex matrices:
>>> A = A + B * 1j
>>> B = B + A.real * 1j
>>> mx, s = compmat(
...          A, B, 0.04, method="row", pdiff_tol=5, verbose=2
...        )
REAL part:
   Maximum Negative Percent Differences (`b` relative to `a`):
      1:      -12.5% at [1, 1]:  14 vs. 16
   Maximum Positive Percent Differences (`b` relative to `a`):
      1:        100% at [0, 0]:   2 vs.  1
      2:      33.33% at [1, 0]:  20 vs. 15

   Statistics on Max and Min Percent Differences:
       [Min, Max, Mean, Std] = [-12.5%, 100%, 16.98%, 35.95%]


IMAGINARY part:
   Maximum Negative Percent Differences (`b` relative to `a`):
      1:        -50% at [0, 0]:   1 vs.  2
      2:        -25% at [1, 0]:  15 vs. 20
   Maximum Positive Percent Differences (`b` relative to `a`):
      1:      14.29% at [1, 1]:  16 vs. 14

   Statistics on Max and Min Percent Differences:
       [Min, Max, Mean, Std] = [-50%, 14.29%, -9.361%, 19.66%]
>>> mx
100.0...