Skip to content

Instantly share code, notes, and snippets.

@jgillis
Last active May 28, 2023 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgillis/b55a3513d8af70ab56fcdfb8d0bdf0d6 to your computer and use it in GitHub Desktop.
Save jgillis/b55a3513d8af70ab56fcdfb8d0bdf0d6 to your computer and use it in GitHub Desktop.
How to do np.interp in CasADi
import casadi as cs
import numpy as np
xp = [1, 2, 3]
fp = [3, 2, 0]
x = [2.5,1.1]
print(np.interp(x=x,xp=xp,fp=fp))
# np.interp as CasADi Function
# Note: CasADi functions always need fixed dimensions
grid = [len(xp)] # size of grid, list is used since can be multi-dimensional
cs_interp = cs.interpolant('cs_interp','linear',grid,1,{"inline": True})
print(cs_interp)
# Using broadcasting on first argument to obtain an evaluation for all values in x
print(cs_interp(cs.DM(x).T,xp,fp))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment