class Maxima::Complex

Constants

COMPLEX_REGEX
WHITESPACE_OR_PARENTHESES_REGEX

Attributes

imaginary[RW]
real[RW]

Public Class Methods

new(real, imaginary, **options) click to toggle source
Calls superclass method
# File lib/maxima/complex.rb, line 10
def initialize(real, imaginary, **options)
  super(**options)
  @real = real
  @imaginary = imaginary
end
parse(maxima_output) click to toggle source
# File lib/maxima/complex.rb, line 19
def self.parse(maxima_output)
  maxima_output = maxima_output.to_s unless maxima_output.is_a?(String)
  string = maxima_output.gsub(WHITESPACE_OR_PARENTHESES_REGEX, "")

  real = 0
  imaginary = 0

  string.scan(COMPLEX_REGEX) do |(float, is_imaginary, is_just_imaginary_one)|
    if is_just_imaginary_one
      imaginary += (is_just_imaginary_one.start_with? "-") ? -1 : 1
    elsif is_imaginary
      imaginary += float.to_f
    else
      real += float.to_f
    end
  end

  if imaginary == 0
    Float.new(real, maxima_output: maxima_output)
  else
    Complex.new(real, imaginary, maxima_output: maxima_output)
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/maxima/complex.rb, line 50
def ==(other)
  @real == other.real && @imaginary == other.imaginary
end
imaginary?() click to toggle source
# File lib/maxima/complex.rb, line 69
def imaginary?
  @imaginary != 0
end
negative?() click to toggle source

At least one scalar must be negative & the others non positive

# File lib/maxima/complex.rb, line 60
def negative?
  (@real < 0 && @imaginary <= 0) ||
  (@imaginary < 0 && @real <= 0)
end
positive?() click to toggle source

Definitions are somewhat contrived and not per se mathematically accurate.

# File lib/maxima/complex.rb, line 55
def positive?
  !negative?
end
real?() click to toggle source
# File lib/maxima/complex.rb, line 73
def real?
  @imaginary == 0
end
to_maxima_input() click to toggle source
# File lib/maxima/complex.rb, line 43
def to_maxima_input
  return "#{@imaginary} * %i" if real == 0

  operand = @real.positive? ? '+' : '-'
  "(#{@imaginary} * %i #{operand} #{@real.abs})"
end
zero?() click to toggle source
# File lib/maxima/complex.rb, line 65
def zero?
  @real == 0 && @imaginary == 0
end