jitty-scripts/julia-0.6.3/share/doc/julia/html/en/manual/complex-and-rational-numbers.html
mollusk 0e4acfb8f2 fix incorrect folder name for julia-0.6.x
Former-commit-id: ef2c7401e0876f22d2f7762d182cfbcd5a7d9c70
2018-06-11 03:28:36 -07:00

192 lines
19 KiB
HTML

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><title>Complex and Rational Numbers · The Julia Language</title><script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-28835595-6', 'auto');
ga('send', 'pageview');
</script><link href="https://cdnjs.cloudflare.com/ajax/libs/normalize/4.2.0/normalize.min.css" rel="stylesheet" type="text/css"/><link href="https://fonts.googleapis.com/css?family=Lato|Roboto+Mono" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css" rel="stylesheet" type="text/css"/><script>documenterBaseURL=".."</script><script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.2.0/require.min.js" data-main="../assets/documenter.js"></script><script src="../siteinfo.js"></script><script src="../../versions.js"></script><link href="../assets/documenter.css" rel="stylesheet" type="text/css"/><link href="../assets/julia-manual.css" rel="stylesheet" type="text/css"/></head><body><nav class="toc"><a href="../index.html"><img class="logo" src="../assets/logo.png" alt="The Julia Language logo"/></a><h1>The Julia Language</h1><select id="version-selector" onChange="window.location.href=this.value" style="visibility: hidden"></select><form class="search" id="search-form" action="../search.html"><input id="search-query" name="q" type="text" placeholder="Search docs"/></form><ul><li><a class="toctext" href="../index.html">Home</a></li><li><span class="toctext">Manual</span><ul><li><a class="toctext" href="introduction.html">Introduction</a></li><li><a class="toctext" href="getting-started.html">Getting Started</a></li><li><a class="toctext" href="variables.html">Variables</a></li><li><a class="toctext" href="integers-and-floating-point-numbers.html">Integers and Floating-Point Numbers</a></li><li><a class="toctext" href="mathematical-operations.html">Mathematical Operations and Elementary Functions</a></li><li class="current"><a class="toctext" href="complex-and-rational-numbers.html">Complex and Rational Numbers</a><ul class="internal"><li><a class="toctext" href="#Complex-Numbers-1">Complex Numbers</a></li><li><a class="toctext" href="#Rational-Numbers-1">Rational Numbers</a></li></ul></li><li><a class="toctext" href="strings.html">Strings</a></li><li><a class="toctext" href="functions.html">Functions</a></li><li><a class="toctext" href="control-flow.html">Control Flow</a></li><li><a class="toctext" href="variables-and-scoping.html">Scope of Variables</a></li><li><a class="toctext" href="types.html">Types</a></li><li><a class="toctext" href="methods.html">Methods</a></li><li><a class="toctext" href="constructors.html">Constructors</a></li><li><a class="toctext" href="conversion-and-promotion.html">Conversion and Promotion</a></li><li><a class="toctext" href="interfaces.html">Interfaces</a></li><li><a class="toctext" href="modules.html">Modules</a></li><li><a class="toctext" href="documentation.html">Documentation</a></li><li><a class="toctext" href="metaprogramming.html">Metaprogramming</a></li><li><a class="toctext" href="arrays.html">Multi-dimensional Arrays</a></li><li><a class="toctext" href="linear-algebra.html">Linear algebra</a></li><li><a class="toctext" href="networking-and-streams.html">Networking and Streams</a></li><li><a class="toctext" href="parallel-computing.html">Parallel Computing</a></li><li><a class="toctext" href="dates.html">Date and DateTime</a></li><li><a class="toctext" href="interacting-with-julia.html">Interacting With Julia</a></li><li><a class="toctext" href="running-external-programs.html">Running External Programs</a></li><li><a class="toctext" href="calling-c-and-fortran-code.html">Calling C and Fortran Code</a></li><li><a class="toctext" href="handling-operating-system-variation.html">Handling Operating System Variation</a></li><li><a class="toctext" href="environment-variables.html">Environment Variables</a></li><li><a class="toctext" href="embedding.html">Embedding Julia</a></li><li><a class="toctext" href="packages.html">Packages</a></li><li><a class="toctext" href="profile.html">Profiling</a></li><li><a class="toctext" href="stacktraces.html">Stack Traces</a></li><li><a class="toctext" href="performance-tips.html">Performance Tips</a></li><li><a class="toctext" href="workflow-tips.html">Workflow Tips</a></li><li><a class="toctext" href="style-guide.html">Style Guide</a></li><li><a class="toctext" href="faq.html">Frequently Asked Questions</a></li><li><a class="toctext" href="noteworthy-differences.html">Noteworthy Differences from other Languages</a></li><li><a class="toctext" href="unicode-input.html">Unicode Input</a></li></ul></li><li><span class="toctext">Standard Library</span><ul><li><a class="toctext" href="../stdlib/base.html">Essentials</a></li><li><a class="toctext" href="../stdlib/collections.html">Collections and Data Structures</a></li><li><a class="toctext" href="../stdlib/math.html">Mathematics</a></li><li><a class="toctext" href="../stdlib/numbers.html">Numbers</a></li><li><a class="toctext" href="../stdlib/strings.html">Strings</a></li><li><a class="toctext" href="../stdlib/arrays.html">Arrays</a></li><li><a class="toctext" href="../stdlib/parallel.html">Tasks and Parallel Computing</a></li><li><a class="toctext" href="../stdlib/linalg.html">Linear Algebra</a></li><li><a class="toctext" href="../stdlib/constants.html">Constants</a></li><li><a class="toctext" href="../stdlib/file.html">Filesystem</a></li><li><a class="toctext" href="../stdlib/io-network.html">I/O and Network</a></li><li><a class="toctext" href="../stdlib/punctuation.html">Punctuation</a></li><li><a class="toctext" href="../stdlib/sort.html">Sorting and Related Functions</a></li><li><a class="toctext" href="../stdlib/pkg.html">Package Manager Functions</a></li><li><a class="toctext" href="../stdlib/dates.html">Dates and Time</a></li><li><a class="toctext" href="../stdlib/iterators.html">Iteration utilities</a></li><li><a class="toctext" href="../stdlib/test.html">Unit Testing</a></li><li><a class="toctext" href="../stdlib/c.html">C Interface</a></li><li><a class="toctext" href="../stdlib/libc.html">C Standard Library</a></li><li><a class="toctext" href="../stdlib/libdl.html">Dynamic Linker</a></li><li><a class="toctext" href="../stdlib/profile.html">Profiling</a></li><li><a class="toctext" href="../stdlib/stacktraces.html">StackTraces</a></li><li><a class="toctext" href="../stdlib/simd-types.html">SIMD Support</a></li></ul></li><li><span class="toctext">Developer Documentation</span><ul><li><a class="toctext" href="../devdocs/reflection.html">Reflection and introspection</a></li><li><span class="toctext">Documentation of Julia&#39;s Internals</span><ul><li><a class="toctext" href="../devdocs/init.html">Initialization of the Julia runtime</a></li><li><a class="toctext" href="../devdocs/ast.html">Julia ASTs</a></li><li><a class="toctext" href="../devdocs/types.html">More about types</a></li><li><a class="toctext" href="../devdocs/object.html">Memory layout of Julia Objects</a></li><li><a class="toctext" href="../devdocs/eval.html">Eval of Julia code</a></li><li><a class="toctext" href="../devdocs/callconv.html">Calling Conventions</a></li><li><a class="toctext" href="../devdocs/compiler.html">High-level Overview of the Native-Code Generation Process</a></li><li><a class="toctext" href="../devdocs/functions.html">Julia Functions</a></li><li><a class="toctext" href="../devdocs/cartesian.html">Base.Cartesian</a></li><li><a class="toctext" href="../devdocs/meta.html">Talking to the compiler (the <code>:meta</code> mechanism)</a></li><li><a class="toctext" href="../devdocs/subarrays.html">SubArrays</a></li><li><a class="toctext" href="../devdocs/sysimg.html">System Image Building</a></li><li><a class="toctext" href="../devdocs/llvm.html">Working with LLVM</a></li><li><a class="toctext" href="../devdocs/stdio.html">printf() and stdio in the Julia runtime</a></li><li><a class="toctext" href="../devdocs/boundscheck.html">Bounds checking</a></li><li><a class="toctext" href="../devdocs/locks.html">Proper maintenance and care of multi-threading locks</a></li><li><a class="toctext" href="../devdocs/offset-arrays.html">Arrays with custom indices</a></li><li><a class="toctext" href="../devdocs/libgit2.html">Base.LibGit2</a></li><li><a class="toctext" href="../devdocs/require.html">Module loading</a></li></ul></li><li><span class="toctext">Developing/debugging Julia&#39;s C code</span><ul><li><a class="toctext" href="../devdocs/backtraces.html">Reporting and analyzing crashes (segfaults)</a></li><li><a class="toctext" href="../devdocs/debuggingtips.html">gdb debugging tips</a></li><li><a class="toctext" href="../devdocs/valgrind.html">Using Valgrind with Julia</a></li><li><a class="toctext" href="../devdocs/sanitizers.html">Sanitizer support</a></li></ul></li></ul></li></ul></nav><article id="docs"><header><nav><ul><li>Manual</li><li><a href="complex-and-rational-numbers.html">Complex and Rational Numbers</a></li></ul><a class="edit-page" href="https://github.com/JuliaLang/julia/blob/master/doc/src/manual/complex-and-rational-numbers.md"><span class="fa"></span> Edit on GitHub</a></nav><hr/><div id="topbar"><span>Complex and Rational Numbers</span><a class="fa fa-bars" href="#"></a></div></header><h1><a class="nav-anchor" id="Complex-and-Rational-Numbers-1" href="#Complex-and-Rational-Numbers-1">Complex and Rational Numbers</a></h1><p>Julia ships with predefined types representing both complex and rational numbers, and supports all standard <a href="mathematical-operations.html#Mathematical-Operations-and-Elementary-Functions-1">Mathematical Operations and Elementary Functions</a> on them. <a href="conversion-and-promotion.html#conversion-and-promotion-1">Conversion and Promotion</a> are defined so that operations on any combination of predefined numeric types, whether primitive or composite, behave as expected.</p><h2><a class="nav-anchor" id="Complex-Numbers-1" href="#Complex-Numbers-1">Complex Numbers</a></h2><p>The global constant <a href="../stdlib/numbers.html#Base.im"><code>im</code></a> is bound to the complex number <em>i</em>, representing the principal square root of -1. It was deemed harmful to co-opt the name <code>i</code> for a global constant, since it is such a popular index variable name. Since Julia allows numeric literals to be <a href="integers-and-floating-point-numbers.html#man-numeric-literal-coefficients-1">juxtaposed with identifiers as coefficients</a>, this binding suffices to provide convenient syntax for complex numbers, similar to the traditional mathematical notation:</p><pre><code class="language-julia-repl">julia&gt; 1 + 2im
1 + 2im</code></pre><p>You can perform all the standard arithmetic operations with complex numbers:</p><pre><code class="language-julia-repl">julia&gt; (1 + 2im)*(2 - 3im)
8 + 1im
julia&gt; (1 + 2im)/(1 - 2im)
-0.6 + 0.8im
julia&gt; (1 + 2im) + (1 - 2im)
2 + 0im
julia&gt; (-3 + 2im) - (5 - 1im)
-8 + 3im
julia&gt; (-1 + 2im)^2
-3 - 4im
julia&gt; (-1 + 2im)^2.5
2.7296244647840084 - 6.960664459571898im
julia&gt; (-1 + 2im)^(1 + 1im)
-0.27910381075826657 + 0.08708053414102428im
julia&gt; 3(2 - 5im)
6 - 15im
julia&gt; 3(2 - 5im)^2
-63 - 60im
julia&gt; 3(2 - 5im)^-1.0
0.20689655172413796 + 0.5172413793103449im</code></pre><p>The promotion mechanism ensures that combinations of operands of different types just work:</p><pre><code class="language-julia-repl">julia&gt; 2(1 - 1im)
2 - 2im
julia&gt; (2 + 3im) - 1
1 + 3im
julia&gt; (1 + 2im) + 0.5
1.5 + 2.0im
julia&gt; (2 + 3im) - 0.5im
2.0 + 2.5im
julia&gt; 0.75(1 + 2im)
0.75 + 1.5im
julia&gt; (2 + 3im) / 2
1.0 + 1.5im
julia&gt; (1 - 3im) / (2 + 2im)
-0.5 - 1.0im
julia&gt; 2im^2
-2 + 0im
julia&gt; 1 + 3/4im
1.0 - 0.75im</code></pre><p>Note that <code>3/4im == 3/(4*im) == -(3/4*im)</code>, since a literal coefficient binds more tightly than division.</p><p>Standard functions to manipulate complex values are provided:</p><pre><code class="language-julia-repl">julia&gt; z = 1 + 2im
1 + 2im
julia&gt; real(1 + 2im) # real part of z
1
julia&gt; imag(1 + 2im) # imaginary part of z
2
julia&gt; conj(1 + 2im) # complex conjugate of z
1 - 2im
julia&gt; abs(1 + 2im) # absolute value of z
2.23606797749979
julia&gt; abs2(1 + 2im) # squared absolute value
5
julia&gt; angle(1 + 2im) # phase angle in radians
1.1071487177940904</code></pre><p>As usual, the absolute value (<a href="../stdlib/math.html#Base.abs"><code>abs()</code></a>) of a complex number is its distance from zero. <a href="../stdlib/math.html#Base.abs2"><code>abs2()</code></a> gives the square of the absolute value, and is of particular use for complex numbers where it avoids taking a square root. <a href="../stdlib/math.html#Base.angle"><code>angle()</code></a> returns the phase angle in radians (also known as the <em>argument</em> or <em>arg</em> function). The full gamut of other <a href="mathematical-operations.html#Elementary-Functions-1">Elementary Functions</a> is also defined for complex numbers:</p><pre><code class="language-julia-repl">julia&gt; sqrt(1im)
0.7071067811865476 + 0.7071067811865475im
julia&gt; sqrt(1 + 2im)
1.272019649514069 + 0.7861513777574233im
julia&gt; cos(1 + 2im)
2.0327230070196656 - 3.0518977991518im
julia&gt; exp(1 + 2im)
-1.1312043837568135 + 2.4717266720048188im
julia&gt; sinh(1 + 2im)
-0.4890562590412937 + 1.4031192506220405im</code></pre><p>Note that mathematical functions typically return real values when applied to real numbers and complex values when applied to complex numbers. For example, <a href="../stdlib/math.html#Base.sqrt"><code>sqrt()</code></a> behaves differently when applied to <code>-1</code> versus <code>-1 + 0im</code> even though <code>-1 == -1 + 0im</code>:</p><pre><code class="language-julia-repl">julia&gt; sqrt(-1)
ERROR: DomainError:
sqrt will only return a complex result if called with a complex argument. Try sqrt(complex(x)).
Stacktrace:
[1] sqrt(::Int64) at ./math.jl:434
julia&gt; sqrt(-1 + 0im)
0.0 + 1.0im</code></pre><p>The <a href="integers-and-floating-point-numbers.html#man-numeric-literal-coefficients-1">literal numeric coefficient notation</a> does not work when constructing a complex number from variables. Instead, the multiplication must be explicitly written out:</p><pre><code class="language-julia-repl">julia&gt; a = 1; b = 2; a + b*im
1 + 2im</code></pre><p>However, this is <em>not</em> recommended; Use the <a href="../stdlib/numbers.html#Base.complex-Tuple{Complex}"><code>complex()</code></a> function instead to construct a complex value directly from its real and imaginary parts:</p><pre><code class="language-julia-repl">julia&gt; a = 1; b = 2; complex(a, b)
1 + 2im</code></pre><p>This construction avoids the multiplication and addition operations.</p><p><a href="../stdlib/numbers.html#Base.Inf"><code>Inf</code></a> and <a href="../stdlib/numbers.html#Base.NaN"><code>NaN</code></a> propagate through complex numbers in the real and imaginary parts of a complex number as described in the <a href="integers-and-floating-point-numbers.html#Special-floating-point-values-1">Special floating-point values</a> section:</p><pre><code class="language-julia-repl">julia&gt; 1 + Inf*im
1.0 + Inf*im
julia&gt; 1 + NaN*im
1.0 + NaN*im</code></pre><h2><a class="nav-anchor" id="Rational-Numbers-1" href="#Rational-Numbers-1">Rational Numbers</a></h2><p>Julia has a rational number type to represent exact ratios of integers. Rationals are constructed using the <a href="../stdlib/math.html#Base.://"><code>//</code></a> operator:</p><pre><code class="language-julia-repl">julia&gt; 2//3
2//3</code></pre><p>If the numerator and denominator of a rational have common factors, they are reduced to lowest terms such that the denominator is non-negative:</p><pre><code class="language-julia-repl">julia&gt; 6//9
2//3
julia&gt; -4//8
-1//2
julia&gt; 5//-15
-1//3
julia&gt; -4//-12
1//3</code></pre><p>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. The standardized numerator and denominator of a rational value can be extracted using the <a href="../stdlib/math.html#Base.numerator"><code>numerator()</code></a> and <a href="../stdlib/math.html#Base.denominator"><code>denominator()</code></a> functions:</p><pre><code class="language-julia-repl">julia&gt; numerator(2//3)
2
julia&gt; denominator(2//3)
3</code></pre><p>Direct comparison of the numerator and denominator is generally not necessary, since the standard arithmetic and comparison operations are defined for rational values:</p><pre><code class="language-julia-repl">julia&gt; 2//3 == 6//9
true
julia&gt; 2//3 == 9//27
false
julia&gt; 3//7 &lt; 1//2
true
julia&gt; 3//4 &gt; 2//3
true
julia&gt; 2//4 + 1//6
2//3
julia&gt; 5//12 - 1//4
1//6
julia&gt; 5//8 * 3//12
5//32
julia&gt; 6//5 / 10//7
21//25</code></pre><p>Rationals can be easily converted to floating-point numbers:</p><pre><code class="language-julia-repl">julia&gt; float(3//4)
0.75</code></pre><p>Conversion from rational to floating-point respects the following identity for any integral values of <code>a</code> and <code>b</code>, with the exception of the case <code>a == 0</code> and <code>b == 0</code>:</p><pre><code class="language-julia-repl">julia&gt; a = 1; b = 2;
julia&gt; isequal(float(a//b), a/b)
true</code></pre><p>Constructing infinite rational values is acceptable:</p><pre><code class="language-julia-repl">julia&gt; 5//0
1//0
julia&gt; -3//0
-1//0
julia&gt; typeof(ans)
Rational{Int64}</code></pre><p>Trying to construct a <a href="../stdlib/numbers.html#Base.NaN"><code>NaN</code></a> rational value, however, is not:</p><pre><code class="language-julia-repl">julia&gt; 0//0
ERROR: ArgumentError: invalid rational: zero(Int64)//zero(Int64)
Stacktrace:
[1] Rational{Int64}(::Int64, ::Int64) at ./rational.jl:13
[2] //(::Int64, ::Int64) at ./rational.jl:40</code></pre><p>As usual, the promotion system makes interactions with other numeric types effortless:</p><pre><code class="language-julia-repl">julia&gt; 3//5 + 1
8//5
julia&gt; 3//5 - 0.5
0.09999999999999998
julia&gt; 2//7 * (1 + 2im)
2//7 + 4//7*im
julia&gt; 2//7 * (1.5 + 2im)
0.42857142857142855 + 0.5714285714285714im
julia&gt; 3//2 / (1 + 2im)
3//10 - 3//5*im
julia&gt; 1//2 + 2im
1//2 + 2//1*im
julia&gt; 1 + 2//3im
1//1 - 2//3*im
julia&gt; 0.5 == 1//2
true
julia&gt; 0.33 == 1//3
false
julia&gt; 0.33 &lt; 1//3
true
julia&gt; 1//3 - 0.33
0.0033333333333332993</code></pre><footer><hr/><a class="previous" href="mathematical-operations.html"><span class="direction">Previous</span><span class="title">Mathematical Operations and Elementary Functions</span></a><a class="next" href="strings.html"><span class="direction">Next</span><span class="title">Strings</span></a></footer></article></body></html>