Difference between revisions of "Functions Min and Max"

(Finding the Max over all indexes)
 
(13 intermediate revisions by 4 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:Math Functions]]
 
[[Category:Math Functions]]
  
= Functions Min and Max =
+
==Min(X, ''I''), Max(X, ''I'') ==
 +
Return the smallest ('''Min''') or largest ('''Max''') value in array «X» over index «I». You can use them to find the smallest or largest value over an explicit list of values enclosed in square brackets, in which case you omit the index:
 +
:<code>Max([a, b, c])</code>
  
  Min( X, I )
+
This form evaluates <code>a, b</code>, and <code>c</code>, and finds the largest value among them. For example:
Max( X, I )
+
:<code>Max([Sin(30), Tan(30), Cos(30)]) &rarr; 0.8660254</code>
  
These return the smallest (Min) or largest (Max) values along an indicated index.
+
Although index «I» is optional, we strongly recommend you include it even if «X» currently only has one index. You might expand the model so that «X» gets another index, <code>J</code>. Then '''Min'''(X) would be ambiguous. Analytica couldn't tell whether you meant '''Min'''(X, I) or '''Min'''(X, J), and might not interpret it the way you want.  
  
To find the larger of separate values, use:
+
If «X» contains text values, it returns the first ('''Min''') or last ('''Max''') value in alphabetic order. If «X» contains mixed text and numbers, it treats text before or "smaller than" numbers. Thus,
Max( [a,b,c] )
+
:<code>Min(['A', 'B', 2]) &rarr; 'A'</code>
 +
:<code>Max(['A', 'B', 2]) &rarr;  2</code>
  
This form evaluates a, b, and c, and finds the largest value among them.  For example:
+
It orders text as case sensitive unless you specify otherwise with the optional «CaseInsensitive» parameter (see below).
Max( [sin(30), tan(30), cos(30)] ) --> 0.8660254
 
  
The index parameter is optional, but it is highly recommended that you always specify the index except in the list-usage just mentioned, or in the case where you can guarantee that X will always be one-dimensional (such as when X is itself an index).  In most cases, even if your array is 1-D today, it is better to specify the index explicitly so that the expression can correctly array abstract if new dimensions are introduced into your model in the future.
+
== Optional parameters ==
 +
=== Multiple indexes ===
 +
:<code>Min(X, I, J, K, ...)</code>
 +
:<code>Min(X, I, J, K, ...)</code>
  
As of 4.0, these functions do not support named parameter syntax.
+
You can specify more than one index, e.g., <code>Max(X, I, J)</code> is equivalent to <code>Max(Max(X, I), J)</code>. This finds the maximum or minimum over all the specified indexes of «X».
  
= Functions CondMin and CondMax =
+
=== IgnoreNonNumbers and IgnoreNAN===
 +
These optional Boolean parameters require a named-parameter syntax. If «IgnoreNonNumbers» is set to true (<code>1</code>), it ignores non-numeric values.  If «IgnoreNAN» is set to <code>True (1)</code>, it ignores [[NaN]] values. It always ignores [[Null]] no matter what the parameter values.
  
CondMin( X : Array[I], cond : Boolean[I] ;  I : IndexType )
+
:<code>Variable A :=</code>
CondMax( X : Array[I], cond : Boolean[I] ; I : IndexType )
+
:{| class="wikitable"
 +
! colspan="7" | I &#9654;
 +
|-
 +
! 1 || 2 || 3 || 4 || 5 || 6 || 7
 +
|-
 +
| 56 || "a" || 4 || "1" || 3 || «null» || 45
 +
|}
  
Conditional Min and Max.  These return the smallest (CondMin) or largest (CondMax) values along a given index, but only along a subset of values as indicated by the parameter cond.
+
:<code>Min(A, I, IgnoreNonNumbers: True) &rarr; 3</code>
 +
:<code>Max(A, I, IgnoreNonNumbers: True) &rarr; 56</code>
  
As of 4.0, these functions do not support named parameter syntax.
+
A [[NaN]] value occurs when an arithmetic operation is indeterminate, such as the result of a divide by zero.  These are would-be numeric values, but just indeterminate, so the result of a '''Min''' or '''Max''' on [[NaN]] is also [[NaN]]. These normally propagate so that you don't get misleading results downstream, but if you also want to ignore [[NaN]] values, then include «IgnoreNaN» set to true.
  
= Related Functions =
+
(''New to [[Analytica 5.3]]'') Date-time numbers are considered numeric, so these aren't ignored when only ignoreNonNumbers is set to true. To also ignore date-time numbers, set «ignoreDates» to true, e.g.,
 +
:<code>Min(A, I, ignoreNonNumbers:True, ignoreDates:true )</code>
  
* [[Ana:Fn:ArgMin_and_ArgMax| ArgMin]] and [[Ana:Fn:ArgMin_and_ArgMax| ArgMax]]
+
=== CaseInsensitive ===
 +
Comparison of text values by '''Min''' and '''Max''' is done in a case-sensitive fashion by default.  You can specify the optional «caseInsensitive» parameter as true to do the comparison in a case-insensitive manner, e.g.:
 +
 
 +
:<code>Min(['DeBois', 'Debbie', 'Debutante']) &rarr; 'DeBois'</code>
 +
:<code>Min(['DeBois', 'Debbie', 'Debutante'], CaseInsensitive: True) &rarr; 'Debbie'</code>
 +
 
 +
== Finding the Max over all indexes ==
 +
{{Assista|key|MaxOverAll}}
 +
:<code>Max( X, ...[[IndexesOf]](X) )</code>
 +
computes the maximum value in any cell of array <code>X</code>, operating across every index of X. The <code>...</code> operator performs [[Repeated parameter forwarding|repeated parameter forwarding]]. More generally, this can be used to find the Max over any computed subset of indexes by supplying a list of index [[handle]]s to <code>...</code>. Another example would be to compute the maximum over every index of <code>X</code> except <code>J</code> (so that the result still has index <code>J</code>.
 +
:<code>Max( X, ...[[IndexesOf]]( X, except: J ) )</code>
 +
 
 +
One situation for this occurs when you have an index defined as:
 +
: <code>Index I := 1..n </code>
 +
This works as long as <code>n</code> is a single number, but if you ever want to replace <code>n</code> with a list of values, then <code>1..n</code> breaks because this would no longer be a rectangular array, and because an index requires a single list as its definition. The solution is to size the index to the maximum n. The challenge is that we must plan for indexes in the future that might not even be present when we write this definition. This is accomplished using
 +
: <code>Index I := 1..Max(n, ...IndexesOf(n))</code>
 +
Generally arrays that use this index will use null-padding for the cases corresponding to <code>n</code> less than the maximum.
 +
 
 +
== CondMin(X, cond, ''I''), CondMax(X, cond, ''I'') ==
 +
[[Syntax]]
 +
:'''CondMin'''(X: Array[I]; cond: Boolean[I];  I: Index)
 +
:'''CondMax'''(X: Array[I]; cond: Boolean[I];  I: Index)
 +
 
 +
Conditional '''Min''' and '''Max'''.  These return the smallest ('''CondMin''') or largest ('''CondMax''') values along a given index, but only along a subset of values as indicated by the parameter «cond».
 +
 
 +
When no cell satisfies «cond», the result of '''CondMax''' is -[[INF]], and the result of '''CondMin''' is [[INF]].
 +
 
 +
Starting with Analytica 4.4, '''CondMax''' and '''CondMin''' support the named parameter syntax and allow multiple indexes to be listed.  The optional flag parameter «CaseInsensitive» can be set to <code>True</code> (default is <code>False</code>).
 +
 
 +
== See Also==
 +
* [[ArgMin and  ArgMax]]
 +
* [[Ana:Fn:ArgMin_and_ArgMax| ArgMin]]
 +
* [[Ana:Fn:ArgMin_and_ArgMax| ArgMax]]
 +
* [[Array-reducing functions]]

Latest revision as of 20:24, 6 February 2025


Min(X, I), Max(X, I)

Return the smallest (Min) or largest (Max) value in array «X» over index «I». You can use them to find the smallest or largest value over an explicit list of values enclosed in square brackets, in which case you omit the index:

Max([a, b, c])

This form evaluates a, b, and c, and finds the largest value among them. For example:

Max([Sin(30), Tan(30), Cos(30)]) → 0.8660254

Although index «I» is optional, we strongly recommend you include it even if «X» currently only has one index. You might expand the model so that «X» gets another index, J. Then Min(X) would be ambiguous. Analytica couldn't tell whether you meant Min(X, I) or Min(X, J), and might not interpret it the way you want.

If «X» contains text values, it returns the first (Min) or last (Max) value in alphabetic order. If «X» contains mixed text and numbers, it treats text before or "smaller than" numbers. Thus,

Min(['A', 'B', 2]) → 'A'
Max(['A', 'B', 2]) → 2

It orders text as case sensitive unless you specify otherwise with the optional «CaseInsensitive» parameter (see below).

Optional parameters

Multiple indexes

Min(X, I, J, K, ...)
Min(X, I, J, K, ...)

You can specify more than one index, e.g., Max(X, I, J) is equivalent to Max(Max(X, I), J). This finds the maximum or minimum over all the specified indexes of «X».

IgnoreNonNumbers and IgnoreNAN

These optional Boolean parameters require a named-parameter syntax. If «IgnoreNonNumbers» is set to true (1), it ignores non-numeric values. If «IgnoreNAN» is set to True (1), it ignores NaN values. It always ignores Null no matter what the parameter values.

Variable A :=
I ▶
1 2 3 4 5 6 7
56 "a" 4 "1" 3 «null» 45
Min(A, I, IgnoreNonNumbers: True) → 3
Max(A, I, IgnoreNonNumbers: True) → 56

A NaN value occurs when an arithmetic operation is indeterminate, such as the result of a divide by zero. These are would-be numeric values, but just indeterminate, so the result of a Min or Max on NaN is also NaN. These normally propagate so that you don't get misleading results downstream, but if you also want to ignore NaN values, then include «IgnoreNaN» set to true.

(New to Analytica 5.3) Date-time numbers are considered numeric, so these aren't ignored when only ignoreNonNumbers is set to true. To also ignore date-time numbers, set «ignoreDates» to true, e.g.,

Min(A, I, ignoreNonNumbers:True, ignoreDates:true )

CaseInsensitive

Comparison of text values by Min and Max is done in a case-sensitive fashion by default. You can specify the optional «caseInsensitive» parameter as true to do the comparison in a case-insensitive manner, e.g.:

Min(['DeBois', 'Debbie', 'Debutante']) → 'DeBois'
Min(['DeBois', 'Debbie', 'Debutante'], CaseInsensitive: True) → 'Debbie'

Finding the Max over all indexes

Max( X, ...IndexesOf(X) )

computes the maximum value in any cell of array X, operating across every index of X. The ... operator performs repeated parameter forwarding. More generally, this can be used to find the Max over any computed subset of indexes by supplying a list of index handles to .... Another example would be to compute the maximum over every index of X except J (so that the result still has index J.

Max( X, ...IndexesOf( X, except: J ) )

One situation for this occurs when you have an index defined as:

Index I := 1..n

This works as long as n is a single number, but if you ever want to replace n with a list of values, then 1..n breaks because this would no longer be a rectangular array, and because an index requires a single list as its definition. The solution is to size the index to the maximum n. The challenge is that we must plan for indexes in the future that might not even be present when we write this definition. This is accomplished using

Index I := 1..Max(n, ...IndexesOf(n))

Generally arrays that use this index will use null-padding for the cases corresponding to n less than the maximum.

CondMin(X, cond, I), CondMax(X, cond, I)

Syntax

CondMin(X: Array[I]; cond: Boolean[I]; I: Index)
CondMax(X: Array[I]; cond: Boolean[I]; I: Index)

Conditional Min and Max. These return the smallest (CondMin) or largest (CondMax) values along a given index, but only along a subset of values as indicated by the parameter «cond».

When no cell satisfies «cond», the result of CondMax is -INF, and the result of CondMin is INF.

Starting with Analytica 4.4, CondMax and CondMin support the named parameter syntax and allow multiple indexes to be listed. The optional flag parameter «CaseInsensitive» can be set to True (default is False).

See Also

Comments


You are not allowed to post comments.