Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Cuadratica
- require 'complex'
- require 'mathn'
- include Math
- def a
- h*b/(-2)
- end
- def b
- -2*a*h
- end
- def c
- a*h**2+k
- end
- def h
- b/(-2*a)
- end
- def k
- c-a*h**2
- end
- def f(x)
- a*x**2+b*x+c
- end
- def inverse_f(y)
- [sqrt((y-k)/a)+h, -sqrt((y-k)/a)+h]
- end
- def OriginOrdinate
- f(0)
- end
- def Vertice
- Point.new(h,k)
- end
- def Roots
- inverse_f(0)
- end
- def to_s
- "<a: #{a}, b: #{b}, c: #{c}, h: #{h}, k: #{k}>"
- end
- end
- class Point
- attr_accessor :x, :y
- def initialize(x, y)
- @x = x
- @y = y
- end
- def to_hash
- {
- :x => x,
- :y => y
- }
- end
- def to_a
- [
- [:x, x],
- [:y, y]
- ]
- end
- def to_s
- "(%+.2f; %+.2f)" % [x, y]
- end
- end
- class Canonica
- include Cuadratica
- attr_accessor :a, :h, :k
- def initialize(a, h, k)
- @a = a
- @h = h
- @k = k
- end
- def to_explicita
- Explicita.new(a, b, c)
- end
- def to_s
- "%.1f(x%+.1f)^2%+.1f" % [a, -h, k]
- end
- end
- class Explicita
- include Cuadratica
- attr_accessor :a, :b, :c
- def initialize(a, b, c)
- @a = a
- @b = b
- @c = c
- end
- def to_canonica
- Canonica.new(a, h, k)
- end
- def to_s
- "%.1fx^2%+.1fx%+.1f" % [a, b, c]
- end
- def f(x)
- a*(x-h)**2+k
- end
- end
- class EcuacionCuadratica
- include Cuadratica
- def initialize(hashes={})
- hashes.each {|n,v| incognita(n) {v}}
- end
- def incognita(name, &block)
- if(self.method(name) rescue false)
- self.class.send(:define_method, name, &block)
- else
- raise "Incognita \"#{name}\" inexistente"
- end
- end
- def to_canonica
- Canonica.new(a, h, k)
- end
- def to_explicita
- Explicita.new(a, b, c)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment