1
$\begingroup$

How can do it?

t = {};
f = 
 Compile[{{l, _Integer}}, 
   Module[
     {list = {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}}, 
     Map[AppendTo[t, list[[#]] &], l]]; 
     t
   ];

CompiledFunction::cfta

there is two related question: this and this.

Update: with help of Sumit (here) I write another function but still there is problem with Sequence@@.

Compile[{{l, _Integer}}, 
  Module[{positions = {1, 2, 4}},
   Table[KroneckerDelta[#1, 
       Sequence @@ Flatten@Position[list, #2]]*#2 &[x, y], 
      {x,positions}, {y, l}]
   ]
]

Or this:

  f = Compile[{{r, _Integer, 2}},
  Table[
    Map[KroneckerDelta[p, Position[r, #][[1]]]*# &, r], {p, {2, 4}}]]
$\endgroup$
6
  • $\begingroup$ You're practicing to use Compile? If not, why not simply use t = list[[l]] ? $\endgroup$
    – xzczd
    Commented Jan 13, 2016 at 7:45
  • $\begingroup$ Yes, Of course , there is not any problem without compile.@xzczd $\endgroup$
    – jack cilba
    Commented Jan 13, 2016 at 8:24
  • $\begingroup$ the simpler question is : How to choose some elements of given list to compile by their position. $\endgroup$
    – jack cilba
    Commented Jan 13, 2016 at 9:00
  • $\begingroup$ this might be helpful mathematica.stackexchange.com/questions/88527/… $\endgroup$
    – Sumit
    Commented Jan 13, 2016 at 10:04
  • $\begingroup$ Then are you just looking for a compiled function or you want to insist on AppendTo, Map etc.? If the former, then how about f = Compile[{{l, _Integer, 1}}, Module[{list = {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}}, list[[l]]]]; t = f[{1, 2, 1}]? $\endgroup$
    – xzczd
    Commented Jan 13, 2016 at 11:42

1 Answer 1

2
$\begingroup$

You are using the global symbol t within a compiled function. This is not going to work like you expect it. What you probably want is the following simple function providing that I understood you correctly:

f = Compile[{{l, _Integer, 1}}, Module[{
     list = {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}},
     t = Most[{{1}}]},
    t = Map[list[[#]] &, l];
    t
  ]
];

No AppendTo needed and it gives you a local t that you can use inside the compiled function itself.

f[{1, 2, 1, 3}]
(* {{1, 1, 1}, {2, 2, 2}, {1, 1, 1}, {3, 3, 3}} *)

Please pay attention that you have to help Compile determining the correct data types. I explicitly defined l as a list of numbers and even the expression

Most[{{1}}]

only gives the empty list {}, BUT the compiler saw that I had a list of integer lists and therefore, assumes t should have exactly this type.

$\endgroup$
6
  • 1
    $\begingroup$ Actually if one insists on AppendTo then Map will fail, not sure about the exact reason though. $\endgroup$
    – xzczd
    Commented Jan 13, 2016 at 15:55
  • $\begingroup$ I have another question but I don't know ask it as a new question or not, actually the list that I want to choose from it is a global variable. I try it to give the list to Compile (function) but it produce another problem, the function should be used in FixedPoint. but I don't know how to give 2 arguments function to FixedPoint. $\endgroup$
    – jack cilba
    Commented Jan 13, 2016 at 17:08
  • 1
    $\begingroup$ Ask it as new question and give your short algorithm without using Compile, so that people understand what you try to do. $\endgroup$
    – halirutan
    Commented Jan 13, 2016 at 17:12
  • $\begingroup$ and another problem: the elements of former list are not in same size. for example {{1},{1,3,4,2},{4,2}} $\endgroup$
    – jack cilba
    Commented Jan 13, 2016 at 17:12
  • 1
    $\begingroup$ This is called a ragged array and it cannot be used as natural inside Compile as you might think. There are ways to solve this, but that are advanced and you already need to be quite confident with Compile to use them. In general, just think of it as impossible to either give ragged arrays as input or return them. $\endgroup$
    – halirutan
    Commented Jan 13, 2016 at 17:16

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.