Class: Complex
Overview
A complex number can be represented as a paired real number with imaginary unit; a+bi. Where a is real part, b is imaginary part and i is imaginary unit. Real a equals complex a+0i mathematically.
In ruby, you can create complex object with Complex, Complex::rect, Complex::polar or to_c method.
Complex(1) #=> (1+0i)
Complex(2, 3) #=> (2+3i)
Complex.polar(2, 3) #=> (-1.9799849932008908+0.2822400161197344i)
3.to_c #=> (3+0i)
You can also create complex object from floating-point numbers or strings.
Complex(0.3) #=> (0.3+0i)
Complex('0.3-0.5i') #=> (0.3-0.5i)
Complex('2/3+3/4i') #=> ((2/3)+(3/4)*i)
Complex('1@2') #=> (-0.4161468365471424+0.9092974268256817i)
0.3.to_c #=> (0.3+0i)
'0.3-0.5i'.to_c #=> (0.3-0.5i)
'2/3+3/4i'.to_c #=> ((2/3)+(3/4)*i)
'1@2'.to_c #=> (-0.4161468365471424+0.9092974268256817i)
A complex object is either an exact or an inexact number.
Complex(1, 1) / 2 #=> ((1/2)+(1/2)*i)
Complex(1, 1) / 2.0 #=> (0.5+0.5i)
Constant Summary
- I
Class Method Summary (collapse)
- + alloc
- + convert
-
+ polar
Returns a complex object which denotes the given polar form.
-
+ rect
Returns a complex object which denotes the given rectangular form.
-
+ rectangular
Returns a complex object which denotes the given rectangular form.
Instance Method Summary (collapse)
-
- *
Performs multiplication.
-
- **
Performs exponentiation.
-
- +
Performs addition.
-
- -
Performs subtraction.
-
- -
Returns negation of the value.
-
- /
Performs division.
-
- ==
Returns true if cmp equals object numerically.
-
- abs
Returns the absolute part of its polar form.
-
- abs2
Returns square of the absolute value.
-
- angle
Returns the angle part of its polar form.
-
- arg
Returns the angle part of its polar form.
-
- coerce
:nodoc:.
-
- complex?
:nodoc:.
-
- conj
Returns the complex conjugate.
-
- conjugate
Returns the complex conjugate.
-
- denominator
Returns the denominator (lcm of both denominator - real and imag).
-
- eql?
:nodoc:.
-
- exact?
:nodoc:.
-
- fdiv
Performs division as each part is a float, never returns a float.
-
- hash
:nodoc:.
-
- imag
Returns the imaginary part.
-
- imaginary
Returns the imaginary part.
-
- inexact?
:nodoc:.
-
- inspect
Returns the value as a string for inspection.
-
- magnitude
Returns the absolute part of its polar form.
-
- marshal_dump
:nodoc:.
-
- marshal_load
:nodoc:.
-
- numerator
Returns the numerator.
-
- phase
Returns the angle part of its polar form.
-
- polar
Returns an array; [cmp.abs, cmp.arg].
- - quo
-
- rationalize
Returns the value as a rational if possible.
-
- real
Returns the real part.
-
- real?
Returns false.
-
- rect
Returns an array; [cmp.real, cmp.imag].
-
- rectangular
Returns an array; [cmp.real, cmp.imag].
-
- to_f
Returns the value as a float if possible.
-
- to_i
Returns the value as an integer if possible.
-
- to_r
If the imaginary part is exactly 0, returns the real part as a Rational, otherwise a RangeError is raised.
-
- to_s
Returns the value as a string.
-
- ~
Returns the complex conjugate.
Methods inherited from Numeric
#%, #+@, #, #ceil, #class, #div, #divmod, #dup, #floor, #i, #initialize_copy, #integer?, #modulo, #nonzero?, #remainder, #round, #scalar?, #singleton_method_added, #step, #to_c, #to_int, #truncate, #zero?
Methods included from Comparable
Methods inherited from NSNumber
#boolValue, #charValue, #compare:, #decimalValue, #descriptionWithLocale:, #doubleValue, #floatValue, #initWithBool:, #initWithChar:, #initWithDouble:, #initWithFloat:, #initWithInt:, #initWithInteger:, #initWithLong:, #initWithLongLong:, #initWithShort:, #initWithUnsignedChar:, #initWithUnsignedInt:, #initWithUnsignedInteger:, #initWithUnsignedLong:, #initWithUnsignedLongLong:, #initWithUnsignedShort:, #intValue, #integerValue, #isEqualToNumber:, #longLongValue, #longValue, numberWithBool:, numberWithChar:, numberWithDouble:, numberWithFloat:, numberWithInt:, numberWithInteger:, numberWithLong:, numberWithLongLong:, numberWithShort:, numberWithUnsignedChar:, numberWithUnsignedInt:, numberWithUnsignedInteger:, numberWithUnsignedLong:, numberWithUnsignedLongLong:, numberWithUnsignedShort:, #objCType, #shortValue, #stringValue, #unsignedCharValue, #unsignedIntValue, #unsignedIntegerValue, #unsignedLongLongValue, #unsignedLongValue, #unsignedShortValue
Methods inherited from NSValue
#getValue:, #initWithBytes:objCType:, #isEqualToValue:, #nonretainedObjectValue, #objCType, #pointerValue, #rangeValue, value:withObjCType:, valueWithBytes:objCType:, valueWithNonretainedObject:, valueWithPointer:, valueWithRange:
Methods inherited from NSObject
#!, #!=, #!~, #, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version
Constructor Details
This class inherits a constructor from NSObject
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class NSObject
Class Method Details
+ (Object) alloc
+ (Object) convert
+ (Object) polar(abs[, arg])
Returns a complex object which denotes the given polar form.
Complex.polar(3, 0) #=> (3.0+0.0i)
Complex.polar(3, Math::PI/2) #=> (1.836909530733566e-16+3.0i)
Complex.polar(3, Math::PI) #=> (-3.0+3.673819061467132e-16i)
Complex.polar(3, -Math::PI/2) #=> (1.836909530733566e-16-3.0i)
Instance Method Details
- (Object) *(numeric)
Performs multiplication.
- (Object) **(numeric)
Performs exponentiation.
For example:
Complex('i') ** 2 #=> (-1+0i)
Complex(-8) ** Rational(1,3) #=> (1.0000000000000002+1.7320508075688772i)
- (Object) +(numeric)
Performs addition.
- (Object) -(numeric)
Performs subtraction.
- (Object) -
Returns negation of the value.
- (Object) /(numeric) - (Object) quo(numeric)
Performs division.
For example:
Complex(10.0) / 3 #=> (3.3333333333333335+(0/1)*i)
Complex(10) / 3 #=> ((10/3)+(0/1)*i) # not (3+0i)
- (Boolean) ==(object)
Returns true if cmp equals object numerically.
- (Object) abs2
Returns square of the absolute value.
- (Float) arg - (Float) angle - (Float) phase
Returns the angle part of its polar form.
Complex.polar(3, Math::PI/2).arg #=> 1.5707963267948966
- (Float) arg - (Float) angle - (Float) phase
Returns the angle part of its polar form.
Complex.polar(3, Math::PI/2).arg #=> 1.5707963267948966
- (Object) coerce
:nodoc:
- (Boolean) complex?
:nodoc:
- (Integer) denominator
Returns the denominator (lcm of both denominator - real and imag).
See numerator.
- (Boolean) eql?
:nodoc:
- (Boolean) exact?
:nodoc:
- (Object) fdiv(numeric)
Performs division as each part is a float, never returns a float.
For example:
Complex(11,22).fdiv(3) #=> (3.6666666666666665+7.333333333333333i)
- (Object) hash
:nodoc:
- (Boolean) inexact?
:nodoc:
- (String) inspect
Returns the value as a string for inspection.
- (Object) marshal_dump
:nodoc:
- (Object) marshal_load
:nodoc:
- (Numeric) numerator
Returns the numerator.
For example:
1 2 3+4i <- numerator
- + -i -> ----
2 3 6 <- denominator
c = Complex('1/2+2/3i') #=> ((1/2)+(2/3)*i)
n = c.numerator #=> (3+4i)
d = c.denominator #=> 6
n / d #=> ((1/2)+(2/3)*i)
Complex(Rational(n.real, d), Rational(n.imag, d))
#=> ((1/2)+(2/3)*i)
See denominator.
- (Float) arg - (Float) angle - (Float) phase
Returns the angle part of its polar form.
Complex.polar(3, Math::PI/2).arg #=> 1.5707963267948966
- (Array) polar
Returns an array; [cmp.abs, cmp.arg].
- (Object) quo
- (Rational) rationalize([eps])
Returns the value as a rational if possible. An optional argument eps is always ignored.
- (Object) real
Returns the real part.
- (false) real?
Returns false.
- (Float) to_f
Returns the value as a float if possible.
- (Integer) to_i
Returns the value as an integer if possible.
- (Rational) to_r
If the imaginary part is exactly 0, returns the real part as a Rational, otherwise a RangeError is raised.
- (String) to_s
Returns the value as a string.