// ------------------------------------------------------------ // cxtext.cpp 7 Sept 2000 // ------------------------------------------------------------ #include "w.h" #include "complex.h" #include "complex.cpp" char prog[40] = "cxtest.cpp"; // ------------------------------------------------------------ void main() { nl(0); banner(prog); Complex c(1,1/3.0),v(7),u; // 7 is 'cast' to (7,0) p("c = "); p(c); p(" v = "); p(v); p(" u = "); pl(u); pfl("real = %.6g imag = %.6g",real(c),imag(c)); u = c + c; p("u = c + c = "); pl(u); u = u*c; p("u*c = "); pl(u); // optional format adjustment setCstr(".15g"); p("u*c = "); pl(u); // reset format string to default value setCstr(".6g"); p("u*c = "); pl(u); p("conj(c) = "); pl(conj(c)); pfl("norm2(c) = %.6g norm(c) = %.6g", norm2(c), norm(c)); u = 3*power(c,Pi); p("3*(c^pi) = "); pl(u); p("(0, 1)^pi = "); pl(power(Complex(0,1),Pi)); double a = Pi; a = a*a*0.5; pfl("check (0, 1)^pi = (%.6g, %.6g)",cos(a),sin(a)); pfl("arg(0,-1) = %.6g",arg(Complex(0,-1))); setCstr(".15g"); p("c^5 = "); pl(power(c,5)); p(" = "); pl(powi(c,5)); Complex q(1,2); p("q = "); p(q); q.real() = 3; // Complex::real() is public and Reference p(" q = "); pl(q); // R(q) has been changed to 3 q = q + 1; pl(q); pl(powi(Complex(5,7),3)); nl(); Complex ca[3] = {Complex(1,2),Complex(3,-1),q}; // array initialization p("ca[] = "); for (int i = 0; i < 3; i++) { p(ca[i]); p(" "); } nl(); ca[0] = ca[1] = ca[2] = powi(Complex(1,-1), 7); // chain of = is OK p("ca[] = "); for (i = 0; i < 3; i++) { p(ca[i]); p(" "); } // i still in scope } /* output ------------ cxtest.cpp ------------ c = (1, 0.333333) v = (7, 0) u = (0, 0) real = 1 imag = 0.333333 u = c + c = (2, 0.666667) u*c = (1.77778, 1.33333) u*c = (1.77777777777778, 1.33333333333333) u*c = (1.77778, 1.33333) conj(c) = (1, -0.333333) norm2(c) = 1.11111 norm(c) = 1.05409 3*(c^pi) = (1.88033, 2.99926) (0, 1)^pi = (0.220584, -0.975368) check (0, 1)^pi = (0.220584, -0.975368) arg(0,-1) = -1.5708 c^5 = (-0.0493827160493826, 1.30041152263374) = (-0.0493827160493826, 1.30041152263374) q = (1, 2) q = (3, 2) (4, 2) (-610, 182) ca[] = (1, 2) (3, -1) (4, 2) ca[] = (8, 8) (8, 8) (8, 8) */