Class: Maxima::Complex

Inherits:
Unit
  • Object
show all
Defined in:
lib/maxima/complex.rb

Constant Summary collapse

WHITESPACE_OR_PARENTHESES_REGEX =
/(\s|\(|\))/
COMPLEX_REGEX =
/(-?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?)((?:\*)?(?:%)?\s*i)?|((?:\s*)-?%i)/

Instance Attribute Summary collapse

Attributes inherited from Unit

#maxima_output, #plot_title

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Unit

#===, #at, #gnu_plot_options, #gnu_plot_text, #gnu_plot_w, #inspect, parse_float, #simplified, #through_maxima, #to_f, #to_gnu_plot, #to_pdf, #to_s, #with_plot_title

Constructor Details

#initialize(real, imaginary, **options) ⇒ Complex

Returns a new instance of Complex.



10
11
12
13
14
# File 'lib/maxima/complex.rb', line 10

def initialize(real, imaginary, **options)
  super(**options)
  @real = real
  @imaginary = imaginary
end

Instance Attribute Details

#imaginaryObject

Returns the value of attribute imaginary.



8
9
10
# File 'lib/maxima/complex.rb', line 8

def imaginary
  @imaginary
end

#realObject

Returns the value of attribute real.



8
9
10
# File 'lib/maxima/complex.rb', line 8

def real
  @real
end

Class Method Details

.parse(maxima_output) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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

Instance Method Details

#==(other) ⇒ Object



50
51
52
# File 'lib/maxima/complex.rb', line 50

def ==(other)
  @real == other.real && @imaginary == other.imaginary
end

#imaginary?Boolean

Returns:



69
70
71
# File 'lib/maxima/complex.rb', line 69

def imaginary?
  @imaginary != 0
end

#negative?Boolean

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

Returns:



60
61
62
63
# File 'lib/maxima/complex.rb', line 60

def negative?
  (@real < 0 && @imaginary <= 0) ||
  (@imaginary < 0 && @real <= 0)
end

#positive?Boolean

Definitions are somewhat contrived and not per se mathematically accurate.

Returns:



55
56
57
# File 'lib/maxima/complex.rb', line 55

def positive?
  !negative?
end

#real?Boolean

Returns:



73
74
75
# File 'lib/maxima/complex.rb', line 73

def real?
  @imaginary == 0
end

#to_maxima_inputObject



43
44
45
46
47
48
# 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?Boolean

Returns:



65
66
67
# File 'lib/maxima/complex.rb', line 65

def zero?
  @real == 0 && @imaginary == 0
end