Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using ComplexMath;
- using CSharpShellCore;
- using System.Numerics;
- using System.Collections.Generic;
- using System.IO;
- namespace ComplexMath {
- delegate double func(double t);
- delegate Cplx cplxFn(Cplx z);
- class Polyn {
- Cplx[] a;
- public int L => a.Length;
- public int N => a.Length - 1;
- public Cplx this[int i] { get { return a[i]; } set { a[i] = value; } }
- public Polyn(params Cplx[] a) {
- this.a = CopyNoZeros(Cplx.ArrayCopy(a));
- }
- public Polyn(Polyn p) : this(p.a) { }
- public Polyn() : this(new Cplx[] { }) { }
- public static Polyn Inv(params Cplx[] a) {
- Cplx[] aa = new Cplx[a.Length];
- for (int i = 0; i < a.Length; i++)
- aa[i] = a[a.Length - 1 - i];
- return new Polyn(aa);
- }
- public static Polyn Zero => new Polyn() { };
- static Cplx[] CopyNoZeros(Cplx[] a) {
- int s = 0;
- for (int i = a.Length - 1; i >= 0; i--)
- if (a[i].IsZero) s++;
- else break;
- Cplx[] aa = new Cplx[a.Length - s];
- for (int i = 0; i < aa.Length; i++) aa[i] = a[i].Copy;
- return aa;
- }
- public void Show(string name, int total = 16, int frac = 12) {
- Console.WriteLine(name + ":");
- for (int i = 0; i < L; i++) {
- a[i].Show(total, frac, true, false);
- Console.Write(" * X" + i + "\n");
- }
- Console.Write("\n");
- }
- public void ShowInv(string name, int total = 16, int frac = 12) {
- Polyn.Inv(a).Show(name, total, frac);
- }
- public Polyn Deriv {
- get {
- if (a.Length < 2) return new Polyn(new Cplx[0]);
- Cplx[] aa = new Cplx[a.Length - 1];
- for (int i = 0; i < aa.Length; i++) {
- aa[i] = a[i + 1] * (i + 1);
- }
- return new Polyn(aa);
- }
- }
- public Cplx Val(Cplx z) {
- Cplx w = 0;
- for (int i = a.Length - 1; i >= 0; i--) {
- w *= z; w += a[i];
- }
- return w;
- }
- public static Polyn operator -(Polyn p) {
- Polyn q = new Polyn(p.a);
- for (int i = 0; i < q.L; i++) q[i] = -q[i];
- return q;
- }
- public static Polyn operator +(Polyn p, Polyn q) {
- if (p.L == 0) return new Polyn(q.a);
- else if (q.L == 0) return new Polyn(p.a);
- int l = Math.Max(p.L, q.L);
- //Console.WriteLine(l);
- Cplx[] a = new Cplx[l];
- for (int i = 0; i < l; i++) {
- a[i] = (i < p.L ? p[i] : 0) + (i < q.L ? q[i] : 0);
- }
- return new Polyn(a);
- }
- public static Polyn operator -(Polyn p, Polyn q) => p + -q;
- public static Polyn operator *(Polyn p, Cplx λ) {
- if (λ.IsZero) return new Polyn(new Cplx[0]);
- Polyn q = new Polyn(p.a);
- for (int i = 0; i < q.L; i++) q[i] *= λ;
- return q;
- }
- public static Polyn operator *(Cplx λ, Polyn p) => p * λ;
- public static Polyn operator *(Polyn p, Polyn q) {
- if (p.L == 0 || q.L == 0) return Zero;
- //Console.WriteLine(p.L+" "+q.L+" "+p.N+" "+ q.N);
- Cplx[] a = new Cplx[p.L + q.L - 1];
- for (int i = 0; i < a.Length; i++) a[i] = 0;
- for (int i = 0; i < p.L; i++)
- for (int j = 0; j < q.L; j++) {
- //Console.WriteLine(i+j-1);
- a[i + j] += p[i] * q[j];
- }
- return new Polyn(a);
- }
- public Polyn Pow(int n) {
- if (n == 0) return new Polyn(new Cplx[] { 1 });
- Polyn pow = new Polyn(a);
- for (int i = 2; i <= n; i++) pow *= this;
- return pow;
- }
- //суперпозиция полиномов
- public Polyn SP(Polyn q) {
- Polyn res = Polyn.Zero;
- for (int i = N; i >= 0; i--) {
- res *= q;
- res += new Polyn(new Cplx[] { a[i] });
- }
- return res;
- }
- public static Polyn[] Div(Polyn p, Polyn q) {
- //Console.WriteLine("In Polyn.Div"); p.Show("p");
- //Console.WriteLine("In Polyn.Div"); q.Show("q");
- if (q.L == 0) return null;
- else if (p.L < q.L) return new Polyn[] { Polyn.Zero, new Polyn(p) };
- Cplx[] pa = Cplx.ArrayCopy(p.a);
- Cplx[] qa = Cplx.ArrayCopy(q.a);
- Cplx[] u = new Cplx[p.L - q.L + 1];
- //int k = 0;
- for (int n = p.N, k = 0; n >= q.N; n--, k++) {
- Cplx g = pa[n] / qa[q.N];
- u[u.Length - 1 - k] = g.Copy;
- for (int i = 0; i < q.L; i++) {
- //Console.WriteLine((n - k - i) + " " + (q.N - i));
- pa[n - i] -= qa[q.N - i] * g;
- }
- }
- //Console.WriteLine("In Polyn.Div"); new Polyn(u).Show("u");
- //Console.WriteLine("In Polyn.Div"); new Polyn(pa).Show("pa");
- return new Polyn[] { new Polyn(u), new Polyn(pa) };
- }
- public static Polyn GCD(Polyn p, Polyn q) {
- //Console.WriteLine("In GCD:");
- Polyn pp = new Polyn(p);
- Polyn qq = new Polyn(q);
- if (pp.L == 1 || qq.L == 1)
- return new Polyn(new Cplx[] { 1 });
- else {
- Polyn r = Div(pp, qq)[1];
- //Console.WriteLine("Div(pp,qq)[1]:");
- //r.Show();
- if (r.L == 0) return qq;
- else {
- pp = new Polyn(qq);
- qq = r;
- return GCD(pp, qq);
- }
- }
- }
- public Cplx[] Roots0() {
- //Console.WriteLine("In Polyn Roots0: Start");
- Cplx[] r = new Cplx[N];
- Polyn q = new Polyn(this);
- for (int i = 0; i < N; i++) {
- r[i] = Cplx.Newton(q, new Cplx(2, 1));
- //Console.WriteLine($"In Polyn.Roots0: Found root{i}"); r[i].Show();
- Polyn[] div = Div(q, new Polyn(new Cplx[] { -r[i], 1 }));
- //Console.WriteLine("In Polyn.Div"); div[0].Show("div[0]");
- q = div[0];
- //Console.WriteLine("In Polyn Roots0"); q.Show("Polyn q");
- }
- //Console.WriteLine("In Polyn Roots0 OK");
- return r;
- }
- public Cplx[] Roots() {
- Polyn q = GCD(this, Deriv);
- //Console.WriteLine("In Polyn.Roots");
- //q.Show("GCD");
- if (q.L == 1) return Roots0();
- else {
- Polyn p = Div(this, q)[0];
- //p.Show(15, 9);
- Cplx[] r0 = p.Roots0();
- Cplx[] r1 = q.Roots();
- Cplx[] r = new Cplx[r0.Length + r1.Length];
- for (int i = 0; i < r.Length; i++) {
- r[i] = (i < r0.Length ? r0[i] : r1[i - r0.Length]).Copy;
- }
- return r;
- }
- }
- }
- class Cplx {
- //delegate double DD(double t);
- static Random rnd = new Random();
- public const double π = Math.PI;
- const double ε = 1e-6;
- func sqr = t => t * t;
- func sqrt = Math.Sqrt;
- func sin = Math.Sin;
- func cos = Math.Cos;
- func cbrt = t => Math.Pow(t, (double)1 / 3);
- func exp = t => Math.Exp(t);
- func asin = Math.Asin;
- func acos = Math.Acos;
- func ln = Math.Log;
- static double pow(double a, double b) => Math.Pow(a, b);
- public double X { get; private set; }
- public double Y { get; private set; }
- public Cplx(double x, double y) {
- X = x;
- Y = y;
- }
- public Cplx Copy => new Cplx(X, Y);
- public bool IsZero => R < ε;
- public bool isReal => Math.Abs(Y) < ε;
- public static implicit operator Cplx(double x) => new Cplx(x, 0);
- public static Cplx operator +(Cplx z, Cplx w) {
- double x = z.X + w.X;
- double y = z.Y + w.Y;
- return new Cplx(x, y);
- }
- public static Cplx I = new Cplx(0, 1);
- public Cplx Iz => new Cplx(-Y, X);
- public double R2 => sqr(X) + sqr(Y);
- public double R => sqrt(R2);
- public double Φ {
- get {
- double φ = acos(X / R);
- return Y < 0 ? -φ : φ;
- }
- }
- public static Cplx operator -(Cplx z) => new Cplx(-z.X, -z.Y);
- public static Cplx operator -(Cplx z, Cplx w) => z + -w;
- public static Cplx operator *(Cplx z, double q) => new Cplx(z.X * q, z.Y * q);
- public static Cplx operator *(double q, Cplx z) => z * q;
- public static Cplx operator *(Cplx z, Cplx w) {
- double x = z.X * w.X - z.Y * w.Y;
- double y = z.X * w.Y + z.Y * w.X;
- return new Cplx(x, y);
- }
- public static Cplx operator ~(Cplx z) => new Cplx(z.X, -z.Y);
- public static Cplx operator /(Cplx z, double q) => new Cplx(z.X / q, z.Y / q);
- public static Cplx operator /(Cplx z, Cplx w) => z * ~w / w.R2;
- public Cplx PowN(int n) {
- if (n > 0) {
- if (n == 1) return this.Copy;
- else {
- int nn = n >> 1;
- Cplx z = (PowN(nn)), zz = z * z;
- return (n & 1) == 1 ? zz * this : zz;
- }
- } else return n == 0 ? 1 : 1 / PowN(-n);
- }
- public Cplx Root(int n) {
- double r = pow(R, (double)1 / n);
- double φ = Φ / n;
- return new Cplx(r * cos(φ), r * sin(φ));
- }
- public Cplx[] Cbrt {
- get {
- double eps = 1e-9;
- Cplx w = new Cplx(5, 4);
- bool f = false;
- while (true) {
- Cplx ww = (this / (w * w) + w + w) / 3;
- if (f) { w = ww; break; } else {
- f = ((w - ww) / ww).R < eps;
- w = ww.Copy;
- }
- }
- Cplx[] res = new Cplx[3];
- res[0] = w;
- Cplx q = new Cplx(-1, sqrt(3)) / 2;
- res[1] = w * q;
- res[2] = w * ~q;
- return res;
- }
- }
- public Cplx Exp {
- get {
- double r = exp(X);
- return r * new Cplx(cos(Y), sin(Y));
- }
- }
- public Cplx Cos => (Iz.Exp + (-Iz).Exp) / 2;
- public Cplx Sin => ((-Iz).Exp - Iz.Exp).Iz / 2;
- public Cplx Tg => Sin / Cos;
- public Cplx Ln => new Cplx(ln(R), Φ);
- public Cplx Pow(Cplx z) => (z * Ln).Exp;
- public static Cplx Newton(Polyn p, Cplx w0) {
- //Console.WriteLine("In Newton"); p.Show("p");
- w0 = new Cplx(0.5 + rnd.NextDouble(), 0.5 + rnd.NextDouble());
- Cplx z = w0.Copy;
- bool flag = false;
- double ε1 = 1e-2, ε2 = 1e-4;
- while (true) {
- Cplx δ = p.Val(z) / p.Deriv.Val(z);
- double λ = δ.R > ε1 ? 0.25 : (δ.R > ε2 ? 0.5 : 1);
- Cplx w = z - δ * λ;
- if (flag) return w;
- flag = (w - z).R < 1e-9;
- z = w.Copy;
- //Console.WriteLine("In Cplx.Newton"); z.Show(15, 9);
- }
- }
- public static cplxFn D1(cplxFn f) {
- double ε = 1e-5;
- double φ = π * (2 * rnd.NextDouble() - 1) / 2;
- Cplx δ = ε * (Math.Cos(φ) + Cplx.I * Math.Sin(φ));
- return z => (f(z + δ / 2) - f(z - δ / 2)) / δ;
- }
- public static Cplx Newton(cplxFn f, Cplx z0) {
- cplxFn df = D1(f);
- Cplx z = z0.Copy;
- bool flag1 = false, flag2 = false;
- int cnt = 0;
- double k = 0.5;
- while (true) {
- Cplx zz = z - k * f(z) / df(z);
- if (flag2) return zz;
- else {
- double δ = (zz - z).R;
- flag1 = δ < 1e-3;
- if (flag1) k = 1;
- flag2 = δ < 1e-9;
- z = zz.Copy;
- cnt++;
- if (cnt > 50) return null;
- }
- }
- }
- //public string Str(int total, int frac, )
- public void Show1(int total = 15, int frac = 9) {
- string s = "";
- for (int i = 0; i < 2; i++) {
- s += "{" + i + "," + total + ":f" + frac + "}";
- }
- s = "(" + s + ")\n";
- Console.ForegroundColor = isReal ? ConsoleColor.Red : ConsoleColor.Cyan;
- Console.Write(s, X, Y);
- Console.ForegroundColor = ConsoleColor.White;
- }
- public void Show(int total = 15, int frac = 9, bool color = true, bool newLine = true) {
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write("(");
- Console.ForegroundColor = ConsoleColor.Red;
- string frm = "{0," + total + ":f" + frac + "}";
- Console.Write(frm, X);
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write(" : ");
- Console.ForegroundColor = isReal ? ConsoleColor.DarkGray : ConsoleColor.Cyan;
- Console.Write(frm, Y);
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write(")");
- if (newLine) Console.Write("\n");
- }
- public static void Show(Cplx[] z, int total, int frac) {
- for (int i = 0; i < z.Length; i++) {
- z[i].Show(total, frac);
- }
- Console.Write("\n");
- }
- public static void Show(Cplx[,] a, int total, int frac, bool real = false) {
- string form = "{0," + total + ":f" + frac + "}";
- for (int i = 0; i < a.GetLength(0); i++) {
- Console.ForegroundColor = ConsoleColor.Red;
- for (int j = 0; j < a.GetLength(1); j++) Console.Write(form, a[i, j].X);
- Console.WriteLine();
- if (!real) {
- for (int j = 0; j < a.GetLength(1); j++) {
- Console.ForegroundColor = a[i, j].isReal ? ConsoleColor.DarkGray : ConsoleColor.Cyan;
- Console.Write(form, a[i, j].Y);
- }
- Console.WriteLine("\n");
- }
- Console.ForegroundColor = ConsoleColor.White;
- }
- if (real) Console.WriteLine();
- }
- public static Cplx[] ArrayCopy(Cplx[] z) {
- Cplx[] w = new Cplx[z.Length];
- for (int i = 0; i < z.Length; i++) w[i] = z[i].Copy;
- return w;
- }
- public static Cplx[,] ArrayCopy(Cplx[,] z) {
- Cplx[,] w = new Cplx[z.GetLength(0), z.GetLength(1)];
- for (int i = 0; i < z.GetLength(0); i++)
- for (int j = 0; j < z.GetLength(1); j++) w[i, j] = z[i, j].Copy;
- return w;
- }
- public static Cplx[] GetRow(Cplx[,] a, int n) {
- Cplx[] b = new Cplx[a.GetLength(1)];
- for (int i = 0; i < b.Length; i++) b[i] = a[n, i].Copy;
- return b;
- }
- public static Cplx[] GetCol(Cplx[,] a, int n) {
- Cplx[] b = new Cplx[a.GetLength(0)];
- for (int i = 0; i < b.Length; i++) b[i] = a[i, n].Copy;
- return b;
- }
- public static void SetRow(Cplx[,] a, int n, Cplx[] b) {
- for (int i = 0; i < b.Length; i++) a[n, i] = b[i].Copy;
- }
- public static void SetCol(Cplx[,] a, int n, Cplx[] b) {
- for (int i = 0; i < b.Length; i++) a[i, n] = b[i].Copy;
- }
- /*
- public static (Cplx[,] arr, int rnk) RankArr(Cplx[,] a0) {
- Cplx[,] a = Cplx.ArrayCopy(a0);
- int n0 = a.GetLength(0), n1 = a.GetLength(1);
- for (int i = 0; i < n0; i++)
- for (int j = 0; j < n1; j++)
- a[i, j] = a0[i, j].Copy;
- int cnt = 0;
- while (cnt < n0) {
- int j0 = -1;
- for (int j = 0; j < n1; j++) if (!a[cnt, j].IsZero) { j0 = j; break; }
- if (j0 == -1) {
- Cplx[,] tmp = new Cplx[n0 - 1, n1];
- for (int i = 0; i < n0 - 1; i++)
- for (int j = 0; j < n1; j++)
- tmp[i, j] = i < cnt ? a[i, j].Copy : a[i + 1, j].Copy;
- n0--;
- a = tmp;
- continue;
- }
- Cplx w = a[cnt, j0].Copy;
- for (int j = 0; j < n1; j++) a[cnt, j] /= w;
- for (int i = 0; i < n0; i++)
- if (i != cnt) {
- w = a[i, j0].Copy;
- for (int j = 0; j < n1; j++) a[i, j] -= a[cnt, j] * w;
- }
- cnt++;
- }
- return (a, Math.Min(a.GetLength(0), a.GetLength(1)));
- }
- */
- }
- class CplxMatrix {
- static Random rnd = new Random();
- public readonly Cplx[,] a;
- public int N { get; private set; }
- public Cplx this[int i, int j] { get { return a[i, j].Copy; } set { a[i, j] = value.Copy; } }
- public CplxMatrix(Cplx[,] a) {
- N = a.GetLength(0);
- this.a = new Cplx[N, N];
- for (int i = 0; i < N; i++)
- for (int j = 0; j < N; j++)
- this.a[i, j] = a[i, j].Copy;
- }
- public CplxMatrix Copy() => new CplxMatrix(a);
- public static CplxMatrix Zero(int n) {
- Cplx[,] a = new Cplx[n, n];
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++) a[i, j] = 0;
- return new CplxMatrix(a);
- }
- public bool IsZero() {
- foreach (Cplx z in a) if (!z.IsZero) return false;
- return true;
- }
- public static CplxMatrix I(int n) {
- Cplx[,] a = new Cplx[n, n];
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++) a[i, j] = (i == j ? 1 : 0);
- return new CplxMatrix(a);
- }
- public static CplxMatrix RandInt(int n, int m) {
- Cplx[,] a = new Cplx[n, n];
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- a[i, j] = rnd.Next(-m, m + 1);
- return new CplxMatrix(a);
- }
- public static CplxMatrix RandErmit(int n, int m, bool real = false) {
- Cplx[,] a = new Cplx[n, n];
- for (int i = 0; i < n; i++) {
- int x = rnd.Next(-m, m + 1);
- a[i, i] = x;
- for (int j = i + 1; j < n; j++) {
- x = rnd.Next(-m, m + 1);
- int y = real ? 0 : rnd.Next(-m, m + 1);
- a[i, j] = new Cplx(x, y);
- a[j, i] = ~a[i, j];
- }
- }
- return new CplxMatrix(a);
- }
- public double Max() {
- double max = 0;
- foreach (Cplx z in a) if (z.R > max) max = z.R;
- return max;
- }
- public CplxMatrix Tr() {
- Cplx[,] a = new Cplx[N, N];
- for (int i = 0; i < N; i++)
- for (int j = 0; j < N; j++)
- a[i, j] = this.a[j, i].Copy;
- return new CplxMatrix(a);
- }
- public CplxMatrix Inv() {
- int n = N, nn = N + N;
- Cplx[,] a = new Cplx[n, nn];
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < nn; j++)
- a[i, j] = j < n ? this[i, j] : j == N + i ? 1 : 0;
- }
- for (int i = 0; i < n; i++) {
- if (a[i, i].IsZero) {
- int ii = i;
- for (int j = i + 1; j < n; j++) if (!a[j, i].IsZero) {
- ii = j;
- break;
- }
- if (ii == i) return null;
- for (int k = i; k < nn; k++) { Cplx tmp = a[i, k].Copy; a[i, k] = a[ii, k].Copy; a[ii, k] = tmp; }
- }
- Cplx aii = a[i, i].Copy;
- for (int j = 0; j < nn; j++) a[i, j] /= aii;
- for (int j = 0; j < n; j++)
- if (j != i) {
- Cplx aji = a[j, i].Copy;
- for (int k = i; k < nn; k++) a[j, k] -= aji * a[i, k];
- }
- }
- Cplx[,] b = new Cplx[n, n];
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- b[i, j] = a[i, n + j].Copy;
- return new CplxMatrix(b);
- }
- public static CplxMatrix operator +(CplxMatrix a, CplxMatrix b) {
- CplxMatrix res = new CplxMatrix(a.a);
- for (int i = 0; i < res.N; i++)
- for (int j = 0; j < res.N; j++)
- res[i, j] += b[i, j];
- return res;
- }
- public static CplxMatrix operator -(CplxMatrix a) {
- CplxMatrix res = new CplxMatrix(a.a);
- for (int i = 0; i < res.N; i++)
- for (int j = 0; j < res.N; j++)
- res[i, j] = -res[i, j];
- return res;
- }
- public static CplxMatrix operator -(CplxMatrix a, CplxMatrix b) => a + -b;
- public static CplxMatrix operator *(CplxMatrix a, Cplx μ) {
- CplxMatrix b = new CplxMatrix(a.a);
- for (int i = 0; i < b.N; i++)
- for (int j = 0; j < b.N; j++) b[i, j] *= μ;
- return b;
- }
- public static CplxMatrix operator *(CplxMatrix a, CplxMatrix b) {
- CplxMatrix res = Zero(a.N);
- for (int i = 0; i < a.N; i++)
- for (int j = 0; j < a.N; j++)
- for (int k = 0; k < a.N; k++) res[i, j] += a[i, k] * b[k, j];
- return res;
- }
- public CplxMatrix PowN(int n) {
- if (n > 1) {
- CplxMatrix r = PowN(n / 2), rr = r * r;
- return (n & 1) == 1 ? rr * this : rr;
- } else if (n == 0) return I(N);
- else if (n == 1) return Copy();
- else return Inv().PowN(-n);
- }
- public static Cplx[] operator *(CplxMatrix a, Cplx[] b) {
- if (a.N != b.Length) return null;
- Cplx[] res = new Cplx[a.N];
- for (int i = 0; i < a.N; i++) {
- res[i] = 0;
- for (int j = 0; j < a.N; j++)
- res[i] += a[i, j] * b[j];
- }
- return res;
- }
- public static Cplx[,] operator *(CplxMatrix a, Cplx[,] b) {
- if (a.N != b.GetLength(0)) return null;
- Cplx[,] res = new Cplx[b.GetLength(0), b.GetLength(1)];
- for (int i = 0; i < res.GetLength(0); i++)
- for (int j = 0; j < res.GetLength(1); j++) {
- res[i, j] = 0;
- for (int k = 0; k < a.N; k++) res[i, j] += a[i, k] * b[k, j];
- }
- return res;
- }
- public static Cplx[] LinEq(Cplx[,] a0) {
- int n = a0.GetLength(0);
- Cplx[,] a = new Cplx[n, n];
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- a[i, j] = a0[i, j].Copy;
- CplxMatrix mx = new CplxMatrix(a).Inv();
- Cplx[] z = new Cplx[n];
- for (int i = 0; i < n; i++) {
- z[i] = 0;
- for (int j = 0; j < n; j++)
- z[i] += mx[i, j] * a0[j, n];
- }
- return z;
- }
- public void Show(string name, int total, int frac, bool real = false) {
- Console.WriteLine(name + ":");
- Cplx.Show(a, total, frac, real);
- }
- Polyn[,] M() {
- Polyn[,] p = new Polyn[N, N];
- for (int i = 0; i < N; i++)
- for (int j = 0; j < N; j++) {
- p[i, j] = (i == j) ? new Polyn(a[i, j], -1) :
- new Polyn(a[i, j]);
- }
- return p;
- }
- static void Swap(int[] q, int i, int j) {
- if (i != j) { q[i] ^= q[j]; q[j] ^= q[i]; q[i] ^= q[j]; }
- }
- public static int[] Permut(int n, int k) {
- int[] p = new int[n];
- for (int i = 0; i < n; i++) p[i] = i;
- for (int i = 0; i < n; i++) {
- int j = k % (n - i);
- Swap(p, i, i + j);
- k /= (n - i);
- }
- return p;
- }
- static int Σ(int[] p) {
- int σ = 1;
- for (int i = 0; i < p.Length - 1; i++)
- for (int j = i + 1; j < p.Length; j++)
- if (p[i] > p[j]) σ = -σ;
- return σ;
- }
- static int Fact(int n) => n == 0 ? 1 : n * Fact(n - 1);
- }
- class Program {
- delegate bool db(Cplx z);
- delegate int di(Polyn p);
- delegate Cplx func(double p);
- static Random rnd = new Random();
- public static void Main() {
- void Show(Polyn p) {
- string s = "";
- string Pow(int n) {
- string pw = "⁰¹²³⁴⁵⁶⁷⁸⁹";
- string res = "";
- for (int i = 0; i < n.ToString().Length; i++) {
- res += "⁰¹²³⁴⁵⁶⁷⁸⁹"[n.ToString()[i] - '0'];
- }
- return res;
- }
- for (int i = p.N; i >= 0; i--) {
- //int a0 = (int)Math.Round(p[i].X);
- //int a = Math.Abs((int)Math.Round(p[i].X));
- double a0 = Math.Round(p[i].X, 3);
- double a = Math.Abs(a0);
- int σ = Math.Sign(a0);
- if (a == 0) continue;
- s += s != "" || s == "" && σ == -1 ? (σ == 1 ? " + " : " – ") : "";
- if (i == 0 || i > 0 && a != 1)
- s += a;
- if (i > 0) {
- s += "x";
- if (i > 1) {
- s += Pow(i);
- //s += "^";
- //s += i;
- }
- }
- }
- Console.WriteLine(s);
- }
- //var w = new Cplx(2, 1);
- //var ww = w.Pow(w);
- //ww.Show();
- cplxFn f = w => w * w.Ln;
- var w0 = Cplx.Newton(w => f(w + 1) - f(w - 1) + new Cplx(0, 4 * Math.PI), new Cplx(rnd.NextDouble() * 10 - 5, rnd.NextDouble() * 10 - 5));
- //var w0 = Cplx.Newton(w => w.Pow(w) - w, new Cplx(2.86, 3.22));
- w0.Show(16, 12);
- f(w0 - 1).Show(18, 14);
- f(w0 + 1).Show(18, 14);
- //((w0 - 1) * w0.Ln).Show();
- //w0.Ln.Show();
- return;
- Cplx sum = 0;
- int n = 100000;
- double π = Math.PI;
- double r = 0.51;
- int σ = 1;
- for (int i = 0; i < n; i++) {
- double φ0 = 2 * π * i / n, φ1 = 2 * π * (i + 1) / n;
- Cplx z0 = r * (Cplx.I * φ0).Exp;
- Cplx z1 = r * (Cplx.I * φ1).Exp;
- Cplx dz = σ * (z1 - z0);
- double φ = 2 * π * (i + 0.5) / n;
- Cplx z = r * (Cplx.I * φ).Exp;
- sum += (-1d / 2) / (z * z - 5 * Cplx.I * z / 2 - 1) * dz;
- //sum += 1 / (z - Cplx.I / 2) * dz;
- }
- sum.Show();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement