peterdsharpe / AeroSandbox

Aircraft design optimization made fast through modern automatic differentiation. Composable analysis tools for aerodynamics, propulsion, structures, trajectory design, and much more.
https://peterdsharpe.github.io/AeroSandbox/
MIT License
690 stars 111 forks source link

Added `aerosandbox.numpy` #41

Closed peterdsharpe closed 3 years ago

peterdsharpe commented 3 years ago

Changed the underlying math backend from what was previously aerosandbox.optimization.math to aerosandbox.numpy.

Basically what's happening here:

  1. First, aerosandbox.numpy (which we will shorthand as anp here for clarity) imports the entirety of NumPy.
  2. Mostly, these NumPy functions will already work for CasADi types, so we're good to go. However, some NumPy functions either don't work correctly with these types (they either throw errors or try to make them into NumPy object arrays). For these functions, we redefine them in aerosandbox.numpy.

For example, np.sum() is overwritten to be:

def sum(x):
    """Returns the sum of a vector x."""
    try:
        return onp.sum(x)
    except Exception:
        return cas.sum1(x)

where onp is "original" NumPy and cas is CasADi.

The result of this is that the only thing the end-user needs to do is replace import numpy as np with import aerosandbox.numpy as np. If the user is working with NumPy types, great - nothing changes, and the backend is just NumPy. If the user is working with CasADi types (like optimization variables), then the backend switches automatically to CasADi.