Difference between revisions of "Size"

 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
 
[[Category:Functions returning information about arrays]]
 
[[Category:Functions returning information about arrays]]
[[Category:Doc Status C]] <!-- For Lumina use, do not change -->
 
  
 
== Size(A) ==
 
== Size(A) ==
Line 14: Line 13:
  
 
== Length of an index ==
 
== Length of an index ==
When used on an index, [[Size]](I) returns the length of the index.  However, be careful -- if used with a self-indexed array, it is the size of the array, not the size of the index, that is returned.  When an array is one-dimensional, these are the same, but a self-indexed array could be multi-dimensional. Two safer ways to obtain the length of an index are:
+
To get the length of an index, use [[IndexLength]](I). (introduced in [[Analytica 5.0]]).
 +
 
 +
When used on an index, <code>Size(I)</code> usually, but not always, returns the length of the index.  However, because [[Size]] actually returns the size of the ''value'' of its parameter, this does not give the index length when the parameter is a [[self-indexed array]], or when the parameter is the dynamic index in a [[dynamic loop]] (usually the [[Time]] index).
 +
 
 +
When the identifier is the local identifier that has been declared as an index, then <code>Size(I)</code> does return the index length rather than the value length, even when <code>I</code> is a [[self-indexed array]] or dynamic index.
 +
 
 +
Two other reliable ways to obtain the length of an index are:
 
:<code>Size(IndexValue(I))</code>
 
:<code>Size(IndexValue(I))</code>
 
or
 
or
 
:<code>Sum(1, I)</code>
 
:<code>Sum(1, I)</code>
  
== Length of the [[Implicit Dimensions|implicit dimension]] ==
+
Or by using a [[User-Defined Function]]:
 +
:<code>Function Index_Length(I : Index) := Size(I)</code>.
 +
 
 +
== Length of the implicit dimension ==
  
 
All dimensions of an Analytica array correspond to an explicit index object, except for one -- the [[Implicit Dimensions|implicit dimension]].  See the article [[Implicit Dimensions]] for more details.   
 
All dimensions of an Analytica array correspond to an explicit index object, except for one -- the [[Implicit Dimensions|implicit dimension]].  See the article [[Implicit Dimensions]] for more details.   
Line 40: Line 48:
 
Inside a [[Dynamic|dynamic loop]], <code>Size(Time)</code> returns 1.  This occurs because in a value context within a [[Dynamic|dynamic loop]], the expression <code>Time</code> (or <code>@Time</code>) evaluates to the current time point (or time position), not the full array of time values.  Since there is only one time point at a time in a dynamic loop, the size is 1.
 
Inside a [[Dynamic|dynamic loop]], <code>Size(Time)</code> returns 1.  This occurs because in a value context within a [[Dynamic|dynamic loop]], the expression <code>Time</code> (or <code>@Time</code>) evaluates to the current time point (or time position), not the full array of time values.  Since there is only one time point at a time in a dynamic loop, the size is 1.
  
This case is worth remembering since it can be kind of confusing when you (incorrectly) attempt to use <code>@Time/Size(Time)</code> to compute progress, which of course does not produce the desired result.  The remedy here is to remember to use <code>Size(IndexValue(Time))</code> to obtain the index length.
+
This case is worth remembering since it can be kind of confusing when you (incorrectly) attempt to use <code>@Time/Size(Time)</code> to compute progress, which of course does not produce the desired result.  The remedy here is to remember to use <code>IndexLength(Time)</code> or <code>Size(IndexValue(Time))</code> to obtain the index length.
  
 
== See Also ==
 
== See Also ==
 +
* [[IndexLength]]
 +
* [[IndexValue]]
 
* [[Sum]]
 
* [[Sum]]
 +
* [[Time]]
 
* [[Implicit Dimensions]]
 
* [[Implicit Dimensions]]
* [[Time]]
 

Latest revision as of 00:44, 6 August 2016


Size(A)

Size returns the total number of elements in array «A».

When applied to a scalar (a zero-dimensional array), it returns 1.

Size does not count elements that are contained within a reference. So if a reference occurs in a cell of an array, it will be counted as one, even if it points to an array that contains many cells.

Library

Array library

Length of an index

To get the length of an index, use IndexLength(I). (introduced in Analytica 5.0).

When used on an index, Size(I) usually, but not always, returns the length of the index. However, because Size actually returns the size of the value of its parameter, this does not give the index length when the parameter is a self-indexed array, or when the parameter is the dynamic index in a dynamic loop (usually the Time index).

When the identifier is the local identifier that has been declared as an index, then Size(I) does return the index length rather than the value length, even when I is a self-indexed array or dynamic index.

Two other reliable ways to obtain the length of an index are:

Size(IndexValue(I))

or

Sum(1, I)

Or by using a User-Defined Function:

Function Index_Length(I : Index) := Size(I).

Length of the implicit dimension

All dimensions of an Analytica array correspond to an explicit index object, except for one -- the implicit dimension. See the article Implicit Dimensions for more details.

When «A» has an implicit dimension, its length can be obtained using:

Size(A, listLen: true)

You can also use this to determine the number of repeated parameters supplied to a User-Defined Function. For example:

Function GeoAve(x : ... Number )
Definition: Exp(Sum(Ln(x))/Size(x, ListLen: true))

Array Abstraction Considerations

Although Size does return a result when the number of dimensions of an array changes, this function does not strictly obey the law of array abstraction. This is because it operations over all indexes that are present, rather than only over a set of explicit specified indexes. Because of this, if used inappropriately within a model, incorrect results from Parametric Analyses could result.

In this context, use of Size to find the length of an index or the length of the implicit dimension is a usage that is consistent with the law of array abstraction.

Notes

Inside a dynamic loop, Size(Time) returns 1. This occurs because in a value context within a dynamic loop, the expression Time (or @Time) evaluates to the current time point (or time position), not the full array of time values. Since there is only one time point at a time in a dynamic loop, the size is 1.

This case is worth remembering since it can be kind of confusing when you (incorrectly) attempt to use @Time/Size(Time) to compute progress, which of course does not produce the desired result. The remedy here is to remember to use IndexLength(Time) or Size(IndexValue(Time)) to obtain the index length.

See Also

Comments


You are not allowed to post comments.