Logic Functions
[Functions]


Functions

list argmax (matrix M)
 Returns the position(s) in a matrix that belong to maximum values.
list argmax (list L)
 Returns the position(s) in a list that belong to maximum values.
list argmin (matrix M)
 Returns the position(s) in a matrix that belong to minimum values.
list argmin (list L)
 Returns the position(s) in a list that belong to minimal values.
list argtrue (matrix M)
 Returns the position(s) in a matrix where the entries are not equal to zero after casting to a scalar.
list argtrue (list L, counter RetainStructure)
 Returns the position(s) in a list where the entries are not equal to zero after casting to a scalar.
list max (matrix M)
 Returns the maximum value for each matrix row.
list max (list L)
 Returns maximum value of a list.
list min (matrix M)
 Returns the minimum value for each matrix row.
list min (list L)
 Returns minimum value of a list.
list sort (list lData, code pCode, counter bApply)
 Sorting of a list of numbers or strings.
list sort (list lData, counter bAscend, counter bApply)
 Sorting of a list of numbers or strings.

Detailed Description

Functions that perform logic operations.

Function Documentation

list argmax matrix  M  ) 
 

Returns the position(s) in a matrix that belong to maximum values.

Parameters:
M A matrix.
Returns:
A list of matrix positions.
Since:
v2.0
The code
    ? argmax( Matrix([ [3,2,1],[0,1,0],[-1,-2,3] ]));

finally produces

    Constant = [[1, 1], [2, 2], [3, 3]]
For more information see function argmin.

list argmax list  L  ) 
 

Returns the position(s) in a list that belong to maximum values.

Parameters:
L A list.
Returns:
A list of list positions.
Since:
v2.0
For more information see function argmin.

list argmin matrix  M  ) 
 

Returns the position(s) in a matrix that belong to minimum values.

Parameters:
M A matrix.
Returns:
A list of matrix positions.
Since:
v2.0
The funtion returns a list of $n$ matrix positions, where $n$ is the number of matrix rows. Each row gives one position. To seek for the position in a matrix that belongs to the minimal value of the whole matrix one can write
    ? argmin(M)(argmin(min(M))(1));

list argmin list  L  ) 
 

Returns the position(s) in a list that belong to minimal values.

Parameters:
L A list.
Returns:
A list of list positions.
Since:
v2.0
In contrast to the function argtrue argmin doesn't return a nested list. Each elementary sublist (i.e. not nested any more) gives one minimum value, e.g.
    
    ? argmin([ [ [1,2], [3,4] ], [ [0,9], [7,5,3,2,1], [4,3] ], [1,2,3,4] ]);

The result will be

    Constant = [[1, 1, 1], [1, 2, 1], [2, 1, 1], [2, 2, 5], [2, 3, 2], [3, 1]]

list argtrue matrix  M  ) 
 

Returns the position(s) in a matrix where the entries are not equal to zero after casting to a scalar.

Parameters:
M A matrix.
Returns:
A list of indices.
Since:
v2.0
The following code gives an example for a typical application to matrices.
    
    ? M = Matrix( [ [0,-3,1,5], [0,0,0,0], [0,8,6,0] ] );
    ? T = argtrue( M );
    ? M(T); 

The code example produces

    M = (| 0 -3 1 5|, | 0 0 0 0|, | 0 8 6 0|)   
    T = [[1, 2], [1, 3], [1, 4], [3, 2], [3, 3]]
    Constant = [-3, 1, 5, 8, 6]
In comparison to the function applied to lists this function just gives a simple list of matrix positions.

list argtrue list  L,
counter  RetainStructure
 

Returns the position(s) in a list where the entries are not equal to zero after casting to a scalar.

Parameters:
L A list.
RetainStructure (optional) A boolean flag indicating whether empty sublists should be removed (=0, default), or not.
Returns:
A (nested) list of indices.
Since:
v2.0
If a sublist only consists of zeros e.g. $[0,0]$ then it will first reduce to $[]$ . These empty sublists will then be removed (recursively) by default. But if the flag RetainStructure is set these entries are kept. Here is an example:
    
    ? L = [ [0,-3,1,5], [0,0,0,0], [0,8,[1,0,3],0] ];
    ? T2 = argtrue( L );
    ? T1 = argtrue( L , 1);
    ? L(T2);    

The output will be

    L = [[0, -3, 1, 5], [0, 0, 0, 0], [0, 8, [1, 0, 3], 0]]
    T2 = [[[1, 2], [1, 3], [1, 4]], [[3, 2], [[3, 3, 1], [3, 3, 3]]]]
    T1 = [[[1, 2], [1, 3], [1, 4]], [], [[3, 2], [[3, 3, 1], [3, 3, 3]]]]
    Constant = [[-3, 1, 5], [8, [1, 3]]]
