From 9fa3f1d768b7983bef4637908916c784070fd235 Mon Sep 17 00:00:00 2001 From: Morten Welinder Date: Sun, 27 May 2018 13:19:31 -0400 Subject: [PATCH] Deriv: cleanups. --- plugins/fn-math/functions.c | 31 +++++++++++++++++++++---------- src/expr-deriv.c | 8 ++++---- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/plugins/fn-math/functions.c b/plugins/fn-math/functions.c index 4963c7f53..7a7c7e311 100644 --- a/plugins/fn-math/functions.c +++ b/plugins/fn-math/functions.c @@ -1751,16 +1751,17 @@ gnumeric_sumsq (GnmFuncEvalInfo *ei, int argc, GnmExprConstPtr const *argv) GNM_ERROR_VALUE); } +// Construct an equivalend expression static GnmExpr const * -gnumeric_sumsq_deriv (GnmFunc *func, - GnmExpr const *expr, GnmEvalPos const *ep, +gnumeric_sumsq_equiv (GnmExpr const *expr, GnmEvalPos const *ep, GnmExprDeriv *info) { - GnmExprList *l, *args = gnm_expr_deriv_collect (expr, ep, info); - GnmExpr const *res; - GnmExpr const *sqsum; - GnmFunc *fsum = gnm_func_lookup_or_add_placeholder ("SUM"); + GnmExprList *l, *args; + GnmFunc *fsum = gnm_func_lookup ("SUM", NULL); + + if (!fsum) return NULL; + args = gnm_expr_deriv_collect (expr, ep, info); for (l = args; l; l = l->next) { GnmExpr const *e = l->data; GnmExpr const *ee = gnm_expr_new_binary @@ -1770,11 +1771,21 @@ gnumeric_sumsq_deriv (GnmFunc *func, l->data = (gpointer)ee; } - sqsum = gnm_expr_new_funcall (fsum, args); - res = gnm_expr_deriv (sqsum, ep, info); - gnm_expr_free (sqsum); + return gnm_expr_new_funcall (fsum, args); +} - return res; +static GnmExpr const * +gnumeric_sumsq_deriv (GnmFunc *func, + GnmExpr const *expr, GnmEvalPos const *ep, + GnmExprDeriv *info) +{ + GnmExpr const *sqsum = gnumeric_sumsq_equiv (expr, ep, info); + if (sqsum) { + GnmExpr const *res = gnm_expr_deriv (sqsum, ep, info); + gnm_expr_free (sqsum); + return res; + } else + return NULL; } /***************************************************************************/ diff --git a/src/expr-deriv.c b/src/expr-deriv.c index 981ccbed5..1dcb68f2f 100644 --- a/src/expr-deriv.c +++ b/src/expr-deriv.c @@ -552,9 +552,9 @@ gnm_expr_deriv (GnmExpr const *expr, case GNM_EXPR_OP_EXP: { COMMON_BINARY_START GnmFunc *fln = gnm_func_lookup ("ln", NULL); - GnmValue const *vb = gnm_expr_get_constant (b); - if (vb && VALUE_IS_FLOAT (vb)) { - GnmExpr const *bm1 = gnm_expr_new_constant (value_new_float (value_get_as_float (vb) - 1)); + gnm_float cb; + if (is_any_const (b, &cb)) { + GnmExpr const *bm1 = gnm_expr_new_constant (value_new_float (cb - 1)); GnmExpr const *t1 = mexp (a, 1, bm1, 0); gnm_expr_free (db); return mmul (mmul (b, 1, t1, 0), 0, da, 0); @@ -575,7 +575,7 @@ gnm_expr_deriv (GnmExpr const *expr, case GNM_EXPR_OP_FUNCALL: { GnmFunc *f = gnm_expr_get_func_def (expr); GnmExpr const *res = gnm_func_derivative (f, expr, ep, info); - GnmExpr const *opt = optimize (res); + GnmExpr const *opt = res ? optimize (res) : NULL; if (opt) { gnm_expr_free (res); res = opt; -- 2.11.4.GIT