[PATCH] eval: Add taylor series evaluation support.
Signed-off-by: Michael Niedermayer <michaelni@gmx.at> --- doc/eval.texi | 7 +++++++ libavutil/eval.c | 20 +++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletions(-) diff --git a/doc/eval.texi b/doc/eval.texi index 92fdc6d..b481bfe 100644 --- a/doc/eval.texi +++ b/doc/eval.texi @@ -106,6 +106,13 @@ the evaluation of @var{y}, return 0 otherwise. @item ifnot(x, y) Evaluate @var{x}, and if the result is zero return the result of the evaluation of @var{y}, return 0 otherwise. + +@item taylor(expr, x) +Evaluate a taylor series at x. +expr represents the LD(0)-th derivates of f(x) at 0. +note, when you have the derivatives at y instead of 0 +taylor(expr, x-y) can be used +When the series does not converge the results are undefined. @end table The following constants are available: diff --git a/libavutil/eval.c b/libavutil/eval.c index 2ee3965..49079c5 100644 --- a/libavutil/eval.c +++ b/libavutil/eval.c @@ -134,7 +134,7 @@ struct AVExpr { e_squish, e_gauss, e_ld, e_isnan, e_mod, e_max, e_min, e_eq, e_gt, e_gte, e_pow, e_mul, e_div, e_add, - e_last, e_st, e_while, e_floor, e_ceil, e_trunc, + e_last, e_st, e_while, e_taylor, e_floor, e_ceil, e_trunc, e_sqrt, e_not, e_random, e_hypot, e_gcd, e_if, e_ifnot, } type; @@ -181,6 +181,21 @@ static double eval_expr(Parser *p, AVExpr *e) d=eval_expr(p, e->param[1]); return d; } + case e_taylor: { + double t = 1, d = 0, ld, v; + double x = eval_expr(p, e->param[1]); + int i; + for(i=0; i<1000; i++) { + double ld = d; + p->var[0] = i; + v = eval_expr(p, e->param[0]); + d += t*v; + if(ld==d && v) + break; + t *= x / (i+1); + } + return d; + } default: { double d = eval_expr(p, e->param[0]); double d2 = eval_expr(p, e->param[1]); @@ -318,6 +333,7 @@ static int parse_primary(AVExpr **e, Parser *p) else if (strmatch(next, "isnan" )) d->type = e_isnan; else if (strmatch(next, "st" )) d->type = e_st; else if (strmatch(next, "while" )) d->type = e_while; + else if (strmatch(next, "taylor")) d->type = e_taylor; else if (strmatch(next, "floor" )) d->type = e_floor; else if (strmatch(next, "ceil" )) d->type = e_ceil; else if (strmatch(next, "trunc" )) d->type = e_trunc; @@ -698,6 +714,8 @@ int main(int argc, char **argv) "if(1, 2)", "ifnot(0, 23)", "ifnot(1, NaN) + if(0, 1)", + "taylor(1, 1)", + "taylor(eq(mod(ld(0),4),1)-eq(mod(ld(0),4),3), PI/2)", NULL }; -- 1.7.5.4
Le tridi 3 ventôse, an CCXX, Michael Niedermayer a écrit :
Signed-off-by: Michael Niedermayer <michaelni@gmx.at> --- doc/eval.texi | 7 +++++++ libavutil/eval.c | 20 +++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletions(-)
diff --git a/doc/eval.texi b/doc/eval.texi index 92fdc6d..b481bfe 100644 --- a/doc/eval.texi +++ b/doc/eval.texi @@ -106,6 +106,13 @@ the evaluation of @var{y}, return 0 otherwise. @item ifnot(x, y) Evaluate @var{x}, and if the result is zero return the result of the evaluation of @var{y}, return 0 otherwise. + +@item taylor(expr, x) +Evaluate a taylor series at x. +expr represents the LD(0)-th derivates of f(x) at 0.
Could the variable id be a parameter too? Maybe optional?
+note, when you have the derivatives at y instead of 0 +taylor(expr, x-y) can be used +When the series does not converge the results are undefined.
Do you have a practical use case in mind?
@end table
The following constants are available: diff --git a/libavutil/eval.c b/libavutil/eval.c index 2ee3965..49079c5 100644 --- a/libavutil/eval.c +++ b/libavutil/eval.c @@ -134,7 +134,7 @@ struct AVExpr { e_squish, e_gauss, e_ld, e_isnan, e_mod, e_max, e_min, e_eq, e_gt, e_gte, e_pow, e_mul, e_div, e_add, - e_last, e_st, e_while, e_floor, e_ceil, e_trunc, + e_last, e_st, e_while, e_taylor, e_floor, e_ceil, e_trunc, e_sqrt, e_not, e_random, e_hypot, e_gcd, e_if, e_ifnot, } type; @@ -181,6 +181,21 @@ static double eval_expr(Parser *p, AVExpr *e) d=eval_expr(p, e->param[1]); return d; } + case e_taylor: { + double t = 1, d = 0, ld, v; + double x = eval_expr(p, e->param[1]); + int i; + for(i=0; i<1000; i++) { + double ld = d;
ld is defined twice.
+ p->var[0] = i; + v = eval_expr(p, e->param[0]); + d += t*v; + if(ld==d && v) + break;
I am not sure if this test is correct: v could be very small but not zero, and be much larger in a future evaluation.
+ t *= x / (i+1); + } + return d; + } default: { double d = eval_expr(p, e->param[0]); double d2 = eval_expr(p, e->param[1]); @@ -318,6 +333,7 @@ static int parse_primary(AVExpr **e, Parser *p) else if (strmatch(next, "isnan" )) d->type = e_isnan; else if (strmatch(next, "st" )) d->type = e_st; else if (strmatch(next, "while" )) d->type = e_while; + else if (strmatch(next, "taylor")) d->type = e_taylor; else if (strmatch(next, "floor" )) d->type = e_floor; else if (strmatch(next, "ceil" )) d->type = e_ceil; else if (strmatch(next, "trunc" )) d->type = e_trunc; @@ -698,6 +714,8 @@ int main(int argc, char **argv) "if(1, 2)", "ifnot(0, 23)", "ifnot(1, NaN) + if(0, 1)", + "taylor(1, 1)", + "taylor(eq(mod(ld(0),4),1)-eq(mod(ld(0),4),3), PI/2)", NULL };
Regards, -- Nicolas George
On Wed, Feb 22, 2012 at 10:16:25AM +0100, Nicolas George wrote:
Le tridi 3 ventôse, an CCXX, Michael Niedermayer a écrit :
Signed-off-by: Michael Niedermayer <michaelni@gmx.at> --- doc/eval.texi | 7 +++++++ libavutil/eval.c | 20 +++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletions(-)
[...]
+ case e_taylor: { + double t = 1, d = 0, ld, v; + double x = eval_expr(p, e->param[1]); + int i; + for(i=0; i<1000; i++) { + double ld = d;
ld is defined twice.
Should we consider to add "check_cflags -Wshadow" in the configure? [...] -- Clément B.
Le quartidi 4 ventôse, an CCXX, Clément Bœsch a écrit :
Should we consider to add "check_cflags -Wshadow" in the configure?
I am not against it, but it is not always a good idea: at the very least, it would be necessary to check how much of the existing code uses legitimate shadowed variables. As for this particular case, -Wunused-variable should have noticed it anyway. Regards, -- Nicolas George
On Wed, Feb 22, 2012 at 10:16:25AM +0100, Nicolas George wrote:
Le tridi 3 ventôse, an CCXX, Michael Niedermayer a écrit :
Signed-off-by: Michael Niedermayer <michaelni@gmx.at> --- doc/eval.texi | 7 +++++++ libavutil/eval.c | 20 +++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletions(-)
diff --git a/doc/eval.texi b/doc/eval.texi index 92fdc6d..b481bfe 100644 --- a/doc/eval.texi +++ b/doc/eval.texi @@ -106,6 +106,13 @@ the evaluation of @var{y}, return 0 otherwise. @item ifnot(x, y) Evaluate @var{x}, and if the result is zero return the result of the evaluation of @var{y}, return 0 otherwise. + +@item taylor(expr, x) +Evaluate a taylor series at x. +expr represents the LD(0)-th derivates of f(x) at 0.
Could the variable id be a parameter too? Maybe optional?
should be possible, ill look into it
+note, when you have the derivatives at y instead of 0 +taylor(expr, x-y) can be used +When the series does not converge the results are undefined.
Do you have a practical use case in mind?
not really, it just felt like a important feature that was missing.
@end table
The following constants are available: diff --git a/libavutil/eval.c b/libavutil/eval.c index 2ee3965..49079c5 100644 --- a/libavutil/eval.c +++ b/libavutil/eval.c @@ -134,7 +134,7 @@ struct AVExpr { e_squish, e_gauss, e_ld, e_isnan, e_mod, e_max, e_min, e_eq, e_gt, e_gte, e_pow, e_mul, e_div, e_add, - e_last, e_st, e_while, e_floor, e_ceil, e_trunc, + e_last, e_st, e_while, e_taylor, e_floor, e_ceil, e_trunc, e_sqrt, e_not, e_random, e_hypot, e_gcd, e_if, e_ifnot, } type; @@ -181,6 +181,21 @@ static double eval_expr(Parser *p, AVExpr *e) d=eval_expr(p, e->param[1]); return d; } + case e_taylor: { + double t = 1, d = 0, ld, v; + double x = eval_expr(p, e->param[1]); + int i; + for(i=0; i<1000; i++) { + double ld = d;
ld is defined twice.
i realized and fixed that a minute after sending the patch
+ p->var[0] = i; + v = eval_expr(p, e->param[0]); + d += t*v; + if(ld==d && v) + break;
I am not sure if this test is correct: v could be very small but not zero, and be much larger in a future evaluation.
yes, but some end condition is needed. If you a better idea ... [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB The greatest way to live with honor in this world is to be what we pretend to be. -- Socrates
On date Tuesday 2012-02-21 20:48:20 +0100, Michael Niedermayer encoded:
Signed-off-by: Michael Niedermayer <michaelni@gmx.at> --- doc/eval.texi | 7 +++++++ libavutil/eval.c | 20 +++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletions(-)
diff --git a/doc/eval.texi b/doc/eval.texi index 92fdc6d..b481bfe 100644 --- a/doc/eval.texi +++ b/doc/eval.texi @@ -106,6 +106,13 @@ the evaluation of @var{y}, return 0 otherwise. @item ifnot(x, y) Evaluate @var{x}, and if the result is zero return the result of the evaluation of @var{y}, return 0 otherwise. + +@item taylor(expr, x) +Evaluate a taylor series at x.
+expr represents the LD(0)-th derivates of f(x) at 0.
Couldn't find the meaning of "LD(0)-th" on the Internet, I'll push a more explicit variant (together with minor typo fixes) if you can explain.
+note, when you have the derivatives at y instead of 0 +taylor(expr, x-y) can be used +When the series does not converge the results are undefined. @end table
[...] -- FFmpeg = Fanciful and Friendly Mind-dumbing Powered Ecstatic Gadget
Le septidi 7 ventôse, an CCXX, Stefano Sabatini a écrit :
Couldn't find the meaning of "LD(0)-th" on the Internet, I'll push a more explicit variant (together with minor typo fixes) if you can explain.
The eval system has the ld/st functions pair: st(5,42) is equivalent to var[5]=42 and st(5) is equivalent to var[5] (as a rvalue). So what it means is that the argument of taylor is toe formula to compute the n-th derivative, except you can not use "n" as a name but need to use "ld(0)". Note: I am currently in the process of rewriting completely the expression parser and evaluator. One of the benefits of the code I intend to propose is to be able to write "a" instead of "ld(0)", "b" instead of "ld(1)", etc., a=... instead of "st(0,...)", etc. (unless, of course, one of the names is shadowed by a one-letter constant name). Regards, -- Nicolas George
participants (4)
-
Clément Bœsch -
Michael Niedermayer -
Nicolas George -
Stefano Sabatini