cop.fun 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #import std
  2. #import nat
  3. #import flo
  4. #comment -[
  5. These functions may be useful in conjunction with non-linear
  6. optimization by the minpack and kinsol libraries.
  7. Copyright (C) 2007-2010 Dennis Furey]-
  8. #library+
  9. #optimize+
  10. ------------------------------------------------------- jacobians -----------------------------------------------------------
  11. (# These construct jacobian functions numerically using the gsl differentiation library functions, which might be better than
  12. a finite difference method but not as good as an analytical form. jacobian_row is useful for solvers that require only one
  13. row of the jacobian matrix at a time. #)
  14. t_derivative = // \/gsldif..t_central
  15. jacobian = # takes (n,m) to a function mapping f: R^m -> R^n to j: R^m -> R^(n x m)
  16. +^\(gang+ (//+)*+ ~&t;|\+ ~&h!*+ iota+ ~&l) gang++ *+ ~&r; gang++ gang+ iota; * "n". "f". "x". ~&H(
  17. derivative "f"+ take("n","x")--+ \/~&lritBPC skip/"n" "x",
  18. ~&h skip/"n" "x")
  19. jacobian_row = # takes n to a function mapping f: R^m -> R^n to j: ({0..n-1} x R^m) -> R^m
  20. iota; \/-*; gang++ //+ * ("f","i"). ("j","x"). ~&H(
  21. derivative ~&h+ skip/"j"+ "f"+ take("i","x")--+ \/~&lritBPC skip/"i" "x",
  22. ~&h skip/"i" "x")
  23. ------------------------------------------------------ changes of variables -------------------------------------------------
  24. (# half_line smoothly maps negatives to the interval 0..1 and positives to 1..inf, with a fixed point at infinity. The
  25. constant appearing in the formula is the empirically determined maximum value consistent with monotonic convergence (within
  26. machine precision). under takes a number to a function that maps the real line to the half line under that number. over takes
  27. a number to a function that maps the real line to the half line over that number. Between takes a pair of numbers to a
  28. function that maps the real line to the interval between the numbers, with unit slope and a fixed point at the center. The
  29. lesser number should be on the left. chov takes a list of pairs of numbers to a function that maps a list of numbers to a list
  30. of numbers between the corresponding intervals. #)
  31. half_line = times^(div\2.+ plus/1.+ math..tanh+ div\2.60080714,math..hypot/2.)
  32. under = "r". minus/"r"+ half_line+ plus/"r"+ negative
  33. over = "l". plus/"l"+ half_line+ minus\"l"
  34. between =
  35. ninf?=l/(inf?=r/~&! under+~&r) inf?=r/over+~&l -+
  36. ("c","w"). plus/"c"+ times/"w"+ math..tanh+ div\"w"+ minus\"c",
  37. (div\2.)^~/plus abs+ minus+-
  38. chov = gang+ ~&t;|\+ ~&h;+* between