Advertisement
qwerty1000000

fi

Feb 23rd, 2024 (edited)
773
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
REBOL 4.59 KB | Source Code | 0 0
  1. print "fi"
  2.  
  3. sqrt: :square-root
  4.  
  5. fi: (1 + sqrt 5) / 2
  6.  
  7. x: 1 + fi
  8.  
  9. {
  10.  
  11. 1 3  7  18 47 123
  12.  
  13. 1 =
  14. 2 = 3 - 1
  15. 3 =
  16. 4 = 3 + 1       >1 , <=4    , f 4 = [ 3 1]      
  17. --------------                
  18. 5 = 7 - 3 + 1 ,  >4 , >=11 , f 5  = [7 -3 1]
  19. 6 = 7 - 1                  , f 6 =  [7 -1]
  20. 7 =
  21. 8 = 7 + 1
  22. 9 = 7 + 3 - 1
  23. 10 = 7 + 3
  24. 11 = 7 + 3 + 1              . f 11 = [7 3 1]
  25. ------------------
  26. 12 = 18 - 7 + 1   >11 , <=29  , f 12 = [18 -7 1]
  27. 13
  28. 14
  29. 15
  30. 16 = 18 - 3 + 1
  31. 17 = 18 - 1
  32. 18 = 18
  33. 19 = 18 + 1
  34. 29 = 18 + 7 + 3  + 1
  35. --------------------------
  36. 47 =
  37. }
  38.  
  39. index: [ 0 1 3  7  18 47 123 ]
  40.  
  41. m: 0
  42.  
  43. limit: clear copy [] ; [1 4 11 29 76 199]
  44.  
  45. foreach n index [ m: m + n  insert tail limit m ]
  46.  
  47.  
  48.  
  49. ;---------------------------------
  50.  
  51. f: func [ n /local m tmp i][
  52.  
  53.   tmp: clear copy []
  54.  
  55.   i: 1
  56.  
  57.   for m -1 + length? limit 1  -1 [
  58.  
  59. if all [ n > limit/:m n <= limit/(m + 1) ][n: n - index/(m + 1 )  insert tail tmp   index/(m + 1) * i  if n < 0 [ i: i * -1 ] n: abs n ]
  60.  
  61. ]
  62. return tmp
  63. ]
  64.  
  65. g: func [ v /local a b y n ][ a: 0 b: 0 n: 0 y: x  foreach  m v [ n: n + 1 if all [ n = length? v 1 = abs m ][y: 1]    a: a +  ((sign? m) * power y abs m) b: b +  ((sign? m) / power y  abs m ) ] return a * b ]
  66.  
  67. h: func [ v /local a b y n ][ a: 0 b: 0 n: 0 y: x  foreach  m v [   a: a +  ((sign? m/1) * power y abs m/2) b: b +  ((sign? m/1) / power y  abs m/2 ) ] return a * b ]
  68.  
  69. val: h [[1 4] [1 2] [1 0]] ; = 64
  70.  
  71. source val
  72.  
  73.  
  74. {
  75. "x^^4+x^^2+1"
  76.  
  77. h [[1 4] [1 2] [1 0]]
  78.  
  79. (( power x 4) + (power x 2) + 1 ) * ((1 / power x 4) + (1 / power x 2) + 1 ) = 64 , g [ 4 2 1]
  80. }
  81.  
  82. {
  83.  
  84. ((power x 2) - 1 ) * ((1 / power x 2) - (1 / 1 )) = -5 , (x^2-1)(1/x^2-1) = -x^2+2 . [ 2 -1] , h [ [1 2] [-1 0]]
  85. ((power x 2) + 1 ) * ((1 / power x 2) + (1 / 1 )) = 9 ,  (x^2+1)(1/x^2+1) =  x^2+2 , [ 2 1] ,  h [ [1 2] [1 0]]
  86. ((power x 3) - 1 ) * ((1 / power x 3) - (1 / 1 )) = -16 ,(x^3-1)(1/x^3-1) = -x^3+2 , [ 3 -1] , h [ [1 3] [-1 0]]
  87. ((power x 3) + 1 ) * ((1 / power x 3) + (1 / 1 )) = 20  ,(x^3+1)(1/x^3+1) =  x^3+2  , [ 3 1 ] , h [ [1 3] [1 0]]
  88. ((power x 4) - 1 ) * ((1 / power x 4) - (1 / 1 )) = -45                             , [ 4 -1]
  89. (power x 4) + 1 ) * ((1 / power x 4) + (1 / 1 )) = 49                               , [ 4 1
  90.  
  91. }
  92.  
  93. ;------------------------------------------------------
  94.  
  95.  
  96. vec-to-str: func [ v /local s n ][n: 0 s: clear copy {} foreach m v [
  97.  
  98.  n: n + 1
  99.  
  100.  either  m/1 < 0 [ insert tail s "-" ] [if n <> 1 [insert tail s "+" ]]
  101.  
  102.  insert  tail s ajoin [ either 1 = abs m/1 [clear copy "" ] [ajoin [ m/1 "*"] ] either 0 = m/2 ["1"][ ajoin ["x^^" m/2]]]]
  103.  
  104. return s ]
  105.  
  106. s: vec-to-str [ [ 1 3] [2 2 ] [ -1 0 ]] ; "x^^3+2*x^^2-1"
  107. source s
  108.  
  109. s: vec-to-str [ [ 1 3] [-1 2 ] [ 1 0 ]] ; "x^^3-x^^2+1"
  110. source s
  111. ;-----------------------------------------------------------------------------------
  112.  
  113. str-to-vec: func [ s /local v i a b int ][
  114.  
  115. i: 0 v: clear copy []
  116.  
  117. int: charset "0123456789"
  118.  
  119. parse/all s [some [["-" (i: -1  )| "+" (i: 1) | (i: 1) ][ copy a some int "*x" | "x" (a: 1 ) | copy a some int] (a: i * to-integer a ) "^^"  copy b some int (b: to-integer b insert tail v reduce [reduce [ a b ]] )] opt END (insert tail v reduce [reduce [a 0] ]) ] return v ]
  120.  
  121. v: str-to-vec "x^^3+2*x^^2-1" ; [[1 3] [2 2] [-1 0]]
  122. source v
  123.  
  124. v: str-to-vec "x^^3-x^^2+1" ; [[1 3] [-1 2] [ 1 0]]
  125. source v
  126.  
  127. ;-------------------------------------------------------------------------------------
  128.  
  129. inv-vec: func [ v /local u m o ][
  130.  
  131. u: clear copy []
  132.  
  133. o: v/1/2
  134.  
  135.  foreach m v [ insert tail u reduce [reduce [ m/1 o - m/2]]]
  136.  
  137. sort/compare u func [ a b ][ a/2 > b/2]
  138.  
  139. return u ]
  140.  
  141. v: inv-vec [[1 4] [1 2] [1 0]]
  142.  
  143. source v
  144. ;--------------------------------------------------------
  145.  
  146.  
  147. mul-vec: func [ v1 v2 /local v3 v4 k l n m][
  148.  
  149. v3: clear copy []
  150.  
  151. v4: clear copy []
  152.  
  153.  
  154. for k 1 length? v1 1 [
  155.  
  156. for l 1 length? v2 1 [
  157.  
  158. insert tail v3 reduce [reduce [v1/:k/1 * v2/:l/1 v1/:k/2 + v2/:l/2  ]]
  159.  
  160. ] ]
  161.  
  162. sort/compare v3 func [ a b ][ a/2 > b/2]
  163.  
  164. source v3
  165.  
  166. a: v3/1/1
  167. b: v3/1/2
  168.  
  169. until [
  170.  
  171. remove v3
  172.  
  173. attempt [
  174.  
  175. either b = v3/1/2 [ a: a + v3/1/1 ] [insert tail v4 reduce [ reduce [a b ] ]
  176.  
  177. a: v3/1/1
  178. b: v3/1/2
  179.  
  180. ]]
  181.  
  182. empty? v3
  183. ]
  184.  
  185. return v4
  186.  
  187. ]
  188.  
  189. ;--------------------------------------------
  190.  
  191. v: mul-vec [[1 4] [1 2] [1 0]] [[1 4] [1 2] [1 0]] ; [[1 8] [1 6] [1 6] [1 4] [1 4] [1 4] [1 2] [1 2] [1 0]] = [[1 8] [ 2 6] [3 4]  [2 2] [1 0]] = x^4 + 2 x^2 + 3
  192.  
  193. source v
  194.  
  195. ;---------------------
  196.  
  197. halt
  198. {
  199.  
  200. (1+x^2+x^4)*(1+1/x^2+1/x^4) =(x^4 + x^2 + 1)^2/x^4 = x^2+2x+3
  201.  
  202. "x^^4+x^^2+1" [[1 4] [1 2] [1 0]]
  203.  
  204. (power x 3) - (power x 2) - (power x ) 1 ) * ((1 / power x 4) + (1 / 1 ))
  205.  
  206. }
  207.  
  208.  
  209. halt
Tags: fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement