Skip to content

pymath.dot

Daniel Flassig edited this page Jun 24, 2026 · 2 revisions

Vector product, matrix multiplication (or more generally contraction of two tensors)

result = pymath.dot(a, b)
result = pymath.dot(a, b, a_axis , b_axis)
Parameter Type Description
a tensor Left vector, matrix or tensor
b tensor Reft vector, matrix or tensor
a_axis integer 1-based position of the index of a that is contracted (summed over). Default: normal vector / matrix multiplication (last axis)
b_axis integer 1-based position of the index of a that is contracted (summed over). Default: normal vector / matrix multiplication (first axis)

Return value

Type Description
result A new vector, matrix or tensor (rank a_rank + b_rank - 2), whose axes are the non-contracted axes of t1 (in order) followed by those of t2. If both operands are vectors, the full contraction yields a single number (scalar dot product).

Notes:

  • The contracted dimensions must be equal: a's a_axis and b's b_axis must have the same size.
  • a and b are not modified. For a function that does not allocate new tables, see pymath.dot_inplace.

For matrices A, B and vectors v and w:

  • pymath.dot(v, w) computes the vector dot product v . w
  • pymath.dot(A, v) computes the matrix-vector product A . v
  • pymath.dot(v, A) computes the matrix-vector product v^T . A (for pymath, row and column vectors are identical)
  • pymath.dot(A, B) computes the matrix product A · B (the last axis of A contracted with the first axis of B)

Example:

local A = {{1, 2}, 
           {3, 4}}

local B = {{5, 6}, 
           {7, 8}}

local v = {2, 5}
local w = {10, 11}

-- vector . vector -> scalar
local s = pymath.dot(v, w)
-- s == 75

-- matrix . vector (last axis of A contracted with the only axis of v)
local y = pymath.dot(A, v)
-- y == {12, 26}

-- matrix * matrix
local C = pymath.dot(A, B)
-- C == {{19, 22}, {43, 50}}

Version Support:

Minimum PYTHA Version: V27

See also:

pymath, pymath.dot_inplace, pymath.transpose

Clone this wiki locally