fit.fun 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #import std
  2. #import nat
  3. #import flo
  4. #import lin
  5. #comment -[some curve fitting functions, Copyright (C) 2007-2010 Dennis Furey]-
  6. #library+
  7. #optimize+
  8. ---------------------------------------------- interpolating function generators -------------------------------------------
  9. # each of these takes a list of points <(x,y)..> and returns a function defined for intermediate values of x
  10. one_piece_polynomial = # takes a list of points to a naive polynomial interpolation function
  11. -+
  12. "c". ~&\1.; ~&\"c"; ~&ar^?\0.! plus^/times+~&alrPrhPX ^R/~&f ^\~&art ^/~&all times+~&al,
  13. ~&x+ msolve^\~&rS ~&lSiiDlSK7tS; * =><1.> :^\~&r times+ ~&lrhPX+-
  14. mp_one_piece_polynomial = # takes a list of points in mpfr format to a naive polynomial interpolation function
  15. -+
  16. "c". ~&\1E0; ~&\"c"; ~&ar^?\0E0! mpfr..add^/mpfr..mul+~&alrPrhPX ^R/~&f ^\~&art ^/~&all mpfr..mul+~&al,
  17. ~&x+ mp_solve^\~&rS ~&lSiiDlSK7tS; * =><1E0> :^\~&r mpfr..mul+ ~&lrhPX+-
  18. sinusoid = # takes a list of points to a sinusoidal interpolation function, best behaved for equally spaced x values
  19. plus:-0.++ gang+ (+^\~&l //times+ ~&r)^*p\~&rS ~&lSiiDrlXS; * -+
  20. %fMk+ chord_fit0; //+ zeroid?/1.! div^/sin ~&,
  21. %eWLMk+ ^(~&r,times/pi+ ~&l)^*T\~&r ~&ltx; * ^\~&r negative+ ~&l,
  22. %eeLXMk; fleq*|; ~&NrxPClX; ~~ num; * ^\~&r float+ ~&l+-
  23. mp_sinusoid = # same as above for mpfr numbers
  24. mpfr..add:-0E0++ gang+ (+^\~&l //mpfr..mul+ ~&r)^*p\~&rS ~&lSiiDrlXS; * -+
  25. %fMk+ mp_chord_fit0; //+ mpfr..zero_p?/1E0! mpfr..div^/mpfr..sin ~&,
  26. %EWLMk+ ^(~&r,mpfr..mul^/~&l mpfr..pi+ mpfr..prec+ ~&r)^*T\~&r ~&ltx; * ^\~&r mpfr..mul/-1E0+ ~&l,
  27. %EZWLWMk+ %EELXMk; mpfr..lessequal_p*|; %ELWMk; ~&NrxPClX; ~~ num; * ^\~&r mpfr..nat2mp+ ~&l+-
  28. plin = # takes a list of points <(x,y)..> to a piecewise linear interpolating function
  29. ~&itB?\<'bad plin spec'>!% fleq-<&l; -+
  30. ~&r+ :-0.! ^/~&ll ?^\~&br \/fleq+ ~&rl,
  31. *ytpbbIS ^/~&ll +^(//plus+ ~&rl,+^(//times+ vid+ bus~~,\/minus+ ~&ll))+-
  32. --------------------------------- functions returning interpolating function generators ------------------------------------
  33. (# chord_fit returns a function taking a list of data points to a piecewise polynomial interpolation function with higher
  34. order polynomials for the inner intervals so that the slope at each data point is parallel to the chord intersecting the two
  35. neighboring points, the concavity is proportional to second difference, etc. limited by n. The n = 0 corresponds to a cubic
  36. polynomial. Values of n greater than 3 are usually unstable. #)
  37. chord_fit =
  38. successor; "n". %eWLMk; -+
  39. "t". ~&\"t"; ~&arv^?(
  40. fleq?alrdPX/~&falrvh2XPR ~&falrvth3XPR,
  41. ^(~&\1.+ ~&al,~&ard); ~&ar^?\0.! plus^/times+~&alrPrhPX ^R/~&f ^\~&art ^/~&all times+~&al),
  42. (%eeLDMk+ ~&r+ ~&lll2rlr2Xllr2lrPrrPNCCVX:-0+ ~&rliNVSPp)^\~&lSytp -+
  43. %eWeLXWLMk; %eLLMk+ * msolve+ ^(length+ --,~&); -+
  44. %eLLeLXMk^\~&rrPS ~&lrlPXS; * *-; * (times^/~&rl pow+ ~&lrrPX)^/~&r float~~+ ~&l,
  45. --+ zipt^~G\~&rbhhlPrDC ~&l; ~&iiDlShyCK9+iota; ^pxlrpS\~&x ~&hxPzSDrlXS; product:-1*+* take^*Dx/~&l ~&rrDlStK9+-,
  46. ^pytp/~& ~&NlrNCXSX; ->ltxK7iFS (
  47. ~&rrk&& leql\iota"n"+ ~&l,
  48. ^rrihBPSPlCrX/~&l ~&r; ~&lrithNCBBPXS+ ~&aititBPB^?\~&a :^/~&ah ~&Bahtth2Xr2O?(
  49. ^R/~&f :^\~&att &athr:=ath :^\~&athr div+ minus~~+ ~&bbIatth2hXrtthPBhYPlX2O,
  50. ~&fatPR))+-+-
  51. mp_chord_fit = # similar to above but uses mpfr numbers for greater stability; compatibility will require type conversions
  52. successor; "n". %EWLMk; -+
  53. "t". ~&\"t"; ~&arv^?(
  54. mpfr..lessequal_p?alrdPX/~&falrvh2XPR ~&falrvth3XPR,
  55. ^(~&\1E0+ ~&al,~&ard); ~&ar^?\0E0! mpfr..add^/mpfr..mul+~&alrPrhPX ^R/~&f ^\~&art ^/~&all mpfr..mul+~&al),
  56. (%EELDMk+ ~&r+ ~&lll2rlr2Xllr2lrPrrPNCCVX:-0+ ~&rliNVSPp)^\~&lSytp -+
  57. %EWELXWLMk; %ELLMk+ * mp_solve+ ^(length+ --,~&); %ELLELXMk+ -+
  58. --; ^\~&rrPS ~&lrlPXS; * *-; * ~&rlX; mpfr..mul^(mpfr..nat2mp+ ~&rl,mpfr..pow_ui+ ~&lrrPX),
  59. zipt^~G\~&rbhhlPrDC ~&l; ~&iiDlShyCK9+iota; ^pxlrpS\~&x ~&hxPzSDrlXS; product:-1*+* take^*Dx/~&l ~&rrDlStK9+-,
  60. ^pytp/~& ~&NlrNCXSX; ->ltxK7iFS (
  61. ~&rrk&& leql\iota"n"+ ~&l,
  62. ^rrihBPSPlCrX/~&l ~&r; ~&lrithNCBBPXS+ ~&aititBPB^?\~&a :^/~&ah ~&Bahtth2Xr2O?(
  63. ^R/~&f :^\~&att &athr:=ath :^\~&athr mpfr..div+ mpfr..sub~~+ ~&bbIatth2hXrtthPBhYPlX2O,
  64. ~&fatPR))+-+-
  65. (# multivariate takes an interpolating function generator such as any of those above and returns an interpolating function
  66. generator for functions of many variables. The function returned by multivariate will take an argument of the form
  67. <<x0..xn,y>..> where the x's are inputs and the y's are outputs, and will return a function that takes an input of the form
  68. <x0..xn> to the corresponding or interpolated y value. #)
  69. multivariate =
  70. "f". ~&ahtt^?\-+~&h;,"f"+ ~&ahthPXS+- -+
  71. "t". -+
  72. ^H\~&lh "f"+ ^p/~&rlS ~&rlHS+ ~&ltPrrSPD,
  73. ^/~& ~&h; ~&/"t"; ~&aldl^?\~&aldr fleq?arldl2X/~&falvh2rXPR ~&falvth3rXPR+-,
  74. %efOXLMk; -+
  75. %eZefOXLXTMk+ ~&ldll3rdlr3XNXlrNCCV:-0; ~&av^?\~&Nadr2XNV ~&avhdlr5NXfavPMV,
  76. ^VNX(~&thlPthl2X,~&)*+ %efOXLLMk+ ||~&iNC (*~ leql/iota4)+ ~&a^& :^\~&fatPR take/4+ ~&a+-,
  77. ~&alPfarPMp^J/~&f ~&a; ~&lSrSX+ fleq-<&h; |=&h; ~&hhPtSXS+-
  78. ----------------------------------------------- other relevant functions ---------------------------------------------------
  79. (# poly_dif takes a number n to a function that takes a polynomial interpolation function returned by one of the above
  80. functions to its nth derivative by modifying the virtual machine code. It should work on functions generated by either
  81. version of one_piece_polynomial and chord_fit, but not sinusoid. #)
  82. poly_dif "n" =
  83. rep"n" -|
  84. ^EZrB/~& ~&a^& -&%eeLDI,~&d&-?a\~&W ~&a; %eeLDdVRk *^0 ~&v?/~& ^V\~&v ||<0.>! ~&t+num+~&d; * times^/float+~&l ~&r,
  85. ^EZrB/~& ~&a^& -&%EELDI,~&d&-?a\~&W ~&a; *^0 ~&v?/~& ^V\~&v ||<0E0>! ~&t+num+~&d; * mpfr..mul^/mpfr..nat2mp+~&l ~&r,
  86. ~&a^& -&~&,%eLI&-?a\~&W ||<0.>! ~&t+num+~&a; * times^/float+~&l ~&r,
  87. ~&a^& -&~&,%ELI&-?a\~&W ||<0E0>! ~&t+num+~&a; * mpfr..mul^/mpfr..nat2mp+~&l ~&r|-