2.6 Built-In Functions

Many Python built-in functions and operations can be used with matrix arguments. We list some useful examples.

len(x)

If x is a dense matrix, returns the product of the number of rows and the number of columns. If x is a sparse matrix, returns the number of nonzero entries.

bool([x])

Returns False if x is a zero matrix and True otherwise.

max(x)

If x is a dense matrix, returns the maximum element of x. If x is a sparse, returns the maximum nonzero element of x.

min(x)

If x is a dense matrix, returns the minimum element of x. If x is a sparse matrix, returns the minimum nonzero element of x.

abs(x)

Returns a matrix with the absolute values of the elements of x.

sum(x[, start=0.0])

Returns the sum of start and the elements of x.

Dense and sparse matrices can be used as arguments to the list(), tuple(), zip(), map(), and filter() functions described in section 2.1 of the Python Library Reference. However, one should note that when used with sparse matrix arguments, these functions only consider the nonzero entries. For example, list(A) and tuple(A) construct a list, respectively a tuple, from the elements of A if A is dense, and of the nonzero elements of A if A is sparse.

zip(A, B, …) returns a list of tuples, with the ith tuple containing the ith elements (or nonzero elements) of A, B, ….

>>> from cvxopt import matrix  
>>> A = matrix([[-11., -5., -20.], [-6., -0., 7.]])  
>>> B = matrix(range(6), (3,2))  
>>> list(A)  
[-11.0, -5.0, -20.0, -6.0, 0.0, 7.0]  
>>> tuple(B)  
(0, 1, 2, 3, 4, 5)  
>>> zip(A, B)  
[(-11.0, 0), (-5.0, 1), (-20.0, 2), (-6.0, 3), (0.0, 4), (7.0, 5)]

map(f,A), where f is a function and A is a dense matrix, returns a list constructed by applying f to each element of A. If A is sparse, the function f is applied to each nonzero element of A. Multiple arguments can be provided, for example, as in map(f,A,B), if f is a function with two arguments. In the following example, we return an integer 0-1 matrix with the result of an elementwise comparison.

>>> A = matrix([ [0.5, -0.1, 2.0], [1.5, 0.2, -0.1], [0.3, 1.0, 0.0]])  
>>> print A  
[ 5.00e-01  1.50e+00  3.00e-01]  
[-1.00e-01  2.00e-01  1.00e+00]  
[ 2.00e+00 -1.00e-01  0.00e+00]  
>>> print matrix(map(lambda x: 0 <= x <= 1, A), A.size)  
[ 1  0  1]  
[ 0  1  1]  
[ 0  0  1]

filter(f,A), where f is a function and A is a matrix, returns a list containing the elements of A (or nonzero elements of A is A is sparse) for which f is true.

>>> filter(lambda x: x%2, A)         # list of odd elements in A  
[5, -7, -1, -5, 1, 5, -1, -3, -7]  
>>> filter(lambda x: -2 < x < 3, A)  # list of elements between -2 and 3  
[-1, 2, 1, 2, -1, 2]

It is also possible to iterate over matrix elements, as illustrated in the following example.

>>> A = matrix([[5, -3], [9, 11]])  
>>> for x in A: print max(x,0)  
...  
5  
0  
9  
11  
>>> [max(x,0) for x in A]  
[5, 0, 9, 11]

The expression ”x in A” returns True if an element of A (or a nonzero element of A if A is sparse) is equal to x and False otherwise.