mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
01e008167b
Added complete call-seq documentation for all Complex methods in mrblib/complex.rb (18 methods): ## Complex Class Methods: - polar: creates complex number from polar coordinates (magnitude, angle) with trigonometric conversion using Math.cos and Math.sin ## Complex Instance Methods: - inspect, to_s: string representation methods for debugging and display with proper formatting of real and imaginary parts - +@, -@: unary plus and minus operators for identity and negation - <=>: spaceship operator for comparison with other numeric types, enables Comparable module functionality with proper nil handling - abs/magnitude: absolute value (magnitude) calculation using hypot - abs2: square of absolute value for performance-critical calculations - arg/angle/phase: argument (angle) calculation using atan2 - conjugate/conj: complex conjugate operation (negates imaginary part) - fdiv: floating-point division ensuring float results - polar: returns [magnitude, angle] array representation - real?: always returns false for complex numbers - rectangular/rect: returns [real, imaginary] array representation - to_c: returns self (identity conversion) - to_r: converts to rational when imaginary part is zero, raises RangeError otherwise ## Numeric Extension Methods: - i: creates pure imaginary number (0+num*i) for convenient complex creation - to_c: converts any numeric to complex with zero imaginary part Co-authored-by: Atlassian Rovo Dev
mruby-complex
mruby-complex is an mrbgem that provides a Complex class for mruby, allowing for the representation and manipulation of complex numbers. It is designed to be compatible with the Complex class in standard Ruby.
Functionality
The Complex class provided by this mrbgem supports:
- Creation of complex numbers from real and imaginary parts, or using the
isuffix for numbers. - Basic arithmetic operations: addition (
+), subtraction (-), multiplication (*), division (/). - Methods to access the real and imaginary parts (
real,imaginaryorimag). - Calculation of absolute value (
abs,magnitude), argument/angle (arg,angle,phase), and square of the absolute value (abs2). - Complex conjugate (
conjugate,conj). - Conversion to polar coordinates (
polar). - Conversion to other numeric types where appropriate (
to_f,to_i,to_r,to_c). - Inspection and string representation (
inspect,to_s).
Usage Examples
Here are some basic examples of how to use the Complex class:
# Creating complex numbers
c1 = Complex(1, 2) # => (1+2i)
c2 = 3 + 4i # => (3+4i)
c3 = Complex.polar(5, Math::PI/2) # => (0.0+5.0i) (approximately)
# Arithmetic operations
puts c1 + c2 # => (4+6i)
puts c1 - c2 # => (-2-2i)
puts c1 * c2 # => (-5+10i)
puts c1 / c2 # => (0.44+0.08i)
# Accessing parts
puts c1.real # => 1
puts c1.imaginary # => 2
# Other methods
puts c2.abs # => 5.0
puts c2.arg # => 0.9272952180016122 (radians)
puts c2.conjugate # => (3-4i)
puts c1.polar # => [2.23606797749979, 1.1071487177940904]
# Numerics can be converted to Complex
puts 5.to_c # => (5+0i)
puts 2.3.to_c # => (2.3+0i)
# The `i` method on numerics creates a complex number with zero real part
puts 5i # => (0+5i)
puts 2.3i # => (0+2.3i)
Note
This mrbgem aims to provide functionality similar to the Complex class found in the standard Ruby library. Refer to the Ruby documentation for Complex for more detailed information on the behavior of various methods.