Difference between revisions of "Sum"
Line 5: | Line 5: | ||
==Sum(x, i)== | ==Sum(x, i)== | ||
Returns the sum of array «x» over the dimension indexed by variable «i». You can also sum over multiple indexes: | Returns the sum of array «x» over the dimension indexed by variable «i». You can also sum over multiple indexes: | ||
− | + | :<code>Sum(A, I, J, K)</code> | |
Sum treats any [[Null]] values in «x» as zero. | Sum treats any [[Null]] values in «x» as zero. | ||
Line 11: | Line 11: | ||
==Examples== | ==Examples== | ||
Compare: | Compare: | ||
− | + | :<code>X := Array(I, [5, 6, 'X', 7])</code> | |
− | + | :<code>IgnoreWarnings(Sum(X, I)) → "11X7"</code> | |
− | + | :<code>Sum(X, I, IgnoreNonNumbers: True) → 18</code> | |
− | |||
− | |||
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. | ||
Line 23: | Line 21: | ||
The following example is based off of the parameters in [[Array Function Example Variables]]. | The following example is based off of the parameters in [[Array Function Example Variables]]. | ||
− | + | :<code>Sum(Car_prices, Years) →</code> | |
− | {| class="wikitable" | + | :{| class="wikitable" |
! colspan="3" style="text-align: left;" | Car_type ▶ | ! colspan="3" style="text-align: left;" | Car_type ▶ | ||
|- | |- | ||
Line 37: | Line 35: | ||
==Optional Parameters== | ==Optional Parameters== | ||
− | + | :<code>Sum(x: Array[i]; i: ... optional Index; IgnoreNonNumbers, IgnoreNaN: Optional Boolean)</code> | |
===IgnoreNaN=== | ===IgnoreNaN=== | ||
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 (or 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 (or 1). | ||
− | + | :<code>Sum(x, i, ignoreNaN: True)</code> | |
===IgnoreNonNumbers=== | ===IgnoreNonNumbers=== | ||
Similarly, set optional parameter «ignoreNonNumbers» to True, to treat non-numeric values such as text or references, as zero, e.g.: | Similarly, set optional parameter «ignoreNonNumbers» to True, to treat non-numeric values such as text or references, as zero, e.g.: | ||
− | + | :<code>Sum(x, i, j, ignoreNonNumbers: True)</code> | |
You must use named parameter syntax to use «IgnoreNonNumbers» and «IgnoreNaN», as shown in the above examples. | You must use named parameter syntax to use «IgnoreNonNumbers» and «IgnoreNaN», as shown in the above examples. | ||
Line 51: | Line 49: | ||
== 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: | ||
− | + | :<code>a1 + a2</code> | |
You can also [[Sum]] to do this: | You can also [[Sum]] to do this: | ||
− | + | :<code>Sum([a1, a2])</code> | |
In this example, the «i» parameter is omitted, and it automatically sums over the implicit index of the list [a1, a2]. The difference between this example and simply using the "+" operator is that using [[Sum]] treats any [[Null]] values as 0, whereas <code>a1 + a2</code> returns [[Null]] if any values to be added are [[Null]]. | In this example, the «i» parameter is omitted, and it automatically sums over the implicit index of the list [a1, a2]. The difference between this example and simply using the "+" operator is that using [[Sum]] treats any [[Null]] values as 0, whereas <code>a1 + a2</code> returns [[Null]] if any values to be added are [[Null]]. | ||
Revision as of 20:01, 11 January 2016
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 treats any Null values in «x» as zero.
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.
The following example is based off of the parameters in Array Function Example Variables.
Sum(Car_prices, Years) →
Car_type ▶ VW Honda BMW 90K 103K 141K
Optional Parameters
Sum(x: Array[i]; i: ... optional Index; IgnoreNonNumbers, IgnoreNaN: Optional Boolean)
IgnoreNaN
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 (or 1).
Sum(x, i, ignoreNaN: True)
IgnoreNonNumbers
Similarly, set optional parameter «ignoreNonNumbers» to True, to treat non-numeric values such as text or references, as zero, e.g.:
Sum(x, i, j, ignoreNonNumbers: True)
You must use named parameter syntax to use «IgnoreNonNumbers» and «IgnoreNaN», as shown in the above examples.
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]. The difference between this example and simply using the "+" operator is that using Sum treats any Null values as 0, whereas a1 + a2
returns Null if any values to be added are Null.
Enable comment auto-refresher