1
$\begingroup$

Suppose I defined list or matrix with numericQ with some purpose,

AA[kx_?NumericQ, ky_?NumericQ] := {{kx^.5, 2 ky^2}, {kx^2, ky^2}},

and I want to numerically integrate this, such as,

NIntegrate[AA[qx, qy][[2,2]], {qx, 0, 10}, {qy, 0, 2}].

This manages to give right result with error,

Part::partd: Part specification AA[qx,qy][[2,2]] is longer than depth of object. >>

This is the first thing I can't understand because there seems to be no syntax error. Next, suppose I mistyped the above integration like,

NIntegrate[AA[qx, qy][[2]], {qx, 0, 10}, {qy, 0, 2}].

Even though AA[qx, qy][[2]] is List, NIntegrate gives number. That's the second I don't understand. Because of error I couldn't trust the result in research level.

$\endgroup$

3 Answers 3

2
$\begingroup$

re: why, since you specify _?NumericQ when NIntegrate makes its first attempt at symbolic evaluation AA returns unevaluated, thus the Part request fails.

here is another way to work around the problem:

AA[kx_?NumericQ, ky_?NumericQ, i_, j_] :=
    {{kx^.5, 2 ky^2}, {kx^2, ky^2}}[[i, j]];
NIntegrate[AA[qx, qy, 2, 2], {qx, 0, 10}, {qy, 0, 2}]

26.6667

as to the even stranger seeming second result, the symbolic expression AA[qx,qy] has first level parts which are the arguments. AA[qx,qy][[2]] -> qy so then your integral is

  NIntegrate[qy, {qx, 0, 10}, {qy, 0, 2}]

20.

$\endgroup$
4
$\begingroup$

You can use Indexed. Indexed works like Part, but only extracts the part when the argument is a list. So:

NIntegrate[
    Indexed[AA[qx,qy], {2,2}],
    {qx, 0, 10}, {qy, 0, 2}
]

26.6667

$\endgroup$
1
  • $\begingroup$ + .. note to document writers, why does neither Part nor Extract say "see also" Indexed ? $\endgroup$
    – george2079
    Nov 29, 2017 at 1:33
2
$\begingroup$

Try this:

AA[kx_, ky_] := {{kx^.5, 2 ky^2}, {kx^2, ky^2}};
NIntegrate[AA[qx, qy][[2, 2]], {qx, 0, 10}, {qy, 0, 2}]

(*  26.7  *)

Have fun!

$\endgroup$
1
  • $\begingroup$ Yeah. I know the answer but what I don't get it is what 's wrong with NumericQ. At the first time, I think NumericQ and NIntegrate would work well but it did not. $\endgroup$
    – L. JIN
    Nov 28, 2017 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.