J and L-systems

I am going to create a program that can generate strings from L-system grammars.

Original Astrid Lindenmayer L-system for modeling algae growth:

  variables: AB
 constants: none  
 axiom: A  
 rules: (A โ†’ AB), (B โ†’ A)

which produces:

  iteration |  resulting model
         0 |  A
         1 |  Ab
         2 |  ABA
         3 |  Abaab
         4 |  ABAABABA
         5 |  ABAABABAABAAB

which is naively implemented by me in J as follows:

algae =: 1&algae : (([: ; (('AB'"0)`('A'"0) @. ('AB' i. ]))&.>"0)^:[) "1 0 1 (i.6) ([;algae)"1 0 1 'A' โ”Œโ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚0โ”‚A โ”‚ โ”œโ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚1โ”‚AB โ”‚ โ”œโ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚2โ”‚ABA โ”‚ โ”œโ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚3โ”‚ABAAB โ”‚ โ”œโ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚4โ”‚ABAABABA โ”‚ โ”œโ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚5โ”‚ABAABABAABAABโ”‚ โ””โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ 

Step by step illustration:

  ('AB' i. ]) 'ABAAB' NB. determine indices of productions for each variable 0 1 0 0 1 'AB'"0`('A'"0)@.('AB' i. ])"0 'ABAAB' NB. apply corresponding productions AB A AB AB A 'AB'"0`('A'"0)@.('AB' i. ])&.>"0 'ABAAB' NB. the same &.> to avoid filling โ”Œโ”€โ”€โ”ฌโ”€โ”ฌโ”€โ”€โ”ฌโ”€โ”€โ”ฌโ”€โ” โ”‚ABโ”‚Aโ”‚ABโ”‚ABโ”‚Aโ”‚ โ””โ”€โ”€โ”ดโ”€โ”ดโ”€โ”€โ”ดโ”€โ”€โ”ดโ”€โ”˜ NB. finally ; and use ^: to iterate 

By analogy, this is the result of the 4th iteration of the L-system that generates the Thue-Morse sequence

  4 (([: ; (0 1"0)`(1 0"0)@.(0 1 i. ])&.>"0)^:[) 0 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 

This is the best I can do so far. I believe that the box-unpacking method is not enough here. This is the first time I've missed linked lists in J - it's much harder to encode grammars without them.

What I really think about is: a) building a list of gerunds of those functions that build the final string (in my examples, these functions are constants of type 'AB'"0 , but in case the tree modeling functions are tortoise graphic commands ) and calling ( `:6 ) it,
or something that I can encode:
b) constructing a string of a legal J-sentence that builds the final string and does ( ". ) this.
But I'm not sure if these programs are effective.

  • Can you show me a better approach?

Any hints, as well as comments about a) and b) are highly appreciated!

+5
source share
2 answers

The following will put a rectangular array with spaces:

  L=: rplc&('A';'AB';'B';'A') L^:(<6) 'A' A AB ABA ABAAB ABAABABA ABAABABAABAAB 

Or if you do not want to fill:

  L&.>^:(<6) <'A' โ”Œโ”€โ”ฌโ”€โ”€โ”ฌโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚Aโ”‚ABโ”‚ABAโ”‚ABAABโ”‚ABAABABAโ”‚ABAABABAABAABโ”‚ โ””โ”€โ”ดโ”€โ”€โ”ดโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ 

Obviously, you will need to check rplc / stringreplace to see what happens under the covers.

+5
source

You can use complex values โ€‹โ€‹in the left argument # to expand the array without a box.

For this particular L-system, I would probably skip gerunds and use a temporary lookup:

 to =: 2 : 'n (Im=y) } y' NB. replace n with m in y ins =: 2 : '(1 j. m=y) #!.ny' NB. insert n after each m in y L =: [: 'c'to'A' [: 'A'ins'B' [: 'B'to'c' ] 

Then:

  L^:(<6) 'A' A AB ABA ABAAB ABAABABA ABAABABAABAAB 

Here's a more general approach that simplifies the code using numbers and a gerund consisting of constant functions:

  'x'-.~"1 'xAB'{~([:,(0:`(1:,2:)`1:)@.]"0)^:(<6) 1 A AB ABA ABAAB ABAABABA ABAABABAABAAB 

ABs are displayed at the end for display. There is no boxing here because I use 0 as a null value. They are a little scattered, but -.~"1 removes them. It fills all the resulting lines with zeros to the right. If you don't want this, you can use <@-.~"1 to enter the results:

  'x'<@-.~"1 'xAB'{~([:,(0:`(1:,2:)`1:)@.]"0)^:(<6) 1 โ”Œโ”€โ”ฌโ”€โ”€โ”ฌโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚Aโ”‚ABโ”‚ABAโ”‚ABAABโ”‚ABAABABAโ”‚ABAABABAABAABโ”‚ โ””โ”€โ”ดโ”€โ”€โ”ดโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ 
+2
source

Source: https://habr.com/ru/post/1207038/


All Articles