<commit_before>#
#   complex.rb -
#   	$Release Version: 0.5 $
#   	$Revision: 1.3 $
#   	$Date: 1998/07/08 10:05:28 $
#   	by <NAME>(SHL Japan Inc.)
#
# ----
#
# complex.rb implements the Complex class for complex numbers.  Additionally,
# some methods in other Numeric classes are redefined or added to allow greater
# interoperability with Complex numbers.
#
# Complex numbers can be created in the following manner:
# - <tt>Complex(a, b)</tt>
# - <tt>Complex.polar(radius, theta)</tt>
#
# Additionally, note the following:
# - <tt>Complex::I</tt> (the mathematical constant <i>i</i>)
# - <tt>Numeric#im</tt> (e.g. <tt>5.im -> 0+5i</tt>)
#
# The following +Math+ module methods are redefined to handle Complex arguments.
# They will work as normal with non-Complex arguments.
#    sqrt exp cos sin tan log log10
#    cosh sinh tanh acos asin atan atan2 acosh asinh atanh
#


#
# Numeric is a built-in class on which Fixnum, Bignum, etc., are based.  Here
# some methods are added so that all number types can be treated to some extent
# as Complex numbers.
#
class Numeric
  #
  # Returns a Complex number <tt>(0,<i>self</i>)</tt>.
  #
  def im
    Complex(0, self)
  end

  #
  # The real part of a complex number, i.e. <i>self</i>.
  #
  def real
    self
  end

  #
  # The imaginary part of a complex number, i.e. 0.
  #
  def image
    0
  end
  alias imag image

  #
  # See Complex#arg.
  #
  def arg
    Math.atan2(0, self)
  end
  alias angle arg
  alias phase arg

  #
  # See Complex#polar.
  #
  def polar
    return abs, arg
  end

  #
  # See Complex#conjugate (short answer: returns <i>self</i>).
  #
  def conjugate
    self
  end
  alias conj conjugate
end


#
# Creates a Complex number.  +a+ and +b+ should be Numeric.  The result will be
# <tt>a+bi</tt>.
#
def Complex(*args)
  Complex.send :convert, *args
end

#
# The complex number class.
#
class Complex < Numeric
  undef_method :step
  undef_method :div
  undef_method :divmod
  undef_method :floor
  undef_method :truncate
  undef_method :ceil
  undef_method :round

  def self.convert(real, imag = undefined)
    if real.equal?(nil) || imag.equal?(nil)
      raise TypeError, "cannot convert nil into Complex"
    end

    real = real.to_c if real.kind_of?(String)
    imag = imag.to_c if imag.kind_of?(String)

    if real.kind_of?(Complex) && !real.imag.kind_of?(Float) && real.imag == 0
      real = real.real
    end

    if imag.kind_of?(Complex) && !imag.imag.kind_of?(Float) && imag.imag == 0
      imag = imag.real
    end

    if real.kind_of?(Complex) && !imag.kind_of?(Float) && imag == 0
      return real
    end

    if imag.equal? undefined
      if real.kind_of?(Numeric) && !real.real?
        return real
      elsif !real.kind_of?(Numeric)
        return Rubinius::Type.coerce_to(real, Complex, :to_c)
      else
        imag = 0
      end
    elsif real.kind_of?(Numeric) && imag.kind_of?(Numeric) && (!real.real? || !imag.real?)
      return real + imag * Complex(0, 1)
    end

    rect(real, imag)
  end

  private_class_method :convert

  def Complex.generic?(other) # :nodoc:
    other.kind_of?(Integer) or
    other.kind_of?(Float) or
    (defined?(Rational) and other.kind_of?(Rational))
  end

  def Complex.rect(real, imag=0)
    raise TypeError, 'not a real' unless check_real?(real) && check_real?(imag)
    new(real, imag)
  end
  class << self; alias_method :rectangular, :rect end

  #
  # Creates a +Complex+ number in terms of +r+ (radius) and +theta+ (angle).
  #
  def Complex.polar(r, theta=0)
    raise TypeError, 'not a real' unless check_real?(r) && check_real?(theta)

    Complex(r*Math.cos(theta), r*Math.sin(theta))
  end

  def Complex.check_real?(obj)
    obj.kind_of?(Numeric) && obj.real?
  end
  private_class_method :check_real?

  def initialize(a, b = 0)
    raise TypeError, "non numeric 1st arg `#{a.inspect}'" if !a.kind_of? Numeric
    raise TypeError, "`#{a.inspect}' for 1st arg" if a.kind_of? Complex
    raise TypeError, "non numeric 2nd arg `#{b.inspect}'" if !b.kind_of? Numeric
    raise TypeError, "`#{b.inspect}' for 2nd arg" if b.kind_of? Complex
    @real = a
    @image = b
  end

  #
  # Addition with real or complex number.
  #
  def + (other)
    if other.kind_of?(Complex)
      re = @real + other.real
      im = @image + other.image
      Complex(re, im)
    elsif Complex.generic?(other)
      Complex(@real + other, @image)
    else
      x , y = other.coerce(self)
      x + y
    end
  end<commit_msg>Fix Complex#-@<commit_after>#
