blob: 1b7d0f2df85927fe8dc0bd34def12f89bb1c7442 [file] [log] [blame]
Austin Schuh9049e202022-02-20 17:34:16 -08001Matlab
2======
3
4Before generating code for a parametric problem, the problem should be first
5specified in the setup phase. See :ref:`matlab_setup` for more details.
6
7
8Codegen
9-------
10The code is generated by running
11
12.. code:: matlab
13
14 m.codegen(dir_name, varargin)
15
16The argument :code:`dir_name` is the name of a directory where the generated
17code is stored.
18The second argument :code:`varargin` specifies additional codegen options
19shown in the following table
20
21
22+-----------------------+---------------------------------------------------+--------------------------------+
23| Option | Description | Allowed values |
24+=======================+===================================================+================================+
25| :code:`project_type` | Build environment | | :code:`''` (default) |
26| | | | :code:`'Makefile'` |
27| | | | :code:`'MinGW Makefiles'` |
28| | | | :code:`'Unix Makefiles'` |
29| | | | :code:`'CodeBlocks'` |
30| | | | :code:`'Xcode'` |
31+-----------------------+---------------------------------------------------+--------------------------------+
32| :code:`parameters` | Problem parameters | | :code:`'vectors'` (default) |
33| | | | :code:`'matrices'` |
34+-----------------------+---------------------------------------------------+--------------------------------+
35| :code:`mexname` | Name of the compiled mex interface | | :code:`'emosqp'` (default) |
36| | | | Nonempty string |
37+-----------------------+---------------------------------------------------+--------------------------------+
38| :code:`force_rewrite` | Rewrite existing directory | | :code:`false` (default) |
39| | | | :code:`true` |
40+-----------------------+---------------------------------------------------+--------------------------------+
41| :code:`FLOAT` | Use :code:`float` type instead of :code:`double` | | :code:`false` (default) |
42| | | | :code:`true` |
43+-----------------------+---------------------------------------------------+--------------------------------+
44| :code:`LONG` | Use :code:`long long` type instead of :code:`int` | | :code:`true` (default) |
45| | | | :code:`false` |
46+-----------------------+---------------------------------------------------+--------------------------------+
47
48You can pass the options as field-value pairs, e.g.,
49
50.. code:: matlab
51
52 m.codegen('code', 'parameters', 'matrices', 'mexname', 'emosqp');
53
54If the :code:`project_type` argument is not passed or is set to :code:`''`,
55then no build files are generated.
56
57
58
59Mex interface
60-------------
61Once the code is generated the following functions are provided through its mex interface. Each function is called as
62
63.. code:: matlab
64
65 emosqp('function_name');
66
67
68where :code:`emosqp` is the name of the mex interface specified in the previous section
69
70.. function:: emosqp('solve')
71 :noindex:
72
73 Solve the problem.
74
75 :returns: multiple variables [x, y, status_val, iter, run_time]
76
77 - **x** (*ndarray*) - Primal solution
78 - **y** (*ndarray*) - Dual solution
79 - **status_val** (*int*) - Status value as in :ref:`status_values`
80 - **iter** (*int*) - Number of iterations
81 - **run_time** (*double*) - Run time
82
83
84.. function:: emosqp('update_lin_cost', q_new)
85 :noindex:
86
87 Update linear cost.
88
89 :param ndarray q_new: New linear cost vector
90
91
92.. function:: emosqp('update_lower_bound', l_new)
93 :noindex:
94
95 Update lower bound in the constraints.
96
97 :param ndarray l_new: New lower bound vector
98
99
100.. function:: emosqp('update_upper_bound', u_new)
101 :noindex:
102
103 Update upper bound in the constraints.
104
105 :param ndarray u_new: New upper bound vector
106
107
108.. function:: emosqp('update_bounds', l_new, u_new)
109 :noindex:
110
111 Update lower and upper bounds in the constraints.
112
113 :param ndarray l_new: New lower bound vector
114 :param ndarray u_new: New upper bound vector
115
116
117If the code is generated with the option :code:`parameters` set to
118:code:`'matrices'`, then the following functions are also provided
119
120
121.. function:: emosqp('update_P', Px, Px_idx, Px_n)
122 :noindex:
123
124 Update nonzero entries of the quadratic cost matrix (only upper triangular) without changing sparsity structure.
125
126 :param ndarray Px: Values of entries to be updated
127 :param ndarray Px_idx: Indices of entries to be updated. Pass :code:`[]` if
128 all the indices are to be updated
129 :param int Px_n: Number of entries to be updated. Used only if Px_idx is not
130 :code:`[]`.
131
132
133.. function:: emosqp('update_A', Ax, Ax_idx, Ax_n)
134 :noindex:
135
136 Update nonzero entries of the constraint matrix.
137
138 :param ndarray Ax: Values of entries to be updated
139 :param ndarray Ax_idx: Indices of entries to be updated. Pass :code:`[]` if
140 all the indices are to be updated
141 :param int Ax_n: Number of entries to be updated. Used only if Ax_idx is not
142 :code:`[]`.
143
144
145.. function:: emosqp('update_P_A', Px, Px_idx, Px_n, Ax, Ax_idx, Ax_n)
146 :noindex:
147
148 Update nonzero entries of the quadratic cost and constraint matrices. It considers only the upper-triangular part of P.
149
150 :param ndarray Px: Values of entries to be updated
151 :param ndarray Px_idx: Indices of entries to be updated. Pass :code:`[]` if
152 all the indices are to be updated
153 :param int Px_n: Number of entries to be updated. Used only if Px_idx is not
154 :code:`[]`.
155 :param ndarray Ax: Values of entries to be updated
156 :param ndarray Ax_idx: Indices of entries to be updated. Pass :code:`[]` if
157 all the indices are to be updated
158 :param int Ax_n: Number of entries to be updated. Used only if Ax_idx is not
159 :code:`[]`.
160
161
162You can update all the nonzero entries in matrix :math:`A` by running
163
164.. code:: matlab
165
166 emosqp('update_A', Ax_new, [], 0);
167
168See C :ref:`C_sublevel_API` for more details on the input arguments.