I am using Compile in my code, defining for 'tmp' as a local variable using Block. Then I am giving it the value zero tmp=0.0 and using Do I am trying to calculate the new value of tmp.

I do not understand why I am getting this error:

Compile::cset: Variable tmp of type {_Real,0} encountered in assignment of type {_Real,1}.

which I don't understand what does 0 ,1 mean andwhat are the possible ways to fix it.

here is my code:

ne = 10;
nl = 20;
nL = 2*nl;
nst = 2 (2 nl + 1);
mvec1 = Table[-(nl - i), {i, 0, nL}]
mvec = Join[mvec1, mvec1]

Lfxn[\[Mu]_] := Mod[\[Mu] - 1, nL] + 1
sfxn[\[Mu]_] := Sign[\[Mu] - ( nst/2 + 0.5)]
kdfxn[i_, j_] := If[i == j, 1, 0]
Lvec = Table[i, {i, 1, nL}];
svec = Table[sfxn[i\[Mu]], {i\[Mu], 1, nst}];
avec = Table[kdfxn[i, j], {i, 1, ne}, {j, 1, nst}];

Vc = Block[{l, L, m, m1, p}, 
With[{code = 
  N[0.5 ThreeJSymbol[{l, 0}, {l, 0}, {L, 0}]^2 ThreeJSymbol[{l, 
      m}, {l, m1}, {L, -(m + m1)}] ThreeJSymbol[{l, -p}, {l, 
      p - (m + m1)}, {L, (m + m1)}] (2 l + 1)^2 (-1)^(
    p - (m + m1))]}, 
    Compile[{{l, _Integer}, {L, _Integer}, {m, _Integer}, {m1, \
    _Integer}, {p, _Integer}}, code, CompilationTarget -> "C"]]];

Vex = Block[{l, L, m, m1, p}, 
 With[{code = 
  N[0.5 ThreeJSymbol[{l, 0}, {l, 0}, {L, 
      0}]^2 ThreeJSymbol[{l, (-p)}, {l, m}, {l, 
      p - m}] ThreeJSymbol[{l, m1 - m + p}, {l, -m1}, {L, 
       m - p}] (2 l + 1)^2 (-1)^(m + m1)]}, 
      Compile[{{l, _Integer}, {L, _Integer}, {m, _Integer}, {m1, \
     _Integer}, {p, _Integer}}, code, CompilationTarget -> "C"]]];

   chmat = With[{Vcc = Vc, Vexx = Vex, kkdfxn = (If[# == #2, 1, 0] &)}, 
   Compile[{{nl, _Integer}, {nL, _Integer}, {nst, _Integer}, {ne, \
   _Integer}, {mvec, _Real}, {Lvec, _Real}, {avec, _Real}, {svec, _Real}},
   Block[{ms, ms1, kf01, L0, tmp1, tmp2, m0, m10, p0, l0, tmp}, Table[
  ms = Compile`GetElement[svec, nms];
  ms1 = Compile`GetElement[svec, nms1];
  L0 = Compile`GetElement[Lvec, nL0];
  l0 = nl;

  tmp = 0.0;
   m0 = Compile`GetElement[mvec, nm];
   m10 = Compile`GetElement[mvec, nm1];
   p0 = Compile`GetElement[mvec, nm3];

   tmp1 = Vcc[l0, L0, m0, m10, p0];
   tmp2 = Vexx[l0, L0, m0, m10, p0];
   kf01 = kkdfxn[ms, ms1];

    tmp += (tmp1  kf01) Compile`GetElement[avec, j, 
        nm3] Compile`GetElement[avec, j, nm3 + nm1 - nm] - 
      tmp2 Compile`GetElement[avec, j, nm3] Compile`GetElement[
        avec, j, nm1 - nm + nm3], {j, 1, ne}], {nm1, 1, nst}, {nm,
     1, nst}, {nm3, 1, nst}];

  , {nms, 1, nst}, {nms1, 1, nst}, {nL0, 1, nL}]]

, CompilationTarget -> "C", 
CompilationOptions -> {"InlineCompiledFunctions" -> True}, 
RuntimeOptions -> "Speed"]];

and also I am also getting nonphysical values using ThreeJSymbol,

 chmat[nl, nL, nst, ne, mvec, Lvec, avec, svec]

can anyone give me a hint, what are the probable mistakes I am making?


1 Answer 1


Re: What do the 0 and 1 mean?

The third entry in the documentation for Compile explains that arguments to the compiled function with the form {x,t,n} are assumed to be rank n arrays of type t objects. If you don't explicitly provide a rank, the default is 0, meaning a scalar. So the error message might be saying you tried to pass a rank 1 array (a simple list) where the compiled function expected a scalar. The fix depends on whether you intended to pass an array.


Part is compilable, so you could simplify your code by using [[]] instead of Compile`GetElement[]


Your Answer

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

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