CMatrixRow:Dot


The Dot method returns the dot product of this CMatrixRow with another CMatrixRow.

Syntax

number = CMatrixRow:Dot( CMatrixRow )

where

    CMatrixRow is abother CMatrixRow object.

    number is the returned dot product.

  

The dot product is the sum of products of CMatrixRow elements at the same indices. For example, consider 2 CMatrixRow's with values at indices 1, 2, and 3: A = {3,4,5} and B = {10,20,30}. Then A:Dot(B) = 3*10 + 4*20 + 5*30 = 250. Normally, the dot product is used with arrays containing only numbers. An error message will result if any of the elements used is not a number.

This method computes the dot product of 2 sparse arrays. Only the indices common to both arrays are used to compute the dot product. For any two CMatrixRow's, even if they are sparsely populated at different indices, A:Dot(B) = B:Dot(A) is true.

Example

The following script computes the straightforward dot product of two CMatrixRow's:

A = CMatrixRow:new()

-- create a CMatrixRow

A:Init( 3, 1.0 )

-- set 3 indices to 1.0

B = CMatrixRow:new()

-- create another CMatrixRow

B:Init( 3, 2.0 )

-- set 3 indices to 2.0

Printf("AdotB = %lg", A:Dot(B) )

-- Result: AdotB = 6

The next example computes the dot product of two CMatrixRow's that have different dimensions:

A = CMatrixRow:new()

-- create a CMatrixRow A

A:Init( 5, 1.0 )

-- define A indices 1, 2, 3, 4, 5

B = CMatrixRow:new()

-- create a CMatrixRow B

B:Init( 3, 2.0 )

-- define B indices 1, 2, 3

Printf("AdotB = %lg, A:Dot(B) )

-- result: AdotB = 14

B:Remove(1)

-- leaves only B indices 2, 3

Printf("AdotB = %lg", A:Dot(B) )

-- result: AdotB = 12

Related Topics

Set

CMatrixRow class