CArray:Count CArray:Empty

CArray:Dot


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

Syntax

number = CArray:Dot( CArray )

Remarks

The dot product is the sum of products of CArray elements at the same indices. For example, consider 2 CArray'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 only used when you know that the arrays are packed with 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 CArray's, even if they are sparsely populated at different indices, A:Dot(B) = B:Dot(A) is true.

Example

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

 

A = CArray:new()

-- create a CArray

A:Init( 3, 1.0 )

-- set 3 indices to 1.0

B = CArray:new()

-- create another CArray

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 CArray's that have different dimensions:

 

A = CArray:new()

-- create a CArray A

A:Init( 5, 1.0 )

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

B = CArray:new()

-- create a CArray 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

CArray, Set