MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Derivatives of user-defined control-flow functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg51115] Re: [mg51096] Re: Derivatives of user-defined control-flow functions
  • From: "Maxim A. Dubinnyi" <maxim at nmr.ru>
  • Date: Tue, 5 Oct 2004 04:37:00 -0400 (EDT)
  • References: <cjoiel$al2$1@smc.vnet.net> <200410041018.GAA25203@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Maxim wrote:

>You can add a definition for D:
>
>In[1]:=
>Clear[myIf]
>myIf /: D[myIf[a_, b_, c_], vars__] := myIf[a, D[b, vars], D[c, vars]]
>
>D[myIf[a[x, y], x^2, -x^2], {x, 2}]
>
>Out[3]=
>myIf[a[x, y], 2, -2]
>
>If you need the definitions for Derivative, you can look at how it is
>done for If and emulate it:
>
>In[4]:=
>Clear[myIf]
>myIf /: Derivative[1, 0, 0][myIf] = 0&;
>myIf /: Derivative[0, 1, 0][myIf] = myIf[#, 1, 0]&;
>myIf /: Derivative[0, 0, 1][myIf] = myIf[#, 0, 1]&;
>
>D[myIf[a[x, y], x^2, -x^2], {x, 2}]
>
>Out[8]=
>-2 myIf[a[x, y], 0, 1] + 2 myIf[a[x, y], 1, 0]
>  
>
>(If you omit TagSet, then the definitions will be stored as SubValues
>for Derivative, which can be pretty confusing). 
>

This answer is numerically correct, but let us see the GENERAL CASE:

In[8a]=  D[myIf[cond, f[x], g[x]], x]
Out[8a]= myIf[cond, 1, 0] f'[x] + myIf[cond, 0, 1] g'[x]

You can see, that values of f'[x] and g'[x] will be always
evaluated, whatever value of "cond" is. But "myIf" should
be CONTROL-FLOW function, which means that only one of two
expression should be evaluated, namely f'[x] if "cond" is True,
or g'[x] if "cond" is False. So preferred answer is

Out[8b]= myIf[cond, f'[x], g'[x]]

The difference between Out[8a] and Out[8b] is essential for
time-consuming functions f'[x] and/or g'[x].

I can introduce rules for simplification of Out[8a] to Out[8b],
for example:

In[10]= myIf/:(myIf[cond_, a_, b_]*c_):=
               myIf[cond, a*c, b*c]
In[11]= myIf/:(myIf[cond_, a1_, b1_]+myIf[cond_, a2_, b2_]):=
               myIf[cond, a1+b1, a2+b2]

Now In[8a] is evaluated to:

In[12]=  D[myIf[cond, f[x], g[x]], x]
Out[12]= myIf[cond, f'[x], g'[x]]

But we have side effects of this definition: rules [10,11] will
be applied whenever it's possible, for example:

In[13]=  myIf[cond, f[x], g[x]]h[x]
Out[13]= myIf[cond, f[x]h[x], g[x]h[x]]

But

In[14]=  If[cond, f[x], g[x]]h[x]
Out[14]= If[cond, f[x], g[x]]h[x]

>Notice that while
>Mathematica's rule for the differentiation of If can be handy, it is,
>strictly speaking, not correct; for example:
>
>In[9]:=
>D[If[x == 0, 0, Sin[x]^2/x], x]
>
>Out[9]=
>If[x == 0, 0, (2*Sin[x]*Cos[x])/x - Sin[x]^2/x^2]
>
>while the correct value for the derivative at zero is 1.
>  
>

This is an interesting observation. And one of the reasons
to write user-defined control-flow functions.

Maxim A. Dubinnyi


  • Prev by Date: Conveniently Restarting Notebooks
  • Next by Date: Re: Re: Derivatives of user-defined control-flow functions
  • Previous by thread: Re: Derivatives of user-defined control-flow functions
  • Next by thread: Re: Derivatives of user-defined control-flow functions