FoscPattern

Returns a FoscPattern.


Description

Pattern.


Attributes Summary

Instance Properties

indices Gets indices of pattern.
inverted Is true when pattern is inverted.
operator Gets operator of pattern.
patterns Gets paterns of pattern.
payload Gets payload of pattern.
period Gets period of pattern.
sum (weight)

Instance Methods: Special Methods

nil [TODO]
& Intersection (logical AND) of two patterns. Synonymous with sect.
Intersection (logical XOR) of two patterns. Synonymous with symmetricDifference.
asCompileString
format
invert Inverts pattern in place.
sect Intersection (logical AND) of two patterns.
size
storeArgs
str
symmetricDifference Intersection (logical XOR) of two patterns.
union Union (logical OR) of two patterns.

Instance Methods

booleanVector Gets boolean vector of pattern applied to input sequence with size.
getMatchingItems Gets matching items from container, selection, or sequenceable collection.
matchesIndex Is true when pattern matches index taken under totalLength.
reverse Reverses pattern.
rotate Rotates pattern by index n.


Usage

  1. Matches three indices in every eight.

    p = FoscPattern(#[0,1,7], period: 8);
    n = 16;
    n.collect { |i| [i, p.matchesIndex(i, n)] }.join("\n");
    [ 0, true ]
    [ 1, true ]
    [ 2, false ]
    [ 3, false ]
    [ 4, false ]
    [ 5, false ]
    [ 6, false ]
    [ 7, true ]
    [ 8, true ]
    [ 9, true ]
    [ 10, false ]
    [ 11, false ]
    [ 12, false ]
    [ 13, false ]
    [ 14, false ]
    [ 15, true ]
  2. Matches three indices in every sixteen.

    p = FoscPattern(#[0,1,7], period: 16);
    n = 16;
    n.collect { |i| [i, p.matchesIndex(i, n)] }.join("\n");
    [ 0, true ]
    [ 1, true ]
    [ 2, false ]
    [ 3, false ]
    [ 4, false ]
    [ 5, false ]
    [ 6, false ]
    [ 7, true ]
    [ 8, false ]
    [ 9, false ]
    [ 10, false ]
    [ 11, false ]
    [ 12, false ]
    [ 13, false ]
    [ 14, false ]
    [ 15, false ]
  3. Works with improper indices.

    p = FoscPattern(#[16,17,23], period: 16);
    n = 16;
    n.collect { |i| [i, p.matchesIndex(i, n)] }.join("\n");
    [ 0, true ]
    [ 1, true ]
    [ 2, false ]
    [ 3, false ]
    [ 4, false ]
    [ 5, false ]
    [ 6, false ]
    [ 7, true ]
    [ 8, false ]
    [ 9, false ]
    [ 10, false ]
    [ 11, false ]
    [ 12, false ]
    [ 13, false ]
    [ 14, false ]
    [ 15, false ]
  4. Sieve from opening of Xenakis’s Psappha.

    ~sieve_1a = FoscPattern.indices(#[0,1,7], 8);
    ~sieve_1b = FoscPattern.indices(#[1,3], 5);
    ~sieve_1 = ~sieve_1a & ~sieve_1b;
    ~sieve_2a = FoscPattern.indices(#[0,1,2], 8);
    ~sieve_2b = FoscPattern.indices(#[0], 5);
    ~sieve_2 = ~sieve_2a & ~sieve_2b;
    ~sieve_3 = FoscPattern.indices(#[3], 8);
    ~sieve_4 = FoscPattern.indices(#[4], 8);
    ~sieve_5a = FoscPattern.indices(#[5,6], 8);
    ~sieve_5b = FoscPattern.indices(#[2,3,4], 5);
    ~sieve_5 = ~sieve_5a & ~sieve_5b;
    ~sieve_6a = FoscPattern.indices(#[1], 8);
    ~sieve_6b = FoscPattern.indices(#[2], 5);
    ~sieve_6 = ~sieve_6a & ~sieve_6b;
    ~sieve_7a = FoscPattern.indices(#[6], 8);
    ~sieve_7b = FoscPattern.indices(#[1], 5);
    ~sieve_7 = ~sieve_7a & ~sieve_7b;
    ~sieve = ~sieve_1 | ~sieve_2 | ~sieve_3 | ~sieve_4 | ~sieve_5 | ~sieve_6 | ~sieve_7;
    
    ~sieve.booleanVector(size: ~sieve.period);
    [ 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0 ]


Instance Properties


indices

Gets indices of pattern.


inverted

Is true when pattern is inverted.


operator

Gets operator of pattern.


patterns

Gets paterns of pattern.


payload

Gets payload of pattern.


period

Gets period of pattern.

  1. Example

    p = FoscPattern.indices(#[0], period: 3) | FoscPattern.indices(#[0], period: 4);
    p.period;
    12
    p = FoscPattern.indices(#[0], period: 3);
    p.period;
    3


sum

(weight)

Gets sum of pattern.

Sum defined equal to number of indices in pattern.

Returns nonnegative integer.

  1. Example

    a = FoscPattern(#[0,2,3]);
    a.sum;
    3


Instance Methods: Special Methods

[TODO]

  1. Example

    a = "abcde";
    p = FoscPattern.first(2) union: FoscPattern.last(2);
    p.getMatchingItems(a);
    [ $a, $b, $d, $e ]


&

Intersection (logical AND) of two patterns. Synonymous with sect.

  1. Example

    a = "abcde";
    p = FoscPattern.first(3) & FoscPattern.last(3);
    p.getMatchingItems(a);
    [ $c ]


Intersection (logical XOR) of two patterns. Synonymous with symmetricDifference.

  1. Example

    a = "abcde";
    p = FoscPattern.first(2) -- FoscPattern.last(2);
    p.getMatchingItems(a);
    [ $a, $b, $d, $e ]


asCompileString

  1. Example

    p = FoscPattern.indices(#[0,1,2], 5);
    p.cs;
    FoscPattern([ 0, 1, 2 ], 5)


format

  1. Example

    p = FoscPattern.indices(#[0,1,2], 5);
    p.format;
    FoscPattern([ 0, 1, 2 ], 5)


invert

Inverts pattern in place.

  1. Example

    p = FoscPattern.indices(#[0,1,3], 4);
    p.booleanVector;
    [ 1, 1, 0, 1 ]
    p.invert;
    p.booleanVector;
    [ 0, 0, 1, 0 ]


sect

Intersection (logical AND) of two patterns.

  1. Example

    a = "abcde";
    p = FoscPattern.first(3) sect: FoscPattern.last(3);
    p.getMatchingItems(a);
    [ $c ]


size

  1. Example

    p = FoscPattern.first(2);
    p.size;
    2
    p = FoscPattern.first(2) | FoscPattern.last(2);
    p.size;
    0


storeArgs


str

  1. Example

    a = FoscPattern.indices(#[0,1,2], 5);
    a.str;
    FoscPattern([ 0, 1, 2 ], 5)


symmetricDifference

Intersection (logical XOR) of two patterns.

  1. Example

    a = "abcde";
    p = FoscPattern.first(2) symmetricDifference: FoscPattern.last(2);
    p.getMatchingItems(a);
    [ $a, $b, $d, $e ]


union

Union (logical OR) of two patterns.

  1. Example

    a = "abcde";
    p = FoscPattern.first(2) union: FoscPattern.last(2);
    p.getMatchingItems(a);
    [ $a, $b, $d, $e ]


Instance Methods


booleanVector

Gets boolean vector of pattern applied to input sequence with size.

  1. size is set to size of pattern when no value is specified for size.

    p = FoscPattern.indices(#[4,5,6,7]);
    FoscPattern([ 4, 5, 6, 7 ])
    p.booleanVector(4);
    [ 0, 0, 0, 0 ]
    p.booleanVector(8);
    [ 0, 0, 0, 0, 1, 1, 1, 1 ]
    p.booleanVector(16);
    [ 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 ]
    p.booleanVector;
    [ 0, 0, 0, 0, 1, 1, 1, 1 ]
  2. Two part pattern with logical OR (union).

    p = FoscPattern.first(3) | FoscPattern.last(3);
    p.booleanVector(8);
    [ 1, 1, 1, 0, 0, 1, 1, 1 ]
  3. Vector of inverted pattern.

    p.invert;
    p.booleanVector(8);
    [ 0, 0, 0, 1, 1, 0, 0, 0 ]


getMatchingItems

Gets matching items from container, selection, or sequenceable collection.

  1. Example

    a = "abcdefghijklmnopqrstuvwxyz";
    p = FoscPattern.indices(#[4,5,6,7]);
    p.getMatchingItems(a);
    [ $e, $f, $g, $h ]
    a = "abcdefghijklmnopqrstuvwxyz";
    p = FoscPattern.indices(#[8,9], period: 10);
    p.getMatchingItems(a);
    [ $i, $j, $s, $t ]
    a = "abcdefghijklmnopqrstuvwxyz";
    p = FoscPattern.first(4);
    p.getMatchingItems(a);
    [ $a, $b, $c, $d ]
    a = "abcdefghijklmnopqrstuvwxyz";
    p = FoscPattern.last(3);
    p.getMatchingItems(a);
    [ $x, $y, $z ]
    a = "abcdefghijklmnopqrstuvwxyz";
    p = FoscPattern.first(1) | FoscPattern.last(2);
    p.getMatchingItems(a);
    [ $a, $y, $z ]
    a = "abcdefghijklmnopqrstuvwxyz";
    p = FoscPattern(#[0,-2,-1]);
    p.getMatchingItems(a);
    [ $a, $y, $z ]


matchesIndex

Is true when pattern matches index taken under totalLength.

  1. Example

    p = FoscPattern.indices(#[0,1,7], period: 8);
    16.collect { |i| [i, p.matchesIndex(i, 16)] }.join("\n");
    [ 0, true ]
    [ 1, true ]
    [ 2, false ]
    [ 3, false ]
    [ 4, false ]
    [ 5, false ]
    [ 6, false ]
    [ 7, true ]
    [ 8, true ]
    [ 9, true ]
    [ 10, false ]
    [ 11, false ]
    [ 12, false ]
    [ 13, false ]
    [ 14, false ]
    [ 15, true ]


reverse

Reverses pattern.

Returns new pattern.

  1. Example

    p = FoscPattern(#[0,1,3], 4);
    p.booleanVector(8);
    [ 1, 1, 0, 1, 1, 1, 0, 1 ]
    p = p.reverse;
    p.booleanVector(8);
    [ 1, 0, 1, 1, 1, 0, 1, 1 ]
  2. Example

    p = FoscPattern.first(3) | FoscPattern.last(1);
    p.booleanVector(6);
    [ 1, 1, 1, 0, 0, 1 ]
    p = p.reverse;
    p.booleanVector(6);
    [ 1, 0, 0, 1, 1, 1 ]


rotate

Rotates pattern by index n.

Returns new pattern.

  1. Example

    p = FoscPattern(#[0,1,3], 4);
    p.booleanVector(8);
    [ 1, 1, 0, 1, 1, 1, 0, 1 ]
    p = p.rotate(1);
    p.booleanVector(8);
    [ 1, 1, 1, 0, 1, 1, 1, 0 ]
    p = p.rotate(-1);
    p.booleanVector(8);
    [ 1, 1, 0, 1, 1, 1, 0, 1 ]
  2. Example

    p = FoscPattern.first(3) | FoscPattern.last(1);
    p.booleanVector(6);
    [ 1, 1, 1, 0, 0, 1 ]
    p = p.rotate(1);
    p.booleanVector(6);
    [ 1, 1, 1, 1, 0, 0 ]
    p = p.rotate(-1);
    p.booleanVector(6);
    [ 1, 1, 1, 0, 0, 1 ]