# Matlab vs Mathematica for computing derivatives

• Mathematica

## Main Question or Discussion Point

I'm trying to do a calculation which involves taking the derivative of a term that is millions of terms long. I'm using a supercomputer right now with Mathematica to do it but it's taking to long. Does Matlab have a faster algorithm for computing derivatives analytically?

Related MATLAB, Maple, Mathematica, LaTeX News on Phys.org
"taking the derivative of a term that is millions of terms long" doesn't provide much information. It isn't clear whether each of your terms have a few or a few thousand items making up the term. And it isn't even clear whether you are using the more or less formal definition of "term", which is strictly a sum of individual items, as opposed to "factor" which is strictly a product of individual items.

If you could paste the first 50 lines or so of your millions of terms then someone might be able to give more information. Or a description that "each of my terms is approximately this structure" might help. Even a LeafCount[yourexpression] might give you and others some information about how complicated this is. Sometimes the vast majority of the items can be almost instantly differentiated, but you have a few or a few thousand which might each require hours or many hours. A million small integers is feasible to handle with Mathematica. A million really complicated things to differentiate may not.

(((4 P - 2 p Cos[q - Q]) (-((
8 Cos[q - Q]^2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)^2) - (
2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)))/(
2 + 2 Sin[q - Q]^2) + ((2 p - 2 P Cos[q - Q]) ((
8 Cos[q - Q]^2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)^2 + (
2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) - (
2 Cos[q - Q] ((
4 Cos[q - Q] (4 P - 2 p Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2 - (2 p Sin[q - Q])/(2 + 2 Sin[q - Q]^2)))/(
2 + 2 Sin[q - Q]^2) + (
2 (-((4 Cos[q - Q] (4 P - 2 p Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2) + (2 p Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) - (
4 ((4 Cos[q - Q] (2 p - 2 P Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2 - (2 P Sin[q - Q])/(2 + 2 Sin[q - Q]^2)))/(
2 + 2 Sin[q - Q]^2) + (
2 Cos[q -
Q] (-((4 Cos[q - Q] (2 p - 2 P Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2) + (2 P Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2)) +
1/(2 + 2 Sin[q - Q]^2) (2 p -
2 P Cos[
q - Q]) (((4 P - 2 p Cos[q - Q]) ((
64 Cos[q - Q]^3 Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^3 - (
8 Cos[q - Q]^3)/(2 + 2 Sin[q - Q]^2)^2 + (
24 Cos[q - Q] Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^2 - (
2 Cos[q - Q])/(2 + 2 Sin[q - Q]^2)))/(
2 + 2 Sin[q - Q]^2) + ((2 p -
2 P Cos[q - Q]) (-((
64 Cos[q - Q]^3 Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^3) + (
8 Cos[q - Q]^3)/(2 + 2 Sin[q - Q]^2)^2 - (
24 Cos[q - Q] Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^2 + (
2 Cos[q - Q])/(2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) - (
2 Cos[q -
Q] (-((32 Cos[q - Q]^2 (4 P - 2 p Cos[q - Q]) Sin[
q - Q]^2)/(2 + 2 Sin[q - Q]^2)^3) + (
4 Cos[q - Q]^2 (4 P - 2 p Cos[q - Q]))/(2 +
2 Sin[q - Q]^2)^2 + (
16 p Cos[q - Q] Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^2 - (
4 (4 P - 2 p Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^2 - (2 p Cos[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) + (
2 ((32 Cos[q - Q]^2 (4 P - 2 p Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^3 - (
4 Cos[q - Q]^2 (4 P - 2 p Cos[q - Q]))/(2 +
2 Sin[q - Q]^2)^2 - (
16 p Cos[q - Q] Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^2 + (
4 (4 P - 2 p Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^2 + (2 p Cos[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) - (
4 (-((32 Cos[q - Q]^2 (2 p - 2 P Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^3) + (
4 Cos[q - Q]^2 (2 p - 2 P Cos[q - Q]))/(2 +
2 Sin[q - Q]^2)^2 + (
16 P Cos[q - Q] Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^2 - (
4 (2 p - 2 P Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^2 - (2 P Cos[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) + (
2 Cos[q - Q] ((
32 Cos[q - Q]^2 (2 p - 2 P Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^3 - (
4 Cos[q - Q]^2 (2 p - 2 P Cos[q - Q]))/(2 +
2 Sin[q - Q]^2)^2 - (
16 P Cos[q - Q] Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^2 + (
4 (2 p - 2 P Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^2 + (2 P Cos[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) - (
4 Cos[q - Q] (4 P - 2 p Cos[q - Q]) Sin[
q - Q] (-((
8 Cos[q - Q]^2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)^2) - (
2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)))/(2 +
2 Sin[q - Q]^2)^2 + (
2 p Sin[q -
Q] (-((8 Cos[q - Q]^2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)^2) - (
2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) - (
4 Cos[q - Q] (2 p - 2 P Cos[q - Q]) Sin[
q - Q] ((8 Cos[q - Q]^2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)^2 + (
2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)))/(2 +
2 Sin[q - Q]^2)^2 + (
2 P Sin[q - Q] ((
8 Cos[q - Q]^2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)^2 + (
2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) + (
8 Cos[q - Q]^2 Sin[
q - Q] ((
4 Cos[q - Q] (4 P - 2 p Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2 - (2 p Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2)^2 + (
2 Sin[q - Q] ((
4 Cos[q - Q] (4 P - 2 p Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2 - (2 p Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) - (
8 Cos[q - Q] Sin[
q - Q] (-((
4 Cos[q - Q] (4 P - 2 p Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2) + (2 p Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2)^2 + (
16 Cos[q - Q] Sin[
q - Q] ((
4 Cos[q - Q] (2 p - 2 P Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2 - (2 P Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2)^2 - (
8 Cos[q - Q]^2 Sin[
q - Q] (-((
4 Cos[q - Q] (2 p - 2 P Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2) + (2 P Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2)^2 - (
2 Sin[q -
Q] (-((4 Cos[q - Q] (2 p - 2 P Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2) + (2 P Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2)) +
1/(2 + 2 Sin[q - Q]^2) (4 P -
2 p Cos[q - Q]) (((2 p - 2 P Cos[q - Q]) ((
64 Cos[q - Q]^3 Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^3 - (
8 Cos[q - Q]^3)/(2 + 2 Sin[q - Q]^2)^2 + (
24 Cos[q - Q] Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^2 - (
2 Cos[q - Q])/(2 + 2 Sin[q - Q]^2)))/(
2 + 2 Sin[q - Q]^2) + ((4 P -
2 p Cos[q - Q]) (-((
64 Cos[q - Q]^3 Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^3) + (
8 Cos[q - Q]^3)/(2 + 2 Sin[q - Q]^2)^2 - (
24 Cos[q - Q] Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^2 + (
2 Cos[q - Q])/(2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) + (
2 (-((32 Cos[q - Q]^2 (4 P - 2 p Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^3) + (
4 Cos[q - Q]^2 (4 P - 2 p Cos[q - Q]))/(2 +
2 Sin[q - Q]^2)^2 + (
16 p Cos[q - Q] Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^2 - (
4 (4 P - 2 p Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^2 - (2 p Cos[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) - (
2 Cos[q - Q] ((
32 Cos[q - Q]^2 (4 P - 2 p Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^3 - (
4 Cos[q - Q]^2 (4 P - 2 p Cos[q - Q]))/(2 +
2 Sin[q - Q]^2)^2 - (
16 p Cos[q - Q] Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^2 + (
4 (4 P - 2 p Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^2 + (2 p Cos[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) + (
2 Cos[q -
Q] (-((32 Cos[q - Q]^2 (2 p - 2 P Cos[q - Q]) Sin[
q - Q]^2)/(2 + 2 Sin[q - Q]^2)^3) + (
4 Cos[q - Q]^2 (2 p - 2 P Cos[q - Q]))/(2 +
2 Sin[q - Q]^2)^2 + (
16 P Cos[q - Q] Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^2 - (
4 (2 p - 2 P Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^2 - (2 P Cos[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) - (
4 ((32 Cos[q - Q]^2 (2 p - 2 P Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^3 - (
4 Cos[q - Q]^2 (2 p - 2 P Cos[q - Q]))/(2 +
2 Sin[q - Q]^2)^2 - (
16 P Cos[q - Q] Sin[q - Q]^2)/(2 + 2 Sin[q - Q]^2)^2 + (
4 (2 p - 2 P Cos[q - Q]) Sin[q - Q]^2)/(2 +
2 Sin[q - Q]^2)^2 + (2 P Cos[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) + (
4 Cos[q - Q] (4 P - 2 p Cos[q - Q]) Sin[
q - Q] (-((
8 Cos[q - Q]^2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)^2) - (
2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)))/(2 +
2 Sin[q - Q]^2)^2 - (
2 p Sin[q -
Q] (-((8 Cos[q - Q]^2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)^2) - (
2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) + (
4 Cos[q - Q] (2 p - 2 P Cos[q - Q]) Sin[
q - Q] ((8 Cos[q - Q]^2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)^2 + (
2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)))/(2 +
2 Sin[q - Q]^2)^2 - (
2 P Sin[q - Q] ((
8 Cos[q - Q]^2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)^2 + (
2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) - (
8 Cos[q - Q]^2 Sin[
q - Q] ((
4 Cos[q - Q] (4 P - 2 p Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2 - (2 p Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2)^2 - (
2 Sin[q - Q] ((
4 Cos[q - Q] (4 P - 2 p Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2 - (2 p Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2) + (
8 Cos[q - Q] Sin[
q - Q] (-((
4 Cos[q - Q] (4 P - 2 p Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2) + (2 p Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2)^2 - (
16 Cos[q - Q] Sin[
q - Q] ((
4 Cos[q - Q] (2 p - 2 P Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2 - (2 P Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2)^2 + (
8 Cos[q - Q]^2 Sin[
q - Q] (-((
4 Cos[q - Q] (2 p - 2 P Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2) + (2 P Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2)^2 + (
2 Sin[q -
Q] (-((4 Cos[q - Q] (2 p - 2 P Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2) + (2 P Sin[q - Q])/(
2 + 2 Sin[q - Q]^2)))/(2 + 2 Sin[q - Q]^2)) - (-((
32 Cos[q - Q] Sin[q - Q])/(2 + 2 Sin[q - Q]^2)^3) - (
32 Cos[q - Q]^2 Sin[q - Q])/(2 + 2 Sin[q - Q]^2)^3) ((
4 Cos[q - Q] (p^2 + 2 P^2 - 2 p P Cos[q - Q]) Sin[q - Q])/(2 +
2 Sin[q - Q]^2)^2 - (2 p P Sin[q - Q])/(2 + 2 Sin[q - Q]^2) +
9.8 Sin[Q]) -

You have lots of denominators and lots of transcendental functions. I can see why doing a derivative of millions of those could take a long time.

You didn't say whether it was just taking a very long time and you were not sure whether it was making progress or how long it might take OR whether you had already completed this and just needed it to do it repeatedly and have it finish in minutes.

If you don't know how much progress it is making then you might try to apply this example:

Code:
In:= n = 0;
expr = 1 + q + q^3;
Print["Length is ", Length[expr]];
Map[(Print[n++]; D[#, q]) &, expr]

During evaluation of In:= Length is 3

During evaluation of In:= 0

During evaluation of In:= 1

During evaluation of In:= 2

Out= 1 + 3 q^2
That should show you how much progress is being made. If it is printing out digits rapidly then you know it is busy working on it. If it the numbers print at a fairly uniform rate then you might even be able to estimate the total calculation time. If there is an extremely long pause after printing one number then that might let you know that one of your "terms" is much much harder.

The printing process is going to slow down the calculation considerably, so your result without the Map and Print should be somewhat faster. But this could at least give you some estimate.