Generating a pattern from user-defined rules
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I want to generate a list of $L$ and $S$ according to the following rule:
upon each iteration, replace
- $S rightarrow L$
- $L rightarrow LS$
Such that, starting with $L$:
$ L rightarrow LS rightarrow LSL rightarrow LSLLS rightarrow LSLLSLSL rightarrow LSLLSLSLLSLLS rightarrow dots$
How can I automate this with Mathematica? I just want to specify the starting letter and the number of iteration and I'd want it to spit out the final result.
list-manipulation pattern-matching string-manipulation
add a comment |Â
up vote
1
down vote
favorite
I want to generate a list of $L$ and $S$ according to the following rule:
upon each iteration, replace
- $S rightarrow L$
- $L rightarrow LS$
Such that, starting with $L$:
$ L rightarrow LS rightarrow LSL rightarrow LSLLS rightarrow LSLLSLSL rightarrow LSLLSLSLLSLLS rightarrow dots$
How can I automate this with Mathematica? I just want to specify the starting letter and the number of iteration and I'd want it to spit out the final result.
list-manipulation pattern-matching string-manipulation
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I want to generate a list of $L$ and $S$ according to the following rule:
upon each iteration, replace
- $S rightarrow L$
- $L rightarrow LS$
Such that, starting with $L$:
$ L rightarrow LS rightarrow LSL rightarrow LSLLS rightarrow LSLLSLSL rightarrow LSLLSLSLLSLLS rightarrow dots$
How can I automate this with Mathematica? I just want to specify the starting letter and the number of iteration and I'd want it to spit out the final result.
list-manipulation pattern-matching string-manipulation
I want to generate a list of $L$ and $S$ according to the following rule:
upon each iteration, replace
- $S rightarrow L$
- $L rightarrow LS$
Such that, starting with $L$:
$ L rightarrow LS rightarrow LSL rightarrow LSLLS rightarrow LSLLSLSL rightarrow LSLLSLSLLSLLS rightarrow dots$
How can I automate this with Mathematica? I just want to specify the starting letter and the number of iteration and I'd want it to spit out the final result.
list-manipulation pattern-matching string-manipulation
list-manipulation pattern-matching string-manipulation
asked 57 mins ago
SuperCiocia
376210
376210
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
For the 5th iterate:
Nest[
Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
L,
5
]
L, S, L, L, S, L, S, L, L, S, L, L, S
And for the list of first 5 iterates:
NestList[
Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
L,
5
]
L, L, S, L, S, L, L, S, L, L, S, L, S, L, L, S, L, S,
L, L, S, L, L, S, L, S, L, L, S, L, L, S
Edit:
SubstitutionSystem
operates on strings instead. It is a bit faster because it is specifically designed for Lindenmayer systems:
L = "L";
S = "S";
a = StringJoin /@ NestList[
Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
L,
20
]; // AbsoluteTiming // First
b = SubstitutionSystem["L" -> "LS", "S" -> "L", "L", 20]; // AbsoluteTiming // First
a == b
0.004506
0.001265
True
What if I just wanted a random sequence of $L$ and $S$?
â SuperCiocia
36 mins ago
Then you can useRandomChoice
:RandomChoice[L, S, 10000]
will generate a list of10000
random letter at once (uniformly distributed). You can also prescribe probabilities for each letter, e.g.RandomChoice[0.1, 0.9 -> L, S, 10000]
.
â Henrik Schumacher
34 mins ago
Great, thank you.
â SuperCiocia
32 mins ago
You're welcome.
â Henrik Schumacher
29 mins ago
@SuperCiocia Please forget what I said aboutSequenceReplace
. Better useReplace
with level specification1
. That does also not suffer from using symbols.
â Henrik Schumacher
17 mins ago
add a comment |Â
up vote
1
down vote
You may use ReplaceRepeated
with its MaxIterations
option.
ReplaceRepeated[L, L -> Sequence[L, S], S -> Sequence[L],
MaxIterations -> 5]
L, S, L, L, S, L, S, L, L, S, L, L, S
Hope this helps.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
For the 5th iterate:
Nest[
Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
L,
5
]
L, S, L, L, S, L, S, L, L, S, L, L, S
And for the list of first 5 iterates:
NestList[
Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
L,
5
]
L, L, S, L, S, L, L, S, L, L, S, L, S, L, L, S, L, S,
L, L, S, L, L, S, L, S, L, L, S, L, L, S
Edit:
SubstitutionSystem
operates on strings instead. It is a bit faster because it is specifically designed for Lindenmayer systems:
L = "L";
S = "S";
a = StringJoin /@ NestList[
Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
L,
20
]; // AbsoluteTiming // First
b = SubstitutionSystem["L" -> "LS", "S" -> "L", "L", 20]; // AbsoluteTiming // First
a == b
0.004506
0.001265
True
What if I just wanted a random sequence of $L$ and $S$?
â SuperCiocia
36 mins ago
Then you can useRandomChoice
:RandomChoice[L, S, 10000]
will generate a list of10000
random letter at once (uniformly distributed). You can also prescribe probabilities for each letter, e.g.RandomChoice[0.1, 0.9 -> L, S, 10000]
.
â Henrik Schumacher
34 mins ago
Great, thank you.
â SuperCiocia
32 mins ago
You're welcome.
â Henrik Schumacher
29 mins ago
@SuperCiocia Please forget what I said aboutSequenceReplace
. Better useReplace
with level specification1
. That does also not suffer from using symbols.
â Henrik Schumacher
17 mins ago
add a comment |Â
up vote
2
down vote
accepted
For the 5th iterate:
Nest[
Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
L,
5
]
L, S, L, L, S, L, S, L, L, S, L, L, S
And for the list of first 5 iterates:
NestList[
Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
L,
5
]
L, L, S, L, S, L, L, S, L, L, S, L, S, L, L, S, L, S,
L, L, S, L, L, S, L, S, L, L, S, L, L, S
Edit:
SubstitutionSystem
operates on strings instead. It is a bit faster because it is specifically designed for Lindenmayer systems:
L = "L";
S = "S";
a = StringJoin /@ NestList[
Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
L,
20
]; // AbsoluteTiming // First
b = SubstitutionSystem["L" -> "LS", "S" -> "L", "L", 20]; // AbsoluteTiming // First
a == b
0.004506
0.001265
True
What if I just wanted a random sequence of $L$ and $S$?
â SuperCiocia
36 mins ago
Then you can useRandomChoice
:RandomChoice[L, S, 10000]
will generate a list of10000
random letter at once (uniformly distributed). You can also prescribe probabilities for each letter, e.g.RandomChoice[0.1, 0.9 -> L, S, 10000]
.
â Henrik Schumacher
34 mins ago
Great, thank you.
â SuperCiocia
32 mins ago
You're welcome.
â Henrik Schumacher
29 mins ago
@SuperCiocia Please forget what I said aboutSequenceReplace
. Better useReplace
with level specification1
. That does also not suffer from using symbols.
â Henrik Schumacher
17 mins ago
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
For the 5th iterate:
Nest[
Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
L,
5
]
L, S, L, L, S, L, S, L, L, S, L, L, S
And for the list of first 5 iterates:
NestList[
Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
L,
5
]
L, L, S, L, S, L, L, S, L, L, S, L, S, L, L, S, L, S,
L, L, S, L, L, S, L, S, L, L, S, L, L, S
Edit:
SubstitutionSystem
operates on strings instead. It is a bit faster because it is specifically designed for Lindenmayer systems:
L = "L";
S = "S";
a = StringJoin /@ NestList[
Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
L,
20
]; // AbsoluteTiming // First
b = SubstitutionSystem["L" -> "LS", "S" -> "L", "L", 20]; // AbsoluteTiming // First
a == b
0.004506
0.001265
True
For the 5th iterate:
Nest[
Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
L,
5
]
L, S, L, L, S, L, S, L, L, S, L, L, S
And for the list of first 5 iterates:
NestList[
Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
L,
5
]
L, L, S, L, S, L, L, S, L, L, S, L, S, L, L, S, L, S,
L, L, S, L, L, S, L, S, L, L, S, L, L, S
Edit:
SubstitutionSystem
operates on strings instead. It is a bit faster because it is specifically designed for Lindenmayer systems:
L = "L";
S = "S";
a = StringJoin /@ NestList[
Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
L,
20
]; // AbsoluteTiming // First
b = SubstitutionSystem["L" -> "LS", "S" -> "L", "L", 20]; // AbsoluteTiming // First
a == b
0.004506
0.001265
True
edited 18 mins ago
answered 42 mins ago
Henrik Schumacher
38.1k250109
38.1k250109
What if I just wanted a random sequence of $L$ and $S$?
â SuperCiocia
36 mins ago
Then you can useRandomChoice
:RandomChoice[L, S, 10000]
will generate a list of10000
random letter at once (uniformly distributed). You can also prescribe probabilities for each letter, e.g.RandomChoice[0.1, 0.9 -> L, S, 10000]
.
â Henrik Schumacher
34 mins ago
Great, thank you.
â SuperCiocia
32 mins ago
You're welcome.
â Henrik Schumacher
29 mins ago
@SuperCiocia Please forget what I said aboutSequenceReplace
. Better useReplace
with level specification1
. That does also not suffer from using symbols.
â Henrik Schumacher
17 mins ago
add a comment |Â
What if I just wanted a random sequence of $L$ and $S$?
â SuperCiocia
36 mins ago
Then you can useRandomChoice
:RandomChoice[L, S, 10000]
will generate a list of10000
random letter at once (uniformly distributed). You can also prescribe probabilities for each letter, e.g.RandomChoice[0.1, 0.9 -> L, S, 10000]
.
â Henrik Schumacher
34 mins ago
Great, thank you.
â SuperCiocia
32 mins ago
You're welcome.
â Henrik Schumacher
29 mins ago
@SuperCiocia Please forget what I said aboutSequenceReplace
. Better useReplace
with level specification1
. That does also not suffer from using symbols.
â Henrik Schumacher
17 mins ago
What if I just wanted a random sequence of $L$ and $S$?
â SuperCiocia
36 mins ago
What if I just wanted a random sequence of $L$ and $S$?
â SuperCiocia
36 mins ago
Then you can use
RandomChoice
: RandomChoice[L, S, 10000]
will generate a list of 10000
random letter at once (uniformly distributed). You can also prescribe probabilities for each letter, e.g. RandomChoice[0.1, 0.9 -> L, S, 10000]
.â Henrik Schumacher
34 mins ago
Then you can use
RandomChoice
: RandomChoice[L, S, 10000]
will generate a list of 10000
random letter at once (uniformly distributed). You can also prescribe probabilities for each letter, e.g. RandomChoice[0.1, 0.9 -> L, S, 10000]
.â Henrik Schumacher
34 mins ago
Great, thank you.
â SuperCiocia
32 mins ago
Great, thank you.
â SuperCiocia
32 mins ago
You're welcome.
â Henrik Schumacher
29 mins ago
You're welcome.
â Henrik Schumacher
29 mins ago
@SuperCiocia Please forget what I said about
SequenceReplace
. Better use Replace
with level specification 1
. That does also not suffer from using symbols.â Henrik Schumacher
17 mins ago
@SuperCiocia Please forget what I said about
SequenceReplace
. Better use Replace
with level specification 1
. That does also not suffer from using symbols.â Henrik Schumacher
17 mins ago
add a comment |Â
up vote
1
down vote
You may use ReplaceRepeated
with its MaxIterations
option.
ReplaceRepeated[L, L -> Sequence[L, S], S -> Sequence[L],
MaxIterations -> 5]
L, S, L, L, S, L, S, L, L, S, L, L, S
Hope this helps.
add a comment |Â
up vote
1
down vote
You may use ReplaceRepeated
with its MaxIterations
option.
ReplaceRepeated[L, L -> Sequence[L, S], S -> Sequence[L],
MaxIterations -> 5]
L, S, L, L, S, L, S, L, L, S, L, L, S
Hope this helps.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You may use ReplaceRepeated
with its MaxIterations
option.
ReplaceRepeated[L, L -> Sequence[L, S], S -> Sequence[L],
MaxIterations -> 5]
L, S, L, L, S, L, S, L, L, S, L, L, S
Hope this helps.
You may use ReplaceRepeated
with its MaxIterations
option.
ReplaceRepeated[L, L -> Sequence[L, S], S -> Sequence[L],
MaxIterations -> 5]
L, S, L, L, S, L, S, L, L, S, L, L, S
Hope this helps.
answered 7 mins ago
Edmund
24.4k32995
24.4k32995
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181998%2fgenerating-a-pattern-from-user-defined-rules%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password