Guest User

Untitled

a guest
May 24th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1.  
  2. (import (rnrs)
  3. (durand-kerner))
  4.  
  5. (define (vectors-index2 proc a b)
  6. (let ((n (vector-length a)))
  7. (let loop ((i 0))
  8. (cond ((>= i n) #f)
  9. ((proc (vector-ref a i)
  10. (vector-ref b i))
  11. i)
  12. (else (loop (+ i 1)))))))
  13.  
  14. ;; (define (~ a b)
  15. ;; (< (abs (- a b)) 1e-15))
  16.  
  17. (define (~ a b)
  18. (let ((c (- a b)))
  19. (and (< (abs (real-part c)) 1e-10)
  20. (< (abs (imag-part c)) 1e-10))))
  21.  
  22. (define (vectors~ a b)
  23. (not
  24. (vectors-index2 (lambda (x y)
  25. (not (~ x y)))
  26. a
  27. b)))
  28.  
  29. ;; (define (vectors~ a b)
  30. ;; (let ((n (vector-length a)))
  31. ;; (let loop ((i 0))
  32. ;; (cond ((>= i n) #t)
  33. ;; ((~ (vector-ref a i)
  34. ;; (vector-ref b i))
  35. ;; (loop (+ i 1)))
  36. ;; (else #f)))))
  37.  
  38. (assert
  39. (vectors~
  40. (roots
  41. (vector 1.0+0.0i -3.0+0.0i 2.0+0.0i))
  42. '#(1.0-0.0i 2.0+0.0i)))
  43.  
  44. (assert
  45. (vectors~
  46. (roots
  47. (vector 1 -3 2))
  48. '#(1.0-0.0i 2.0+0.0i)))
  49.  
  50. (assert
  51. (vectors~
  52. (roots
  53. (vector 1 -3 2))
  54. '#(1 2)))
  55.  
  56. (assert
  57. (vectors~
  58. (roots
  59. (vector 1.0+0.0i -2.0+0.0i 1.0+0.0i))
  60. '#(1.0-0.0i 1.0-2.220446049250313e-16i)))
  61.  
  62. (assert
  63. (vectors~
  64. (roots
  65. (vector 1.0+0.0i 0 4.0+0.0i))
  66. '#(0.0-2.0i 0.0+2.0i)))
  67.  
  68. (assert
  69. (vectors~
  70. (roots
  71. (vector 4.0+0.0i 0 1.0+0.0i))
  72. '#(0.0-0.5i 0.0+0.5i)))
  73.  
  74. (assert
  75. (vectors~
  76. (roots
  77. (vector 1.0+0.0i -3.0+0.0i 2.0+0.0i 0))
  78. '#(1.0+0.0i 2.0+0.0i 0.0+0.0i)))
  79.  
  80. (assert
  81. (vectors~
  82. (roots
  83. (vector 1 0 -13 0 36))
  84. '#(2.0-5.994355921380061e-33i
  85. 3.0-1.7219155529623352e-41i
  86. -2.0+3.4258111894079275e-17i
  87. -3.0-6.595538179953504e-17i)))
  88.  
  89. (assert
  90. (vectors~
  91. (roots
  92. (vector 1 0 -13 0 36 0))
  93. '#(-2.0+7.174648137343064e-43i
  94. 2.9999999999999996+1.2037062152420224e-34i
  95. -3.0-1.4349296274686127e-42i
  96. 2.350988701644575e-38-2.350988701644575e-38i
  97. 2.0+1.6851887013388314e-34i)))
  98.  
  99. (assert
  100. (vectors~
  101. (roots
  102. (vector 1 1 1))
  103. '#(-0.5-0.8660254037844387i -0.5+0.8660254037844386i)))
  104.  
  105. (assert
  106. (vectors~
  107. (roots
  108. (vector 1 1 1 1))
  109. '#(-8.481300776135252e-18-1.0i 8.475424015331716e-18+1.0i -1.0+0.0i)))
  110.  
  111. (assert
  112. (vectors~
  113. (roots
  114. (vector 1 1 1 1 1))
  115. '#(0.3090169943749475-0.9510565162951535i
  116. 0.3090169943749475+0.9510565162951535i
  117. -0.8090169943749473+0.5877852522924731i
  118. -0.8090169943749475-0.5877852522924731i)))
  119.  
  120. ;; " x^6 + 4*x^5 - 72*x^4 - 214*x^3 + 1127*x^2 + 1602*x - 5040 "
  121.  
  122. (assert
  123. (vectors~
  124. (roots
  125. (vector 1 4 -72 -214 1127 1602 -5040))
  126. '#(1.9999999999999993+0.0i
  127. 7.0+0.0i
  128. 3.000000000000001+0.0i
  129. -3.0+0.0i
  130. -8.000000000000002+0.0i
  131. -4.999999999999998+0.0i)))
Add Comment
Please, Sign In to add comment