正在加载图片...
5.4 Complex Arithmetic 177 Actually,complex arithmetic is not quite trivial.Addition and subtraction are done in the obvious way,performing the operation separately on the real and imaginary parts of the operands.Multiplication can also be done in the obvious way, with 4 multiplications,one addition,and one subtraction, (a+ib)(c+id)=(ac-bd)+i(bc+ad) (5.4.1) (the addition before the i doesn't count;it just separates the real and imaginary parts notationally).But it is sometimes faster to multiply via (a +ib)(c+id)=(ac-bd)+il(a+b)(c+d)-ac-bd (5.4.2) which has only three multiplications (ac,bd,(a+b)(c+d)).plus two additions and three subtractions.The total operations count is higher by two,but multiplication is a slow operation on some machines. While it is true that intermediate results in equations(5.4.1)and(5.4.2)can 形 overflow even when the final result is representable,this happens only when the final answer is on the edge of representability.Not so for the complex modulus,if you 令 are misguided enough to compute it as la+ibl Va2+62 (5.4.3) Press. (bad!) whose intermediate result will overflow if either a or b is as large as the square Programs root of the largest representable number(e.g.,1019 as compared to 1038).The right way to do the calculation is SCIENTIFIC 6 |a+b= fla1+(b/a严la≥bl 1Ib1+(a/b)2 (5.4.4) lal b Complex division should use a similar trick to prevent avoidable overflows, 1920 COMPUTING (ISBN underflow,or loss of precision, [a+b(dlcl+b-a(dlc】 装 Numerical 10521 a ib c+d(d/c) d≥ (5.4.5) 43108 c+id [a(c/d)++b(cld-a可 ldl ldl Recipes c(c/d)+d (outside Of course you should calculate repeated subexpressions,like c/d or d/c,only once. North Software. Complex square root is even more complicated,since we must both guard intermediate results,and also enforce a chosen branch cut (here taken to be the negative real axis).To take the square root of c+id,first compute 0 c=d=0 1+vV1+(d/c)2 1c≥ld 0三 2 (5.4.6) lc/d+1+(c/d)2 lel la 25.4 Complex Arithmetic 177 Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copyin Copyright (C) 1988-1992 by Cambridge University Press. Programs Copyright (C) 1988-1992 by Numerical Recipes Software. Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5) g of machine￾readable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to directcustserv@cambridge.org (outside North America). Actually, complex arithmetic is not quite trivial. Addition and subtraction are done in the obvious way, performing the operation separately on the real and imaginary parts of the operands. Multiplication can also be done in the obvious way, with 4 multiplications, one addition, and one subtraction, (a + ib)(c + id)=(ac − bd) + i(bc + ad) (5.4.1) (the addition before the i doesn’t count; it just separates the real and imaginary parts notationally). But it is sometimes faster to multiply via (a + ib)(c + id)=(ac − bd) + i[(a + b)(c + d) − ac − bd] (5.4.2) which has only three multiplications (ac, bd, (a + b)(c + d)), plus two additions and three subtractions. The total operations count is higher by two, but multiplication is a slow operation on some machines. While it is true that intermediate results in equations (5.4.1) and (5.4.2) can overflow even when the final result is representable, this happens only when the final answer is on the edge of representability. Not so for the complex modulus, if you are misguided enough to compute it as |a + ib| = a2 + b2 (bad!) (5.4.3) whose intermediate result will overflow if either a or b is as large as the square root of the largest representable number (e.g., 10 19 as compared to 1038). The right way to do the calculation is |a + ib| = |a| 1+(b/a)2 |a|≥|b| |b| 1+(a/b)2 |a| < |b| (5.4.4) Complex division should use a similar trick to prevent avoidable overflows, underflow, or loss of precision, a + ib c + id =    [a + b(d/c)] + i[b − a(d/c)] c + d(d/c) |c|≥|d| [a(c/d) + b] + i[b(c/d) − a] c(c/d) + d |c| < |d| (5.4.5) Of course you should calculate repeated subexpressions, like c/d or d/c, only once. Complex square root is even more complicated, since we must both guard intermediate results, and also enforce a chosen branch cut (here taken to be the negative real axis). To take the square root of c + id, first compute w ≡    0 c = d = 0 |c|  1 + 1+(d/c)2 2 |c|≥|d| |d|  |c/d| + 1+(c/d)2 2 |c| < |d| (5.4.6)
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有