Copio qui, continuando da qui.

**Broadcasting
**

*Nota per me*: in altri linguaggi broadcasting vuol dire un’altra cosa.

Broadcasting refers to how Octave binary operators and functions behave when their matrix or array operands or arguments differ in size. […] Octave […] automatically broadcasts vectors, matrices, and arrays when using elementwise binary operators and functions. Broadly speaking, smaller arrays are “broadcast” across the larger one, until they have a compatible shape. The rule is that corresponding array dimensions must either

- be equal, or
- one of them must be 1.

In case all dimensions are equal, no broadcasting occurs and ordinary element-by-element arithmetic takes place. For arrays of higher dimensions, if the number of dimensions isn’t the same, then missing trailing dimensions are treated as 1. When one of the dimensions is 1, the array with that singleton dimension gets copied along that dimension until it matches the dimension of the other array. For example, consider

`x = [1 2 3; 4 5 6; 7 8 9]; y = [10 20 30]; x + y`

Without broadcasting, ** x + y** would be an error because the dimensions do not agree. However, with broadcasting it is as if the following operation were performed:

`x = [1 2 3 4 5 6 7 8 9]; y = [10 20 30 10 20 30 10 20 30];`

That is, the smaller array of size [1 3] gets copied along the singleton dimension (the number of rows) until it is [3 3]. No actual copying takes place, however. The internal implementation reuses elements along the necessary dimension in order to achieve the desired effect without copying in memory.

Both arrays can be broadcast across each other, for example, all pairwise differences of the elements of a vector with itself:

Here the vectors of size [1 3] and [3 1] both get broadcast into matrices of size [3 3] before ordinary matrix subtraction takes place.

A special case of broadcasting that may be familiar is when all dimensions of the array being broadcast are 1, i.e., the array is a scalar. Thus for example, operations like ** x - 42** and

**are basic examples of broadcasting.**

`max (x, 2)`

For a higher-dimensional example, suppose ** img** is an RGB image of size [m n 3] and we wish to multiply each color by a different scalar. The following code accomplishes this with broadcasting,

`img .*= permute ([0.8, 0.9, 1.2], [1, 3, 2]);`

Note the usage of ** permute** to match the dimensions of the [0.8, 0.9, 1.2] vector with

**.**

`img`

For functions that are not written with broadcasting semantics, ** bsxfun** can be useful for coercing them to broadcast.

Built-in Function: `bsxfun (f, A, B)`

The binary singleton expansion function performs broadcasting, that is, it applies a binary function ** f** element-by-element to two array arguments

**and**

`A`

**, and expands as necessary singleton dimensions in either input argument.**

`B`

**is a function handle, inline function, or string containing the name of the function to evaluate. The function**

`f`

**must be capable of accepting two column-vector arguments of equal length, or one column vector argument and a scalar.**

`f`

The dimensions of

**and**

`A`

**must be equal or singleton. The singleton dimensions of the arrays will be expanded to the same dimensionality as the other array.**

`B`

Broadcasting is only applied if either of the two broadcasting conditions hold. As usual, however, broadcasting does not apply when two dimensions differ and neither is 1:

Besides common arithmetic operations, several functions of two arguments also broadcast. The full list of functions and operators that broadcast is

`plus + .+ minus - .- times .* rdivide ./ ldivide .\ power .^ .** lt < le <= eq == gt > ge >= ne != ~= and & or | atan2 hypot max min mod rem xor += -= .+= .-= .*= ./= .\= .^= .**= &= |=`

Beware of resorting to broadcasting if a simpler operation will suffice. For matrices ** a** and

**, consider the following:**

`b`

`c = sum (permute (a, [1, 3, 2]) .* permute (b, [3, 2, 1]), 3);`

This operation broadcasts the two matrices with permuted dimensions across each other during elementwise multiplication in order to obtain a larger 3-D array, and this array is then summed along the third dimension. A moment of thought will prove that this operation is simply the much faster ordinary matrix multiplication, `c = a*b;.`

A note on terminology: “broadcasting” is the term popularized by the Numpy numerical environment in the Python programming language. In other programming languages and environments, broadcasting may also be known as binary singleton expansion (** BSX**, in MATLAB, and the origin of the name of the bsxfun function),

**(R programming language), single-instruction multiple data (SIMD), or replication. Ah! ecco proprio come dicevo 😉**

`recycling`

**Broadcasting e legacy code**

Yesss! non so tradurre, sarebbe quacosa come “ereditarietà” o “come si faceva una volta”.

The new broadcasting semantics almost never affect code that worked in previous versions of Octave. Consequently, all code inherited from MATLAB that worked in previous versions of Octave should still work without change in Octave. The only exception is code such as

`try c = a.*b; catch c = a.*a; end_try_catch`

that may have relied on matrices of different size producing an error. Because such operation is now valid Octave syntax, this will no longer produce an error. Instead, the following code should be used:

`if (isequal (size (a), size (b))) c = a .* b; else c = a .* a; endif`

Dubbi, chissà se si dovrà indagare in proposito 😦