10
|
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
2
|
|
3
|
|
4
|
|
5
|
|
6
|
|
7
|
|
8
|
|
9
|
|
10
|
|
11
|
|
12
|
|
13 <html xmlns="http://www.w3.org/1999/xhtml">
|
|
14 <head>
|
|
15 <title>Math - The Commons Math User Guide - Complex Numbers</title>
|
|
16 <style type="text/css" media="all">
|
|
17 @import url("../css/maven-base.css");
|
|
18 @import url("../css/maven-theme.css");
|
|
19 @import url("../css/site.css");
|
|
20 </style>
|
|
21 <link rel="stylesheet" href="../css/print.css" type="text/css" media="print" />
|
|
22 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
|
|
23 </head>
|
|
24 <body class="composite">
|
|
25 <div id="banner">
|
|
26 <span id="bannerLeft">
|
|
27
|
|
28 Commons Math User Guide
|
|
29
|
|
30 </span>
|
|
31 <div class="clear">
|
|
32 <hr/>
|
|
33 </div>
|
|
34 </div>
|
|
35 <div id="breadcrumbs">
|
|
36
|
|
37
|
|
38
|
|
39
|
|
40
|
|
41
|
|
42
|
|
43
|
|
44 <div class="xright">
|
|
45
|
|
46
|
|
47
|
|
48
|
|
49
|
|
50
|
|
51
|
|
52 </div>
|
|
53 <div class="clear">
|
|
54 <hr/>
|
|
55 </div>
|
|
56 </div>
|
|
57 <div id="leftColumn">
|
|
58 <div id="navcolumn">
|
|
59
|
|
60
|
|
61
|
|
62
|
|
63
|
|
64
|
|
65
|
|
66
|
|
67 <h5>User Guide</h5>
|
|
68 <ul>
|
|
69
|
|
70 <li class="none">
|
|
71 <a href="../userguide/index.html">Contents</a>
|
|
72 </li>
|
|
73
|
|
74 <li class="none">
|
|
75 <a href="../userguide/overview.html">Overview</a>
|
|
76 </li>
|
|
77
|
|
78 <li class="none">
|
|
79 <a href="../userguide/stat.html">Statistics</a>
|
|
80 </li>
|
|
81
|
|
82 <li class="none">
|
|
83 <a href="../userguide/random.html">Data Generation</a>
|
|
84 </li>
|
|
85
|
|
86 <li class="none">
|
|
87 <a href="../userguide/linear.html">Linear Algebra</a>
|
|
88 </li>
|
|
89
|
|
90 <li class="none">
|
|
91 <a href="../userguide/analysis.html">Numerical Analysis</a>
|
|
92 </li>
|
|
93
|
|
94 <li class="none">
|
|
95 <a href="../userguide/special.html">Special Functions</a>
|
|
96 </li>
|
|
97
|
|
98 <li class="none">
|
|
99 <a href="../userguide/utilities.html">Utilities</a>
|
|
100 </li>
|
|
101
|
|
102 <li class="none">
|
|
103 <strong>Complex Numbers</strong>
|
|
104 </li>
|
|
105
|
|
106 <li class="none">
|
|
107 <a href="../userguide/distribution.html">Distributions</a>
|
|
108 </li>
|
|
109
|
|
110 <li class="none">
|
|
111 <a href="../userguide/fraction.html">Fractions</a>
|
|
112 </li>
|
|
113
|
|
114 <li class="none">
|
|
115 <a href="../userguide/transform.html">Transform Methods</a>
|
|
116 </li>
|
|
117
|
|
118 <li class="none">
|
|
119 <a href="../userguide/geometry.html">3D Geometry</a>
|
|
120 </li>
|
|
121
|
|
122 <li class="none">
|
|
123 <a href="../userguide/optimization.html">Optimization</a>
|
|
124 </li>
|
|
125
|
|
126 <li class="none">
|
|
127 <a href="../userguide/ode.html">Ordinary Differential Equations</a>
|
|
128 </li>
|
|
129
|
|
130 <li class="none">
|
|
131 <a href="../userguide/genetics.html">Genetic Algorithms</a>
|
|
132 </li>
|
|
133 </ul>
|
|
134 <a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
|
|
135 <img alt="Built by Maven" src="../images/logos/maven-feather.png"></img>
|
|
136 </a>
|
|
137
|
|
138
|
|
139
|
|
140
|
|
141
|
|
142
|
|
143
|
|
144
|
|
145 </div>
|
|
146 </div>
|
|
147 <div id="bodyColumn">
|
|
148 <div id="contentBox">
|
|
149 <div class="section"><h2><a name="a7_Complex_Numbers"></a>7 Complex Numbers</h2>
|
|
150 <div class="section"><h3><a name="a7.1_Overview"></a>7.1 Overview</h3>
|
|
151 <p>
|
|
152 The complex packages provides a complex number type as well as complex
|
|
153 versions of common transcendental functions and complex number
|
|
154 formatting.
|
|
155 </p>
|
|
156 </div>
|
|
157 <div class="section"><h3><a name="a7.2_Complex_Numbers"></a>7.2 Complex Numbers</h3>
|
|
158 <p><a href="../apidocs/org/apache/commons/math/complex/Complex.html">
|
|
159 org.apache.commons.math.complex.Complex</a> provides a complex number
|
|
160 type that forms the basis for the complex functionality found in
|
|
161 commons-math.
|
|
162 </p>
|
|
163 <p>
|
|
164 Complex functions and arithmetic operations are implemented in
|
|
165 commons-math by applying standard computational formulas and
|
|
166 following the rules for <code>java.lang.Double</code> arithmetic in
|
|
167 handling infinite and <code>NaN</code> values. No attempt is made
|
|
168 to comply with ANSII/IEC C99x Annex G or any other standard for
|
|
169 Complex arithmetic. See the class and method javadocs for the
|
|
170 <a href="../apidocs/org/apache/commons/math/complex/Complex.html">
|
|
171 Complex</a> and
|
|
172 <a href="../apidocs/org/apache/commons/math/complex/ComplexUtils.html">
|
|
173 ComplexUtils</a> classes for details on computing formulas.
|
|
174 </p>
|
|
175 <p>
|
|
176 To create a complex number, simply call the constructor passing in two
|
|
177 floating-point arguments, the first being the real part of the
|
|
178 complex number and the second being the imaginary part:
|
|
179 <div class="source"><pre>Complex c = new Complex(1.0, 3.0); // 1 + 3i</pre>
|
|
180 </div>
|
|
181 </p>
|
|
182 <p>
|
|
183 Complex numbers may also be created from polar representations
|
|
184 using the <code>polar2Complex</code> method in
|
|
185 <code>ComplexUtils</code>.
|
|
186 </p>
|
|
187 <p>
|
|
188 The <code>Complex</code> class provides basic unary and binary
|
|
189 complex number operations. These operations provide the means to add,
|
|
190 subtract, multiply and divide complex numbers along with other
|
|
191 complex number functions similar to the real number functions found in
|
|
192 <code>java.math.BigDecimal</code>:
|
|
193 <div class="source"><pre>Complex lhs = new Complex(1.0, 3.0);
|
|
194 Complex rhs = new Complex(2.0, 5.0);
|
|
195
|
|
196 Complex answer = lhs.add(rhs); // add two complex numbers
|
|
197 answer = lhs.subtract(rhs); // subtract two complex numbers
|
|
198 answer = lhs.abs(); // absolute value
|
|
199 answer = lhs.conjugate(rhs); // complex conjugate</pre>
|
|
200 </div>
|
|
201 </p>
|
|
202 </div>
|
|
203 <div class="section"><h3><a name="a7.3_Complex_Transcendental_Functions"></a>7.3 Complex Transcendental Functions</h3>
|
|
204 <p><a href="../apidocs/org/apache/commons/math/complex/Complex.html">
|
|
205 org.apache.commons.math.complex.Complex</a> also provides
|
|
206 implementations of serveral transcendental functions involving complex
|
|
207 number arguments. Prior to version 1.2, these functions were provided
|
|
208 by <a href="../apidocs/org/apache/commons/math/complex/ComplexUtils.html">
|
|
209 org.apache.commons.math.complex.ComplexUtils</a> in a way similar to the real
|
|
210 number functions found in <code>java.lang.Math</code>, but this has been
|
|
211 deprecated. These operations provide the means to compute the log, sine,
|
|
212 tangent, and other complex values :
|
|
213 <div class="source"><pre>Complex first = new Complex(1.0, 3.0);
|
|
214 Complex second = new Complex(2.0, 5.0);
|
|
215
|
|
216 Complex answer = first.log(); // natural logarithm.
|
|
217 answer = first.cos(); // cosine
|
|
218 answer = first.pow(second); // first raised to the power of second</pre>
|
|
219 </div>
|
|
220 </p>
|
|
221 </div>
|
|
222 <div class="section"><h3><a name="a7.4_Complex_Formatting_and_Parsing"></a>7.4 Complex Formatting and Parsing</h3>
|
|
223 <p><code>Complex</code> instances can be converted to and from strings
|
|
224 using the<a href="../apidocs/org/apache/commons/math/complex/ComplexFormat.html">
|
|
225 org.apache.commons.math.complex.ComplexFormat</a> class.
|
|
226 <code>ComplexFormat</code> is a <code>java.text.Format</code>
|
|
227 extension and, as such, is used like other formatting objects (e.g.
|
|
228 <code>java.text.SimpleDateFormat</code>):
|
|
229 <div class="source"><pre>ComplexFormat format = new ComplexFormat(); // default format
|
|
230 Complex c = new Complex(1.1111, 2.2222);
|
|
231 String s = format.format(c); // s contains "1.11 + 2.22i"</pre>
|
|
232 </div>
|
|
233 </p>
|
|
234 <p>
|
|
235 To customize the formatting output, one or two
|
|
236 <code>java.text.NumberFormat</code> instances can be used to construct
|
|
237 a <code>ComplexFormat</code>. These number formats control the
|
|
238 formatting of the real and imaginary values of the complex number:
|
|
239 <div class="source"><pre>NumberFormat nf = NumberFormat.getInstance();
|
|
240 nf.setMinimumFractionDigits(3);
|
|
241 nf.setMaximumFractionDigits(3);
|
|
242
|
|
243 // create complex format with custom number format
|
|
244 // when one number format is used, both real and
|
|
245 // imaginary parts are formatted the same
|
|
246 ComplexFormat cf = new ComplexFormat(nf);
|
|
247 Complex c = new Complex(1.11, 2.2222);
|
|
248 String s = format.format(c); // s contains "1.110 + 2.222i"
|
|
249
|
|
250 NumberFormat nf2 = NumberFormat.getInstance();
|
|
251 nf.setMinimumFractionDigits(1);
|
|
252 nf.setMaximumFractionDigits(1);
|
|
253
|
|
254 // create complex format with custom number formats
|
|
255 cf = new ComplexFormat(nf, nf2);
|
|
256 s = format.format(c); // s contains "1.110 + 2.2i"</pre>
|
|
257 </div>
|
|
258 </p>
|
|
259 <p>
|
|
260 Another formatting customization provided by
|
|
261 <code>ComplexFormat</code> is the text used for the imaginary
|
|
262 designation. By default, the imaginary notation is "i" but, it can be
|
|
263 manipulated using the <code>setImaginaryCharacter</code> method.
|
|
264 </p>
|
|
265 <p>
|
|
266 Formatting inverse operation, parsing, can also be performed by
|
|
267 <code>ComplexFormat</code>. Parse a complex number from a string,
|
|
268 simply call the <code>parse</code> method:
|
|
269 <div class="source"><pre>ComplexFormat cf = new ComplexFormat();
|
|
270 Complex c = cf.parse("1.110 + 2.222i");</pre>
|
|
271 </div>
|
|
272 </p>
|
|
273 </div>
|
|
274 </div>
|
|
275
|
|
276 </div>
|
|
277 </div>
|
|
278 <div class="clear">
|
|
279 <hr/>
|
|
280 </div>
|
|
281 <div id="footer">
|
|
282 <div class="xright">©
|
|
283 2003-2010
|
|
284
|
|
285
|
|
286
|
|
287
|
|
288
|
|
289
|
|
290
|
|
291
|
|
292
|
|
293 </div>
|
|
294 <div class="clear">
|
|
295 <hr/>
|
|
296 </div>
|
|
297 </div>
|
|
298 </body>
|
|
299 </html>
|