lib/hpsqrt.rb in hpsqrt-1.5.0 vs lib/hpsqrt.rb in hpsqrt-1.6.0
- old
+ new
@@ -18,10 +18,11 @@
attr_reader :terms
def initialize(terms)
@terms = terms.freeze
+ @cache = {}
freeze
end
def -@
terms = @terms.map{|t,c| [t, -c]}.to_h
@@ -115,10 +116,20 @@
else
super.==(other)
end
end
+ def <=>(other)
+ if self==other
+ 0
+ elsif !self.real? || !other.real?
+ nil
+ else
+ self.real <=> other.real
+ end
+ end
+
def rect
to_c.rect
end
def polar
@@ -130,10 +141,10 @@
end
alias_method :angle, :arg
alias_method :phase, :arg
def to_c
- @terms.map {|t, c|
+ @cache[:to_c] ||= @terms.map {|t, c|
t.number * Math.sqrt(Complex(t.sqrt)) * c
}.sum.to_c
end
def real