symforce-org / symforce

Fast symbolic computation, code generation, and nonlinear optimization for robotics
https://symforce.org
Apache License 2.0
1.41k stars 145 forks source link

Accept list vec args in genned code by default #251

Open bradley-solliday-skydio opened 1 year ago

bradley-solliday-skydio commented 1 year ago

By default, I mean if use_numba=False and reshape_vecs=True.

Previously, only ndarrays could be passed as vector args to generated code. However, lists in python are ubiquitous and natural, so it would be good for our generated functions to accept them.

So, this commit allows generated functions to accept lists for matrix arguments when the argument is either a row vector or a column vector.

Nested lists are not accepted to represent matrices because I simply did not consider that until right now. (Perhaps we should add that?)

PythonConfig.use_numba must be False because list arguments have been deprecated in numba (instead you should use numba.typed.List, but if you have to use that you might as well use numpy.ndarray as far as I can tell).

PythonConfig.reshape_vecs must be True because accepting lists requires conversion to an ndarray and reshaping, which is presumably more or less the entire thing meant to be avoided by setting reshape_vecs to False.

This change did involve mucking up a bit the python util.jinja type printing macros, as a sf.Matrix type should be rendered as an numpy.ndarray is it is a return type, reshape_vectors=False, use_numba=True, or is not a row or column vector, and rendered as T.Union[T.Sequence[float], numpy.ndarray] otherwise.