2

I have a function that take p as a parameter, p is a 2X3X3 list,

( a 2X3X3 list can like { {{2,3},{1,2.},{1,1.}}, {{2,3.},{1,2},{1,1}}, {{2,3},{1,2},{1,1}} } )

the function just as:

func[p_]:= Module[{}, {Main Fuction} ];

If I want to use Complie Function:

func= Compile[{p,_Real,???},
Module[{}, {Main Fuction} ]
];

How do I define ??? (above)
YuYong
  • 129
  • 6
  • Try and read your own question to see if it is clear enough, because I have hard time understanding it. – Sektor Jul 25 '14 at 11:14
  • 1
    ??? is Length@Dimensions[p]. In this case, 3. – Oleksandr R. Jul 25 '14 at 11:31
  • @Oleksandr I would (and did) use TensorRank myself. :^) – Mr.Wizard Jul 25 '14 at 11:32
  • Considering the code you showed in your previous question, I want to remind you that Compile isn't a panacea for speed, it'll lead to more limitation in programming and may bring you endless grief indeed. Have you already read this and this post? – xzczd Jul 25 '14 at 11:48
  • @ xzczd, You are so clever, I just want to add a Compile Function to speed up my code, But found it cannot..... – YuYong Jul 25 '14 at 11:51
  • Well, you should not place a space between "@" and my name, or I won't get the reminder. – xzczd Jul 25 '14 at 11:57
  • BTW, If you feel satisfied with an answer, you can accept it by clicking the tick at the left-top corner of it. – xzczd Jul 25 '14 at 13:11

1 Answers1

2

Compile:

enter image description here

TensorRank:

enter image description here

{ {{2,3},{1,2.},{1,1.}}, {{2,3.},{1,2},{1,1}}, {{2,3},{1,2},{1,1}} } // TensorRank
3

Therefore:

cfn = Compile[{{p, _Real, 3}}, Plus @@@ p];

{{{2, 3}, {1, 2.}, {1, 1.}}, {{2, 3.}, {1, 2}, {1, 1}}, {{2, 3}, {1, 2}, {1, 1}}} // cfn
{{4., 6.}, {4., 6.}, {4., 6.}}

If you are using an older version of Mathematica use ArrayDepth instead.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371