lib/hpsqrt.rb in hpsqrt-1.9.0 vs lib/hpsqrt.rb in hpsqrt-1.10.0

- old
+ new

@@ -143,14 +143,23 @@ to_c.arg end alias_method :angle, :arg alias_method :phase, :arg + def to_rc + @cache[:to_rc] ||= @terms.map {|t, c| + nc = Complex(t.number.real.to_r, t.number.imag.to_r) + + sc = Math.sqrt(Complex(t.sqrt)) + sc = Complex(sc.real.to_r, sc.imag.to_r) + + nc * sc * c + }.sum(Complex(0.to_r, 0.to_r)) + end + def to_c - @cache[:to_c] ||= @terms.map {|t, c| - t.number * Math.sqrt(Complex(t.sqrt)) * c - }.sum.to_c + @cache[:to_c] ||= Complex(to_rc.real.to_f, to_rc.imag.to_f) end def real to_c.real end @@ -174,67 +183,58 @@ else raise RangeError, "can't convert %s into Float" % to_c end end - def to_rc - @cache[:to_rc] ||= @terms.map {|t, c| - nc = Complex(t.number.real.to_r, t.number.imag.to_r) - - sc = Math.sqrt(Complex(t.sqrt)) - sc = Complex(sc.real.to_r, sc.imag.to_r) - - nc * sc * c - }.sum.nonzero? || Complex(0.to_r, 0.to_r) - end - def to_r if to_rc.imag.zero? to_rc.real else raise RangeError, "can't convert %s into Rational" % to_rc end end def expr - value_to_s = -> (v) { - if Complex===v && v.imag.zero? - v = v.real - end - if Rational===v && v.denominator==1 - v = v.numerator - end - v = v.to_s - if v !~ /^[\d\.]+$/ - v = "(%s)" % v - end - v - } + @cache[:expr] ||= begin + value_to_s = -> (v) { + if Complex===v && v.imag.zero? + v = v.real + end + if Rational===v && v.denominator==1 + v = v.numerator + end + v = v.to_s + if v !~ /^[\d\.]+$/ + v = "(%s)" % v + end + v + } - result = @terms.map {|t, c| - n = t.number * c - s = t.sqrt + result = @terms.map {|t, c| + n = t.number * c + s = t.sqrt - if s!=1 - if n==1 - "\u221A%s" % value_to_s[s] - elsif 0<n.real - "%s\u221A%s" % [value_to_s[n], value_to_s[s]] - elsif n==-1 - "(-\u221A%s)" % value_to_s[s] + if s!=1 + if n==1 + "\u221A%s" % value_to_s[s] + elsif 0<n.real + "%s\u221A%s" % [value_to_s[n], value_to_s[s]] + elsif n==-1 + "(-\u221A%s)" % value_to_s[s] + else + "(%s\u221A%s)" % [value_to_s[n], value_to_s[s]] + end else - "(%s\u221A%s)" % [value_to_s[n], value_to_s[s]] + value_to_s[n] end + } + + if 0<result.length + result.join(" + ") else - value_to_s[n] + "0" end - } - - if 0<result.length - result.join(" + ") - else - "0" end end def inspect to_s @@ -254,19 +254,19 @@ def real? false end def complex? - !imag.zero? + !to_rc.imag.zero? end def integer? - imag.zero? && real==real.to_i + to_rc.imag.zero? && to_rc.real.denominator==1 end def float? - imag.zero? && Float===real && real!=real.to_i + to_rc.imag.zero? && to_rc.real.denominator!=1 end def self.create(v) if self===v v @@ -283,10 +283,10 @@ end end def self.sqrt(v) if self===v - v = v.to_c + v = v.to_rc end if v!=0 new({Term.new(sqrt: v) => 1}) else zero