# 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
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