Difference between revisions of "Sequence"

 
(24 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
[[Category:Functions that create lists]]
 
[[Category:Functions that create lists]]
 +
[[Category:Array Library]]
 
[[Category:Doc Status C]] <!-- For Lumina use, do not change -->
 
[[Category:Doc Status C]] <!-- For Lumina use, do not change -->
 
   
 
   
= Sequence( Start, End'', step'' ) =
+
== Sequence(start, end, ''step'') ==
 +
Creates a list of numbers from «start» to «end» by equal increments (or decrements) of «step». You may omit «step», which defaults to 1. «step» must be a positive number.  «start», «end», and «step» must be deterministic scalar numbers, not arrays and not uncertain. See [[Expressions that don't array-abstract]].
  
Creates a list of numbers increasing or decreasing from ''Start'' to ''End'' by increments (or decrements) of ''Stepsize''.  Stepsize is optional and must be a positive number; if it is omitted, Analytica uses increments of 1.  Start, End and Stepsize must be deterministic scalar numbers, not arrays.
+
You can also select this function using the '''Sequence''' option from the '''expr''' menu.
  
= Declaration =
+
The expression <code>m..n</code> using the [[Sequence Operator]] <tt>".."</tt> is equivalent to <code>Sequence(m, n, 1)</code>.
  
+
==Example==
= Examples =
+
:<code>Sequence(1, 5) &rarr; [1, 2, 3, 4, 5]</code>
 
 
= Strict sequences =
 
 
 
[[Sequence]] expects the «step» to be positive (or omitted, in which case a step of 1 is assumed).  A decreasing sequence is obtained by specifying a «start» less than «end».  As a result of this convention, a sequence will always have at least one element.
 
 
 
(''new to [[What's new in Analytica 4.2?|Analytica 4.2]]'') There are cases where a strict sequence is desired, such that the sequence proceeds from «start» in increments of «step», according to the [[Sign|sign]] of «step».  When «step» proceeds in the direction away from «end», then a zero-length sequence results.  For example, in a [[For..Do]] loop, you may want zero iterations when «end» is less than «start».  A strict sequence is obtained by specifying the optional parameter «strict» as true, e.g.
 
 
 
[[Sequence]](x1,x2,strict:true)
 
 
 
When «strict» is specified as true, the «step» may be negative, and must be negative to obtain a decreasing sequence.
 
 
 
= Date Sequences =
 
 
 
''new to [[What's new in Analytica 4.2?|Analytica 4.2]]''
 
  
You can use [[Sequence]] to generate a sequence of dates between a given start and stop date, in increments such as months and years, by specifying the optional «dateUnit» parameter.  Possible date units include: "Y" (years), "Q" (quarters), "M" (months), "WD" (weekdays), "D" (days), "h" (hours), "m" (minutes), "s" (seconds).
+
[[File:Chapter11_77.jpg]]
  
[[Sequence]]( [[MakeDate]](2009,1,1), [[MakeDate]](2010,12,31), 4, dateUnit:"M" ) &rarr;
+
The parameter value for «end» is greater than the value for «start», so the sequence is increasing.
  [ 1-Jan-2009, 1-May-2009, 1-Sep-2009, 1-Jan-2010, 1-May-2010, 1-Sep-2010 ]
 
  
= Character Sequences =
+
== Optional parameters ==
 +
=== Strict ===
 +
Normally, if «start» is larger than «end», and «step» is positive, the sequence decrements by «step». As a result of this convention, a sequence will always have at least one element.
  
''New to Analytica 4.4 or patch release 4.3.3''
+
There are cases where a strict sequence is desired, such that the sequence proceeds from «start» in increments of «step», according to the [[Sign|sign]] of «step». When «step» proceeds in the direction away from «end», then a zero-length sequence results. For example, in a [[For..Do]] loop, you may want zero iterations when «end» is less than «start».  Specifying the optional parameter «strict» as true obtains a strict Sequence, for example:
  
The «start» and «stop» characters may be single characters.  In this case, a sequence of characters following ascii order is returned.
+
:<code>Sequence(x1, x2, strict: True)</code>
  
:<code>[[Sequence]]('T','f')</code> &rarr; ['T','U','V','W','X','Y','Z','[','\',']','^','_','`','a','b','c','d','e','f']
+
When «strict» is specified as True, the step may be negative, and must be negative to obtain a decreasing sequence. For example:
  
:<code>[[Sequence]]('!','@')</code> &rarr; ['!','""','#','$','%','&',''','(',')','*','+',',','-','.','/','0','1','2','3','4','5','6','7','8','9',':',';','<','=','>','?','@']
+
:<code>Sequence(5, 1, strict: True) &rarr;  []</code>
 +
:<code>Sequence(5, 1, -2, strict: True)  &rarr;  [5, 3, 1]</code>
  
= Spreadsheet Column Name Sequences =
+
=== DateUnit ===
 +
You can use [[Sequence]] to create a sequence of successive days,  hours, or 15 minute intervals, the first day of each week, month, or year, and so on. You specify the units with the optional «dateUnit» parameter. For example, this generates dates at quarterly (3-month) intervals:
  
''New to Analytica 4.4 or patch 4.3.3''
+
:<code>Sequence(MakeDate(2015, 1, 1), MakeDate(2016, 6, 31), 1, dateUnit: "Q") &rarr; </code>
 +
:<code>[1-Jan-2015, 1-Apr-2015, 1-Jul-2015, 1-Oct-2015, 1-Jan-2016, 1-Apr-2016]</code>
  
When «start» and «stop» are multi-letter sequences, containing only letters, with all letters having the same upper-case or lower-case, then a spreadsheet column sequence is returned. 
+
These are the characters defining each date and time unit:
 +
* Years: <tt>'Y'</tt>
 +
* Quarters: <tt>'Q'</tt>
 +
* Months: <tt>'M'</tt>
 +
* Days: <tt>'D'</tt> (the default when «dateUnit» is omitted)
 +
* Weekdays: <tt>'WD'</tt>
 +
* Hours: <tt>'h'</tt>
 +
* Minutes: <tt>'m'</tt>
 +
* Seconds: <tt>'s'</tt>
  
These sequences are letter sequences that go from 'A'..'Z', then from 'AA'..'AZ', 'BA'..'BZ', ... 'ZA'..'ZZ', then 'AAA'..'ZZZ', 'AAAA'..'ZZZZ', etc., up to six digits total.
 
  
:<code>Sequence('W','AF')</code> &rarr; ['W','X','Y','Z','AA','AB','AC','AD','AE','AF']
+
==Details & More Examples==
 +
===Character Sequences===
 +
You can specify «start» and «end» each as a character, and it will generate a sequence of characters in ASCII order:
  
:For these sequences, the start and stop must consist entirely of letters, and all letters must be all upper case or all lower case.
+
:<code>Sequence('T', 'f') &rarr;</code>
 +
:<code>['T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f']</code>
 +
:<code>Sequence('!', '@') &rarr;</code>
 +
:<code>['!', '""', '#', '$', '%', '&', '<nowiki>'</nowiki>', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@']</code>
  
 +
===Spreadsheet Column Name Sequences===
 +
You can also use Sequence to generate a sequence of spreadsheet column names -- which go from 'A' to 'Z', then  'AA' to 'AZ', 'BA' to 'BZ', ... 'ZA' to 'ZZ', then 'AAA'..'ZZZ', 'AAAA'..'ZZZZ', etc., up to six letters total. In this case, «start» and «end» must contain one or more letters, either all uppercase or all lowercase:
  
=USER GUIDE=
+
:<code>Sequence('W', 'AF') &rarr;</code>
Creates a list of numbers increasing or decreasing from '''start''' to '''end''' by increments (or decrements) of '''stepSize''', which is optional and defaults to 1. When the '''strict''' parameter is omitted or false, '''stepSize''' must be a positive number and the sequence will decrement by '''stepSize''' when '''end''' is less than '''start''', guaranteeing at least one element. When '''strict''' is specified as true, a positive '''stepSize''' increments and negative '''stepSize''' returns a decrementing sequence, possibly with zero elements if '''end''' would come before '''start'''.
+
:<code>['W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF']</code>
  
The optional '''dateUnit''' parameter is used when creating a sequence of dates, with increments in units of Years (<tt>dateUnit:'Y'</tt>), Months (<tt>'M'</tt>), Days (<tt>'D'</tt> or omitted), Weekdays (<tt>'WD'</tt>), Hours (<tt>'h'</tt>), minutes (<tt>'m'</tt>) or seconds (<tt>'s'</tt>).
+
===Examples===
 +
If «start» is greater than «end», the sequence is decreasing:
  
All parameters must be deterministic scalar numbers, not arrays.
+
:<code>Sequence(5, 1) &rarr;  [5, 4, 3, 2, 1]</code>
  
You can also select this function using the '''Sequence''' option from the ''expr'' menu, as described in “Create a list with the Sequence option” on page 173.
+
If «start» and «end» are not integers, and you omit «step», the function rounds them:
  
The expression '''m .. n''' using the operator <tt>".."</tt> is equivalent to '''Sequence(m, n, 1)'''.
+
:<code>Sequence(1.2, 4.8) &rarr; [1, 2, 3, 4, 5]</code>
  
==Library==
+
If you specify «step», the function can create non-integer values:
Array
 
  
==Examples==
+
:<code>Sequence(0.5, 2.5, 0.5) &rarr; [0.5, 1, 1.5, 2, 2.5]</code>
If <tt>end</tt> is greater than <tt>start</tt>, the sequence is increasing:
 
Sequence(1,5) →
 
  
{|
 
|-
 
! scope="col"| List View
 
! scope="col"| Expression View
 
|-
 
| IMAGE of List View
 
| <tt>[1,2,3,4,5]</tt>
 
|}
 
  
If <tt>start</tt> is greater than <tt>end</tt>, the sequence is decreasing:
+
== See Also==
Sequence(5,1) → [5, 4, 3, 2, 1]
+
* [[Sequence Operator]]
Unless '''strict''' is true:
+
* [[Date functions]]
Sequence(5, 1, strict:true) → []
 
Sequence(5, 1, -2, strict:true)  → [5, 3, 1]
 
If <tt>start</tt> and <tt>end</tt> are not integers, and you omit <tt>stepSize</tt>, it rounds them:
 
Sequence(1.2, 4.8) → [1, 2, 3, 4, 5]
 
If you specify <tt>stepSize</tt>, it can create non-integer values:
 
Sequence(0.5, 2.5, 0.5) → [0.5, 1, 1.5, 2, 2.5]
 

Latest revision as of 00:42, 22 April 2016


Sequence(start, end, step)

Creates a list of numbers from «start» to «end» by equal increments (or decrements) of «step». You may omit «step», which defaults to 1. «step» must be a positive number. «start», «end», and «step» must be deterministic scalar numbers, not arrays and not uncertain. See Expressions that don't array-abstract.

You can also select this function using the Sequence option from the expr menu.

The expression m..n using the Sequence Operator ".." is equivalent to Sequence(m, n, 1).

Example

Sequence(1, 5) → [1, 2, 3, 4, 5]

Chapter11 77.jpg

The parameter value for «end» is greater than the value for «start», so the sequence is increasing.

Optional parameters

Strict

Normally, if «start» is larger than «end», and «step» is positive, the sequence decrements by «step». As a result of this convention, a sequence will always have at least one element.

There are cases where a strict sequence is desired, such that the sequence proceeds from «start» in increments of «step», according to the sign of «step». When «step» proceeds in the direction away from «end», then a zero-length sequence results. For example, in a For..Do loop, you may want zero iterations when «end» is less than «start». Specifying the optional parameter «strict» as true obtains a strict Sequence, for example:

Sequence(x1, x2, strict: True)

When «strict» is specified as True, the step may be negative, and must be negative to obtain a decreasing sequence. For example:

Sequence(5, 1, strict: True) → []
Sequence(5, 1, -2, strict: True) → [5, 3, 1]

DateUnit

You can use Sequence to create a sequence of successive days, hours, or 15 minute intervals, the first day of each week, month, or year, and so on. You specify the units with the optional «dateUnit» parameter. For example, this generates dates at quarterly (3-month) intervals:

Sequence(MakeDate(2015, 1, 1), MakeDate(2016, 6, 31), 1, dateUnit: "Q") →
[1-Jan-2015, 1-Apr-2015, 1-Jul-2015, 1-Oct-2015, 1-Jan-2016, 1-Apr-2016]

These are the characters defining each date and time unit:

  • Years: 'Y'
  • Quarters: 'Q'
  • Months: 'M'
  • Days: 'D' (the default when «dateUnit» is omitted)
  • Weekdays: 'WD'
  • Hours: 'h'
  • Minutes: 'm'
  • Seconds: 's'


Details & More Examples

Character Sequences

You can specify «start» and «end» each as a character, and it will generate a sequence of characters in ASCII order:

Sequence('T', 'f') →
['T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f']
Sequence('!', '@') →
['!', '""', '#', '$', '%', '&', ''', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@']

Spreadsheet Column Name Sequences

You can also use Sequence to generate a sequence of spreadsheet column names -- which go from 'A' to 'Z', then 'AA' to 'AZ', 'BA' to 'BZ', ... 'ZA' to 'ZZ', then 'AAA'..'ZZZ', 'AAAA'..'ZZZZ', etc., up to six letters total. In this case, «start» and «end» must contain one or more letters, either all uppercase or all lowercase:

Sequence('W', 'AF') →
['W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF']

Examples

If «start» is greater than «end», the sequence is decreasing:

Sequence(5, 1) → [5, 4, 3, 2, 1]

If «start» and «end» are not integers, and you omit «step», the function rounds them:

Sequence(1.2, 4.8) → [1, 2, 3, 4, 5]

If you specify «step», the function can create non-integer values:

Sequence(0.5, 2.5, 0.5) → [0.5, 1, 1.5, 2, 2.5]


See Also

Comments


You are not allowed to post comments.