Skip to content

Add automatic differentiation support to sample_points() function (Proposal Type - Dev)#40

Open
ChinmayRout9040895625 wants to merge 2 commits into
GeomScale:developfrom
ChinmayRout9040895625:feature/expose-autodiff-to-r
Open

Add automatic differentiation support to sample_points() function (Proposal Type - Dev)#40
ChinmayRout9040895625 wants to merge 2 commits into
GeomScale:developfrom
ChinmayRout9040895625:feature/expose-autodiff-to-r

Conversation

@ChinmayRout9040895625
Copy link
Copy Markdown

Summary

This PR exposes the automatic differentiation (autodiff) functionality of volesti to the R interface, allowing users to pass only function definitions without explicitly providing gradient implementations.

1. R Function Enhancements (R/sample_points.R)

  • Added use_autodiff parameter (default: FALSE for backward compatibility)
  • Added grad_function parameter for function/gradient specification
  • Added parameter validation
  • Routing logic to appropriate C++ backend

2. Rcpp Bindings (NEW FILES)

  • src/sample_points_standard.cpp: Refactored existing implementation

    • Handles standard (manual gradient) sampling path
  • src/sample_points_autodiff.cpp: NEW automatic differentiation path

    • Wraps user functions for autodiff types (autodiff::Real)
    • Integrates AutoDiffFunctor from volesti C++ library
    • Automatically computes gradients via autodiff library

3. Documentation (man/sample_points.Rd)

  • Updated function signature documentation
  • Added parameter descriptions for use_autodiff and grad_function
  • Added comprehensive examples showing both approaches

4. Examples (examples/autodiff_example.R) NEW

  • Demonstrates autodiff sampling with log-concave distribution
  • Shows how to use new use_autodiff=TRUE parameter
  • Compares autodiff vs manual gradient approaches

5. Tests (tests/testthat/test_autodiff.R) NEW

  • Unit tests for autodiff functionality
  • Validates autodiff produces valid samples
  • Compares autodiff vs manual gradient (should be identical with same seed)
  • Tests with different polytope types
  • Parameter validation tests

User-Facing Changes

Before (still works):

# Users had to manually compute gradient
neg_log_prob <- function(x) sum(x^2) / 2
neg_grad <- function(x) -x

samples <- sample_points(P, grad_function = neg_grad, N=1000)

**After**

# Users can now just define the function
neg_log_prob <- function(x) sum(x^2) / 2

# Gradient computed automatically!
samples <- sample_points(P, use_autodiff=TRUE, 
                        grad_function=neg_log_prob, N=1000)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant