Message Boards Message Boards

0
|
3922 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Compile substitution system?

Posted 6 years ago

I'm trying to loop a substitution system where I want the initial condition to be user-defined but the rules that the substitution follows is randomized with each iteration so I thought of compiling a function where x and n are the user input where x is the initial condition and n is the number of iterations required I'm getting all sorts of errors and I would appreciate any assistance.

RandomSA = 
 Compile[{x, {n, _Integer}}, 
  Module[{f, numb = 0}, 
   f = SubstitutionSystem[{1 -> 
       Table[RandomInteger[{1, 0}], {3}, {3}, {3}], 
      0 -> Table[0, {3}, {3}, {3}]}, x, 1]; 
   While[numb < n, 
    f = SubstitutionSystem[{1 -> 
        Table[RandomInteger[{1, 0}], {3}, {3}, {3}], 
       0 -> Table[0, {3}, {3}, {3}]}, f, 1]; numb++]; f]]
POSTED BY: Omar Mahmoud
2 Replies

Probably you should give up on compiling your function. The compiling system has limited type system. Lists have to be rectangular arrays of numbers of the same type (only Integer, Real or Complex; no Rational arrays), and so forth. Rules such as you have are not supported with the runtime system (the "Wolfram Virtual Machine" or WVM). Using non-supported types result in a call back to the main kernel for evaluation. Such call backs inside a loop are to be avoided, since it causes the code to run quite a bit slower, even slower than the uncompiled version. For the call back to function properly, you often have to declare the return type of the called, which must be one of the supported types. These call backs show up in the output of CompilePrint as a MainEvaluate call. Furthermore, some functions do not have a WVM version and also result in a call back. SubstitutionSystem is one of them. Your hope, I suppose, is that SubstitutionSystem runs as efficiently as possible already.

One slight improvement is to take advantage of the second argument of the Random* functions, and replace

Table[RandomInteger[{1, 0}], {3}, {3}, {3}]

with

RandomInteger[{1, 0}, {3, 3, 3}]

You can also use Nest instead of the while loop, but I expect that will save only a few microseconds. Compared to the time SubstitutionSystem will take, it isn't important in this example.

Finally, the uncompiled version of your function doesn't seem to work. I couldn't figure out what you wanted the output to be, so I cannot offer any suggestions about it.

POSTED BY: Michael Rogers
Posted 6 years ago

giving up compiling seemed logical as many errors occurred, as for the output I wanted was to use the substitution system with a known initial condition and randomize the substitution rule with each iteration but it always randomize the first rule and keep using it

POSTED BY: Omar Mahmoud
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract