The MOSEK Python optimizer API manual Version 7.0 (Revision 141)

Optimizer API for Python - Documentation - Mosek Optimizer API for Python - Documentation - Mosek

25.11.2015 Views

74 CHAPTER 5. BASIC API TUTORIAL ptrb 0 2 3 5 ptre 2 3 5 7 asub 0 2 1 0 3 0 1 2 aval 1.1 3.1 2.2 1.3 4.4 1.4 3.4 2.5 Column 0 Column 1 Figure 5.1: The matrix A (5.17) represented in column ordered packed sparse matrix format. A = ⎡ ⎢ ⎣ 1.1 1.3 1.4 2.2 2.5 3.1 3.4 4.4 which can be represented in the column ordered sparse matrix format as ⎤ ⎥ ⎦ . (5.17) ptrb = [0, 2, 3, 5, 7], ptre = [2, 3, 5, 7, 8], asub = [0, 2, 1, 0, 3, 0, 2, 1], aval = [1.1, 3.1, 2.2, 1.3, 4.4, 1.4, 3.4, 2.5]. Fig. 5.1 illustrates how the matrix A (5.17) is represented in column ordered sparse matrix format. 5.13.3.3 Row ordered sparse matrix The matrix A (5.17) can also be represented in the row ordered sparse matrix format as: ptrb = [0, 3, 5, 7], ptre = [3, 5, 7, 8], asub = [0, 2, 3, 1, 4, 0, 3, 2], aval = [1.1, 1.3, 1.4, 2.2, 2.5, 3.1, 3.4, 4.4].

5.13. CONVENTIONS EMPLOYED IN THE API 75 5.13.4 Array objects The MOSEK Python API provides a simple array object in the module mosekarr. This includes a one-dimensional dense array which can be of type Float, Int or Object, and a few operators and functions to create and modify array objects. Arrays can be constructed in several ways: # Create an array of integers a0 = array([1,2,3],int) # Create an array of floats a1 = array([1,2,3],float) # Create an integer array of ones a2 = ones(10) # Create an float array of ones a3 = ones(10,float) # Create a range of integers 5,6,...,9 a4 = range(5,10) # Create and array of objects a5 = array([’a string’, ’b string’, 10, 2.2]) A limited set of operations on arrays are available - these should work more or less like the equivalent Numeric operations: a = ones(10,float) b = 1.0 * arange(10) # element-wise multiplication, addition and subtraction c0 = a * b c1 = a + b c2 = a - b # multiplly each element by 2.1 c4 = a * 2.1 # add 2 to each element c5 = a + 2 If more advanced array operations is needed, it is necessary to install the Python Numeric package. 5.13.5 Typical problems using the Python API Since all all type-information in Python is implicit, type-checking is performed only when required, and in certain cases it is necessary to explicitly write type information. The MOSEK API currently only supports its own array object (mosek.array.array) and Pythonnumpy arrays. Other array or list compatible objects will are accepted but are converted. Typically type errors occur in two situations:

74 CHAPTER 5. BASIC <strong>API</strong> TUTORIAL<br />

ptrb<br />

0 2 3 5<br />

ptre<br />

2 3 5 7<br />

asub<br />

0 2 1 0 3 0 1 2<br />

aval<br />

1.1 3.1 2.2 1.3 4.4 1.4 3.4 2.5<br />

Column 0 Column 1<br />

Figure 5.1:<br />

<strong>The</strong> matrix A (5.17) represented in column ordered packed sparse matrix format.<br />

A =<br />

⎡<br />

⎢<br />

⎣<br />

1.1 1.3 1.4<br />

2.2 2.5<br />

3.1 3.4<br />

4.4<br />

which can be represented in the column ordered sparse matrix format as<br />

⎤<br />

⎥<br />

⎦ . (5.17)<br />

ptrb = [0, 2, 3, 5, 7],<br />

ptre = [2, 3, 5, 7, 8],<br />

asub = [0, 2, 1, 0, 3, 0, 2, 1],<br />

aval = [1.1, 3.1, 2.2, 1.3, 4.4, 1.4, 3.4, 2.5].<br />

Fig. 5.1 illustrates how the matrix A (5.17) is represented in column ordered sparse matrix format.<br />

5.13.3.3 Row ordered sparse matrix<br />

<strong>The</strong> matrix A (5.17) can also be represented in the row ordered sparse matrix format as:<br />

ptrb = [0, 3, 5, 7],<br />

ptre = [3, 5, 7, 8],<br />

asub = [0, 2, 3, 1, 4, 0, 3, 2],<br />

aval = [1.1, 1.3, 1.4, 2.2, 2.5, 3.1, 3.4, 4.4].

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!