|
| 1 | +# SciPy Wrappers |
| 2 | + |
| 3 | +NonlinearSolve provides thin wrappers around selected algorithms from the |
| 4 | +[SciPy](https://scipy.org/) Python library via |
| 5 | +[`PythonCall.jl`](https://github.com/cjdoris/PythonCall.jl). They allow you to |
| 6 | +call the original `scipy.optimize` routines while keeping the standard |
| 7 | +`SciMLBase`‐style problem / solution interface. |
| 8 | + |
| 9 | +!!! note "Python dependency" |
| 10 | + These algorithms require a working Python installation with the `scipy` |
| 11 | + package available. When the wrapper is first used, `PythonCall` will |
| 12 | + automatically create a private Conda environment and install SciPy via |
| 13 | + `CondaPkg`. No manual setup is necessary on typical CI or end‐user |
| 14 | + systems. |
| 15 | + |
| 16 | +## Algorithms |
| 17 | + |
| 18 | +| Julia type | SciPy function | Problem type | |
| 19 | +|--------------------------------|------------------------------------|--------------| |
| 20 | +| `SciPyLeastSquares` (default) | `scipy.optimize.least_squares` | nonlinear least-squares | |
| 21 | +| `SciPyLeastSquaresTRF` | `scipy.optimize.least_squares` with `method="trf"` | nonlinear least-squares | |
| 22 | +| `SciPyRoot` | `scipy.optimize.root` | vector root-finding | |
| 23 | +| `SciPyRootScalar` | `scipy.optimize.root_scalar` | scalar bracketing root | |
| 24 | + |
| 25 | +!!! warning "Single-process restriction" |
| 26 | + SciPy itself is written in C/Fortran and **is not thread-safe across |
| 27 | + multiple embedded Python interpreters**. For that reason the |
| 28 | + `NonlinearSolveSciPy` test suite runs single-process; you should avoid |
| 29 | + launching many Julia processes that each call SciPy in parallel. |
| 30 | + |
| 31 | +## Basic usage |
| 32 | + |
| 33 | +```julia |
| 34 | +using NonlinearSolve, NonlinearSolveSciPy |
| 35 | + |
| 36 | +# --- nonlinear least-squares --------------------------------------------- |
| 37 | + |
| 38 | +xdata = 0:0.1:1 |
| 39 | +ydata = 2 .* xdata .+ 1 # exact line, no noise |
| 40 | + |
| 41 | +residuals(p, _) = ydata .- (p[1] .* xdata .+ p[2]) |
| 42 | +prob = NonlinearLeastSquaresProblem(residuals, [1.0, 0.0]) |
| 43 | + |
| 44 | +sol = solve(prob, SciPyLeastSquares()) # defaults to method="trf" |
| 45 | +``` |
| 46 | + |
| 47 | +```julia |
| 48 | +# --- vector root-finding -------------------------------------------------- |
| 49 | + |
| 50 | +f(u, p) = [2 - 2u[1]; u[1] - 4u[2]] |
| 51 | +prob_vec = NonlinearProblem(f, zeros(2)) |
| 52 | +sol_vec = solve(prob_vec, SciPyRoot()) |
| 53 | +``` |
| 54 | + |
| 55 | +```julia |
| 56 | +# --- scalar bracketing root ---------------------------------------------- |
| 57 | + |
| 58 | +g(x, p) = x^2 - 2 |
| 59 | +prob_scalar = IntervalNonlinearProblem(g, (1.0, 2.0)) |
| 60 | +sol_scalar = solve(prob_scalar, SciPyRootScalar()) |
| 61 | +``` |
| 62 | + |
| 63 | +All three calls return a standard `SciMLBase.NonlinearSolution` with the usual |
| 64 | +`u`, `resid`, `retcode`, etc. If SciPy raises an error the wrapper translates |
| 65 | +it to an appropriate `ReturnCode` and propagates the original Python message |
| 66 | +through `solution.message`. |
| 67 | + |
| 68 | +## Keyword options |
| 69 | + |
| 70 | +The constructors forward most keywords verbatim to the underlying SciPy |
| 71 | +functions. Refer to the [SciPy documentation](https://docs.scipy.org/doc/) for |
| 72 | +exhaustive lists. |
| 73 | + |
| 74 | +Examples: |
| 75 | + |
| 76 | +```julia |
| 77 | +# switch least-squares loss function |
| 78 | +solve(prob, SciPyLeastSquares(loss="soft_l1")) |
| 79 | + |
| 80 | +# change the scalar root solver method |
| 81 | +solve(prob_scalar, SciPyRootScalar(method="brentq")) |
| 82 | +``` |
| 83 | + |
| 84 | +## Implementation notes |
| 85 | + |
| 86 | +The wrapper lives in the standalone sub-package `NonlinearSolveSciPy` rather |
| 87 | +than as a conditional extension. This lets its code be developed and tested |
| 88 | +independently while still integrating smoothly with the main |
| 89 | +`NonlinearSolve.jl` umbrella package. |
0 commit comments