Difference between revisions of "Sum"

m
(ignoreDates parameter)
 
(13 intermediate revisions by 3 users not shown)
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 X over the dimension indexed by variable I.
+
<!-- For Lumina use, do not change -->
 +
==Sum(x, i)==
 +
Returns the sum of array «x» over Index «i».  
 +
 
 
You can also sum over multiple indexes:
 
You can also sum over multiple indexes:
Sum(A, I, J, K)
+
:<code>Sum(A, I, J, K)</code>
It treats any Null values in A as zero.
+
 
 +
It is one of a number of [[Array-reducing functions]], so-called because they reduce the number of dimensions (indexes) of an array.
 +
 
 +
Sum treats any [[Null]] values in «x» as zero.  It can also treat [[NaN]] or [[text]] and other non-numerical values as zero, if you set optional parameters, «ignoreNaN» or «ignoreNonNumbers»  to True.
  
Sum(a: Array[i]; i: ... optional Index; IgnoreNonNumbers, IgnoreNaN: Optional Boolean)
+
==Example==
 +
This example uses [[Array Function Example Variables]].
  
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).
+
:<code>Sum(Car_prices, Years) &rarr;</code>
Sum(A, I, IgnoreNaN: True)
+
:{| class="wikitable"
 +
! colspan="3" style="text-align: left;" | Car_type &#9654;
 +
|-
 +
! style="width:75px;" scope="col"| VW
 +
! style="width:75px;" scope="col"| Honda
 +
! style="width:75px;" scope="col"| BMW
 +
|-
 +
| 90K
 +
| 103K
 +
| 141K
 +
|}
  
Similarly, set optional parameter IgnoreNonNumbers to True, to treat non-numeric values such as text or references, as zero, e.g.:
+
==Optional Parameters==
Sum(A, I, J, IgnoreNonNumbers:true)
+
:<code>Sum(x: Array[i]; i: ... optional Index; IgnoreNonNumbers, IgnoreNaN: Optional Boolean)</code>
  
You must use named parameter syntax to use IgnoreNonNumbers and IgnoreNaN, as shown in the above examples.
+
=====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).
 +
:<code>Sum(x, i, ignoreNaN: True)</code>
  
Compare:
+
=====IgnoreNonNumbers=====
X := Array(I,[5,6,'X',7])
+
Similarly, set optional parameter «ignoreNonNumbers» to True, to treat non-numeric values such as text or references, as zero, e.g.:
IgnoreWarnings(Sum(X,I)) &rarr; "11X7"
+
:<code>Sum(x, i, j, ignoreNonNumbers: True)</code>
Sum(X,I,IgnoreNonNumbers: True) &rarr; 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.
+
You must use named parameter syntax for «IgnoreNonNumbers» and «IgnoreNaN», as shown in the above examples, because parameter «i» is optional and repeated.
  
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.
+
Date-time numbers are not ignored (they are considered numbers).
  
 +
=====IgnoreDates=====
 +
''New to [[Analytica 5.3]]''
  
= Using Sum to add arrays =
+
Set «ignoreDates» to True to (also) ignore date-time numbers.
  
 +
==== 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
+
:<code>a1 + a2</code>
 +
 
 +
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]].
 +
 
 +
=== Sum over text values ===
 +
 
 +
In older releases, [[Sum]] concatenates values as text, of any values contain text. This feature hides a common error, when you inadvertently try to sum text values thinking that they are numbers, so we strongly discourage using [[Sum]] this way.  It still works for backward compatibility with older models, but since release 4.5, it gives a warning. 
 +
 
 +
If you want to  sum over the numbers only and ignore the text, use the  «ignoreNonNumbers» parameter:<blockquote><code>Sum(X, I, IgnoreNonNumbers: True) &rarr; 18</code></blockquote>
 +
 
 +
If you really want to concatenate text, use the [[Text Concatenation Operator: &|"&" operator]] or [[JoinText]] function instead,  to make it clear that's what you want to do.
  
You can also Sum to do this:
+
Example:
Sum([a1, a2])
+
:<code>X := Array(I, [5, 6, 'X', 7])</code>
In this example, the I parameter is omitted, and it automatically Sums over the implicit index of the list [a1, a2]
+
:<code>IgnoreWarnings(Sum(X, I)) &rarr; "11X7"</code>
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 =
+
This sum evaluates <code>(5 + 6 + 'X' + 7)</code>  as <code>(((5 + 6) & 'X') & 7)</code>. It converts the numbers to text and concatenates them to form <code>"11X7".</code>
  
 +
== See Also ==
 
* [[Cumulate]]
 
* [[Cumulate]]
 
* [[Product]]
 
* [[Product]]
 +
* [[Array-reducing functions]]
 +
* [[Operators]]

Latest revision as of 22:43, 27 August 2019

Sum(x, i)

Returns the sum of array «x» over Index «i».

You can also sum over multiple indexes:

Sum(A, I, J, K)

It is one of a number of Array-reducing functions, so-called because they reduce the number of dimensions (indexes) of an array.

Sum treats any Null values in «x» as zero.  It can also treat NaN or text and other non-numerical values as zero, if you set optional parameters, «ignoreNaN» or «ignoreNonNumbers» to True.

Example

This example uses 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 for «IgnoreNonNumbers» and «IgnoreNaN», as shown in the above examples, because parameter «i» is optional and repeated.

Date-time numbers are not ignored (they are considered numbers).

IgnoreDates

New to Analytica 5.3

Set «ignoreDates» to True to (also) ignore date-time numbers.

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.

Sum over text values

In older releases, Sum concatenates values as text, of any values contain text. This feature hides a common error, when you inadvertently try to sum text values thinking that they are numbers, so we strongly discourage using Sum this way. It still works for backward compatibility with older models, but since release 4.5, it gives a warning.

If you want to sum over the numbers only and ignore the text, use the «ignoreNonNumbers» parameter:

Sum(X, I, IgnoreNonNumbers: True) → 18

If you really want to concatenate text, use the "&" operator or JoinText function instead, to make it clear that's what you want to do.

Example:

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

This sum evaluates (5 + 6 + 'X' + 7) as (((5 + 6) & 'X') & 7). It converts the numbers to text and concatenates them to form "11X7".

See Also

Comments


You are not allowed to post comments.