I am an advanced novice Mathematica user and have done a fair amount of single-variable calculus things with it. This semester I am using Mathematica for multi-variate calculus and when I tried to use Mathematica to solve a double integral I got incorrect results. I discovered that the syntax for the Integrate command has you list the variables in the opposite order they appear in the integrand. At first blush this seems dumb and annoying but perhaps there is a good reason for it. If so, I'd like to know what it is. If not, is anyone else annoyed by this?
1 Answers
This ordering is used in all functions in Mathematica, not just Integrate.
Here's an example with Table:
In[1]:= Table[f[i, j], {i, 3}, {j, 4}]
Out[1]= {{f[1, 1], f[1, 2], f[1, 3], f[1, 4]},
{f[2, 1], f[2, 2], f[2, 3], f[2, 4]},
{f[3, 1], f[3, 2], f[3, 3], f[3, 4]}}
The inner loop is according to j, the outer according i.
In[2]:= Dimensions[%]
Out[2]= {3, 4}
Looking at the dimensions it starts to make some kind of sense for Table. Even more if I write it like this:
In[3]:= Table[0, {3}, {4}]
Out[3]= {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}
Which I could replace by
In[4]:= ConstantArray[0, {3, 4}]
Out[4]= {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}
Now for ConstantArray it makes perfect sense: if I want an array with dimensions {3,4}, I'll write {3,4} in ConstantArray.
It also makes sense to make all these functions consistent with each other, so you never have to think about which convention a particular function uses: they all use the same.
In the end of course it is just a convention, and I don't think that this particular convention is better than others. I just wanted to point out that it does make sense, as it's consistent with the rest of the system: Integrate, Sum, Table, RandomReal, etc. all work the same way.
- 234,956
- 30
- 623
- 1,263
-
Thanks Szabolcs, I figured there had to be a reason and you provided a solid explanation of why. I think I'll just use the symbolic mode for double and triple integrals since I know I will screw them up otherwise. Thanks :) – Glenn Apr 12 '14 at 03:17
-
1Perhaps the convention is so you can form a single-variable integral, then cut and paste another single-variable integral into the argument of the first integral, thus yielding the "reverse" order of integration variables. – David G. Stork Jun 26 '15 at 17:38
Integrate[x + y, {x, 0, a}, {y, 0, b}]seems fine to me. What opposite order? Can someone explain the problem to me? I'm not trying to criticise the question, I'm just curious. – Apr 12 '14 at 04:18Integrate[x+y,{x,0,a},{y,0,f[x]}]refers to an integral $\int_0^a\int_0^{f(x)}(x+y)dydx$. The inversion of the $dydx$ terms with the iterator may seem unnatural to some. – VF1 Apr 12 '14 at 05:19Integrate[1, {y, 0, f[x]}, {x, 0, a}]wheref[x]shows up before any mention ofx. – Apr 13 '14 at 01:18