MyNixOS website logo
Description

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.

Metadata

Version

0.4.0

License

Unknown

Platforms (79)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    uefi
    wasip1
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-uefi
  • aarch64-windows
  • aarch64_be-none
  • arc-linux
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-linux
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • sh4-linux
  • vc4-none
  • wasm32-wasip1
  • wasm64-wasip1
  • x86_64-cygwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-uefi
  • x86_64-windows