This normalized form for a ratio of integers is unique, so equality of rational values can be tested by checking for equality of the numerator and denominator. If the numerator and denominator of a rational have common factors, they are reduced to lowest terms such that the denominator is non-negative: julia> 6//9 Rationals are constructed using the // operator: julia> 2//3 Julia has a rational number type to represent exact ratios of integers. Inf and NaN propagate through complex numbers in the real and imaginary parts of a complex number as described in the Special floating-point values section: julia> 1 + Inf*im This construction avoids the multiplication and addition operations. Instead, use the more efficient complex function to construct a complex value directly from its real and imaginary parts: julia> a = 1 b = 2 complex(a, b) Instead, the multiplication must be explicitly written out: julia> a = 1 b = 2 a + b*im The literal numeric coefficient notation does not work when constructing a complex number from variables. Sqrt will only return a complex result if called with a complex argument. For example, sqrt behaves differently when applied to -1 versus -1 + 0im even though -1 = -1 + 0im: julia> sqrt(-1) Note that mathematical functions typically return real values when applied to real numbers and complex values when applied to complex numbers. angle returns the phase angle in radians (also known as the argument or arg function). abs2 gives the square of the absolute value, and is of particular use for complex numbers since it avoids taking a square root. Julia> angle(1 + 2im) # phase angle in radiansĪs usual, the absolute value ( abs) of a complex number is its distance from zero. Julia> abs2(1 + 2im) # squared absolute value Julia> abs(1 + 2im) # absolute value of z Julia> conj(1 + 2im) # complex conjugate of z Julia> imag(1 + 2im) # imaginary part of z Standard functions to manipulate complex values are provided: julia> z = 1 + 2im Note that 3/4im = 3/(4*im) = -(3/4*im), since a literal coefficient binds more tightly than division. The promotion mechanism ensures that combinations of operands of different types just work: julia> 2(1 - 1im) You can perform all the standard arithmetic operations with complex numbers: julia> (1 + 2im)*(2 - 3im) (Using mathematicians' i or engineers' j for this global constant was rejected since they are such popular index variable names.) Since Julia allows numeric literals to be juxtaposed with identifiers as coefficients, this binding suffices to provide convenient syntax for complex numbers, similar to the traditional mathematical notation: julia> 1+2im The global constant im is bound to the complex number i, representing the principal square root of -1. Conversion and Promotion are defined so that operations on any combination of predefined numeric types, whether primitive or composite, behave as expected. Julia includes predefined types for both complex and rational numbers, and supports all the standard Mathematical Operations and Elementary Functions on them. Edit on GitHub Complex and Rational Numbers
0 Comments
Leave a Reply. |