MathGroup Archive 2004

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

Search the Archive

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

  • To: mathgroup at
  • Subject: [mg51105] Re: [mg51090] Re: Derivatives of user-defined control-flow functions
  • From: "Maxim A. Dubinnyi" <maxim at>
  • Date: Tue, 5 Oct 2004 04:36:48 -0400 (EDT)
  • References: <cjoiel$al2$> <>
  • Sender: owner-wri-mathgroup at

pein wrote:

>Maxim A. Dubinnyi schrieb:
>>Can anyone correctly define derivatives of
>>user-defined control-flow functions?
>>The derivative of build-in control function 'If' is evaluated as:
>>In[]=  D[If[f[x], g[x], h[x]], x]
>>Out[]= If[f[x],g'[x],h'[x]]
>>Which is perfectly correct answer. I wish to introduce my own
>>control flow function MyIf and define it's derivatives with
>>properties identical to ones in build-in If function.
>>Usually derivatives are defined via properties of
>>symbol 'Derivative' by setting
>>Derivative[order...][function_name]:= (derivative definition)
>>but this method fails in case of control-flow expressions.
>>The problem origin is in the rule for derivatives of
>>composite functions:
>>In[]=  D[f[g[x]], x]
>>Out[]= f'[g[x]]g'[x]
>>This rule should not be applied if 'f' is control-flow function such
>>as 'If', 'Which', etc, and it is really so for build-in control-flow
>>expressions. But how can I suppress this deepely build-in rule for
>>some user-defined symbols?
>>I am experienced user of Mathematica, and I use widely symbolic
>>and functional programming in my applications,
>>but can't find any solution of this problem.
>>Is it the task which can't be solved by means of Mathematica
>>symbolic programming language?
>>Maxim A. Dubinnyi
>That is what TagSet has been made for:
>MyIf /: D[MyIf[cond_, yes_, no_], x_] := MyIf[cond, D[yes, x], D[no, x]]
Unfortunatly, this is not complete solution.
After this definition symbol D works correctly:

In[]=  D[MyIf[a, f[x], g[x]],x]
Out[]= MyIf[a, f'[x], g'[x]]

And high-order derivatives are evaluated automatically:

In[]=  D[MyIf[a, f[x], g[x]], {x, 2}]
Out[]= MyIf[a, f''[x], g''[x]]

But let us define function:

In[]=  MyCond[x_]:= MyIf[a, f[x], g[x]]

And evaluate it's derivative:

In[]=  MyCond'[x]
Out[]= g'[x] MyIf(0,0,1)[a, f[x], g[x]] + f'[x] MyIf(0,1,0)[a, f[x], g[x]]

It is due to implicit call of Derivative function, which is low-level
function for evaluation of derivatives. Actually, each call of D is
internally transformed to call of Derivative, which was not redefined.
And I don't see ways to correctly redefine Derivative to obtain
correct answer.

Let us define MyCond2 as:

In[]=  MyCond2[x_]:= If[a, f[x], g[x]]
In[]=  MyCond2'[x]
Out[]= If[a, f'[x], g'[x]]

This is correct. How should I obtain this for MyCond and MyIf?

  • Prev by Date: Re: Re: Derivatives of user-defined control-flow functions
  • Next by Date: Re: Re: Conditinal Plots
  • Previous by thread: Re: Derivatives of user-defined control-flow functions
  • Next by thread: Re: Derivatives of user-defined control-flow functions