Weights in Pwrand as patterns?

I try to implement weighted sums in the Patterns system and thought this should work (ignore the boringness of the code, it is simplified) but it apparently throws an error for me, anyone knows why?

a = Pwrand([false, true], [Pseq([1, 0], inf), Pseq([0, 1], inf)], inf).asStream;
a.next;

error:

// [...]
^^ The preceding error dump is for ERROR: Primitive '_ArrayWIndex' failed.
Wrong type.
RECEIVER: [ a Pseq, a Pseq ]

anyone has an idea what I am doing wrong?

Hey till :slightly_smiling_face:
I’ve got an improved Pw that can take patterns for weights, and doesn’t require normalized weight values, I’ll post when I’m at my computer.
(To answer your q: I recall this being a limitation of Pwrand, which lead me to make the newer version)

1 Like

Simpler answer: for an array of patterns, you need Ptuple.

Ptuple([Pseq([1, 0], inf), Pseq([0, 1], inf)]).collect(_.normalizeSum)

hjh

3 Likes

I’ve found I use this class VERY heavily - composing by using Pwnrands containing Pseg's for each weight has become a pretty core compositional pattern for me.

3 Likes

That is very nice! Would be worth quarking.

hjh

Thanks, @scztt, this is cool!

I also tried @jamshark70 's solution:

Pwrand([1, 2, 3], Ptuple([10, 1, 1]).collect(_.normalizeSum), 100).asStream.all.histo.plot; // weights are normalized for each .next()
Pwrand([1, 2, 3], Ptuple([2, 1, 1]).collect(_.normalizeSum), 100).asStream.all.histo.plot; 
Pwrand([1, 2], Ptuple([1, Pseq([0.0, 1]).stutter(50)]).collect(_.normalizeSum), 100).asStream.all.histo.plot; // Weights can be streams

Which allows the same behaviour (regarding normalisation and integration of patterns into the weights). So, there we have two solutions… but I am required by the mechanism of this forum to pick one and James’ works without extensions…

I learned a lot from this, thanks!

2 Likes