Class: Maxima::Complex
Constant Summary collapse
- WHITESPACE_OR_PARENTHESES_REGEX =
/(\s|\(|\))/- COMPLEX_REGEX =
/(-?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?)((?:\*)?(?:%)?i)?/
Instance Attribute Summary collapse
-
#imaginary ⇒ Object
Returns the value of attribute imaginary.
-
#real ⇒ Object
Returns the value of attribute real.
Attributes inherited from Unit
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #imaginary? ⇒ Boolean
-
#initialize(real, imaginary, **options) ⇒ Complex
constructor
A new instance of Complex.
-
#negative? ⇒ Boolean
At least one scalar must be negative & the others non positive.
-
#positive? ⇒ Boolean
Definitions are somewhat contrived and not per se mathematically accurate.
- #pretty_to_s ⇒ Object
- #real? ⇒ Boolean
- #to_maxima_input ⇒ Object
- #zero? ⇒ Boolean
Methods inherited from Unit
#===, #at, #gnu_plot_options, #gnu_plot_text, #gnu_plot_w, #inspect, parse_float, #simplified, #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, **) super(**) @real = real @imaginary = imaginary end |
Instance Attribute Details
#imaginary ⇒ Object
Returns the value of attribute imaginary.
8 9 10 |
# File 'lib/maxima/complex.rb', line 8 def imaginary @imaginary end |
#real ⇒ Object
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 |
# 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)| if 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
57 58 59 |
# File 'lib/maxima/complex.rb', line 57 def ==(other) @real == other.real && @imaginary == other.imaginary end |
#imaginary? ⇒ Boolean
77 78 79 |
# File 'lib/maxima/complex.rb', line 77 def imaginary? @imaginary != 0 end |
#negative? ⇒ Boolean
At least one scalar must be negative & the others non positive
68 69 70 71 |
# File 'lib/maxima/complex.rb', line 68 def negative? (@real < 0 && @imaginary <= 0) || (@imaginary < 0 && @real <= 0) end |
#positive? ⇒ Boolean
Definitions are somewhat contrived and not per se mathematically accurate.
63 64 65 |
# File 'lib/maxima/complex.rb', line 63 def positive? !negative? end |
#pretty_to_s ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/maxima/complex.rb', line 41 def pretty_to_s if real == 0 "#{@imaginary}i" else operand = @real.positive? ? '+' : '-' "#{@imaginary}i #{operand} #{@real.abs}" end end |
#to_maxima_input ⇒ Object
50 51 52 53 54 55 |
# File 'lib/maxima/complex.rb', line 50 def to_maxima_input return "#{@imaginary} * %i" if real == 0 operand = @real.positive? ? '+' : '-' "(#{@imaginary} * %i #{operand} #{@real.abs})" end |
#zero? ⇒ Boolean
73 74 75 |
# File 'lib/maxima/complex.rb', line 73 def zero? @real == 0 && @imaginary == 0 end |