Class: SparseMatrix::Fraction

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

Overview

clase para operaciones de numeros fraccionales

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Fraction

Returns a new instance of Fraction.



272
273
274
275
276
277
278
279
280
281
# File 'lib/sparse_matrix.rb', line 272

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



282
283
284
# File 'lib/sparse_matrix.rb', line 282

def den_
  @den_
end

#num_Object

Returns the value of attribute num_.



282
283
284
# File 'lib/sparse_matrix.rb', line 282

def num_
  @num_
end

Instance Method Details

#*(b) ⇒ Object

multiplicacion de numeros fraccionarios



345
346
347
348
349
350
351
352
353
354
355
# File 'lib/sparse_matrix.rb', line 345

def *(b)  #multiplicacion de numeros fraccionarios
    r =Fraction.new
    if b.instance_of? Fraction
        r.num_=@num_ * b.num_
        r.den_=@den_ * b.den_
        r.num_,r.den_ = minimiza(r.num_,r.den_)
        r
    else
        r=self*Fraction.new(b,1)
    end
end

#+(b) ⇒ Object

suma de numeros fraccionarios



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/sparse_matrix.rb', line 311

def +(b) #suma de numeros fraccionarios
    r=Fraction.new
    if b.instance_of? Fraction
        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
    else
        r=self+Fraction.new(b,1)
    end
    r.num_,r.den_ = minimiza(r.num_,r.den_)
    r
end

#-(b) ⇒ Object

resta de numeros fraccionarios



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/sparse_matrix.rb', line 328

def -(b) #resta de numeros fraccionarios
    r =Fraction.new
    if b.instance_of? Fraction
        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
    else
        r=self-Fraction.new(b,1)
    end
    r.num_,r.den_ = minimiza(r.num_,r.den_)
    r
end

#-@Object

opuesto



307
308
309
# File 'lib/sparse_matrix.rb', line 307

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

#/(b) ⇒ Object

division de numeros fraccionarios



357
358
359
360
361
362
363
364
365
366
367
# File 'lib/sparse_matrix.rb', line 357

def /(b)  #division de numeros fraccionarios
    r =Fraction.new
    if b.instance_of? Fraction
        r.num_=@num_ / b.num_
        r.den_=@den_ * b.den_
        r.num_,r.den_ = minimiza(r.num_,r.den_)
        r
    else
        r=self/Fraction.new(b,1)
    end
end

#<=>(b) ⇒ Object

comparar



369
370
371
# File 'lib/sparse_matrix.rb', line 369

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

#==(b) ⇒ Object

comparar



291
292
293
# File 'lib/sparse_matrix.rb', line 291

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

#absObject

valor absoluto



295
296
297
298
# File 'lib/sparse_matrix.rb', line 295

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

#coerce(b) ⇒ Object



380
381
382
# File 'lib/sparse_matrix.rb', line 380

def coerce(b)
    [self,Fraction.new(b,1)]
end

#minimiza(x, y) ⇒ Object

minimizar fraccion



373
374
375
376
377
378
# File 'lib/sparse_matrix.rb', line 373

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

#reciprocalObject

devuelve fraccion dada la vuelta



300
301
302
303
304
305
# File 'lib/sparse_matrix.rb', line 300

def reciprocal #devuelve fraccion dada la vuelta
    f=Fraction.new
    f.num_=@den_
    f.den_ = @num_
    f
end

#to_fObject

devuelve float



287
288
289
# File 'lib/sparse_matrix.rb', line 287

def to_f #devuelve float
    @num_.to_f/@den_.to_f
end

#to_sObject

devuelve string



284
285
286
# File 'lib/sparse_matrix.rb', line 284

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