Matlab vs Mathematica for computing derivatives

Click For Summary

Discussion Overview

The discussion revolves around the performance of Matlab versus Mathematica in computing derivatives, particularly in the context of handling expressions with millions of terms. Participants explore the efficiency of algorithms in both software packages for this specific computational task.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant reports using Mathematica on a supercomputer for a derivative calculation involving millions of terms and questions whether Matlab has a faster algorithm for this task.
  • Another participant points out the ambiguity in the term "term" and suggests that the complexity of the individual terms is crucial for understanding the computational challenge. They request more details about the structure of the terms to provide better insights.
  • The second participant proposes that while a million small integers may be manageable in Mathematica, a million complex items could present significant difficulties in differentiation.
  • A lengthy mathematical expression is shared, presumably as an example of the type of terms being differentiated, but no specific analysis or comparison of performance between Matlab and Mathematica is provided.

Areas of Agreement / Disagreement

Participants do not reach a consensus on whether Matlab is definitively faster than Mathematica for the described task. Multiple views on the complexity of the terms and their impact on computational efficiency remain unresolved.

Contextual Notes

The discussion highlights the need for clarity regarding the structure and complexity of the terms involved in the differentiation process, which may affect the performance of the software used.

xdrgnh
Messages
415
Reaction score
0
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?
 
Physics 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]) -
 
Ah. That was extremely helpful.

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[1]:= n = 0;
expr = 1 + q + q^3;
Print["Length is ", Length[expr]];
Map[(Print[n++]; D[#, q]) &, expr]

During evaluation of In[5]:= Length is 3

During evaluation of In[5]:= 0

During evaluation of In[5]:= 1

During evaluation of In[5]:= 2

Out[4]= 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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K