{"id":3920,"library":"casadi","title":"CasADi","description":"CasADi is an open-source symbolic framework for numerical optimization and automatic differentiation. It enables users to formulate complex optimization problems (e.g., optimal control, nonlinear programming) using symbolic expressions and then efficiently solve them using state-of-the-art numerical solvers. It is actively maintained with several releases per year. Current version: 3.7.2.","status":"active","version":"3.7.2","language":"en","source_language":"en","source_url":"https://github.com/casadi/casadi","tags":["optimization","numerical","symbolic","automatic differentiation","control"],"install":[{"cmd":"pip install casadi","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"casadi","correct":"import casadi as ca"},{"note":"In CasADi v3.x, SXFunction/MXFunction were replaced by a unified Function interface, typically accessed via higher-level APIs like nlpsol.","wrong":"solver = ca.SXFunction(...) or ca.MXFunction(...)","symbol":"Function","correct":"solver = ca.nlpsol(...)"}],"quickstart":{"code":"import casadi as ca\nimport numpy as np\n\n# Declare symbolic variables\nx = ca.MX.sym(\"x\")\ny = ca.MX.sym(\"y\")\n\n# Define objective function (e.g., Rosenbrock function)\nf = (x - 1)**2 + 10*(y - x**2)**2\n\n# Define constraints (e.g., x + y >= 0)\ng = x + y\n\n# Formulate NLP problem\nnlp = {'x': ca.vertcat(x,y), 'f': f, 'g': g}\n\n# Choose a solver (IPOPT is common, ensure it's available)\nsolver_opts = {'ipopt': {'print_level': 0}, 'print_time': 0}\nsolver = ca.nlpsol('solver', 'ipopt', nlp, solver_opts)\n\n# Solve the NLP\n# Set lower/upper bounds for variables (lbx, ubx) and constraints (lbg, ubg)\nsol = solver(\n    lbx=[-ca.inf, -ca.inf], ubx=[ca.inf, ca.inf],\n    lbg=[0], ubg=[ca.inf] # g >= 0\n)\n\nprint(f\"Optimal solution x: {sol['x'].full().flatten()}\")\nprint(f\"Optimal objective f: {sol['f'].full().item()}\")","lang":"python","description":"This quickstart demonstrates how to define a simple nonlinear programming problem (NLP) using CasADi's symbolic `MX` variables, formulate it, and solve it using the IPOPT optimizer. It includes defining an objective function and a constraint."},"warnings":[{"fix":"Refer to the v3.x documentation for updated API usage, especially for creating symbolic functions and setting up solvers (e.g., `ca.Function`, `ca.nlpsol`). Migrate old `SXFunction`/`MXFunction` calls to the `ca.Function` API.","message":"Major API changes occurred between CasADi v2.x and v3.x. Code written for v2.x (e.g., using `SXFunction`, `MXFunction`, or older solver interfaces) will not directly work with v3.x.","severity":"breaking","affected_versions":"All v3.x versions (compared to v2.x)"},{"fix":"Be mindful of the type of symbolic variables you create (e.g., `ca.MX.sym('x')` vs `ca.SX.sym('x')`). For general-purpose nonlinear programming, `MX` is usually the safe choice. Explicitly convert NumPy arrays to CasADi's numerical types (`ca.DM(numpy_array)`) when interacting with CasADi functions.","message":"CasADi uses different symbolic types (`MX`, `SX`) and numerical types (`DM`, `DMatrix`). Mixing them or using the wrong type can lead to performance issues or errors. `MX` is generally recommended for complex NLPs and AD due to its flexibility, while `SX` is for highly sparse, structured problems.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Thoroughly test your symbolic expressions and functions with simple inputs before integrating into larger solvers. Pay close attention to error messages during function compilation, as they often point to issues in the symbolic formulation.","message":"CasADi functions are often JIT-compiled to C code. Errors in symbolic expressions might only surface during the compilation step when a function is constructed (e.g., `ca.Function(...)` or `ca.nlpsol(...)`), not necessarily when the symbolic expression is initially defined.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}