Differentiation

Differentiation is supported for scalar valued functions currently. This is achieved using DifferentiationInterface.jl.

using SparseGridsKit, ForwardDiff
n = 2
p(x) = [3*x[1]^3*x[2]^2]
p_prime(x) = [9*x[1]^2*x[2]^2 6*x[1]^3*x[2]]

sg = create_sparsegrid(create_smolyak_miset(n, 4))
f_on_Z = p.(get_grid_points(sg))
f_sga = SparseGridApproximation(sg, f_on_Z)
f_sg_diff = derivative(f_sga)

# Create test points
test_points = [[x,y] for x in range(-1, stop=1, length=10) for y in range(-1, stop=1, length=10)]
# Evaluate the original function and its derivative at the test points
f_test = p_prime.(test_points)
f_test_diff = f_sg_diff.(test_points)
error = abs.(sum(f_test - f_test_diff))
println("Error: $error")
Error: [6.245221353951003e-14 3.4039611407354897e-15]

Function Reference

SparseGridsKit.derivativeMethod
derivative(sg::SparseGrid, f_on_z::Vector)

Computes the derivative of a function defined on a sparse grid.

Arguments

  • sg: A sparse grid object.
  • f_on_z: A vector of function values on the sparse grid.

Returns

  • A tuple containing the sparse grid and the derivative values on the grid.
source
SparseGridsKit.derivativeMethod
derivative(sga::SparseGridApproximation)

Computes the derivative of a SparseGridApproximation object.

Arguments

  • sga: An instance of SparseGridApproximation.

Returns

-SparseGridApproximation object representing the derivative.

source
SparseGridsKit.derivativeMethod
derivative(ssg::SpectralSparseGridApproximation; sparsegrid=nothing)

Computes the derivative of a SpectralSparseGridApproximation object.

Arguments

  • ssg: An instance of SpectralSparseGridApproximation.
  • sparsegrid: Optional sparse grid to use for differentiation.

Returns

  • A SpectralSparseGridApproximation object representing the derivative.
source