Difference between revisions of "Sum"
Line 1: | Line 1: | ||
[[Category:Array-reducing functions]] | [[Category:Array-reducing functions]] | ||
− | [[Category:Doc Status C]] <!-- For Lumina use, do not change --> | + | [[Category:Doc Status C]] |
− | + | [[Category:Array Library]] | |
− | Returns the sum of array | + | <!-- For Lumina use, do not change --> |
− | You can also sum over multiple indexes: | + | ==Sum(x, i)== |
+ | 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) | Sum(A, I, J, K) | ||
− | + | ==Syntax== | |
− | Sum( | + | Sum(x: Array[i]; i: ... optional Index; IgnoreNonNumbers, IgnoreNaN: Optional Boolean) |
− | Sum treats any Null values in | + | Sum treats any Null values in «x» as zero. |
− | Set | + | 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) | Sum(A, I, IgnoreNaN: True) | ||
− | Similarly, set optional parameter | + | 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) | Sum(A, I, J, IgnoreNonNumbers:true) | ||
− | You must use named parameter syntax to use | + | You must use named parameter syntax to use «IgnoreNonNumbers» and «IgnoreNaN», as shown in the above examples. |
+ | ==Examples== | ||
Compare: | Compare: | ||
X := Array(I,[5,6,'X',7]) | X := Array(I,[5,6,'X',7]) | ||
Line 24: | Line 26: | ||
Sum(X,I,IgnoreNonNumbers: True) → 18 | 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. | + | 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 ). | + | 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. |
+ | Sum(Car_prices, Years) → | ||
+ | {| class="wikitable" | ||
+ | ! colspan="3" style="text-align: left;" | Car_type ▶ | ||
+ | |- | ||
+ | ! style="width:75px;" scope="col"| VW | ||
+ | ! style="width:75px;" scope="col"| Honda | ||
+ | ! style="width:75px;" scope="col"| BMW | ||
+ | |- | ||
+ | | 90K | ||
+ | | 103K | ||
+ | | 141K | ||
+ | |} | ||
− | == Using Sum to add arrays == | + | == Using [[Sum]] to add arrays == |
− | |||
With [[Intelligent Arrays]], you can of course add two arrays with the "+" operator: | With [[Intelligent Arrays]], you can of course add two arrays with the "+" operator: | ||
a1 + a2 | a1 + a2 | ||
− | You can also Sum to do this: | + | You can also [[Sum]] to do this: |
Sum([a1, a2]) | Sum([a1, a2]) | ||
− | In this example, the | + | 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. | + | These two methods are equivalent except for <<null>> values: [[Sum]]([a1, a2]) treats any <<null>> values as zero. |
But | But | ||
a1 + a2 | a1 + a2 | ||
Line 43: | Line 56: | ||
== See Also == | == See Also == | ||
− | |||
* [[Cumulate]] | * [[Cumulate]] | ||
* [[Product]] | * [[Product]] | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
Revision as of 05:56, 14 August 2015
Sum(x, i)
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)
Syntax
Sum(x: Array[i]; i: ... optional Index; IgnoreNonNumbers, IgnoreNaN: Optional Boolean)
Sum treats any Null values in «x» as zero.
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.
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.
Sum(Car_prices, Years) →
Car_type ▶ | ||
---|---|---|
VW | Honda | BMW |
90K | 103K | 141K |
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>>.
Enable comment auto-refresher