Class: SparseMatrix::Fraction

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/sparse_matrix.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Fraction

Returns a new instance of Fraction.



116
117
118
119
120
121
122
123
124
125
# File 'lib/sparse_matrix.rb', line 116

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_.



126
127
128
# File 'lib/sparse_matrix.rb', line 126

def den_
  @den_
end

#num_Object

Returns the value of attribute num_.



126
127
128
# File 'lib/sparse_matrix.rb', line 126

def num_
  @num_
end

Instance Method Details

#*(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_)
    return r
end

#+(b) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/sparse_matrix.rb', line 155

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



168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/sparse_matrix.rb', line 168

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



151
152
153
# File 'lib/sparse_matrix.rb', line 151

def -@
    Fraction.new(-@num_,@den_)
end

#/(b) ⇒ Object



189
190
191
192
193
194
195
# File 'lib/sparse_matrix.rb', line 189

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



197
198
199
# File 'lib/sparse_matrix.rb', line 197

def <=>(b)
    self.to_f <=> b.to_f
end

#==(b) ⇒ Object



135
136
137
# File 'lib/sparse_matrix.rb', line 135

def ==(b)
    return @num_.eql?(b.num_) && @den_.eql?(b.den_)
end

#absObject



139
140
141
142
# File 'lib/sparse_matrix.rb', line 139

def abs
    c = @num_.to_f/@den_.to_f
    return c.abs
end

#minimiza(x, y) ⇒ Object



201
202
203
204
205
206
# File 'lib/sparse_matrix.rb', line 201

def minimiza(x,y)
    d = gcd(x,y)
    x = x/d
    y = y/d
    return x,y
end

#reciprocalObject



144
145
146
147
148
149
# File 'lib/sparse_matrix.rb', line 144

def reciprocal
    f=Fraction.new
    f.num_=@den_
    f.den_ = @num_
    f
end

#to_fObject



131
132
133
# File 'lib/sparse_matrix.rb', line 131

def to_f
    @num_.to_f/@den_.to_f
end

#to_sObject



128
129
130
# File 'lib/sparse_matrix.rb', line 128

def to_s
    "#{@num_}/#{@den_}"
end