Module: Magick::Maths
- Defined in:
- lib/magick/maths.rb
Class Method Summary collapse
-
.add ⇒ Object
for addition, we’ll just use Ruby.
- .average ⇒ Object
- .div_to_f ⇒ Object
- .divide ⇒ Object (also: div)
- .exp ⇒ Object
- .fibonacci ⇒ Object
-
.fibonacci_matrix_multiply ⇒ Object
x and y are pairs of ints.
- .mod ⇒ Object
- .multiply ⇒ Object (also: mult)
- .negate ⇒ Object
- .power ⇒ Object
- .power_2 ⇒ Object
- .power_accumulate ⇒ Object
-
.power_accumulate_positive ⇒ Object
power, as written, will have a stack trace error if we try to do something like power.(mult).(2).(32) If we eliminate the recursion, we can avoid this, but it means we need to use some regular Ruby constructs (eg, ‘while`, instead of the Y combinator) The following is essentially how Stepanov & McJones define `power` in Elements of Programming.
- .powers_of_two ⇒ Object
- .subtract ⇒ Object
Class Method Details
.add ⇒ Object
for addition, we’ll just use Ruby
7 |
# File 'lib/magick/maths.rb', line 7 def add = ->(x){ ->(y) { x + y }} |
.average ⇒ Object
130 131 132 133 134 135 |
# File 'lib/magick/maths.rb', line 130 def average Smullyan::Birds::Phi. (Magick::Maths::div_to_f). (Magick::Loops::sum). (Magick::Loops::length) end |
.div_to_f ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/magick/maths.rb', line 29 def div_to_f ->(n){ ->(d){ # in order to get a float, we'll just # use Ruby's `/` operator, and make sure # at least one argument is cast to float. n / d.to_f } } end |
.divide ⇒ Object Also known as: div
18 19 20 21 22 23 24 25 26 |
# File 'lib/magick/maths.rb', line 18 def divide Smullyan::Birds::Y.(->(f){ ->(n){ ->(d){ n < d ? 0 : 1 + f.(n - d).(d) } } }) end |
.exp ⇒ Object
50 51 52 |
# File 'lib/magick/maths.rb', line 50 def exp power_2.(multiply) end |
.fibonacci ⇒ Object
151 152 153 154 155 156 |
# File 'lib/magick/maths.rb', line 151 def fibonacci ->(n){ n == 0 ? 0 : power_2.(fibonacci_matrix_multiply).([1,0]).(n)[0] } end |
.fibonacci_matrix_multiply ⇒ Object
x and y are pairs of ints
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/magick/maths.rb', line 140 def fibonacci_matrix_multiply = ->(x){ ->(y){ [ x[0] * (y[1] + y[0]) + x[1] * y[0], x[0] * y[0] + x[1] * y[1] ] } } |
.mod ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/magick/maths.rb', line 40 def mod Smullyan::Birds::Y.(->(f){ ->(n){ ->(d){ n < d ? n : f.(n - d).(d) } } }) end |
.multiply ⇒ Object Also known as: mult
13 14 15 |
# File 'lib/magick/maths.rb', line 13 def multiply power_2.(add) end |
.negate ⇒ Object
9 |
# File 'lib/magick/maths.rb', line 9 def negate = ->(x) { -x } |
.power ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/magick/maths.rb', line 54 def power # power needs a base, an exponent, and an # operation. Powers of addition is equivalent # to multiplication; powers of multiplication # is equivalent to exponentiation ->(op) { ->(base) { ->(exponent) { Smullyan::Birds::Y.(->(f) { ->(b) { ->(e) { e == 1 ? b : mod.(e).(2) == 0 ? f.(op.(b).(b)).(divide.(e).(2)) : op.(f.(op.(b).(b)).(divide.(e).(2))).(b) } } }).(base).(exponent) } } } end |
.power_2 ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/magick/maths.rb', line 114 def power_2 ->(op){ ->(b){ ->(e){ while (e % 2 == 0) b = op.(b).(b) e = e/2 end e = e / 2 return b if e == 0 power_accumulate.(op).(b).(op.(b).(b)).(e) } } } end |
.power_accumulate ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/magick/maths.rb', line 101 def power_accumulate ->(op){ ->(r){ ->(n){ ->(a){ return r if n == 0 power_accumulate_positive.(op).(r).(n).(a) } } } } end |
.power_accumulate_positive ⇒ Object
power, as written, will have a stack trace error if we try to do something like power.(mult).(2).(32) If we eliminate the recursion, we can avoid this, but it means we need to use some regular Ruby constructs (eg, ‘while`, instead of the Y combinator) The following is essentially how Stepanov & McJones define `power` in Elements of Programming
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/magick/maths.rb', line 82 def power_accumulate_positive ->(op){ ->(r){ ->(a){ ->(n){ while (true) if n % 2 != 0 r = op.(r).(a) return r if n == 1 end a = op.(a).(a) n = n/2 end } } } } end |
.powers_of_two ⇒ Object
137 |
# File 'lib/magick/maths.rb', line 137 def powers_of_two = exp.(2) |
.subtract ⇒ Object
11 |
# File 'lib/magick/maths.rb', line 11 def subtract = ->(x){ ->(y) { add.(x).(negate.(y)) }} |