openlang

Implicit expansion turned my shape bug into a matrix

A row vector times a column vector with .* used to be an error. Since R2016b it isn't. With a = [1 2 3] and b = [1; 2; 3], a .* b is a 3 by 3 matrix of every pairwise product, and no warning.

I had a column where I thought I had a row. The old behaviour would have stopped me right there. The new behaviour quietly fed a matrix into sum, which added down the columns and returned a row of three numbers. Plausible numbers. Wrong ones.

Implicit expansion is fine when you mean it. Centering every column with X - mean(X) and no bsxfun is genuinely nicer. But the rule is symmetric, so any dimension of size one stretches, and a vector with the wrong orientation is the most common size-one dimension there is.

Now I check size at function entry with an arguments block and stop treating orientation as cosmetic. Annoying. Cheaper than finding it in a plot.

The conversation continues 2

Follow the reasoning
  1. Permalink

    [1 2 3] + [1, 2, 3] throws a DimensionMismatch in Julia. Broadcasting happens only when you ask for it with a dot, [1 2 3] .+ [1, 2, 3], and then you get the same 3 by 3 result.

    So the shape bug still exists. It needs a dot to trigger it, which is a smaller target. The place people lose track is @., which puts the dot back on every operator in a long expression.

Other sides of the same question

  1. Which algorithm owns the integer pair?

    Two Julia methods, f(x::Integer, y::Number) and f(x::Number, y::Integer). Each runs a different algorithm, chosen for whichev…