Return-Path: X-Original-To: apmail-commons-dev-archive@www.apache.org Delivered-To: apmail-commons-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 38900102D3 for ; Sun, 25 Aug 2013 23:19:42 +0000 (UTC) Received: (qmail 86165 invoked by uid 500); 25 Aug 2013 23:19:41 -0000 Delivered-To: apmail-commons-dev-archive@commons.apache.org Received: (qmail 86066 invoked by uid 500); 25 Aug 2013 23:19:41 -0000 Mailing-List: contact dev-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Developers List" Delivered-To: mailing list dev@commons.apache.org Received: (qmail 86058 invoked by uid 99); 25 Aug 2013 23:19:41 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 25 Aug 2013 23:19:41 +0000 X-ASF-Spam-Status: No, hits=2.5 required=5.0 tests=FREEMAIL_REPLY,HTML_MESSAGE,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of ajo.fod@gmail.com designates 209.85.128.50 as permitted sender) Received: from [209.85.128.50] (HELO mail-qe0-f50.google.com) (209.85.128.50) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 25 Aug 2013 23:19:37 +0000 Received: by mail-qe0-f50.google.com with SMTP id s14so1426277qeb.9 for ; Sun, 25 Aug 2013 16:19:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=cVsYE8HtkkEWS6Wu1Bf2v6r4kXBvW9d1Jxkig0RkIn0=; b=DtNdrpRgii5hMGu+C+AfPhrvPFdsX/xs5nYuOM1XqxbGnsT6pvVCXP3IdShrTvy9nH 43maNPyX8Kb58gyBsBYiak86maZGx1OzJCQjJPNXaXc3aKYTtXZRWkOOQOsQJAkfIXrz gZ/MPVivSapiBUsVrdnDSTMEtE1ciWxg181v2AHTKL/4ObMVOT/zFn4il1OY3Jd1lbu6 pgIMZBBPEZCv0mDJKCqevxzJha+A0fp5mnI/DKPhVufbvS+34tgoMOprtlRjsW7CLSFD wPBpcsRH4df72EFCK0GALorB+KTJokQTCGVty1MjI/h6ZHiVV7f37QJYbV2YAlZy6LDW FMVQ== MIME-Version: 1.0 X-Received: by 10.224.68.4 with SMTP id t4mr12388594qai.67.1377472756992; Sun, 25 Aug 2013 16:19:16 -0700 (PDT) Received: by 10.49.71.11 with HTTP; Sun, 25 Aug 2013 16:19:16 -0700 (PDT) In-Reply-To: <521A6718.9010100@spaceroots.org> References: <5217903D.7020407@free.fr> <52187BC5.9090301@free.fr> <521A6718.9010100@spaceroots.org> Date: Sun, 25 Aug 2013 16:19:16 -0700 Message-ID: Subject: Re: [MATH] What is the derivative of 0^x From: Ajo Fod To: Commons Developers List Content-Type: multipart/alternative; boundary=001a11c2e3925e507304e4cddee9 X-Virus-Checked: Checked by ClamAV on apache.org --001a11c2e3925e507304e4cddee9 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Are you saying patched the code? Can you provide the link? -Ajo On Sun, Aug 25, 2013 at 1:20 PM, Luc Maisonobe wrote: > Le 24/08/2013 11:24, Luc Maisonobe a =E9crit : > > Le 23/08/2013 19:20, Ajo Fod a =E9crit : > >> Hello, > > > > Hi Ajo, > > > >> > >> This shows one way of interpreting the derivative for strictly +ve > numbers. > >> > >> public static void main(final String[] args) { > >> final double x =3D 1d; > >> DerivativeStructure dsA =3D new DerivativeStructure(1, 1, 0, x= ); > >> System.out.println("Derivative of |a|^x wrt x"); > >> for (int p =3D 10; p < 21; p++) { > >> double a; > >> if (p < 20) { > >> a =3D 1d / Math.pow(2d, p); > >> } else { > >> a =3D 0d; > >> } > >> final DerivativeStructure a_ds =3D new DerivativeStructure= (1, > 1, > >> a); > >> final DerivativeStructure out =3D a_ds.pow(dsA); > >> final double calc =3D (Math.pow(a, x + EPS) - Math.pow(a, = x)) > / > >> EPS; > >> System.out.format("Derivative@%f=3D%f %f\n", a, calc, > >> out.getPartialDerivative(new int[]{1})); > >> } > >> } > >> > >> At this point I"m explicitly substituting the rule that > derivative(|a|^x) =3D > >> 0 for |a|=3D0. > > > > Yes, but this fails for x =3D 0, as the limit of the finite difference = is > > -infinity and not 0. > > > > You can build your own function which explicitly assumes a is constant > > and takes care of special values as follows: > > > > public static DerivativeStructure aToX(final double a, > > final DerivativeStructure x) { > > final double lnA =3D (a =3D=3D 0 && x.getValue() =3D=3D 0) ? > > Double.NEGATIVE_INFINITY : > > FastMath.log(a); > > final double[] function =3D new double[1 + x.getOrder()]; > > function[0] =3D FastMath.pow(a, x.getValue()); > > for (int i =3D 1; i < function.length; ++i) { > > function[i] =3D lnA * function[i - 1]; > > } > > return x.compose(function); > > } > > > > This will work and provides derivatives to any order for almost any > > values of a and x, including a=3D0, x=3D1 as in your exemple, but also > > slightly better for a=3D0, x=3D0. However, it still has an important > > drawback: it won't compute the n-th order derivative correctly for a=3D= 0, > > x=3D0 and n > 1. It will provide NaN for these higher order derivatives > > instead of +/-infinity according to parity of n. > > I have added a similar function to the DerivativeStructure class (with > some errors above corrected). The main interesting property of this > function is that it is more accurate that converting a to a > DerivativeStructure and using the general x^y function. It does its best > to handle the special case, but as written above, this does NOT work for > general combination (i.e. more than one variable or more than one > order). As soon as there is a combination, the derivative will involve > something like df/dx * dg/dy and as infinities and zeros are everywheren > NaN appears immediately for these partial derivatives. This cannot be > avoided. > > If you stay away from the singularity, the function behaves correctly. > > best regards, > Luc > > > > > This is a known problem that we already encountered when dealing with > > rootN. Here is an extract of a comment in the test case > > testRootNSingularity, where similar NaN appears instead of +/- infinity= . > > The dsZero instance in the comment is simple the x parameter of the > > function, as a derivativeStructure with value 0.0 and depending on > > itself (dsZero =3D new DerivativeStructure(1, maxOrder, 0, 0.0)): > > > > > > // the following checks shows a LIMITATION of the current implementatio= n > > // we have no way to tell dsZero is a pure linear variable x =3D 0 > > // we only say: "dsZero is a structure with value =3D 0.0, > > // first derivative =3D 1.0, second and higher derivatives =3D 0.0". > > // Function composition rule for second derivatives is: > > // d2[f(g(x))]/dx2 =3D f''(g(x)) * [g'(x)]^2 + f'(g(x)) * g''(x) > > // when function f is the nth root and x =3D 0 we have: > > // f(0) =3D 0, f'(0) =3D +infinity, f''(0) =3D -infinity (and higher > > // derivatives keep switching between +infinity and -infinity) > > // so given that in our case dsZero represents g, we have g(x) =3D 0, > > // g'(x) =3D 1 and g''(x) =3D 0 > > // applying the composition rules gives: > > // d2[f(g(x))]/dx2 =3D f''(g(x)) * [g'(x)]^2 + f'(g(x)) * g''(x) > > // =3D -infinity * 1^2 + +infinity * 0 > > // =3D -infinity + NaN > > // =3D NaN > > // if we knew dsZero is really the x variable and not the identity > > // function applied to x, we would not have computed f'(g(x)) * g''(x) > > // and we would have found that the result was -infinity and not NaN > > > > Hope this helps > > Luc > > > >> > >> Thanks, > >> Ajo. > >> > >> > >> > >> On Fri, Aug 23, 2013 at 9:39 AM, Luc Maisonobe >wrote: > >> > >>> Hi Ajo, > >>> > >>> Le 23/08/2013 17:48, Ajo Fod a =E9crit : > >>>> Try this and I'm happy to explain if necessary: > >>>> > >>>> public class Derivative { > >>>> > >>>> public static void main(final String[] args) { > >>>> DerivativeStructure dsA =3D new DerivativeStructure(1, 1, 0, > 1d); > >>>> System.out.println("Derivative of constant^x wrt x"); > >>>> for (int a =3D -3; a < 3; a++) { > >>> > >>> We have chosen the classical definition which implies c^x is not > defined > >>> for real r and negative c. > >>> > >>> Our implementation is based on the decomposition c^r =3D exp(r * ln(c= )), > >>> so the NaN comes from the logarithm when c <=3D 0. > >>> > >>> Noe also that as explained in the documentation here: > >>> < > >>> > http://commons.apache.org/proper/commons-math/userguide/analysis.html#a4.= 7_Differentiation > >>>> , > >>> there are no concepts of "constants" and "variables" in this framewor= k, > >>> so we cannot draw a line between c^r as seen as a univariate function > of > >>> r, or as a univariate function of c, or as a bivariate function of c > and > >>> r, or even as a pentavariate function of p1, p2, p3, p4, p5 with both= c > >>> and r being computed elsewhere from p1...p5. So we don't make special > >>> cases for the case c =3D 0 for example. > >>> > >>> Does this explanation make sense to you? > >>> > >>> best regards, > >>> Luc > >>> > >>> > >>>> final DerivativeStructure a_ds =3D new > DerivativeStructure(1, > >>> 1, > >>>> a); > >>>> final DerivativeStructure out =3D a_ds.pow(dsA); > >>>> System.out.format("Derivative@%d=3D%f\n", a, > >>>> out.getPartialDerivative(new int[]{1})); > >>>> } > >>>> } > >>>> } > >>>> > >>>> > >>>> > >>>> On Fri, Aug 23, 2013 at 7:59 AM, Gilles >>>> wrote: > >>>> > >>>>> On Fri, 23 Aug 2013 07:17:35 -0700, Ajo Fod wrote: > >>>>> > >>>>>> Seems like the DerivativeCompiler returns NaN. > >>>>>> > >>>>>> IMHO it should return 0. > >>>>>> > >>>>> > >>>>> What should be 0? And Why? > >>>>> > >>>>> > >>>>> > >>>>>> Is this worthy of an issue? > >>>>>> > >>>>> > >>>>> As is, no. > >>>>> > >>>>> Gilles > >>>>> > >>>>> > >>>>>> Thanks, > >>>>>> -Ajo > >>>>>> > >>>>> > >>>>> > >>>>> > >>> > ------------------------------**------------------------------**--------- > >>>>> To unsubscribe, e-mail: dev-unsubscribe@commons.**apache.org< > >>> dev-unsubscribe@commons.apache.org> > >>>>> For additional commands, e-mail: dev-help@commons.apache.org > >>>>> > >>>>> > >>>> > >>> > >>> > >>> --------------------------------------------------------------------- > >>> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org > >>> For additional commands, e-mail: dev-help@commons.apache.org > >>> > >>> > >> > > > > > > --------------------------------------------------------------------- > > To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org > > For additional commands, e-mail: dev-help@commons.apache.org > > > > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org > For additional commands, e-mail: dev-help@commons.apache.org > > --001a11c2e3925e507304e4cddee9--