Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab vs Mathematica for computing derivatives

  1. Sep 4, 2014 #1
    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?
     
  2. jcsd
  3. Sep 5, 2014 #2
    "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. Sep 5, 2014 #3
    (((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]) -
     
  5. Sep 6, 2014 #4
    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 (Text):
    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Matlab vs Mathematica for computing derivatives
  1. Matlab vs mathematica (Replies: 20)

Loading...