# Creating a function that differentiates rational functions

I want to create my own differentiation function Derivada. So, I already set some properties like:

Derivada[x_^(n_: 1), x_Symbol] /; FreeQ[n, x] := n*x^(n - 1)
Derivada[n_*x_, x_Symbol] /; FreeQ[n, x] := n
Derivada[Log[a_: E, x_], x_Symbol] := 1/(x*Log[a])
Derivada[f_, x_Symbol] /; FreeQ[f, x] := 0


I have a problem with rational functions.

The functions works for:

In[1224]:= Derivada[x/(x + 1), x]

Out[1224]= 1/(1 + x)^2


But the function doesn't works for:

In[1225]:= Derivada[1/(x + 1), x]



What can be wrong?

• just add the rule Derivada[1 /v_, x_Symbol] := -Derivada[v, x]/v^2? – kglr Aug 30 '18 at 23:05
• Hi, I edited your question to improve the English a bit. Please have a look, and check that you're happy with the edit. Otherwise, you can always choose to rollback. – QuantumDot Aug 31 '18 at 15:16
• Note that the derivative of x/(x+1) is -x/(1+x)^2 + 1/(1+x), NOT 1/(1+x)^2 as in your result! – evanb Aug 31 '18 at 15:39

The problem: Mathematica manages to change the form of the underlying expression, and the expression no longer matches the pattern.

u_/v_ // FullForm

Times[Pattern[u,Blank[]],Power[Pattern[v,Blank[]],-1]]

1/(x + 1) // FullForm

Power[Plus[1,x],-1]


It also can not match the rule Derivada[x_^(n_), x_Symbol] as in the case of Power[Plus[1,x],-1] the first argument is not the same x as the second argument in your rule (for a good reason).

The solution: You could keep on implementing a rule for every special case or you could implement a chain rule. Simplification of symbolic expressions is a field of research and sooner or later you will start publishing your methods in journals :)

EDIT

Just because it is a fun excuse to take the pattern matching to the limits, here is an implementation of 2 level chain rule:

Derivada[f:fh_[farg1___,g:gh_[garg1___,x_,garg2___],farg2___],x_]/;And[