#   complex.rb -
#   	$Release Version: 0.5 $
#   	$Revision: 1.3 $
#   	$Date: 1998/07/08 10:05:28 $
#   	by Keiju ISHITSUKA(SHL Japan Inc.)
#
# ----
#
# complex.rb implements the Complex class for complex numbers.  Additionally,
# some methods in other Numeric classes are redefined or added to allow greater
# interoperability with Complex numbers.
#
# Complex numbers can be created in the following manner:
# - <tt>Complex(a, b)</tt>
# - <tt>Complex.polar(radius, theta)</tt>
#
# Additionally, note the following:
# - <tt>Complex::I</tt> (the mathematical constant <i>i</i>)
# - <tt>Numeric#im</tt> (e.g. <tt>5.im -> 0+5i</tt>)
#
# The following +Math+ module methods are redefined to handle Complex arguments.
# They will work as normal with non-Complex arguments.
#    sqrt exp cos sin tan log log10
#    cosh sinh tanh acos asin atan atan2 acosh asinh atanh
#


#
# Numeric is a built-in class on which Fixnum, Bignum, etc., are based.  Here
# some methods are added so that all number types can be treated to some extent
# as Complex numbers.
#
class Numeric
  #
  # Returns a Complex number <tt>(0,<i>self</i>)</tt>.
  #
  def im
    Complex(0, self)
  end

  #
  # The real part of a complex number, i.e. <i>self</i>.
  #
  def real
    self
  end

  #
  # The imaginary part of a complex number, i.e. 0.
  #
  def image
    0
  end
  alias imag image

  #
  # See Complex#arg.
  #
  def arg
    Math.atan2(0, self)
  end
  alias angle arg
  alias phase arg

  #
  # See Complex#polar.
  #
  def polar
    return abs, arg
  end

  #
  # See Complex#conjugate (short answer: returns <i>self</i>).
  #
  def conjugate
    self
  end
  alias conj conjugate
end


#
# Creates a Complex number.  +a+ and +b+ should be Numeric.  The result will be
# <tt>a+bi</tt>.
#
def Complex(*args)
  Complex.send :convert, *args
end

#
# The complex number class.
#
class Complex < Numeric
  undef_method :step
  undef_method :div
  undef_method :divmod
  undef_method :floor
  undef_method :truncate
  undef_method :ceil
  undef_method :round

  def self.convert(real, imag = undefined)
    if real.equal?(nil) || imag.equal?(nil)
      raise TypeError, "cannot convert nil into Complex"
    end

    real = real.to_c if real.kind_of?(String)
    imag = imag.to_c if imag.kind_of?(String)

    if real.kind_of?(Complex) && !real.imag.kind_of?(Float) && real.imag == 0
      real = real.real
    end

    if imag.kind_of?(Complex) && !imag.imag.kind_of?(Float) && imag.imag == 0
      imag = imag.real
    end

    if real.kind_of?(Complex) && !imag.kind_of?(Float) && imag == 0
      return real
    end

    if imag.equal? undefined
      if real.kind_of?(Numeric) && !real.real?
        return real
      elsif !real.kind_of?(Numeric)
        return Rubinius::Type.coerce_to(real, Complex, :to_c)
      else
        imag = 0
      end
    elsif real.kind_of?(Numeric) && imag.kind_of?(Numeric) && (!real.real? || !imag.real?)
      return real + imag * Complex(0, 1)
    end

    rect(real, imag)
  end

  private_class_method :convert

  def Complex.generic?(other) # :nodoc:
    other.kind_of?(Integer) or
    other.kind_of?(Float) or
    (defined?(Rational) and other.kind_of?(Rational))
  end

  def Complex.rect(real, imag=0)
    raise TypeError, 'not a real' unless check_real?(real) && check_real?(imag)
    new(real, imag)
  end
  class << self; alias_method :rectangular, :rect end

  #
  # Creates a +Complex+ number in terms of +r+ (radius) and +theta+ (angle).
  #
  def Complex.polar(r, theta=0)
    raise TypeError, 'not a real' unless check_real?(r) && check_real?(theta)

    Complex(r*Math.cos(theta), r*Math.sin(theta))
  end

  def Complex.check_real?(obj)
    obj.kind_of?(Numeric) && obj.real?
  end
  private_class_method :check_real?

  def initialize(a, b = 0)
    raise TypeError, "non numeric 1st arg `#{a.inspect}'" if !a.kind_of? Numeric
    raise TypeError, "`#{a.inspect}' for 1st arg" if a.kind_of? Complex
    raise TypeError, "non numeric 2nd arg `#{b.inspect}'" if !b.kind_of? Numeric
    raise TypeError, "`#{b.inspect}' for 2nd arg" if b.kind_of? Complex
    @real = a
    @image = b
  end

  def -@
    Complex(-real, -imag)
  end

  #
  # Addition with real or complex number.
  #
  def + (other)
    if other.kind_of?(Complex)
      re = @real + other.real
      im = @image + other.image
      Complex(re, im)
    elsif Complex.generic?(other)
      Complex(@real + other, @image)
    else
      x , y = other.coerce(self)
      x + y
    end
  end
{'fraction_non_alphanumeric': 0.12811942959001782, 'fraction_numerical': 0.009581105169340463, 'mean_word_length': 2.901347240330291, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '888461', 'n_tokens_mistral': 3305, 'n_tokens_neox': 3206, 'n_words': 955}