Class: SparseMatrix::Fraction
- Inherits:
-
Object
- Object
- SparseMatrix::Fraction
- Includes:
- Comparable
- Defined in:
- lib/sparse_matrix.rb
Instance Attribute Summary collapse
-
#den_ ⇒ Object
Returns the value of attribute den_.
-
#num_ ⇒ Object
Returns the value of attribute num_.
Instance Method Summary collapse
- #*(b) ⇒ Object
- #+(b) ⇒ Object
- #-(b) ⇒ Object
- #-@ ⇒ Object
- #/(b) ⇒ Object
- #<=>(b) ⇒ Object
- #==(b) ⇒ Object
- #abs ⇒ Object
-
#initialize(*args) ⇒ Fraction
constructor
A new instance of Fraction.
- #minimiza(x, y) ⇒ Object
- #reciprocal ⇒ Object
- #to_f ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(*args) ⇒ Fraction
Returns a new instance of Fraction.
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/sparse_matrix.rb', line 108 def initialize (*args) if args.size == 2 c = gcd(args[0],args[1]) @num_ = (args[0]/c) @den_ = (args[1]/c) else @num_ = args[0] @den_ = 1 end end |
Instance Attribute Details
#den_ ⇒ Object
Returns the value of attribute den_.
118 119 120 |
# File 'lib/sparse_matrix.rb', line 118 def den_ @den_ end |
#num_ ⇒ Object
Returns the value of attribute num_.
118 119 120 |
# File 'lib/sparse_matrix.rb', line 118 def num_ @num_ end |
Instance Method Details
#*(b) ⇒ Object
173 174 175 176 177 178 179 |
# File 'lib/sparse_matrix.rb', line 173 def *(b) r =Fraction.new r.num_=@num_ * b.num_ r.den_=@den_ * b.den_ r.num_,r.den_ = minimiza(r.num_,r.den_) return r end |
#+(b) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/sparse_matrix.rb', line 147 def +(b) r=Fraction.new if (@den_==b.den_) r.num_ = @num_ + b.num_ r.den_ = @den_ else r.num_ = @num_ * b.den_ + b.num_ * @den_ r.den_ = @den_ * b.den_ end r.num_,r.den_ = minimiza(r.num_,r.den_) return r end |
#-(b) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/sparse_matrix.rb', line 160 def -(b) r =Fraction.new if (@den_ == b.den_) r.num_=@num_- b.num_ r.den_=@den_ else r.num_=@num_ * b.den_ - b.num_ * @den_ r.den_ = @den_ * b.den_ end r.num_,r.den_ = minimiza(r.num_,r.den_) r end |
#-@ ⇒ Object
143 144 145 |
# File 'lib/sparse_matrix.rb', line 143 def -@ Fraction.new(-@num_,@den_) end |
#/(b) ⇒ Object
181 182 183 184 185 186 187 |
# File 'lib/sparse_matrix.rb', line 181 def /(b) r =Fraction.new r.num_=@num_ / b.num_ r.den_=@den_ * b.den_ r.num_,r.den_ = minimiza(r.num_,r.den_) r end |
#<=>(b) ⇒ Object
189 190 191 |
# File 'lib/sparse_matrix.rb', line 189 def <=>(b) self.to_f <=> b.to_f end |
#==(b) ⇒ Object
127 128 129 |
# File 'lib/sparse_matrix.rb', line 127 def ==(b) return @num_.eql?(b.num_) && @den_.eql?(b.den_) end |
#abs ⇒ Object
131 132 133 134 |
# File 'lib/sparse_matrix.rb', line 131 def abs c = @num_.to_f/@den_.to_f return c.abs end |
#minimiza(x, y) ⇒ Object
193 194 195 196 197 198 |
# File 'lib/sparse_matrix.rb', line 193 def minimiza(x,y) d = gcd(x,y) x = x/d y = y/d return x,y end |
#reciprocal ⇒ Object
136 137 138 139 140 141 |
# File 'lib/sparse_matrix.rb', line 136 def reciprocal f=Fraction.new f.num_=@den_ f.den_ = @num_ f end |
#to_f ⇒ Object
123 124 125 |
# File 'lib/sparse_matrix.rb', line 123 def to_f @num_.to_f/@den_.to_f end |
#to_s ⇒ Object
120 121 122 |
# File 'lib/sparse_matrix.rb', line 120 def to_s "#{@num_}/#{@den_}" end |