Deep Binary Operators

Prev Next

Introduction

All valid binary operators can be applied as deep operators in order to address the parameter 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 parameter sets (vectors, matrics) is supported. In this case, the scalar provided as one operand will be calculated with every element in the parameter set specified as the other operand.

If parameter 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 parameter 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: |)
  • Parameter set: Empty set {}

  echo("Deep binary operators on simple parameter 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( a[] +   b[] );                   // Simple concatenation
  echo( a[] +^  b[] );                   // The elements containing 3 numbers are concatenated
  echo( a[] +^^ b[] );                   // What you want: Addition of the elements
Deep binary operators on simple parameter 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},{4,5,6},{1,-1,0},{2,-2,10}}
{{1,2,3,1,-1,0},{4,5,6,2,-2,10}}
{{2,1,3},{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 parameter set until all values in the longer parameter 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 parameter 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( a[] +   b[] );                     // Catenation: { {1,2},{3,4},{5,6},{7,8} }
  echo( a[] +^  b[] );                     // Catenating the 1st level elements: { {1,2,5,6},{3,4,7,8} }
  echo( a[] +^^ b[] );                     // Calculating elements: { {6,8},{10,12} }
  echo;
  echo( a[] +^^ 10 );                      // Adds 10 to all elements
  echo( a[] +^^ {10} );                    // {10} acts as scalar to {1,2} -> { {11,12},{3,4} }   
  echo( a[] +^^:{10} );                    // Repetition suffix - Repeat the {10}:Like { {1,2},{3,4} } +^^ {10,10} } -> { {11,12},{13,14} }   
  echo( a[] +^^ {10,10} );                 // Like above
  echo;
  echo( a[] +^^ {{10}} );                  // Affects the 1st nested element only: { {11,2},{3,4} }
  echo( a[] +^^:{{10}} );                  // Repetition suffix - Repeat the {10}: All elements affected: { {11,12},{13,14} }
  echo( a[] +^^ {10,20} );                 // Calculates { {11,12},{23,24} }
  echo( a[] +^^:{10,20} );                 // Same result  (Repetition suffix)
  echo;
  echo( a[] +^^ {{10,20}} );               // Calculates { {11,22},{ 3, 4} }
  echo( a[] +^^ {{10},{20}} );             // Calculates { {11, 2},{23, 4} }
  echo( a[] +^^:{{10},{20}} );             // Calculates { {11,12},{23,24} }  (Repetition suffix)
Deep binary operators on simple parameter sets (vectors) and scalars:
{2,3,4,5,6}
{12,8,6,4.8,4}

Matrices combined with vectors and scalars
{{1,2},{3,4},{5,6},{7,8}}
{{1,2,5,6},{3,4,7,8}}
{{6,8},{10,12}}

{{11,12},{13,14}}
{{11,12},{3,4}}
{{11,12},{13,14}}
{{11,12},{13,14}}

{{11,2},{3,4}}
{{11,12},{13,14}}
{{11,12},{23,24}}
{{11,12},{23,24}}

{{11,22},{3,4}}
{{11,2},{23,4}}
{{11,12},{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