Vectorization

Prev Next

Introduction

Most mathematical, type conversion, string and date functions, as well as various additional B4P functions, support vectorization in the first and, in a few cases, second function parameters. This feature enables functions, which accept single values and return single values, to process sets and also nested sets containing multiple values in any structure and return sets in the corresponding structure containing the results.

Example: sqrt( {4, 16, { 9, 81 }} ) returns { 2, 4, { 3, 9 }}.

Key benefits:

  • Vectors, defined in 1-level sets, can be processed with a single function call
  • Matrices, defined in 2-level parameter, can also be processed with a single function call
  • Same applies to any form of nested sets
  • Complete table columns can be processed with a single assignment without using the table process() function.
  • The cumbersome functions deep() and deepr() become unneccessary.



Some functions, including, but not limited to, mod(), pow(), excel coordinates(), round(), support vectorization on both first two parameters. In these cases, either the two sets must contain the same number of elements, or a set in one function parameter can be matched with a scalar in the other function parameter where the scalar will be used repeatedly.

Allowed examples with 2 vectorized parameters:

Formulation Functionality
pow( {1, 2, 3}, {4, 5, 6 ) Calculates 1^4, 2^5 and 3^6
pow( 10, {1, 2, 3} ) Calculates 10^1, 10^2 and 10^3
pow( { 1, 2, 3 }, 2 ) Calculates 1^2, 2^2 and 3^2
pow( { 1, {2, 3}}, {4, 5} ) Calculates 1^4, 2^5 and 3^5
pow( { 1, 2, 3}, { 4, 5 } ) Error: Sizes of sets do not match




Following functions share the same names for processing strings and sets, so alternative function names beginning with the prefix letter v have been introduced to support vectorization and avoid confusing between processing strings and sets.

Deviating function names to support vectorization on processing sets with multiple strings inside:

Original Functions Vectorizable Functions Explanation
left() vleft() Extract left parts of strings
left include() vleft include() "
left last match() vleft last match() "
left last match include() vleft last match include() "
right() vright() Extract right parts of strings
right include() vright include() "
right last match() vright last match() "
right last match include() vright last match include() "
middle() vmiddle() Extract middle parts of strings
middle include() vmiddle include() "
middle until() vmiddle until() "
middle until include() vmiddle until include() "
outside() voutside() Remove middle parts of strings
outside exclude() voutside exclude() "
outside until() voutside until() "
outside until exclude() voutside until exclude() "
length() vlength() Note: No 'v' prefix is needed for width() and height()
trim() vtrim() Trim strings
trim all() vtrim all() "
str() vstr() Convert to quoted string type
soft() vsoft() Convert to softquoted string type
type() vtype() Identify types
subtype() vsubtype() Identify subtypes

Note: Vectorization cannot be activated for user-defined procedures and functions.

       echo("Simple example with vectorization");
       p[] = { 0, 30, 60, 90 };
       echo( "With vectorization: ", sin deg( p[] ) );

       // And the old way: Same results, but supports 1-level sets only.
       echo( "with deep function: ", deep ( sin deg, {p[]} ) );

       echo( new line, "Example with nested parameters:");
       echo( log( { 1, 10, { 100, 1000 }, { 0.01, 0.001 } } ) );

       echo(new line, "Example with matrices:");

       m[]  = { { 1, 2 }, { 3, 4 } };
       m[] *^^= m[]; // Square the four values in the matrix (^^ is 2 level deep operator prefix)

       echo("Original value: ", m[], "  Square root: ", sqrt(m[]));

       echo(new line, "2 vectorized parameters");

       echo( "x^y repeated 4x: ", pow( { 1, 2, 3, 4 }, { 4, 3, 2, 1 } ) );
       echo( "All squared    : ", pow( { 1, 2, 3, 4 }, 2 ) );
       echo( "Powers of ten  : ", pow( 10, { 1, 2, 3, 4 } ) );

       echo(new line, "Do something more sophisticated:");

       echo( " Mod(x,7) on all values except the first one: ");
       echo( mod( { 10, { 20, 30, { 8, 9 } }}, { 5, 7 } ) );

       echo( " Mod(100,x) on all values: ");
       echo( mod( 100, { 10, { 20, 30, { 40, 50 } }} ) );
Simple example with vectorization
With vectorization: {0,0.5,0.8660254038,1}
with deep function: {0,0.5,0.8660254038,1}

Example with nested parameters:
{0,1,{2,3},{-2,-3}}

Example with matrices:
Original value: {{1,4},{9,16}}  Square root: {{1,2},{3,4}}

2 vectorized parameters
x^y repeated 4x: {1,8,9,4}
All squared    : {1,4,9,16}
Powers of ten  : {10,100,1000,10000}

Do something more sophisticated:
Mod(x,7) on all values except the first one:
{0,{6,2,{1,2}}}
Mod(100,x) on all values:
{0,{0,10,{20,0}}}
Try it yourself: Open LAN_Features_Vectorization.b4p in B4P_Examples.zip. Decompress before use.