/**************************************************************************** Kalkulon - A programmable calculator for programmers This is an example Kalkulon script. To load from within Kalkulon: Load("examples/scriptname.k") then you can call functions like: funcname() or funcname(arg0, ...) Author: Juergen Holetzeck 2003 - 2007 e-mail: contact@kalkulon.de homepage: www.kalkulon.de ****************************************************************************/ /////////////////////////////////////////////////////// // simple implementation to support complex numbers // // complex numbers are 2-element lists, e.g. {2,3} // the following conventions are used: // z is complex number in cartesian coordinates, e.g. {2,3} -> 2+3i // zp is complex number in polar coordinates, e.g. {2,3} -> r=2, phi=3, r*exp(i*phi) // x is real number ::Pi=2*acos(0); reC(z) = z[0]; imC(z) = z[1]; C(x) = {x, 0}; absC(z) = sqrt(z[0]*z[0]+z[1]*z[1]); conjC(z)= {z[0], -z[1]}; phiC(z)= ( x=z[0], y=z[1], if(x==0 && y==0; phi=0; // {0, 0} if(x==0; phi=Pi/2; phi=atan(abs(y/x))), // quadrant I if(x<0 && y>=0; phi=Pi-phi; // quadrant II if(x<=0 && y<0; phi=phi+Pi; // quadrant III if(x>0 && y<0; phi=2*Pi-phi) // quadrant IV ) ) ), phi ); // returns zp polarC(z) = {absC(z), phiC(z)}; // returns z cartC(zp) = (r=zp[0], phi=zp[1], {r*cos(phi), r*sin(phi)}); addC(z1, z2) = {z1[0]+z2[0], z1[1]+z2[1]}; subC(z1, z2) = {z1[0]-z2[0], z1[1]-z2[1]}; mulC(z1, z2) = ( x1=z1[0], x2=z2[0], y1=z1[1], y2=z2[1], {x1*x2-y1*y2, x1*y2+x2*y1} ); divC(z1, z2) = ( x1=z1[0], x2=z2[0], y1=z1[1], y2=z2[1], d=x2*x2+y2*y2, {(x1*x2+y1*y2)/d, (x2*y1-x1*y2)/d} ); powC(z, x) = cartC({absC(z)**x, x*phiC(z)}); expC(z) = mulC({exp(z[0]), 0}, cartC({1, z[1]})); lnC(z) = {ln(absC(z)), phiC(z)};