Deep Binary Operators

Prev Next

Introduction

All valid binary operators can be applied as deep operators in order to address the set elements directly. The number of accent circumflex symbols behind the operator determines the depth to use. A mix of combining simple values (scalars) and sets (vectors, matrics) is supported. In this case, the scalar provided as one operand will be calculated with every element in the set specified as the other operand.

If sets are specified in both sides of the operator, and the number of elements differ, then the shorter parameter set will be stuffed with neutral values to match the length of the longer set before doing the calculation.
Example: { 3, 4 } * { 2, 1, 3, 2 } extends to { 3, 4, 1, 1 } * { 2, 1, 3, 2 } where 1 is used as a neutral value for multiplication.

List of neutral values:

  • String: Blank string
  • Numeral: 0 for + and -; 1 for * and /
  • Dates: 0
  • Boolean: true (for AND: &), 0 (for OR: |)
  • Set: Empty set {}

       echo("Deep binary operators on simple sets (vectors):");
       a[] = {  1, 2, 3, 4, 5 };
       b[] = { 10,15,20,25,30 };
       c[] = {  2,-2,'',-2, 2 };

       s[] = { Ha, He, Ho, Hu, Hi };
       t[] = { ngar, ring, ney, nger, ll };

       echo( a[] + b[]);         // Conventional way: Concatenates vectors
       echo( a[] *^ b[]);        // Multiplies the elements together

       echo( s[] +^ t[]);        // Combines strings

       echo( { rein, 1, date("2020-04-30") } +^ { deer, 2, 1 } );
                                 // Mixed types are OK

       echo(new line, "Vectors of different sizes");

       echo( a[] *^ { 10, 100, 1000 } );      // First 3 elements are multiplied
       echo( b[] -^ { 2,4,5} );               // First 3 elements are negated
       echo( { 3,3,3,} -^ a[] );              // First 3 elements are calculated, remaining ones negated

       echo( a[] /^ { 2,4,6 } );              // First 3 elemnts are divided

       echo( a[] +^ c[] );                    // Like with normal arithmetic operators, blank strings
                                              // are interpreted as zero.


       echo(new line, "Matrices");

a[] =  {  { 1,  2, 3 }, { 4,  5,  6 } };
b[] =  {  { 1, -1, 0 }, { 2, -2, 10 } };

echo matrix( "0", 3, " | ", "a[]=", a[], " b[]=", b[] );
echo matrix( "0", 3, " | ", "a[] +   b[] = ",  a[] +   b[] );
echo matrix( "0", 3, " | ", "a[] +^  b[] = ",  a[] +^  b[] );
echo matrix( "0", 3, " | ", "a[] +^^ b[] = ",  a[] +^^ b[] );
Deep binary operators on simple sets (vectors):
{1,2,3,4,5,10,15,20,25,30}
{10,30,60,100,150}
{'Hangar','Hering','Honey','Hunger','Hill'}
{'reindeer',3,'2020-05-01'}

Vectors of different sizes
{10,200,3000,4,5}
{8,11,15,25,30}
{2,1,0,-4,-5}
{0.5,0.5,0.5,4,5}
{3,0,3,2,7}

Matrices
     |   1    2    3 |       |   1   -1    0 |
a[]= |   4    5    6 |  b[]= |   2   -2   10 |

               |   1    2    3 |
               |   4    5    6 |
a[] +   b[] =  |   1   -1    0 |
               |   2   -2   10 |

               |   1    2    3    1   -1    0 |
a[] +^  b[] =  |   4    5    6    2   -2   10 |

               |   2    1    3 |
a[] +^^ b[] =  |   6    3   16 |

Try it yourself: Open LAN_Features_deep_binary_operators.b4p in B4P_Examples.zip. Decompress before use.

Repetition Suffix

Particularly useful for operations with matrices of differen length is the repetition suffix which is a colon :. Add the colon after the last accent circumflex symbol to repeat the values in the shorter set until all values in the longer set have been calculated.

Adding the colon as repetition suffix
       a[] = { 1, 2, 4, 8, 16 };
       b[] = { 2, 3 };

       echo( a[] +^ b[] );  // 3, 5, 4,  8, 16
       echo( a[] +^:b[] );  // 3, 5, 6, 11, 18
