R Interface to the 'SparseDiffEngine' Sparse Differentiation Backend

Bindings for the 'SparseDiffEngine' C library, the sparse Jacobian and Hessian differentiation backend used by 'CVXPY' for its Disciplined Nonlinear Programming (DNLP) extension. Provides low-level routines for building nonlinear expression graphs and evaluating sparse derivatives, intended as a backend for higher-level modeling layers such as 'CVXR'. This is the R analog of the 'sparsediffpy' Python package and wraps the same C library.


sparsediff sparsediff logo

Lifecycle: experimental

sparsediff is a thin R interface to the SparseDiffEngine C library --- the sparse Jacobian and Hessian differentiation backend used by CVXPY for its Disciplined Nonlinear Programming (DNLP) extension. It is the R analog of the Python sparsediffpy package and wraps the same C library.

You build a nonlinear expression graph from the sd_* atom constructors, assemble it into a problem, and evaluate --- at any primal point --- the objective value, its gradient, the sparse constraint Jacobian (COO), and the lower-triangular Lagrangian Hessian (COO).

This is a low-level backend. The intended user is a higher-level modelling layer such as CVXR, not someone writing models by hand.

Installation

# install.packages("remotes")
remotes::install_github("bnaras/sparsediff")

The SparseDiffEngine C sources are bundled and built with R's own toolchain and BLAS; there is nothing external to install.

Quick example

f(x) = sum(exp(x)) with one linear constraint g(x) = sum(x):

library(sparsediff)

n   <- 3L
x   <- sd_variable(d1 = n, d2 = 1L, var_id = 0L, n_vars = n)
obj <- sd_sum(sd_exp(x), axis = -1L)        # sum(exp(x))
g1  <- sd_sum(x, axis = -1L)                # sum(x)

prob <- sd_problem(obj, constraints = list(g1), verbose = FALSE)
sd_init_derivatives(prob)
sd_init_jacobian_coo(prob)
sd_init_hessian_coo(prob)

u <- c(0, 0.5, 1)
sd_objective_forward(prob, u)               # 5.367003
sd_gradient(prob)                           # exp(u): 1.000 1.649 2.718
sd_jacobian_values(prob)                    # d/dx sum(x): 1 1 1
sd_hessian_values(prob, obj_w = 1, w = 0)   # diag(exp(u))

See vignette("sparsediff") for the full walk-through, including parameters and fast re-evaluation.

License

Apache License 2.0. The bundled SparseDiffEngine is by Daniel Cederberg and William Zijie Zhang.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("sparsediff")

0.6.1 by Balasubramanian Narasimhan, 8 days ago


https://bnaras.github.io/sparsediff/, https://github.com/bnaras/sparsediff


Report a bug at https://github.com/bnaras/sparsediff/issues


Browse source code at https://github.com/cran/sparsediff


Authors: Balasubramanian Narasimhan [aut, cre] , Daniel Cederberg [aut, cph] (Author of the bundled SparseDiffEngine C library) , William Zijie Zhang [aut, cph] (Author of the bundled SparseDiffEngine C library)


Documentation:   PDF Manual  


Apache License (== 2.0) license


Suggests cpp11, knitr, rmarkdown, testthat

Linking to cpp11

System requirements: GNU make


Enhanced by CVXR.


See at CRAN