Class: SyMath::Poly
- Inherits:
-
Object
- Object
- SyMath::Poly
- Defined in:
- lib/symath/poly.rb
Overview
Abstract base class for polynomial classes
Defined Under Namespace
Instance Attribute Summary collapse
-
#arr ⇒ Object
readonly
Returns the value of attribute arr.
-
#p ⇒ Object
readonly
Returns the value of attribute p.
-
#var ⇒ Object
readonly
Returns the value of attribute var.
Instance Method Summary collapse
- #%(other) ⇒ Object
- #*(other) ⇒ Object
- #**(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #/(other) ⇒ Object
- #==(other) ⇒ Object
- #[](i) ⇒ Object
-
#degree ⇒ Object
Return the degree of the highest power of the polynomial.
-
#lc ⇒ Object
Returns leading coefficient (i.e coefficient of the biggest power).
- #sort_factors(list) ⇒ Object
- #sort_factors_multiple(list) ⇒ Object
- #strip! ⇒ Object
-
#to_m ⇒ Object
Transform polynomial back to expression form.
- #zero? ⇒ Boolean
Instance Attribute Details
#arr ⇒ Object (readonly)
Returns the value of attribute arr.
4 5 6 |
# File 'lib/symath/poly.rb', line 4 def arr @arr end |
#p ⇒ Object (readonly)
Returns the value of attribute p.
4 5 6 |
# File 'lib/symath/poly.rb', line 4 def p @p end |
#var ⇒ Object (readonly)
Returns the value of attribute var.
4 5 6 |
# File 'lib/symath/poly.rb', line 4 def var @var end |
Instance Method Details
#%(other) ⇒ Object
110 111 112 113 114 115 116 117 118 |
# File 'lib/symath/poly.rb', line 110 def %(other) if other.is_a?(self.class) return rem(other) elsif other.is_a?(Integer) return trunc(other) else raise 'Cannot divide modulo ' + other.to_s end end |
#*(other) ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/symath/poly.rb', line 90 def *(other) if other.is_a?(self.class) return mul(other) elsif other.is_a?(Integer) return mul_ground(other) else raise 'Cannot multiply with ' + other.to_s end end |
#**(other) ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/symath/poly.rb', line 120 def **(other) if other.is_a?(Integer) and other == 2 return sqr else raise 'Cannot raise to the power of ' + other.to_s end end |
#+(other) ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/symath/poly.rb', line 66 def +(other) if other.is_a?(self.class) return add(other) elsif other.is_a?(Integer) return add_ground(other) else raise 'Cannot add ' + other.to_s end end |
#-(other) ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/symath/poly.rb', line 76 def -(other) if other.is_a?(self.class) return sub(other) elsif other.is_a?(Integer) return sub_ground(other) else raise 'Cannot subtract ' + other.to_s end end |
#-@ ⇒ Object
86 87 88 |
# File 'lib/symath/poly.rb', line 86 def -@() return neg end |
#/(other) ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'lib/symath/poly.rb', line 100 def /(other) if other.is_a?(self.class) return quo(other) elsif other.is_a?(Integer) return quo_ground(other) else raise 'Cannot divide by ' + other.to_s end end |
#==(other) ⇒ Object
132 133 134 |
# File 'lib/symath/poly.rb', line 132 def ==(other) return (self.class == other.class and @arr == other.arr) end |
#[](i) ⇒ Object
128 129 130 |
# File 'lib/symath/poly.rb', line 128 def [](i) return arr[i] end |
#degree ⇒ Object
Return the degree of the highest power of the polynomial.
48 49 50 51 52 |
# File 'lib/symath/poly.rb', line 48 def degree() # Hack: Degree of 0 should really be negative infinity return -1 if zero? return @arr.size - 1 end |
#lc ⇒ Object
Returns leading coefficient (i.e coefficient of the biggest power)
137 138 139 140 |
# File 'lib/symath/poly.rb', line 137 def lc() return 0 if zero? return @arr[0] end |
#sort_factors(list) ⇒ Object
54 55 56 57 58 |
# File 'lib/symath/poly.rb', line 54 def sort_factors(list) return list.sort do |a, b| [a.degree, a.arr] <=> [b.degree, b.arr] end end |
#sort_factors_multiple(list) ⇒ Object
60 61 62 63 64 |
# File 'lib/symath/poly.rb', line 60 def sort_factors_multiple(list) return list.sort do |a, b| [a[0].degree, a[1], a[0].arr] <=> [b[0].degree, b[1], b[0].arr] end end |
#strip! ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/symath/poly.rb', line 32 def strip!() i = @arr.index { |e| e != 0 } if i.nil? @arr = [] else @arr = @arr[i..-1] end return self end |
#to_m ⇒ Object
Transform polynomial back to expression form
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/symath/poly.rb', line 7 def to_m() if zero? return 0.to_m end exp = 0.to_m d = degree (0..d).each do |i| next if @arr[i] == 0 if d - i == 1 a = @var elsif d - i == 0 a = 1.to_m else a = @var**(d - i) end exp += @arr[i].to_m*a end return exp end |
#zero? ⇒ Boolean
43 44 45 |
# File 'lib/symath/poly.rb', line 43 def zero?() return @arr.empty? end |