Combining vectors and matrices with scalars
       echo("Deep binary operators on simple sets (vectors) and scalars:");

       a[] = {  1, 2, 3, 4, 5 };
       b[] = { 10,15,20,25,30 };
       c[] = {  2,-2,'',-2, 2 };

       s[] = { Ha, He, Ho, Hu, Hi };
       t[] = { ngar, ring, ney, nger, ll };

       echo( a[] +^ 1 );         // Add 1 to all elements
       echo( 120 /^ b[]);        // Divide 120 by all elements


       echo(new line, "Matrices combined with vectors and scalars");

       a[] = { {1,2},{3,4} };  
       b[] = { {5,6},{7,8} };

       echo matrix( "0", 3, " | ", a[], " +   ", b[], " = ", a[] +   b[] );
       echo matrix( "0", 3, " | ", a[], " +^  ", b[], " = ", a[] +^  b[] );
       echo matrix( "0", 3, " | ", a[], " +^^ ", b[], " = ", a[] +^^ b[] );

       echo matrix( "0", 3, " | ", a[], " +^^ ", 10, " = ", a[] +^^ 10 ); // Add 10 to all scalars
       echo matrix( "0", 3, " | ", a[], " +^^ ", {10}, " = ", a[] +^^ {10} ); // Added to top row
       echo matrix( "0", 3, " | ", a[], " +^^:", {10}, " = ", a[] +^^:{10} ); // Repetition rule, add to both rows
       echo matrix( "0", 3, " | ", a[], " +^^ ", {10,20}, " = ", a[] +^^{10,20} ); // Top row + 10, bottom row +20
       echo matrix( "0", 3, " | ", a[], " +^^:", {10,20}, " = ", a[] +^^:{10,20} ); // Same results

       echo matrix( "0", 3, " | ", a[], " +^^ ", {{10}}, " = ", a[] +^^ {{10}} ); // Add to 1st element only
       echo matrix( "0", 3, " | ", a[], " +^^:", {{10}}, " = ", a[] +^^:{{10}} ); // Repetition rule, add to all elements

       echo matrix( "0", 3, " | ", a[], " +^^ ", {10,20}, " = ", a[] +^^{10,20} ); // Top row + 10, bottom row +20

       echo matrix( "0", 3, " | ", a[], " +^^ ", {{10,20}}, " = ", a[] +^^{{10,20}} ); // Addition to upper row
       echo matrix( "0", 3, " | ", a[], " +^^:", {{10,20}}, " = ", a[] +^^:{{10,20}} ); // Repetition rule, Done on both rows
       echo matrix( "0", 3, " | ", a[], " +^^ ", {{10},{20}}, " = ", a[] +^^{{10},{20}} ); // Addition to left column
       echo matrix( "0", 3, " | ", a[], " +^^:", {{10},{20}}, " = ", a[] +^^:{{10},{20}} ); // Addition to both columns
Deep binary operators on simple sets (vectors) and scalars:
{2,3,4,5,6}
{12,8,6,4.8,4}

Matrices combined with vectors and scalars
                                     |   1    2 |
|   1    2 |       |   5    6 |     |   3    4 |
|   3    4 |  +    |   7    8 |  =  |   5    6 |
                                     |   7    8 |

|   1    2 |       |   5    6 |     |   1    2    5    6 |
|   3    4 |  +^   |   7    8 |  =  |   3    4    7    8 |

|   1    2 |       |   5    6 |     |   6    8 |
|   3    4 |  +^^  |   7    8 |  =  |  10   12 |

|   1    2 |            |  11   12 |
|   3    4 |  +^^ 10 =  |  13   14 |

|   1    2 |                   |  11   12 |
|   3    4 |  +^^  |  10 |  =  |   3    4 |

|   1    2 |                   |  11   12 |
|   3    4 |  +^^: |  10 |  =  |  13   14 |

|   1    2 |       |  10 |     |  11   12 |
|   3    4 |  +^^  |  20 |  =  |  23   24 |

|   1    2 |       |  10 |     |  11   12 |
|   3    4 |  +^^: |  20 |  =  |  23   24 |

|   1    2 |                   |  11    2 |
|   3    4 |  +^^  |  10 |  =  |   3    4 |

|   1    2 |                   |  11   12 |
|   3    4 |  +^^: |  10 |  =  |  13   14 |

|   1    2 |       |  10 |     |  11   12 |
|   3    4 |  +^^  |  20 |  =  |  23   24 |

|   1    2 |                        |  11   22 |
|   3    4 |  +^^  |  10   20 |  =  |   3    4 |

|   1    2 |                        |  11   22 |
|   3    4 |  +^^: |  10   20 |  =  |  13   24 |

|   1    2 |       |  10 |     |  11    2 |
|   3    4 |  +^^  |  20 |  =  |  23    4 |

|   1    2 |       |  10 |     |  11   12 |
|   3    4 |  +^^: |  20 |  =  |  23   24 |

Try it yourself: Open LAN_Features_deep_binary_operators_01.b4p in B4P_Examples.zip. Decompress before use.

See also

Deep assignment operators
Matrix Operations Summary
echo matrix
print matrix