Thompson - Computing for Scientists and Engineers (523188), страница 7
Текст из файла (страница 7)
nNow that we have summarized the formal basis of complex-variable algebra, it istime to consider complex arithmetic, especially for computer applications.Programming with complex numbersFew computer languages are designed to include complex-variable types in theirstandard definition. They are available in Fortran, but not in C or Pascal. In Wolfram’s Mathematica system for doing mathematics by computer, which has bothsymbolic and numeric capabilities, complex numbers can be handled readily. Anintroduction to their use is provided in Section 1.1 of Wolfram’s book.To appreciate why computer hardware is not built and computer software is notdesigned to assume that numbers they handle are complex, consider the followingexercise.Exercise 2.2Show that the total number of real-arithmetic operations needed for complexnumber addition and subtraction is 2, the number for multiplication is 6, and thenumber for division is 11 or 14, depending on whether or not the divisor in(2.9) is stored.
nWe now show a simple program in C language for performing complex arithmetic by the rules given in the preceding subsection. The purpose of this program istwofold: if you are unfamiliar with the C language the program will provide a simpleintroduction, while it will also develop your understanding of complex arithmetic.The program Complex-Arithmetic Functions takes as input x1, y1, x2, y2for the components of two complex numbers z1 and z2. After checking that bothnumbers are nonzero, it calls the functions for addition, subtraction, multiplication,and division, namely CAdd, CSub, CMult, and CDiv, then prints the results beforereturning for more input. Here is the program.2.1 ALGEBRA AND COMPUTING WITH COMPLEX NUMBERS21PROGRAM 2.1 Functions for performing complex arithmetic; addition, subtraction, multiplication, and division.#include <stdio.h>#include <math.h>main (){/* Complex-Arithmetic Functions */doublex1,x2,y1,y2,x1a2,y1a2,x1S2,y1S2,x1m2,y1m2,x1d2,y1d2;void CAdd(),CSub(),CMult(),CDiv();printf("Complex-Arithmetic Functions\n");x1 = 1; y1 = 1; x2 = 1; y2 = 1;/* Check that at least one complex numbers is not zero */while ( xl !=0 || yl != 0 || x2 != 0 || y2 != 0 )printf("\nInput x1,y1,x2,y2 (all zero to end):\n");scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);if ( xl == 0 && y1 == 0 && x2 == 0 && y2 == 0 )printf("\nEndexit(0);}Complex-ArithmeticFunctions");CAdd(xl,yl,x2,y2,&x1a2,&y1a2); /* complex Add *//* returns x1a2, y1a2 */CSub(xl,yl,x2,y2,&x1s2,&y1s2); /* complex Subtract *//* returns x1s2, yls2 */CMult(x1,y1,x2,y2,&x1m2,&y1m2);/* complex Multiply *//* returns x1m2, y1m2 */CDiv(x1,yl,x2,y2,&x1d2,&y1d2); /* complex Divide *//* returns x1d2, y1d2 */printf("\nz1+z2=(%lf) + i(%lf)",x1a2,y1a2);printf("\nz1-z2=(%lf) + i(%lf)",x1s2,y1s2);printf("\nz1*z2=(%lf) + i(%lf)",X1m2,y1m2);printf("\nz1/z2=(%lf) + i(%lf)\n",x1d2,y1d2);22COMPLEX VARIABLESvoid CAdd(x1,y1,x2,y2,x1a2,y1a2)/* Complex Addition function */double x1,y1,x2,y2,*x1a2,*y1a2;{*x1a2 = x1+x2; *y1a2 = y1+y2;void CSub(x1,y1,x2,y2,x1s2,y1s2)/* Complex Subtraction function */double x1,y1,x2,y2,*x1s2,*y1s2;{*x1s2 = xl-x2; *y1s2 = y1-y2;}void CMult(x1,y1,x2,y2,x1m2,y1m2)/* Complex Multiplication function */double x1,y1,x2,y2,*x1m2,*y1m2;{*x1m2 = x1*x2-y1*y2;*y1m2 = x1*y2+y1*x2;}void CDiv(x1,y1,x2,y2,x1d2,y1d2)/* Complex Division function */double x1,y1,x2,y2,*x1d2,*y1d2;{double den;den = x2*x2+y2*y2;if ( den == 0 ){printf("!! CDiv denominator = 0; dividend set to zero");*x1d2 = 0; *y1d2 = 0;}else{*x1d2 = (x1*x2+y1*y2)/den;*y1d2 = (y1*x2-x1*y2)/den;The program reveals an immediate difficulty with modifying a language to include complex variables, in that two values must be returned by a complex-variable2.1 ALGEBRA AND COMPUTING WITH COMPLEX NUMBERS23function.
In C this cannot be done simply by a conventional function (which returnsjust one value, at most). One can get around the problem by using the indirection(dereferencing) operator, written as an asterisk (*) preceding a variable name, asused for each of the two real-variable values returned by the program functions.Here are some suggestions for exploring complex numbers by using the program Complex-Arithmetic Functions.Exercise 2.3(a) Use several pairs of real numbers for both inputs ( y1 = 0, y2 = 0 ) in order to verify that the complex numbers contain real numbers as special cases.(b) Input purely imaginary numbers ( xl = 0, x2 = 0 ) to the program andverify the correctness of the arithmetic.(c) Show by a careful analytical proof that if the product of two complex numbers is zero, then at least one of the complex numbers is identically zero (bothreal and imaginary parts zero). Prove that if one of a pair of complex numbers iszero, their product is zero.
Verify this by using the program. nWith this background of algebraic and arithmetic properties of complex numbers, we are prepared to review some more formal definitions and properties.Complex conjugation, modulus, argumentIn complex-variable algebra and arithmetic one often needs complex quantities thatare related by reversal of the sign of just their imaginary parts. We therefore havethe operation called complex conjugation. In mathematics texts the notation for thisoperation is often denoted by a bar, - , while other scientists often use an asterisk,as * .
In the latter notation the complex-conjugate value of z isz*=x-iy(2.11)z=x+iy(2.12)if and only ifFrom the definition of complex conjugation we can readily derive several interestingresults.Exercise 2.4(a) Prove that(2.13)where the notation Re stands for “real part of.”24COMPLEX VARIABLES(b) Similarly, prove that(2.14)where Im denotes “imaginary part of.”(c) Derive the following properties of complex conjugation:(2.15)(2.17)which show that complex conjugation is distributive over addition, subtraction,multiplication, and division. nThe identityzz*=x2 y2(2.18)shows that z z * is zero only if z is identically zero, which is an example of the condition from Exercise 2.3 (c) for vanishing of the product of two complex numbers.The frequent occurrence of z z * and its connection with vectors in two dimensions lead to the notation of the modulus of a complex number z, denoted by(2.19)Thus mod z indicates the magnitude of z if we picture it as a vector in the x - yplane.
Another name for modulus is absolute value. For example, the modulus of areal number is just its value without regard to sign, that is, its absolute value. Themodulus of a pure imaginary number is just the value of y without regard to sign.The modulus of a complex number is zero if and only if both its real and its imaginary part are zero.The argument of a complex number is introduced similarly to polar coordinatesfor two-dimensional vectors. One defines the arg function for a complex variableby the requirement that(2.20)and the requirement that252.1 ALGEBRA AND COMPUTING WITH COMPLEX NUMBERS(2.21)which are necessary and sufficient conditions for definition of arg z to within amultiple of 27Exercise 2.5Explain why the commonly given formula(2.22)is not sufficient to specify arg z uniquely, even to within a multiple of 27nIn Section 2.6, in the program to convert between coordinates, we return to thisproblem of ambiguous angles.
The argument of a complex number is sometimescalled the phase angle, or (confusingly) the amplitude.One aspect of the argument relates to the analogy between complex variables andplanar vectors. If the pair (x,y) formed the components of a vector, then arg zwould be the angle that the vector makes with the positive x axis. For example,arg (Re z) = ± arg i = /2, and arg (-i) = - / 2 .A program for complex conjugate and modulusFor applications with complex variables it is worthwhile to have available programsfor complex functions. We provide here a program that invokes functions returningcomplex conjugate and modulus values. The more-involved coding for the argument function is provided in the programming project, Section 2.6.
Here is the program Conjugate & Modulus Functions.PROGRAM 2.2 Conjugate and modulus functions for complex numbers.#include#include<stdio.h><math.h>main(){/* Conjugate & Modulus Functions */double x,y,xc,yc, zmod;void CConjugate();double CModulus();26COMPLEX VARIABLESprintf("Complex Conjugate & Modulus Functions\n");x = 1; y = 1;/* Check for x not zero or y not zero */while ( x != 0 || y != 0 ){printf("\nInput x,y (both zero to end):\n");scanf("%lf%lf",&x,&y) ;if ( x == 0 && y == 0 ){printf("\nEnd Conjugate & Modulus Functions");exit (0) ;}CConjugate(x,y,&xc,&yc); /* conjugate x+iy */zmod = CModulus(x,y); /* modulus of x+iy */printf("z* = (%lf) + i(%lf)",xc,yc);printf("\nmod [(%lf) + i(%lf)] = %lf\n",x,y,zmod);}void CConjugate(x,y,xc,yc)/* Complex Conjugation function */double x,y,*xc,*yc; /* xc & yc are returned */{*xc = x; "yc = -y;}double CModulus(x,y)/* Complex Modulus function */double x,y;{double mod;mod = sqrt (x*x+y*y);return mod;Some remarks on programming the functions CConjugate and CModulus inthe C language are in order:1.