trace_diff
Builtin index · Signal processing
result = trace_diff(a, b, [rtol=1e-9], [atol=1e-12])
Input: a, b, rtol, atol · Output: result
What it is
Compares two traces sample by sample and returns a record instead of raising, so a test can assert on it and a report can print it. A sample passes when abs(a - b) <= atol + rtol * max(abs(a), abs(b)) (symmetric in a and b; set rtol=0 or atol=0 to use only the other, both 0 for exact equality). Equal values and NaN facing NaN pass; a NaN or an infinity facing anything else is an infinite error.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = trace_diff(a, b, [rtol=1e-9], [atol=1e-12]) | a —a NaN or an infinity facing anything else is an infinite error.b —A sample passes when abs(a - b) <= atol + rtol * max(abs(a), abs(b)) (symmetric in a and b.rtol optional (default 1e-9)set rtol=0 or atol=0 to use only the other, both 0 for exact equality).atol optional (default 1e-12)set rtol=0 or atol=0 to use only the other, both 0 for exact equality). | result number or vector | Returns {ok, max_abs, max_rel, worst_index, first_bad_index, n, n_bad}: ok is true when every sample passes; max_abs the largest absolute difference and worst_index where it is (-1 if the traces are identical); max_rel the largest abs(a - b) / max(abs(a), abs(b)); first_bad_index the first sample outside the tolerance (-1 if none); n the number of samples compared and n_bad how many failed. a and b are two vectors, two matrices of the same shape (rows are samples, columns are signals; the result adds worst_col and first_bad_col, and the indices are row numbers), or two traces (records or tables with the same signal names in any order; the result adds worst_signal and first_bad_signal, empty text if none). A length or shape mismatch is an error that names both sizes, and so is a signal present on one side only. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
function sho(t, y)
return [y[1], -y[0]]
end function
te = linspace(0, 5, 51)
r = ode45("sho", [0, 5], [1, 0], t_eval=te, rtol=1e-10, atol=1e-12)
trace_save("reference.npz", {t = r.t, y = r.y[:, 0]})
ref = trace_load("reference.npz")
c = ode23("sho", [0, 5], [1, 0], t_eval=te) # the run under test
d = trace_diff(ref, {t = c.t, y = c.y[:, 0]}, rtol=1e-6, atol=1e-6)
print(d.ok) # false
print(d.worst_signal) # y
print(d.max_abs) # 2.652263e-6, at sample d.worst_index (32)
output
false y 2.652263e-6
from signal-processing