0
$\begingroup$

Here's a simplified form of a function I would like to compile, but it produces errors

DataType = Compile[{{Inputdata, _Real, 1}}, Module[{CIEL, CIEa, CIEb}, dim = Dimensions@Inputdata; If[dim[[1]] > 3, {CIEL, CIEa, CIEb} = Inputdata ] ], CompilationTarget -> "C" ]; 

The error Mathematica produces is:

Compile::extscalar: dim=Dimensions[Inputdata] cannot be compiled and will be evaluated externally. The result is assumed to be of type Void. >>

Is there a workaround to allow these types of functions to compile for conditionals that depend on the dimensions of a list?

$\endgroup$
6
  • $\begingroup$ You are setting global variables from inside the compiled function, I don't think that is possible, is that your intention? What should the function return? $\endgroup$ Commented Nov 27, 2013 at 2:55
  • $\begingroup$ @s0rce Nah, fixed that. Looks like it should now. $\endgroup$ Commented Nov 27, 2013 at 3:12
  • 1
    $\begingroup$ If (Dimensions@Inputdata)[[1]] > 3 then you can't set {CIEL, CIEa, CIEb} = Inputdata since Dimensions@{CIEL, CIEa, CIEb} == {3}. $\endgroup$ Commented Nov 27, 2013 at 3:21
  • $\begingroup$ @s0rce The error is above this part of the function. so change that to whatever and the error is the same $\endgroup$ Commented Nov 27, 2013 at 3:37
  • $\begingroup$ It compiles for me, try a fresh kernel. What version are you using, I'm using 9. $\endgroup$ Commented Nov 27, 2013 at 3:42

1 Answer 1

0
$\begingroup$

Conditionals are fine in compiled functions...

DataType = Compile[{{Inputdata, _Real, 1}}, If[Length@Inputdata > 3, 1, 0], CompilationTarget -> "C"]; DataType[{5, 6, 7}] (* 0 *) DataType[{5, 6, 7, 8}] (* 1 *) 

The problem is your function returns nothing, you are trying to set global variables from inside the compiled function and also you never use a, b, and c.

$\endgroup$
3
  • $\begingroup$ Length won't work in this case. I changed the variable names at the last minute and just chose the wrong names. fixed $\endgroup$ Commented Nov 27, 2013 at 3:08
  • $\begingroup$ Isn't Length equivalent to the first element in the output of Dimensions? $\endgroup$ Commented Nov 27, 2013 at 3:19
  • $\begingroup$ It is but because dim is used multiple times I need Dimensions. $\endgroup$ Commented Nov 27, 2013 at 3:36

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.