CArray:Dot


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

Syntax

number = CArray:Dot( CArray )

bullet.gif    CArray is the other array.

bullet.gif    number is the returned dot product.

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 computes the straightforward dot product of two CArray's:

A = new_array()

-- create a CArray

A:Init( 3, 1.0 )

-- set 3 indices to 1.0

B = new_array()

-- 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 = new_array()

-- create a CArray A

A:Init( 5, 1.0 )

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

B = new_array()

-- 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


Mira Pro x64 Script User's Guide, Copyright Ⓒ 2023 Mirametrics, Inc. All Rights Reserved.