Contents - Index


SUM

 

There are two forms for the SUM function.  EES determines which format is in use by context.  Note that the Index form should not be used in Functions/Procedures with variable arguments.

 

Index Form 

SUM(Arg, i=1,50) returns the sum of a series of terms.  The index is represented as i here but it can be any EES variable name.  The lower and upper limits must be integers or variables that have been previously assigned to integer values.  The function is best explained by examples. SUM(j, j=1,4) will return 1+2+3+4 or 10.  The SUM function is most useful when used with array variables, e.g., X[j].  For example, the scalar product of two vectors, X and Y, each with 10 elements can be obtained as SUM(X[j]*Y[j], j=1,10).  The SUM function can be used with array variables to manipulate matrices.  Note that the limits appear in the Sum command must be integers or EES variables that have been previously set to integer values.  The following equations would be valid.

 

 N=50

 S=SUM(X[i], i=1,N)

 

Note:  When used within Functions or Procedures, the index limits must be known at the time of compilation. This requirement means that the limits cannot be provided as arguments or calculated within the Function/Procedure.  For example, the following Function may not work.

 

Function sumit(a,b)  {This function will not work because variable a is not known compile time}

   X[1..10]=[1,2,3,4,5,6,7,8,9,10]

   sumit=sum(X[i],i=a,b)

End

g=sumit(2,5) 

h=sumit(1,5)  {!This will produce an incorrect result}

 

As an alternative, use the List form of the Sum command or a Repeat-Until group, as illustrated below.

 

 

List Form

SUM(Arg1, Arg2, ...ArgN) returns the sum of the arguments.  This is the recommend form for use of the Sum command in Functions and Procedures.  Array range notation is particularly convenient for the List form.  For example, SUM(X[1..100]) returns the sum of the 100 elements in the X array. Note, however, that mathematical operations within the List Form of the Sum function is not provided.  For example:

 

Z=SUM(0.1*X[1..100]) "!This will not compile'

 

The List form will work in Functions and Procedures with variable limits.  For example, the example above that failed for the Index form will work properly if it is modified to use the List form, as shown.

 

Function sumit(a,b)   {This function will work properly}

   X[1..10]=[1,2,3,4,5,6,7,8,9,10]

   sumit=sum(X[a..b])

End

g=sumit(2,5)  

h=sumit(1,5)   {!This will work properly}

 

If mathematical operations are required, do them in separate equations or use the Index Form if possible or a Repeat-Until group.

 

 

Repeat Until Group

The Repeat-Until group can be used in Functions and Procedures to Sum or do any sort of looping operation.  An example of the using it to sum the elements of array follows.

 

Function sumit(a,b)   {This function will work properly}

   X[1..10]=[1,2,3,4,5,6,7,8,9,10]

   i=a

   sumit=0

   Repeat

      sumit=sumit+X[i]

      i=i+1

   Until (i>b)

End

g=sumit(2,5)  

h=sumit(1,5) 

 

 

Mathematical functions