Skip to contents

Convert a function from multiple function arguments to a single function

Usage

make_share(func, arg_fn, arg_gr)

Arguments

func

The function that takes in two function arguments

arg_fn

The function (first) argument name of func

arg_gr

The gradient (second) argument name of func

Value

A new function that evaluates the two arguments together

Examples

quad_share <- function(x){list(sum(x^4), 4*x^3)}
lbfgs_share <- make_share(lbfgs::lbfgs, 'call_eval', 'call_grad')
make_share(lbfgs::lbfgs, 'call_eval', 'call_grad')(quad_share, vars=c(5,-4))
#> Iteration 1: 
#> fx = 443.11
#> 
#>   xnorm = 5.42706, gnorm = 329.884, step = 0.00178023
#> 
#> Iteration 2: 
#> fx = 122.702
#> 
#>   xnorm = 3.9407, gnorm = 125.586, step = 1
#> 
#> Iteration 3: 
#> fx = 42.3111
#> 
#>   xnorm = 3.01937, gnorm = 56.5329, step = 1
#> 
#> Iteration 4: 
#> fx = 13.4228
#> 
#>   xnorm = 2.26605, gnorm = 23.896, step = 1
#> 
#> Iteration 5: 
#> fx = 4.39775
#> 
#>   xnorm = 1.71442, gnorm = 10.3483, step = 1
#> 
#> Iteration 6: 
#> fx = 1.42312
#> 
#>   xnorm = 1.29306, gnorm = 4.43992, step = 1
#> 
#> Iteration 7: 
#> fx = 0.462725
#> 
#>   xnorm = 0.976426, gnorm = 1.91178, step = 1
#> 
#> Iteration 8: 
#> fx = 0.150178
#> 
#>   xnorm = 0.736989, gnorm = 0.822054, step = 1
#> 
#> Iteration 9: 
#> fx = 0.0487752
#> 
#>   xnorm = 0.556363, gnorm = 0.353667, step = 1
#> 
#> Iteration 10: 
#> fx = 0.015837
#> 
#>   xnorm = 0.419978, gnorm = 0.152125, step = 1
#> 
#> Iteration 11: 
#> fx = 0.0051427
#> 
#>   xnorm = 0.317035, gnorm = 0.0654393, step = 1
#> 
#> Iteration 12: 
#> fx = 0.00166991
#> 
#>   xnorm = 0.239322, gnorm = 0.0281491, step = 1
#> 
#> Iteration 13: 
#> fx = 0.000542252
#> 
#>   xnorm = 0.180659, gnorm = 0.0121086, step = 1
#> 
#> Iteration 14: 
#> fx = 0.000176079
#> 
#>   xnorm = 0.136375, gnorm = 0.00520865, step = 1
#> 
#> Iteration 15: 
#> fx = 5.71759e-05
#> 
#>   xnorm = 0.102947, gnorm = 0.00224055, step = 1
#> 
#> Iteration 16: 
#> fx = 1.85661e-05
#> 
#>   xnorm = 0.0777121, gnorm = 0.000963794, step = 1
#> 
#> Iteration 17: 
#> fx = 6.02873e-06
#> 
#>   xnorm = 0.0586631, gnorm = 0.000414586, step = 1
#> 
#> Iteration 18: 
#> fx = 1.95764e-06
#> 
#>   xnorm = 0.0442835, gnorm = 0.000178338, step = 1
#> 
#> Iteration 19: 
#> fx = 6.3568e-07
#> 
#>   xnorm = 0.0334286, gnorm = 7.67138e-05, step = 1
#> 
#> Iteration 20: 
#> fx = 2.06417e-07
#> 
#>   xnorm = 0.0252345, gnorm = 3.29992e-05, step = 1
#> 
#> Iteration 21: 
#> fx = 6.70272e-08
#> 
#>   xnorm = 0.019049, gnorm = 1.41949e-05, step = 1
#> 
#> Iteration 22: 
#> fx = 2.17649e-08
#> 
#>   xnorm = 0.0143796, gnorm = 6.10608e-06, step = 1
#> 
#> L-BFGS optimization terminated with status code = 0
#> fx = 2.17649e-08
#> 
#> $value
#> [1] 2.176494e-08
#> 
#> $par
#> [1]  0.010830524 -0.009459067
#> 
#> $convergence
#> [1] 0
#>