필터 지우기
필터 지우기

Function implementation for monotony check

조회 수: 8 (최근 30일)
Firan Lucian
Firan Lucian 2019년 8월 26일
댓글: Firan Lucian 2019년 8월 27일
Is there a build-in MATLAB function (way) that calculates one function implementation monotony against a true (correct, golden, reference) implementation ?
One way I’m thinking, is to check if the actual function and the reference have the same behavior on two successive inputs.
(successive as stepping; -+eps, next float, or with different granularity)
Consider 2 function implementation one to be measured and one reference:
f(x), f(x+1) and ref(x), ref(x+1)
Monotony in an strict way may be
Count inconsistencies or errors as
  • ref(x) > ref(x+1) not implies f(x) > f(x+1)
  • ref(x) < ref(x+1) not implies f(x) < f(x+1)
  • ref(x) == ref(x+1) not implies f(x) == f(x+1)
Monotony relaxed
  • ref(x) >= ref(x+1) not implies f(x) >= f(x+1)
  • ref(x) <= ref(x+1) not implies f(x) <= f(x+1)
over some interval.
Example
x=1;
stp=1;
err_strict = (ref(x) > ref(x+stp)) == (f(x) > f(x+stp)) | ...
(ref(x) < ref(x+stp)) == (f(x) < f(x+stp)) | ...
(ref(x) == ref(x+stp)) == (f(x) == f(x+stp));
err_relaxed = (ref(x) >= ref(x+stp)) == (f(x) >= f(x+stp)) | ...
(ref(x) <= ref(x+stp)) == (f(x) <= f(x+stp));
disp(err_strict)
disp(err_relaxed)
function [y] = ref(x)
y = x+1;
end
function [y] = f(x)
y = single(x)+1;
end
---
  댓글 수: 2
Firan Lucian
Firan Lucian 2019년 8월 26일
code typo, upper one count conformities, below non-conformities
x=1;
stp=1;
err_strict = (ref(x) > ref(x+stp)) ~= (f(x) > f(x+stp)) | ...
(ref(x) < ref(x+stp)) ~= (f(x) < f(x+stp)) | ...
(ref(x) == ref(x+stp)) ~= (f(x) == f(x+stp));
err_relaxed = (ref(x) >= ref(x+stp)) ~= (f(x) >= f(x+stp)) | ...
(ref(x) <= ref(x+stp)) ~= (f(x) <= f(x+stp));
disp(err_strict)
disp(err_relaxed)
function [y] = ref(x)
y = x+1;
end
function [y] = f(x)
y = single(x)+1;
end
Is this the fastest way to implement ? considering large number of tests (stepping size constraint).
Firan Lucian
Firan Lucian 2019년 8월 27일
Some issues may be with comparations (>=, <=) and equality == in floats.
There are also some special cases (corner cases) like inf, nan that should be handled in an meaningful way.
Monotony evaluation kernels examples
(where f is f(x) next_f is f(x+deltax) )
function [err] = monErr_single(f, next_f, ref, next_ref)
err = (ref >= next_ref) ~= (f >= next_f) | ...
(ref <= next_ref) ~= (f <= next_f);
end
or
if (ref >= next_ref)
if (f >= next_f)
err = 0;
else
err = 1;
end
else
if (f <= next_f)
err = 0;
else
err = 1;
end
end
function [err] = monStrictErr_single(f, next_f, ref, next_ref)
err = (ref > next_ref) ~= (f > next_f) | ...
(ref < next_ref) ~= (f < next_f) | ...
(ref == next_ref) ~= (f == next_f);
end
or
if (ref > next_ref)
if (f > next_f)
err = 0;
else
err = 1;
end
elseif (ref < next_ref)
if (f < next_f)
err = 0;
else
err = 1;
end
else
if (f == next_f)
err = 0;
else
err = 1;
end
end
one implemnatation may be faster than the other ?

댓글을 달려면 로그인하십시오.

답변 (1개)

Andrey Kiselnikov
Andrey Kiselnikov 2019년 8월 26일
편집: Andrey Kiselnikov 2019년 8월 26일
You can add the additional functionality to this code to perform your tasks.
a = 1:10;
isIncreasing = all(diff(a)) %or all(diff(a)>=0) if you want to allow 0 difference
  댓글 수: 2
Firan Lucian
Firan Lucian 2019년 8월 26일
The idea here is not to check if one function has one monotony.
The idea is to compare 2 functions monotony and measure how close are monotony-wise.
Keeping all values may overflow system memory (let’s say for all single precision numbers), so I think is better to iterate and check.
(not to mention multivariable functions)
Andrey Kiselnikov
Andrey Kiselnikov 2019년 8월 26일
편집: Andrey Kiselnikov 2019년 8월 26일
MATLAB is a good tool for working with big data arrays. Moreover, it designed to perform matrix operations. So I can't recommend using iteration, because it can be really slow in interpreted languages (can be speeded up by JIT compilation). Also, MATLAB has special tall arrays that do not fit system memory.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by