Difference between revisions of "Sum"

m
Line 3: Line 3:
 
   
 
   
 
Returns the sum of array X over the dimension indexed by variable I.
 
Returns the sum of array X over the dimension indexed by variable I.
 
+
You can also sum over multiple indexes:
= Declaration =
+
Sum(A, I, J, K)
 +
It treats any Null values in A as zero.
  
 
  Sum(a: Array[i]; i: ... optional Index; IgnoreNonNumbers, IgnoreNaN: Optional Boolean)
 
  Sum(a: Array[i]; i: ... optional Index; IgnoreNonNumbers, IgnoreNaN: Optional Boolean)
  
You can sum over multiple indexes (since release 4.1):
+
Set IgnoreNaN to treat NaN, indeterminate numeric values, as zero.  Normally NaNs propagate through a model -- Sum(x, i) returns NaN if any value in x is NaN. This can help tracking down numeric problems in your logic, such as 0/0 or Sqrt(-1). But if you know there are NaNs that you want to ignore, set IgnoreNaN parameter to True (1).
  Sum(A, I, J, K)
+
  Sum(A, I, IgnoreNaN: True)
  
It ignores any Null values in A -- i.e. treats them as zero.
+
Similarly, set optional parameter IgnoreNonNumbers to True, to treat non-numeric values such as text or references, as zero, e.g.:
 +
Sum(A, I, J, IgnoreNonNumbers:true)
  
If the optional IgnoreNonNumbers is specified as true, it also ignores non-numeric values such as text or references, e.g.:
+
You must use named parameter syntax to use IgnoreNonNumbers and IgnoreNaN, as shown in the above examples.
Sum(A, I, J, IgnoreNonNumbers:true)
 
  
 
Compare:
 
Compare:
  X := Array(I,[5,6,'0',7])
+
  X := Array(I,[5,6,'X',7])
  IgnoreWarnings(Sum(X,I)) → "1107"  
+
  IgnoreWarnings(Sum(X,I)) → "11X7"  
  Sum(X,I,IgnoreNonNumbers:true) → 18
+
  Sum(X,I,IgnoreNonNumbers: True) → 18
  
Using Sum to concatenate text is strongly discouraged, and the [[JoinText]] function should be used for that purpose instead.  A straight sum over X results in a warning to this effect, but the functionality is still supported for legacy reasons.  In that case, (5+6+'0'+7) evaluates as ( ( (5+6)&'0' ) & 7 ).  In the second example, numbers are ignored so only the numbers, 5+6+7, are added.
+
In older releases, you could use Sum to concatenate text, if all or some of the values are text. We strongly discourage this usage. It still works for backward compatibility, but gives a warning.  Use the [[JoinText]] function instead to make it clear you want to join (concatenating) text -- not just summing over values, some of which turn out to be text inadvertently.
  
The IgnoreNaN parameter ignores indeterminate numeric values, known as NaNsNormally NaNs propagate through (which helps in spotting numeric problems in your logic).  But if you know there are NaNs that you don't want to sum over, include the optional IgnoreNaN parameter.
+
A straight sum over X results in a warning to this effect, but the functionality is still supported for legacy reasonsIn that case, (5+6+'X'+7) evaluates as ( ( (5+6)&'X' ) & 7 ).  In the second example, numbers are ignored so only the numbers, 5+6+7, are added.
Sum(A,I,IgnoreNaN:true)
 
  
Use of IgnoreNonNumbers and IgnoreNaN parameters requires the named parameter syntax, as shown in the above examples.
 
  
 
= Using Sum to add arrays =
 
= Using Sum to add arrays =
  
Arrays can be added using a simple expression in the form: (Array1 + Array2)
+
With [[Intelligent Arrays]], you can of course add two arrays with the "+" operator:  
 
+
a1 + a2
The sum function can also be used to add arrays:  Sum([Array1, Array2]).  In this example, the I parameter is omitted.  The reduced index is the self-index of the list of arrays.  Therefore no array indexes are reduced.  The two methods are equivalent except for the handling of <<null>> values:
 
 
 
(Array1 + Array2):  A null value will nullify the result for a particular coordinate location within the array.
 
  
Sum([Array1, Array2]): Null values are treated as zero.
+
You can also Sum to do this: 
 +
Sum([a1, a2])
 +
In this example, the I parameter is omitted, and it automatically Sums over the implicit index of the list [a1, a2]. 
 +
These two methods are equivalent except for <<null>> values: Sum([a1, a2]) treats any <<null>> values as zero.
 +
But
 +
a1 + a2
 +
returns <<null>> if any values to be added are <<null>>.
  
 
= See Also =
 
= See Also =

Revision as of 18:27, 24 April 2014


Returns the sum of array X over the dimension indexed by variable I. You can also sum over multiple indexes:

Sum(A, I, J, K)

It treats any Null values in A as zero.

Sum(a: Array[i]; i: ... optional Index; IgnoreNonNumbers, IgnoreNaN: Optional Boolean)

Set IgnoreNaN to treat NaN, indeterminate numeric values, as zero. Normally NaNs propagate through a model -- Sum(x, i) returns NaN if any value in x is NaN. This can help tracking down numeric problems in your logic, such as 0/0 or Sqrt(-1). But if you know there are NaNs that you want to ignore, set IgnoreNaN parameter to True (1).

Sum(A, I, IgnoreNaN: True)

Similarly, set optional parameter IgnoreNonNumbers to True, to treat non-numeric values such as text or references, as zero, e.g.:

Sum(A, I, J, IgnoreNonNumbers:true)

You must use named parameter syntax to use IgnoreNonNumbers and IgnoreNaN, as shown in the above examples.

Compare:

X := Array(I,[5,6,'X',7])
IgnoreWarnings(Sum(X,I)) → "11X7" 
Sum(X,I,IgnoreNonNumbers: True) → 18

In older releases, you could use Sum to concatenate text, if all or some of the values are text. We strongly discourage this usage. It still works for backward compatibility, but gives a warning. Use the JoinText function instead to make it clear you want to join (concatenating) text -- not just summing over values, some of which turn out to be text inadvertently.

A straight sum over X results in a warning to this effect, but the functionality is still supported for legacy reasons. In that case, (5+6+'X'+7) evaluates as ( ( (5+6)&'X' ) & 7 ). In the second example, numbers are ignored so only the numbers, 5+6+7, are added.


Using Sum to add arrays

With Intelligent Arrays, you can of course add two arrays with the "+" operator:

a1 + a2

You can also Sum to do this:

Sum([a1, a2])

In this example, the I parameter is omitted, and it automatically Sums over the implicit index of the list [a1, a2]. These two methods are equivalent except for <<null>> values: Sum([a1, a2]) treats any <<null>> values as zero. But

a1 + a2

returns <<null>> if any values to be added are <<null>>.

See Also

Comments


You are not allowed to post comments.