Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question already has an answer here:

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?

share|improve this question

marked as duplicate by Oleksandr R., Sjoerd C. de Vries, m_goldberg, Yves Klett, rasher Apr 27 at 20:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
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? –  s0rce Nov 27 '13 at 2:55
    
@s0rce Nah, fixed that. Looks like it should now. –  R Hall Nov 27 '13 at 3:12
1  
If (Dimensions@Inputdata)[[1]] > 3 then you can't set {CIEL, CIEa, CIEb} = Inputdata since Dimensions@{CIEL, CIEa, CIEb} == {3}. –  s0rce Nov 27 '13 at 3:21
    
@s0rce The error is above this part of the function. so change that to whatever and the error is the same –  R Hall Nov 27 '13 at 3:37
    
It compiles for me, try a fresh kernel. What version are you using, I'm using 9. –  s0rce Nov 27 '13 at 3:42

1 Answer 1

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.

share|improve this answer
    
Length won't work in this case. I changed the variable names at the last minute and just chose the wrong names. fixed –  R Hall Nov 27 '13 at 3:08
    
Isn't Length equivalent to the first element in the output of Dimensions? –  s0rce Nov 27 '13 at 3:19
    
It is but because dim is used multiple times I need Dimensions. –  R Hall Nov 27 '13 at 3:36

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