| Title: | R Interface to the 'SparseDiffEngine' Sparse Differentiation Backend |
|---|---|
| Description: | 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. |
| 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) |
| Maintainer: | Balasubramanian Narasimhan <[email protected]> |
| License: | Apache License (== 2.0) |
| Version: | 0.4.0 |
| Built: | 2026-06-04 19:30:06 UTC |
| Source: | https://github.com/bnaras/sparsediff |
Returns the version string of the SparseDiffEngine C library bundled with this package.
engine_version()engine_version()
A character scalar, e.g. "0.3.0".
engine_version()engine_version()
Affine combinations and shape manipulations of expressions. These have constant (zero) second derivatives but participate in the Jacobian.
left, right, child, c
|
expression handles. |
d1, d2
|
target row and column dimensions (for |
indices |
0-based column-major flat indices selected by |
args |
a list of expression handles to stack
( |
n_vars |
total number of variables in the problem. |
axis |
reduction axis for |
sd_addelementwise sum of two expressions.
sd_sumsum reduction along axis.
sd_tracematrix trace.
sd_transposematrix transpose.
sd_diag_vecdiagonal matrix from a vector.
sd_diag_matdiagonal vector from a matrix.
sd_upper_trithe strict upper-triangular entries.
sd_promotepromote a scalar to shape d1 x d2.
sd_reshapereshape to d1 x d2 (column-major).
sd_broadcastbroadcast to d1 x d2.
sd_indexselect entries by 0-based flat indices.
sd_hstack, sd_vstack
horizontal / vertical stacking.
An expression handle.
sparsediff-elementwise, sparsediff-matrix
Functions of two expression arguments.
l, r, x, y
|
expression handles. |
sd_elementwise_multelementwise (Hadamard) product.
sd_matmulmatrix product .
sd_quad_over_linthe quadratic-over-linear .
sd_rel_entrelementwise relative entropy .
sd_rel_entr_first_scalar, sd_rel_entr_second_scalar
relative entropy with a scalar first or second argument broadcast against the other.
An expression handle.
sparsediff-elementwise, sparsediff-reduction
Smooth elementwise functions of a single expression. Each returns an expression of the same shape as its argument.
child, c
|
an expression handle (the argument). |
p |
exponent for |
sd_exp, sd_log
exponential and natural logarithm.
sd_sin, sd_cos, sd_tan
trigonometric functions.
sd_sinh, sd_tanh, sd_asinh, sd_atanh
hyperbolic and inverse-hyperbolic functions.
sd_logisticthe logistic .
sd_xexp.
sd_normal_cdfthe standard normal CDF.
sd_entrthe elementwise entropy .
sd_powerthe power .
sd_negnegation .
An expression handle.
sparsediff-affine, sparsediff-bivariate
Create the leaves of an expression graph. A variable is a slice of the
differentiation vector; a parameter is fixed data that can be updated between
evaluations (see sd_register_params).
d1, d2
|
row and column dimensions of the leaf. |
var_id |
0-based flat (column-major) offset of this variable's first
entry within the primal vector |
param_id |
0-based parameter offset, analogous to |
n_vars |
total number of variables in the problem. |
values |
numeric data for the parameter (length |
An expression handle.
sparsediff-elementwise, sd_problem
Operations that combine an expression with fixed data — a registered parameter node or a constant matrix — so the data can flow through the differentiated graph (and, for parameters, be updated between evaluations).
param |
a parameter expression handle (see |
child |
an expression handle (the variable argument). |
Qp, Qi, Qx
|
the column-pointer, row-index and value arrays of a
compressed-sparse-column matrix |
Ap, Ai, Ax
|
the compressed-sparse-column arrays of a constant matrix
|
ncol |
number of columns of the sparse constant matrix |
m, n
|
row and column dimensions of the dense constant matrix. |
data |
the dense constant-matrix entries (length |
sd_scalar_mult, sd_vector_mult
multiply a child by a scalar / vector parameter.
sd_convolveconvolution of a parameter kernel with a child.
sd_quad_formthe quadratic form with sparse
constant .
sd_left_matmul, sd_right_matmul
left / right product
with a sparse constant matrix .
sd_left_matmul_dense, sd_right_matmul_dense
left / right product with a dense constant matrix.
An expression handle.
sd_parameter, sd_register_params
Initialise and evaluate the value, gradient, sparse constraint Jacobian and
sparse lower-triangular Lagrangian Hessian of a problem built with
sd_problem.
prob |
a problem handle from |
u |
a numeric vector: the primal point at which to evaluate, laid out in
the engine's column-major flat ordering (the same ordering used by the
|
obj_w |
a scalar weight |
w |
a numeric vector of constraint multipliers (length equal to the total constraint size) weighting the constraint Hessians. |
Evaluation is ordered: a forward pass first
(sd_objective_forward / sd_constraint_forward) populates the
node values at u, after which sd_gradient,
sd_jacobian_values and sd_hessian_values read them. Sparsity
patterns are structural — fixed once the corresponding sd_init_*
routine has run — so they are queried once and reused, while the values are
recomputed at each new point. Row and column indices are 0-based (the engine
convention; a higher-level modelling layer such as CVXR translates them
to 1-based as needed).
sd_init_derivatives, sd_init_jacobian,
sd_init_jacobian_coo, sd_init_hessian_coo
called for their
side effect; return NULL invisibly.
sd_objective_forwardthe scalar objective value at u.
sd_constraint_forwardthe constraint vector at u.
sd_gradientthe objective gradient, length n_vars.
sd_jacobian_sparsity, sd_hessian_sparsity
a list with
integer rows/cols (0-based COO indices) and nrow/ncol.
sd_jacobian_valuesthe constraint-Jacobian nonzeros, matching the Jacobian sparsity order.
sd_hessian_valuesthe nonzeros of
, lower triangle, matching
the Hessian sparsity order.
Combine an objective expression and a list of constraint expressions
(built with the sd_* atom constructors) into a single problem object
whose value and sparse derivatives can be evaluated repeatedly.
objective |
an expression handle for the scalar objective. |
constraints |
a list of expression handles, stacked vertically to form
the constraint vector |
verbose |
logical; if |
prob |
a problem handle returned by |
params |
a list of parameter expression handles (see |
theta |
a numeric vector of new parameter values, concatenated in the order the parameters were registered. |
The typical lifecycle is: build expressions with the sd_* constructors,
assemble them with sd_problem(), initialise the derivative structures
once (sd_init_derivatives and friends), then evaluate the
objective/constraints and their sparse derivatives at as many primal points
as needed. When the problem has parameters, register them once with
sd_register_params() and push new values with sd_update_params()
to re-evaluate without rebuilding the graph (the DPP-style fast path).
sd_problem() returns an external-pointer problem handle. The
handle owns the underlying expression graph and frees it when garbage
collected. sd_register_params() and sd_update_params() are
called for their side effect and return NULL invisibly.
sd_init_derivatives for the evaluation oracle,
sd_variable and the atom constructors for building expressions.
Multiplicative reductions of an expression.
c |
an expression handle. |
sd_prodproduct of all entries.
sd_prod_axis_zerocolumn-wise products (reduce down rows).
sd_prod_axis_onerow-wise products (reduce across columns).
An expression handle.