sum (replaces TSum)


The sum function returns the sum of a CImage, a 1-dimensional array, or a list of numbers. The array and list parameters must contain a mixture of numbers and strings that can be converted to numbers. This function is similar to the CStat:Sum method but does not require calling new_stats() beforehand to create a CStats object.

Whereas this function operates on a pure (indexed) array, the slightly slower sumn function operates on a general table that may be a multi-dimensional array or contain a mixture of numbers, strings, named keys, and sub-tables.

Syntax

nSum = sum( Data )

bullet.gif    Data is a 1-dimensional array, list of numbers, or a CImage object.

bullet.gif    n1, n2, n3, ... are numbers or string values that can be converted to numbers.

bullet.gif    nSum is the sum of the data.

Remarks

The CImage class supports values ranging from byte to double plus other pixel types.

Examples

The following script returns the sum of a table containing 1 million random numbers.

t = random( 1000000 )

-- create a table of numbers

v = sum( t )

-- find the sum

Printf("Sum=%lg\n", v )

-- list the result

  

The next script returns the sum value for pixels inside a rectangle at the corner of a CImage.

V = AttachView( "CImageView" )

-- attach the topmost image window

Assert( V and V:Count() > 0 )

-- make sure an image window is on top

R = new_rect( 1, 20, 1, 40 )

-- rectangle for corner of image

v = sum( V:GetImage(), R )

-- use the current image in the CImageView

Printf("Sum=%lg\n", v )

-- list the result

  

The next example computes the sum of a list of numbers:

s = sum( 5, 6, -2, 5.4 }

-- find the sum

Printf("Sum = %lg\n", s )

-- result: Max = 14.4

Related Topics

Table and Array Functions

sumn

CImageView Class


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