Dot product oddities

Discussions related to mathematics, numerical methods, graph plotting etc.
Richard Russell
Posts: 457
Joined: Tue 18 Jun 2024, 09:32

Dot product oddities

Post by Richard Russell »

Mathematically speaking, the dot product is defined as operating on two vectors of the same size, although in BBC BASIC it's also used (probably wrongly, strictly speaking) to represent the multiplication of two matrices.

In BASIC a vector will usually be represented by a one-dimensional array, and a matrix by a two-dimensional array. Therefore a simple use for the dot product when operating on two vectors might be as follows:

Code: Select all

      DIM a(9), b(9), c(0)
      a() = 1,2,3,4,5,6,7,8,9,0
      b() = 9,8,7,6,5,4,3,2,1,0
      c() = a() . b()
      PRINT c(0)
which prints the correct result 165 in all my BASICs.

The surprising thing is that this code, despite its simplicity, doesn't work in either Matrix Brandy BASIC or Sophie's ARM BASIC! In Matrix Brandy it runs but prints the incorrect value 405 (I've not the slightest idea why) and in ARM BASIC 5 it produces the error message 'Can't assign to array of this size' (unhelpfully it gives no clue of what size of array it could assign to!).

To make it produce the correct answer of 165 in Matrix Brandy and ARM BASIC it's necessary to modify the code as follows:

Code: Select all

      DIM a(9), b(9,0), c(0)
      a() = 1,2,3,4,5,6,7,8,9,0
      b() = 9,8,7,6,5,4,3,2,1,0
      c() = a() . b()
      PRINT c(0)
This might perhaps make some sense if vector a() was declared as a(0,9), then at least you could argue that it was multiplying a matrix consisting of one column with a matrix consisting of one row - an unnecessary complication given that the dot-product is defined as operating on two vectors.

But in fact it doesn't work at all if you make that change, ARM BASIC prints -6.46249256E-38 and Matrix Brandy reports the error ''Type mismatch: Arrays must have the same dimensions'!

I'm totally confused by all this. The dot product is one of the most valuable features of BBC BASIC but by my reckoning both ARM BASIC 5 and Matrix Brandy get it wrong - albeit that you can persuade them to produce the right result if you know what incantation to use.
Richard Russell
Posts: 457
Joined: Tue 18 Jun 2024, 09:32

Re: Dot product oddities

Post by Richard Russell »

Richard Russell wrote: Mon 11 Aug 2025, 14:21 Mathematically speaking, the dot product is defined as operating on two vectors of the same size, although in BBC BASIC it's also used (probably wrongly, strictly speaking) to represent the multiplication of two matrices.
DeepSeek concurs:
  • Formally, the term dot product is reserved for vectors (or the Frobenius inner product for matrices).
  • Informally, one might say "dot product" when referring to the scalar products involved in matrix multiplication (e.g., "the dot product of a row and column"), but this is just a description of the computation, not a formal name for the operation.
  • Best practice is to use matrix multiplication for AB and Frobenius inner product for A:B.
Irrespective, BBC BASIC will of course continue to use the dot operator for matrix multiplication.