Note that the returned index list is nested and so reflects the structure of the original list. The input list may be completely nested also.

list max matrix  M  ) 
 

Returns the maximum value for each matrix row.

Parameters:
M A matrix.
Returns:
A list of maximum values.
Since:
v2.0
See also the analogous function min.

list max list  L  ) 
 

Returns maximum value of a list.

Parameters:
L A (nested) list.
Returns:
A list of maximum values.
Since:
v2.0
This function evaluates the maximum values of a regularly nested list. Each elementary sublist (i.e. not nested any more) gives one maximum. For an example see min.

list min matrix  M  ) 
 

Returns the minimum value for each matrix row.

Parameters:
M A matrix.
Returns:
A list of minimum values.
Since:
v2.0
To compute the minimum value of the whole matrix M , type min(min(M));.

list min list  L  ) 
 

Returns minimum value of a list.

Parameters:
L A (nested) list.
Returns:
A list of minimum values.
Since:
v2.0
This function evaluates the minimum values of a regularly nested list. Each elementary sublist (i.e. not nested any more) gives one minimum.
    
    L = [ [ [1,2], [3,4] ], [ [0,9], [7,5,3,2,1], [4,3] ], [ 1,2,3,4] ];
    ? min( L ); // works
    // ? min( [ 1,2,3, [1,2] ] ); // not regular - doesn't work
    // ? min(min( L )); // not regular - doesn't work
From the above example one obtains Constant = [[1, 3], [0, 1, 3], 1].

list sort list  lData,
code  pCode,
counter  bApply
 

Sorting of a list of numbers or strings.

Parameters:
lData A list of values that is to be sorted.
pCode (optional) A user defined function to compare pairs of elements in the list lData.
bApply (optional) If this is non-zero, the list lData will be reordered (default), otherwise the list itself will not be reordered but the function still returns a sorting index list.
Returns:
The return value is an index list that gives the ordering of the initial list components.
This version of sort() allows for the sorting of lists according to arbitrary specifications. The user defined function has to accept two parameters and return a value greater than zero if the first parameter should appear to the left of the second parameter in the ordered list. Otherwise it has to return a value less or equal to zero.

The following example sorts a list of vectors according their lengths, from the shortest to the longest.

    SortFunc = { abs(_P(1)) < abs(_P(2)) }
    
    ?lVec = [VecE3(1,2,0), VecE3(0,1,0), VecE3(1,-1,0)];
    ?lIdx = sort(lVec, SortFunc);
    ?lVec;

Output:

lVec (3)=
[ 1 e1 + 2 e2 , 1 e2 , 1 e1 + -1 e2 ]


lIdx (3)=
[ [ 2 ] , [ 3 ] , [ 1 ] ]


lVec (3)=
[ 1 e2 , 1 e1 + -1 e2 , 1 e1 + 2 e2 ]


list sort list  lData,
counter  bAscend,
counter  bApply
 

Sorting of a list of numbers or strings.

Parameters:
lData A list of values that is to be sorted.
bAscend (optional) If this is non-zero, the list will be sorted in ascending order (default), otherwise in descending order.
bApply (optional) If this is non-zero, the list lData will be reordered (default), otherwise the list itself will not be reordered but the function still returns a sorting index list.
Returns:
The return value is an index list that gives the ordering of the initial list components.
In its simplest form, sort() has the following effect.

    ?lData = [3,1,2];
    ?lIdx = sort(lData);
    ?lData;

Output:

lData (3)=
[ 3 , 1 , 2 ]


lIdx (3)=
[ [ 2 ] , [ 3 ] , [ 1 ] ]


lData (3)=
[ 1 , 2 , 3 ]


The list lIdx contains a list of indices giving the new ordering of the initial list lData.

    ?lData = [3,1,2];
    ?lIdx = sort(lData, true, false);
    ?lData;
    ?lData(lIdx);

Output:

lData (3)=
[ 3 , 1 , 2 ]


lIdx (3)=
[ [ 2 ] , [ 3 ] , [ 1 ] ]


lData (3)=
[ 3 , 1 , 2 ]


Constant (3)=
[ 1 , 2 , 3 ]


Here the sorting index list is returned, but the list itself is not reordered. Note that sort() also orders lists of strings and mixed lists of scalars and string. In the latter case, scalars are always regarded as 'smaller' than strings.