static int Main() { /* * double x=1; * char c='ø'; * string s="hello"; * int i=100; * * Write("sin({0})={1}\n",x,Sin(x)); * Write($"sin({x})={Sin(x)}\n"); * Write($"i={i}\n"); * double y=x*Exp(x); * Write($"y={y} E={E}\n"); */ complex I = new complex(0, 1); Write($"i^2={(I*I).Re}\n"); Write($"√2={Sqrt(2)}\n"); Write($"exp(i)={exp(I).Re} + i·({exp(I).Im})\n"); Write($"exp(iπ)={exp(I*PI).Re} + i·({exp(I*PI).Im})\n"); Write($"sin(iπ)={sin(I*PI).Re} + i·({sin(I*PI).Im})\n"); Write($"I.pow(I)={I.pow(I).Re} + i·({I.pow(I).Im})\n"); return(0); }
static int Main() { /* * double x = 1; * char c ='ø'; * string s = "hello"; * Write("sin{0}={1}\n",x,Sin(x)); * Write($"sin({x})={Sin(x)}\n"); * int i = 100; * Write($"i={i}\n"); */ // complex i = new complex (complex.sqr(2),0); complex sqr_2 = sqrt(2); complex I = new complex(0, 1); complex e = E; complex ei = e.pow(I); complex eipi = e.pow(I * PI); complex ipowi = I.pow(I); complex sipi = sin(I * PI); Write($"sqrt(2) ={sqr_2}\n "); Write($"i^i = {ipowi}\n "); Write($"sin(i*pi)={sipi}\n"); Write($"e^i={ei}\n"); Write($"e^(ipi)={eipi}\n"); return(0); }
static void exerciseA() { // Exercise: Calculate a bunch of stuff: Write("Math Exercise A:\n"); // Sqrt(2): double x = 2; Write($"sqrt(2): {sqrt(x)}\n"); // e^i complex I = new complex(0, 1); Write($"e^i: {exp(I)}\n"); // e^(i*PI) Write($"e^(i*pi): {exp(I*PI)}\n"); // i^i Write($"i^i: {I.pow(I)}\n"); // sin(i*pi) Write($"sin(i*pi): {sin(I*PI)}\n"); // Just another write for nice formatting of output: Write("\n"); }
static int Main() { double a = 2; complex one = new complex(1, 0); complex i = new complex(0, 1); Write("i={0}\n", i); Write("a={0}\n", a); Write("Sqrt({0}) = {1} Expected: 1.41421\n", a, Math.Sqrt(a)); Write("Sqrt({0})*Sqrt({0}) = {1} Expected: 2\n", a, Math.Sqrt(a) * Math.Sqrt(a)); complex ei = exp(i); complex e = Math.E; complex ei_ = e.pow(i); Write("exp(i) = {0} Expected: 0.54+0.84i\n", ei); //Write("Math.E^Math.Sqrt(-1) = {0}\n",ei_); complex eipowpi = ei.pow(Math.PI); Write("exp(i).pow(PI) = {0} Expected: -1\n", eipowpi); complex eipi = exp((i * Math.PI)); Write("exp(i*Math.PI) = {0} Expected: -1\n", eipi); complex eulerid = eipi + one; Write("exp(i*Math.PI)+1 = {0} Expected: 0\n", eulerid); complex sipi = sin((i * Math.PI)); Write("sin(i*Math.PI) = {0} Expected: 0+11.5i\n", sin((i * Math.PI))); return(0); }
static int Main() { double x = 1; Write("sin({0})={1:f2}\n", x, Sin(x)); Write($"sin({x})={Sin(x)}\n"); double y = x * Exp(x); Write($"y={y} (E={E})\n"); complex I = new complex(0, 1); complex ii = I * I; Write($"I*I={ii}\n"); complex sini = sin(I); Write($"sin(I)={sini}\n"); complex si2ci2 = sin(I).pow(2) + cos(I).pow(2); Write($"sin(I)^2+cos(I)^2={si2ci2}\n"); complex ipowi = I.pow(I); Write($"I.pow(I)={ipowi}, exp(-PI/2)={Exp(-PI/2)}\n"); complex si = (-(complex)1.0).pow(0.5); si.print("sqrt(-1)="); si.printf("sqrt(-1)=({0:f2},{1:f2})"); var invi = 1 / I; invi.print("1/I="); log(I).print("log(I)="); (I * PI / 2).print("I*PI/2="); return(0); }
public static void Main() { complex a = sqrt(2); complex e = E; complex i = new complex(0, 1); complex b = e.pow(i); complex c = e.pow(i * PI); complex d = i.pow(i); complex f = sin(i * PI); Write(a.ToString() + "\n"); Write(b.ToString() + "\n"); Write(c.ToString() + "\n"); Write(d.ToString() + "\n"); Write(f.ToString() + "\n"); }
static int Main() { double x = 2; WriteLine($"sqrt(2)={Sqrt(x)}"); complex I = new complex(0, 1); complex ie = exp(I); WriteLine($"exp(i)={ie}"); complex ie_1 = exp(PI * I); WriteLine($"exp(pi*i)={ie_1}"); complex i_i = I.pow(I); WriteLine($"i^i={i_i}"); complex isin = sin(PI * I); WriteLine($"sin(i*pi)={isin}"); complex isinh = sinh(I); WriteLine($"sinh(i)={isinh}"); complex icosh = cosh(I); WriteLine($"cosh(i)={icosh}"); WriteLine($"sqrt(-1)={sqrt(-1)}"); WriteLine($"sqrt(i)={sqrt(I)}"); return(0); }
static void Main() { var a = new complex(0, 1); var b = new complex(1, 1); a.print("a ="); Write("b ={0}\n", b); Write("{0}\n", exp(1.2)); Write("exp(b)={0}\n", exp(b)); Write("exp(log(b))={0}\n", exp(log(b))); Write("sin(b)={0}\n", sin(b)); Write("cos(b)={0}\n", cos(b)); Write("log(exp(b))={0}\n", log(exp(b))); Write("a/b={0}\n", a / b); Write("(a/b)*b={0}\n", (a / b) * b); Write($"{a}.pow(2)={a.pow(2)}\n"); Write($"{a}.pow({a})={a.pow(a)}, e^(-pi/2)={exp(-PI/2)}\n"); }
static Func <vector, vector> makexyp(double p) { Func <vector, vector> xyp = delegate(vector xy) { complex z = new complex(xy[0], xy[1]); complex zp = z.pow(p) + 5; return(new vector(zp.Re, zp.Im)); }; return(xyp); }
static int Main() { Write("sqrt(2)={0}\n", sqrt(2.0)); complex I = new complex(0, 1); Write("exp(i)={0}\n", exp(I)); Write("exp(i*pi)={0}\n", exp(I * PI)); Write("i^i={0}\n", I.pow(I)); Write("sin(i)={0}\n", sin(I * PI)); return(0); }
static void Main() { complex i = new complex(0, 1); i.print("i= "); (i * i).print("i*i="); exp(i * PI).print("exp(i*PI)="); sin(i).print("sin(i)="); i.pow(i).print("i^i="); abs(i).print("abs(i)="); (arg(i) / PI).print("arg(i)/PI="); WriteLine("i.Re={0}, i.Im={1}", i.Re, i.Im); } //Main
static int Main() { complex I = new complex(0, 1); Write($"sqrt(2)={sqrt(2)}\n"); Write($"exp(I)={exp(I)}\n"); Write($"exp(I*PI)={exp(I*PI)}\n"); complex ipowi = I.pow(I); Write($"I.pow(I)={ipowi}\n"); Write($"sin(I*PI)={sin(I*PI)}\n"); return(0); }
static int Main() { double x = 2; WriteLine($"Sqrt(2) = {Sqrt(x)}"); complex I = new complex(0, 1); WriteLine($"I.pow(I)={I.pow(I)}"); WriteLine($"exp(i)={exp(I)}"); WriteLine($"exp(i*pi)={exp(I*PI)}"); WriteLine($"Sin(i*pi)={sin(I*PI)}"); return(0); }
static void Main() { WriteLine("Calculation of e = {0}", System.Math.E); WriteLine("Actual: 2.718281828459..."); WriteLine("sqrt(2) = {0}", sqrt(2)); WriteLine("Actual: 1.414213562373..."); complex I = new complex(0, 1); WriteLine("e^i = {0}", exp(I)); WriteLine("Actual: 0.54030230.. + i 0.84147098.."); WriteLine("exp(i*pi) = {0}", exp(I * PI)); WriteLine("Actual: -1"); WriteLine("i^i = {0}", I.pow(I)); WriteLine("Actual: 0.20787957..."); WriteLine($"sin(i*pi) = {sin(I*PI)}"); WriteLine("Actual: i*11.54873935..."); }
public static int Main() { ans = new complex(sqrt(2), 0); print("sqrt(2)", ans); ans = exp(i); print("exp(i)", ans); ans = exp(i * PI); print("exp(i*pi)", ans); ans = i.pow(i); print("i^i", ans); ans = sin(i * PI); print("sin(i*pi)", ans); Write("All answers have been checked, and are correct!\n"); return(0); }
static int Main() { double x = 2; Write($"sqrt(2) = {Sqrt(x)}\n"); complex i = new complex(0, 1); Write($"e.pow(i) = {E.pow(i)}\n"); Write($"i={i}\n"); double y = Pow(x, 2) * i; Write($"y={y}\n"); complex J = new complex(0, 1); Write($"J*J={J*J}\n"); Write($"J.pow(J)={J.pow(J)}\n"); return(0); }
static void Main() { double a = sqrt(2); WriteLine("sqrt(2) = {0}. \t\t\t\t Correct result: 1.41421", a); complex i = new complex(0, 1); complex ipowi = i.pow(i); WriteLine("i^i = {0}. \t\t\t\t Correct result: 0.20787", ipowi); complex sinipi = sin(i * PI); WriteLine("sin(i*pi) = {0}. \t\t\t Correct result: 11.54873*i", sinipi); complex epowi = exp(i); WriteLine("e^i = {0}. \t\t Correct result: 0.54030 + 0.84147*i", epowi); complex epowipi = exp(i * PI); WriteLine("e^(pi) = {0}. \t\t\t Correct result: -1", epowipi); complex sinhi = sinh(i); WriteLine("sinh(i) = {0}. \t\t\t Correct result: 0.84147*i", sinhi); complex coshi = cosh(i); WriteLine("cosh(i) = {0}. \t\t\t Correct result: 0.54030", coshi); complex minusOne = new complex(-1, 0); complex sqrtMinusOne = sqrt(minusOne); WriteLine("sqrt(-1) = {0}. \t\t\t Correct result: i", sqrtMinusOne); complex sqrti = sqrt(i); WriteLine("sqrt(i) = {0}. \t Correct result: 0.70710 + 0.70710*i", sqrti); }
static int Main() { Write($"check, sqrt(2)={Sqrt(2)}\n"); //system.math Write($"cmath, sqrt(2)={sqrt(2)}\n\n"); //library complex I = new complex(0, 1); complex IpowI = I.pow(I); Write($"check, exp(i)*exp(-i)={exp(I)*exp(-I)}\n"); Write($"cmath,exp(i)={exp(I)}\n\n"); Write($"check, abs(exp(i*Pi))={abs(exp(I*PI))}\n"); Write($"cmath,exp(i*Pi)={exp(I*PI)}\n\n"); Write($"check, exp(-pi/2)={exp(-PI/2)}\n"); Write($"cmath,i^i={IpowI}\n\n"); Write($"check, euler={I/2*(exp(PI)-exp(-PI))}\n"); Write($"cmath,sin(ipi)={sin(I*PI)}\n\n"); return(0); }
static int Main() { /* * double x=1; * //char c='ø'; * //string s="hello"; * int i=100; * //Write($"{s}\n"); * * Write("sin({0})={1}\n",x,Sin(x)); * Write($"sin({x})={Sin(x)}\n"); * Write($"i={i}\n"); * double y=x*Exp(x); * Write($"y={y}\n"); */ complex I = new complex(0, 1); Write($"I*I={I*I}\n"); Write($"sin(I)={sin(I)}\n"); Write($"I.pow(I)={I.pow(I)}, exp(-PI/2)={Exp(-PI/2)}\n"); return(0); }
static void Main() { WriteLine("\n----------------------------------------------------------------\n"); WriteLine("\nExam project 15 - complex rootfinding - Jeppe ST\n"); WriteLine("\n----------------------------------------------------------------\n"); WriteLine("\nPart A: Test of complex newton implementation\n"); complex i = new complex(0, 1); // Complex rootfinding of f = z^3 + 5, finite difference step int fcalls = 0; Func <complex, complex> fc = delegate(complex z) { fcalls++; return(z * z * z + 5); }; complex z0 = new complex(0.5, 0.1); // Complex rootfinder setup double eps = 1e-3; complex dz = 1e-7 * (1 + i); complex five = new complex(5, 0); complex minusfive = new complex(-5, 0); complex minusone = new complex(-1, 0); complex res1 = -five.pow(1.0 / 3); complex res2 = minusfive.pow(1.0 / 3); complex res3 = minusone.pow(2.0 / 3) * five.pow(1.0 / 3); // Complex rootfinding, analytical derivative fcalls = 0; Func <complex, complex> df = delegate(complex z) { fcalls++; return(3 * z * z); }; (complex croot2, int nstepsc2) = rootf.newton(fc, z0, eps: eps, dz: dz, df: df); // Write WriteLine($"Doing complex 1D newton rootf of f = z^3 + 5 close to {z0}"); WriteLine($"Using analytical derivative 3*z²"); WriteLine($"Accuracy goal eps: {eps}"); WriteLine($"Analytical roots: {res1} , {res2} , {res3} "); WriteLine($"Found root: {croot2}"); WriteLine($"Deviation: {(croot2-res2).Re:f3}+{(croot2-res2).Im:f2}i"); WriteLine($"Value of function at root: {fc(croot2)}"); WriteLine($"Number of function calls: {fcalls}"); WriteLine($"Number of rootfinder steps: {nstepsc2}\n"); // 2D real rootfinding of f = z^3 + 5 fcalls = 0; Func <vector, vector> fr = delegate(vector xy) { fcalls++; double x = xy[0]; double y = xy[1]; complex fz = x * x * x - y * y * x - 2 * x * y * y + 2 * i * x * x * y + i * x * x * y - i * y * y * y + 5; return(new vector(fz.Re, fz.Im)); }; vector xy0 = new vector(0.5, 0.1); // 2D real rootfinder call eps = 1e-3; double dxy = 1e-7; (vector rroot, int nstepsr) = rootf.newton(fr, xy0, eps: eps, dx: dxy); complex rrootc = new complex(rroot[0], rroot[1]); vector frval = fr(rroot); complex frvalc = new complex(frval[0], frval[1]); // Write WriteLine($"Doing real 2D rootfinding of f = z^3 + 5 close to {z0}"); WriteLine($"Using Jacobi matrix with finite difference approximation"); WriteLine($"Accuracy goal eps: {eps}"); WriteLine($"Analytical roots: {res1} , {res2} , {res3} "); WriteLine($"Found root: {rrootc}"); WriteLine($"Deviation: {(rrootc-res2).Re:f3}+{(rrootc-res2).Im:f2}i"); WriteLine($"Value of function at root: {frvalc}"); WriteLine($"Number of function calls: {fcalls}"); WriteLine($"Number of rootfinder steps: {nstepsr}\n"); // 1D complex rootfinding of f = sin(z)*exp(z), roots z=pi*n + 0i Func <complex, complex> fc2 = delegate(complex z) { fcalls++; return(sin(z) * exp(z)); }; Func <complex, complex> dfc2 = delegate(complex z) { fcalls++; return(exp(z) * (sin(z) + cos(z))); }; z0 = 1 + 0.1 * i; res1 = 0; WriteLine("*************\n"); // Analytical derivative fcalls = 0; (complex croot, int nstepsc) = rootf.newton(fc2, z0, eps: eps, dz: dz, df: dfc2); // Write WriteLine($"Doing complex 1D newton rootf of f = sin(z)*exp(z) close to {z0}"); WriteLine($"Using analytical derivative exp(z)*(sin(z)+cos(z))"); WriteLine($"Accuracy goal eps: {eps}"); WriteLine($"Expected root: {res1}"); WriteLine($"Found root: {croot}"); WriteLine($"Deviation: {(croot-res1).Re:f3}+{(croot-res1).Im:f2}i"); WriteLine($"Value of function at root: {fc2(croot)}"); WriteLine($"Number of function calls: {fcalls}"); WriteLine($"Number of rootfinder steps: {nstepsc}\n"); // 2D real rootfinding of f = sin(z)*exp(z), roots z=pi*n + 0i Func <vector, vector> fr2 = delegate(vector xy) { fcalls++; double x = xy[0]; double y = xy[1]; complex fz = sin(x + i * y) * exp(x + i * y); return(new vector(fz.Re, fz.Im)); }; xy0 = new vector(z0.Re, z0.Im); fcalls = 0; (rroot, nstepsr) = rootf.newton(fr2, xy0, eps: eps, dx: dxy); rrootc = new complex(rroot[0], rroot[1]); frval = fr2(rroot); frvalc = new complex(frval[0], frval[1]); // Write WriteLine($"Doing real 2D rootfinding of f = sin(z)*exp(z) close to {z0}"); WriteLine($"Using Jacobi matrix with finite difference approximation"); WriteLine($"Accuracy goal eps: {eps}"); WriteLine($"Analytical roots: {res1}"); WriteLine($"Found root: {rrootc}"); WriteLine($"Deviation: {(rrootc-res1).Re:f3}+{(rrootc-res1).Im:f2}i"); WriteLine($"Value of function at root: {frvalc}"); WriteLine($"Number of function calls: {fcalls}"); WriteLine($"Number of rootfinder steps: {nstepsr}\n"); WriteLine("\n----------------------------------------------------------------\n"); }
static void Main() { WriteLine("\n----------------------------------------------------------------\n"); WriteLine("\nPart C: Plotting\n"); complex i = new complex(0, 1); // Test rootfinders for increasing power p var writerz = new System.IO.StreamWriter("out.z.txt"); var writerxy = new System.IO.StreamWriter("out.xy.txt"); Stopwatch sw = new Stopwatch(); int fcalls = 0;; complex minusfive = new complex(-5, 0); double eps = 1e-3; for (double p = 1; p < 1000; p += 1) { // Make base functions, and add call counters to them Func <complex, complex> zp_base = makezp(p); Func <complex, complex> zp = delegate(complex z) { fcalls++; return(zp_base(z)); }; Func <complex, complex> dzp_base = makedzp(p); Func <complex, complex> dzp = delegate(complex z) { fcalls++; return(dzp_base(z)); }; Func <vector, vector> xyp_base = makexyp(p); Func <vector, vector> xyp = delegate(vector xy) { fcalls++; return(xyp_base(xy)); }; // Reset stopwatch sw.Reset(); // Starting points and expected root complex res = minusfive.pow(1.0 / p); complex startc = res * 1.1 + 0.2; vector startv = new vector(startc.Re, startc.Im); double dxy1 = 1e-7; complex dz1 = 1e-7 * (1 + i); // Complex 1D rootf call fcalls = 0; sw.Start(); (complex zroot, int nsteps1) = rootf.newton(zp, startc, eps: eps, dz: dz1); sw.Stop(); double time1 = sw.Elapsed.TotalMilliseconds; int fcalls1 = fcalls; // 2D real rootf call fcalls = 0; sw.Reset(); sw.Start(); (vector xyroot, int nsteps2) = rootf.newton(xyp, startv, eps: eps, dx: dxy1); sw.Stop(); double time2 = sw.Elapsed.TotalMilliseconds; complex xyrootc = new complex(xyroot[0], xyroot[1]); int fcalls2 = fcalls; // Write writerz.WriteLine($"{p} {fcalls1} {nsteps1} {time1} {abs(zroot-res)}"); writerxy.WriteLine($"{p} {fcalls2} {nsteps2} {time2} {abs(xyrootc-res)}"); } writerz.Close(); writerxy.Close(); WriteLine("\nIn Plot.calls.svg, rootfinding efficiency for functions f(z) = z^p + 5 is compared."); WriteLine("Here, starting conditions for z is generated as z0=1.1*(-5).pow(p)+0.2."); WriteLine("Then, it is expected that the rootfinders finds the root (-5).pow(p)."); WriteLine("Shown is both: The amount of function (f(z)) calls necessary to find the root,"); WriteLine("and the amount of loop iterations required."); WriteLine("The complex rootfinder makes fewer function calls than the multi-variable one."); WriteLine("\nIn Plot.time.svg, execution time is compared for the two. Here, both also scale"); WriteLine("linearly, but the 1D complex rootfinder is significantly faster. Most likely"); WriteLine("because it is more streamlined given its loss of generality."); WriteLine("\n----------------------------------------------------------------\n"); }
// complex gamma function public static complex cgamma(complex z) { return(z.pow(z - 1.0 / 2) * exp(-1 * z) * Sqrt(2 * PI)); }
public static complex sqrt(complex z) { return(z.pow(-0.5)); }