# How to define a derivative/Variation operation in Mathematica from scratch

I am looking for a method in Mathematica 8 to define my own derivative/variation (independent of built-in operations). I do not want to re-define built-in objects since I fear that this could cause subtle changes at different places.

Does anybody know how to define such a "proto-derivative"? I would be thankful for posting a code and some explanations/documentation or a link to some document where it is described in detail.

-
Thanks, but I am looking more for a definition on a more fundamental level. –  user1712223 Oct 1 '12 at 16:20
@user1712223 I think this answer is about the best you're going to get for your question. That link does give some information that would be helpful for defining a custom derivative operator. I think the real problem is that your original question is not well posed, even after (our) edits. I'm flagging your question because of this. –  Douglas B. Staple Oct 1 '12 at 16:39
You might be interested in this. –  Ｊ. Ｍ. Oct 3 '12 at 2:23

## migrated from stackoverflow.comOct 2 '12 at 19:22

This question came from our site for professional and enthusiast programmers.

To start with, give your derivative a name, say myderiv. Let's for simplicity assume that there's no freedom to choose a variable for derivation, do your definvatives are always just written as myderiv[expression].

Now all derivatives share the following rules:

• The derivative of a constant is zero

deriv[_?NumericQ] := 0

• The derivative is linear

deriv[a_?NumericQ x_] := a deriv[x]
deriv[x_ + y_] := deriv[x]+deriv[y];

• The derivative follows the product rule (I'm assuming whatever you derive uses the normal commutative product):

deriv[a_ b_] := deriv[a] b + a deriv[b]

• Using the commutative product also allows you to use the following power rule (for non-commutative products it gets more complicated):

deriv[a_^n_Integer] := n a^(n-1) deriv[a]


Now you have to define the specifics of your derivative. Let's use as example the normal derivative for x. Then we clearly have the following rule:

    deriv[x] = 1


With this, we already can calculate the derivative of arbitrary polynomials:

    deriv[3 x^2 + 5x + 7]
(*
==> 5 + 6 x
*)


Now we have to define how to derive general functions. For that, we need a notation to denote the derivative of f; for simplicity I'll restrict it to one-argument functions. So denote the derivative of a function as d[f]. Then we can define the chain rule:

   deriv[f_[expr_]] := d[f][expr] deriv[expr]


We can also define the derivatives of some specific functions:

   d[Sin] = Cos; d[Cos] = (-Sin[#])&;


Now let's try it:

   deriv[Sin[x]+Sin[Cos[x]]]
(*
==> Cos[x] - Cos[Cos[x]] Sin[x]
*)
deriv[f[g[x]]]
(*
==> d[f][g[x]] d[g][x]
*)


Now this definition is of course still quite incomplete and also has quite some potential for optimization, but it should give you the idea.

-