Advertisement
VNM24ix

Untitled

Jun 10th, 2025
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 20.17 KB | Source Code | 0 0
  1. using System;
  2. //using CSharpShellCore;
  3. //using System.Collections.Generic;
  4. //using Vectors3D;
  5. //using Vectors3D.M;
  6. //using System.Net.Mail;
  7. //using Vectors3D.M.P;
  8.  
  9. namespace Vectors3D;
  10.  
  11. delegate double func(double t);
  12. delegate double func2(double u, double v);
  13. delegate double funcN(double[] t);
  14.  
  15. static class Arr {
  16.   static Random rnd = new Random();
  17.   public static T[] Copy<T>(T[] a) {
  18.     T[] aa = new T[a.Length];
  19.     a.CopyTo(aa, 0);
  20.     return aa;
  21.   }
  22.   public static T[,] Copy<T>(T[,] a) {
  23.     int n0 = a.GetLength(0), n1 = a.GetLength(1);
  24.     T[,] aa = new T[n0, n1];
  25.     for (int i = 0; i < n0; i++)
  26.       for (int j = 0; j < n1; j++)
  27.         aa[i, j] = a[i, j];
  28.     return aa;
  29.   }
  30.   public static double[] Plus(double[] a, double[] b, double μ) {
  31.     double[] res = Copy(a);
  32.     for (int i = 0; i < res.Length; i++) res[i] += b[i] * μ;
  33.     return res;
  34.   }
  35.   public static double[] Mult(double[] a, double μ) => Plus(a, a, μ - 1);
  36.   /*
  37.   public static void Show(double[] a, string name = "", int n0 = 13, int n1 = 9) {
  38.     if (name != "") Console.WriteLine(name + "=");
  39.     string fmt = "{0," + n0 + ":f" + n1 + "}";
  40.     foreach (double x in a) Console.Write(fmt, x);
  41.     Console.WriteLine();
  42.   }
  43.   */
  44.   public static T[] Vector<T>(T[,] a, int n, bool flag = false) {
  45.     T[] b = new T[flag ? a.GetLength(0) : a.GetLength(1)];
  46.     for (int i = 0; i < b.Length; i++)
  47.       b[i] = flag ? a[i, n] : a[n, i];
  48.     return b;
  49.   }
  50.   public static T[,] Modify<T>(T[,] a, T[] b, int n, bool flag = false) {
  51.     T[,] aa = Copy(a);
  52.     for (int i = 0; i < b.Length; i++)
  53.       if (flag) aa[i, n] = b[i];
  54.       else aa[n, i] = b[i];
  55.     return aa;
  56.   }
  57.   public static T[,] Swap<T>(T[,] a, int k, int l, bool flag = false) {
  58.     T[] b = Vector(a, k, flag);
  59.     T[] c = Vector(a, l, flag);
  60.     return Modify(Modify(a, c, k, flag), b, l, flag);
  61.   }
  62.   public static void Show(double[] a, int l0 = 10, int l1 = 6, string name = "") {
  63.     if (name != "") Console.WriteLine(name + " =");
  64.     string fmt = "{0," + l0 + ":f" + l1 + "}";
  65.     foreach (double b in a) Console.Write(fmt, b);
  66.     Console.WriteLine();
  67.   }
  68.   public static void ShowNL(double[] a, int l0 = 10, int l1 = 6, string name = "") {
  69.     if (name != "") Console.WriteLine(name + " =");
  70.     string fmt = "{0," + l0 + ":f" + l1 + "}";
  71.     foreach (double b in a) Console.WriteLine(fmt, b);
  72.     Console.WriteLine();
  73.   }
  74.   public static void ShowInt(int[] a, int l, string name = "") {
  75.     if (name != "") Console.Write(name + " :\n");
  76.     string fmt = "{0," + l + "}";
  77.     foreach (int x in a) Console.Write(fmt, x);
  78.     Console.WriteLine();
  79.   }
  80.   public static void Show(double[,] a, int l0 = 10, int l1 = 6, string name = "") {
  81.     if (name != "") Console.WriteLine(name + " =");
  82.     string fmt = "{0," + l0 + ":f" + l1 + "}";
  83.     for (int i = 0; i < a.GetLength(0); i++)
  84.       Show(Vector(a, i), l0, l1);
  85.     Console.WriteLine();
  86.   }
  87.   public static double[] RandArr(int n, double min, double max) {
  88.     double[] a = new double[n];
  89.     for (int i = 0; i < n; i++) a[i] = min + max * rnd.NextDouble();
  90.     return a;
  91.   }
  92.   public static double[,] RandArr(int n0, int n1, double min, double max) {
  93.     double[,] a = new double[n0, n1];
  94.     for (int i = 0; i < n0; i++)
  95.       for (int j = 0; j < n1; j++)
  96.         a[i, j] = min + max * rnd.NextDouble();
  97.     return a;
  98.   }
  99.   public static double Abs(double[] a) {
  100.     double s = 0;
  101.     foreach (double x in a) s += x * x;
  102.     return Math.Sqrt(s);
  103.   }
  104. }
  105.  
  106. static class M {
  107.   static func D(func f, double δ) =>
  108.   t => (f(t + δ / 2) - f(t - δ / 2)) / δ;
  109.   public static func D(func f) {
  110.     double δ = 1e-3, δ1 = δ * Math.Sqrt(2);
  111.     return t => 2 * D(f, δ)(t) - D(f, δ1)(t);
  112.   }
  113.   public static funcN D(funcN f, int n) {
  114.     return x => {
  115.       func g = t => {
  116.         double[] xx = Arr.Copy(x);
  117.         xx[n] = t;
  118.         return f(xx);
  119.       };
  120.       return D(g)(x[n]);
  121.     };
  122.   }
  123.   public static double[] Grad(funcN f, params double[] x) {
  124.     double[] res = new double[x.Length];
  125.     for (int i = 0; i < x.Length; i++)
  126.       res[i] = D(f, i)(x);
  127.     return res;
  128.   }
  129.  
  130.   public class Mx {
  131.     double[,] a;
  132.     public int N0 => a.GetLength(0);
  133.     public int N1 => a.GetLength(1);
  134.     public Mx(double[,] a) => this.a = Arr.Copy(a);
  135.     static void swap(ref int p, ref int q) {
  136.       p ^= q; q ^= p; p ^= q;
  137.     }
  138.     static int fact(int n) => n < 2 ? 1 : n * fact(n - 1);
  139.     public static long[] permut(int n) {
  140.       long[] p = new long[fact(n)];
  141.       for (int i = 0; i < p.Length; i++) {
  142.         int[] q = new int[n];
  143.         for (int j = 0; j < n; j++) q[j] = j;
  144.         int σ = 0;
  145.         int ii = i;
  146.         for (int j = 0; j < n; j++) {
  147.           int k = j + ii % (n - j);
  148.           if (j != k) {
  149.             swap(ref q[j], ref q[k]); σ = 1 - σ;
  150.           }
  151.           ii /= n - j;
  152.           p[i] |= (long)q[j] << (j << 2);
  153.         }
  154.         p[i] |= (long)σ << (n << 2);
  155.       }
  156.       return p;
  157.     }
  158.     public static (int[] p, int σ) getPermut(long p0, int n) {
  159.       int[] p = new int[n];
  160.       for (int i = 0; i < n; i++) p[i] = (int)(p0 >> (i << 2)) & 0b1111;
  161.       int σ = 1 - (int)((p0 >> (n << 2)) & 0b1111) * 2;
  162.       return (p, σ);
  163.     }
  164.     public double Det() {
  165.       double res = 0;
  166.       long[] p = permut(N0);
  167.       for (int i = 0; i < p.Length; i++) {
  168.         var w = getPermut(p[i], N0);
  169.         double t = w.σ;
  170.         for (int j = 0; j < N0; j++)
  171.           t *= a[j, w.p[j]];
  172.         res += t;
  173.       }
  174.       return res;
  175.     }
  176.     public static double[] LinEqRoots(double[,] a, double[] b) {
  177.       double[] res = new double[b.Length];
  178.       double detA = new Mx(a).Det();
  179.       //Console.WriteLine("In LinEqRoots: Det A ="+ detA);
  180.       for (int i = 0; i < res.Length; i++) {
  181.         Mx mx = new Mx(Arr.Modify(a, b, i, true));
  182.         res[i] = mx.Det() / detA;
  183.       }
  184.       return res;
  185.     }
  186.     public static double[] LinEqRoots1(double[,] a, double[] b) {
  187.       int n = b.Length, nn = n + 1;
  188.       double[,] q = new double[n, nn];
  189.       for (int i = 0; i < n; i++) {
  190.         for (int j = 0; j < n; j++) q[i, j] = a[i, j];
  191.         q[i, n] = b[i];
  192.       }
  193.       for (int i = 0; i < n; i++) {
  194.         double qii = q[i, i];
  195.         for (int j = i; j < nn; j++) q[i, j] /= qii;
  196.         for (int j = 0; j < n; j++)
  197.           if (j != i) {
  198.             double qji = q[j, i];
  199.             for (int k = i; k < nn; k++) q[j, k] -= q[i, k] * qji;
  200.           }
  201.       }
  202.       double[] res = new double[n];
  203.       for (int i = 0; i < n; i++) res[i] = q[i, n];
  204.       return res;
  205.     }
  206.   }
  207.   public class NLinEq {
  208.     funcN[] f;
  209.     public NLinEq(funcN[] f) {
  210.       this.f = f;
  211.     }
  212.     public double[] Solve(double[] x0) {
  213.       double[] x = Arr.Copy(x0);
  214.       bool flag1 = false, flag2 = false;
  215.       int cnt = 0;
  216.       while (true) {
  217.         double[,] a = new double[f.Length, f.Length];
  218.         double[] b = new double[f.Length];
  219.         for (int i = 0; i < f.Length; i++) {
  220.           for (int j = 0; j < f.Length; j++)
  221.             a[i, j] = M.D(f[i], j)(x);
  222.           b[i] = -f[i](x);
  223.         }
  224.         //Console.WriteLine("Matrix Created");
  225.         //Arr.Show(a, 10, 6, "A"); Arr.Show(b, 10, 6, "B"); Console.ReadLine();
  226.         double[] dx = Mx.LinEqRoots(a, b);
  227.         //Console.WriteLine("LinEq Solved");
  228.         double μ = flag1 ? 1 : 0.5;
  229.         double[] xx = Arr.Plus(x, dx, μ);
  230.  
  231.         //Console.Clear();
  232.         //Arr.Show(xx, 16, 12, "InSolve");
  233.         //Console.ReadLine();
  234.  
  235.         if (flag2) {
  236.           cnt++;
  237.           if (cnt == 3) return xx;
  238.         } else {
  239.           double λ = Arr.Abs(dx) / Arr.Abs(xx);
  240.           if (λ < 1e-9) flag2 = true;
  241.           else if (λ < 1e-4) flag1 = true;
  242.         }
  243.         x = xx;
  244.       }
  245.     }
  246.   }
  247.  
  248.   public class Lagrange {
  249.     funcN f;
  250.     funcN[] φ;
  251.     double[] x0;
  252.     double[] λ0;
  253.     public Lagrange(funcN f, funcN[] φ, double[] x0, double[] λ0) {
  254.       this.f = f;
  255.       this.φ = φ;
  256.       this.x0 = Arr.Copy(x0);
  257.       this.λ0 = Arr.Copy(λ0);
  258.     }
  259.     static funcN[] DF(int n, funcN f) {
  260.       funcN[] df = new funcN[n];
  261.       for (int ii = 0; ii < n; ii++) {
  262.         int i = ii;
  263.         df[i] = D(f, i);
  264.       }
  265.       return df;
  266.     }
  267.     static funcN[,](int n, funcN[] φ) {
  268.       funcN[,]= new funcN[φ.Length, n];
  269.       for (int ii = 0; ii < φ.Length; ii++) {
  270.         int i = ii;
  271.         for (int jj = 0; jj < n; jj++) {
  272.           int j = jj;
  273.           dφ[i, j] = D(φ[i], j);
  274.         }
  275.       }
  276.       return;
  277.     }
  278.  
  279.     public double[] Solve() {
  280.       funcN[] F = new funcN[φ.Length + x0.Length];
  281.       for (int i = 0; i < φ.Length; i++) {
  282.         int j = i;
  283.         F[j] = φ[j];
  284.       }
  285.       /*
  286.       funcN[] df = new funcN[x0.Length];
  287.       for (int ii = 0; ii < x0.Length; ii++) {
  288.         int i = ii;
  289.         df[i] = D(f, i);
  290.       }
  291.       */
  292.       funcN[] df = DF(x0.Length, f);
  293.       /*
  294.       funcN[,] dφ = new funcN[φ.Length, x0.Length];
  295.       for (int ii = 0; ii < φ.Length; ii++) {
  296.         int i = ii;
  297.         for (int jj = 0; jj < x0.Length; jj++) {
  298.           int j = jj;
  299.           dφ[i, j] = D(φ[i], j);
  300.         }
  301.       }
  302.       */
  303.       funcN[,]=(x0.Length, φ);
  304.       for (int ii = φ.Length; ii < F.Length; ii++) {
  305.         int i = ii;
  306.         //Console.WriteLine(i);
  307.         F[i] = t => {
  308.           double s = 0;
  309.           for (int jj = 0; jj < φ.Length; jj++) {
  310.             int j = jj;
  311.             s +=[j, i - φ.Length](t) * t[i];
  312.           }
  313.           //Console.WriteLine(i + "OK");
  314.           return df[i - φ.Length](t) - s;
  315.         };
  316.       }
  317.       double[] t0 = new double[x0.Length + φ.Length];
  318.       for (int i = 0; i < t0.Length; i++)
  319.         t0[i] = i < x0.Length ? x0[i] : λ0[i - x0.Length];
  320.       double W(double[] t) {
  321.         double s = 0;
  322.         for (int i = 0; i < F.Length; i++) {
  323.           double ft = F[i](t);
  324.           s += ft * ft;
  325.         }
  326.         return s;
  327.       }
  328.       int cnt = 0, cnt1 = 0;
  329.       double δ = 0.01;
  330.       while (δ > 1e-17) {
  331.         double w0 = W(t0);
  332.         double[] gr = Grad(W, t0);
  333.         double[] t = Arr.Plus(t0, gr, -δ);
  334.         double wt = W(t);
  335.         if (wt < w0) {
  336.           t0 = Arr.Copy(t);
  337.           cnt++;
  338.           cnt1++;
  339.           if (cnt == 10) {
  340.             cnt = 0;
  341.             δ *= 1.5;
  342.             if (cnt1 == 0) {
  343.               Console.Clear();
  344.               Console.WriteLine(wt);
  345.               foreach (double x in t0)
  346.                 Console.WriteLine("{0,18:f12}", x);
  347.               foreach (funcN fn in φ)
  348.                 Console.WriteLine(fn(t0));
  349.               //Console.WriteLine(φ[1](t0));}
  350.             }
  351.           } else {
  352.             cnt = 0;
  353.             δ /= 2;
  354.           }
  355.           if (wt < 1e-4) break;
  356.         }
  357.         //Console.ReadLine();
  358.         //return t0;
  359.       }
  360.       return new NLinEq(F).Solve(t0);
  361.     }
  362.   }
  363.   public class P {
  364.     public static Random rnd => new Random();
  365.     func sqr = t => t * t;
  366.     func sqrt = t => double.Sqrt(t);
  367.     public double x, y, z;
  368.     public double this[int i] {
  369.       get { return i == 0 ? x : i == 1 ? y : z; }
  370.       set { if (i == 0) x = value; else if (i == 1) y = value; else z = value; }
  371.     }
  372.     public P(params double[] p) {
  373.       double[] pp = new double[3];
  374.       for (int i = 0; i < 3; i++)
  375.         if (i < p.Length) pp[i] = p[i];
  376.       x = pp[0]; y = pp[1]; z = pp[2];
  377.     }
  378.     public P Copy => new P(x, y, z);
  379.     public static P RandInt(int min, int max) {
  380.       double[] x = new double[3];
  381.       for (int i = 0; i < 3; i++) x[i] = rnd.Next(min, max + 1);
  382.       return new P(x);
  383.     }
  384.     public void Show(string name, int total = 14, int frac = 9) {
  385.       Console.Write(name + " :\n");
  386.       string format = "{0," + total + ":f" + frac + "}\n";
  387.       for (int i = 0; i < 3; i++) Console.Write(String.Format(format, this[i]));
  388.       Console.Write("\n");
  389.     }
  390.     public void Show2(string name, int total = 6, int frac = 2) {
  391.       Console.Write(name + " :\n");
  392.       string format = "{0," + total + ":f" + frac + "}\n";
  393.       for (int i = 0; i < 2; i++) Console.Write(String.Format(format, this[i]));
  394.       Console.Write("\n");
  395.     }
  396.     public static P O => new P(0, 0, 0);
  397.     public void ShowLine(string name = "", int total = 10, int frac = 6) {
  398.       if (name != "") Console.Write(name + " = ");
  399.       string format = "{0," + total + ":f" + frac + "}";
  400.       Console.Write("(");
  401.       for (int i = 0; i < 3; i++) {
  402.         Console.Write(format, this[i]);
  403.         Console.Write(i < 2 ? " ; " : ")\n");
  404.       }
  405.       Console.Write("\n");
  406.     }
  407.     public double R2 => sqr(x) + sqr(y) + sqr(z);
  408.     public double R => sqrt(R2);
  409.     public double L(P p) => (this - p).R;
  410.     public P E => this / R;
  411.     public static P Rand(double λ) {
  412.       double x = 2 * rnd.NextDouble() - 1;
  413.       double y = 2 * rnd.NextDouble() - 1;
  414.       double z = 2 * rnd.NextDouble() - 1;
  415.       return new P(x, y, z) * λ;
  416.     }
  417.     public static P operator -(P a) => new P(-a.x, -a.y, -a.z);
  418.     public static P operator +(P a, P b) {
  419.       P res = a.Copy;
  420.       res.x += b.x; res.y += b.y; res.z += b.z;
  421.       return res;
  422.     }
  423.     public static P operator -(P a, P b) => a + -b;
  424.     public static P operator *(P a, double μ) =>
  425.     new P(a.x * μ, a.y * μ, a.z * μ);
  426.     public static P operator *(double μ, P a) => a * μ;
  427.     public static P operator /(P a, double μ) => a * (1 / μ);
  428.     public static double operator *(P a, P b) =>
  429.     a.x * b.x + a.y * b.y + a.z * b.z;
  430.     public static P operator &(P a, P b) {
  431.       double x = a.y * b.z - a.z * b.y;
  432.       double y = a.z * b.x - a.x * b.z;
  433.       double z = a.x * b.y - a.y * b.x;
  434.       return new P(x, y, z);
  435.     }
  436.     public static double Mul(P a, P b, P c) => a * (b & c);
  437.     public P Pr(P a) => a.E * (this * a.E);
  438.     public P Pr(P a, P b) => this - Pr(a & b);
  439.     public P Ort(P a) => this - Pr(a);
  440.     public P Ort(P a, P b) => this - Pr(a, b);
  441.     public P Rot(P w, double φ) {
  442.       P u = Ort(w);
  443.       P v = w.E & this;
  444.       return this.Pr(w) + u * double.Cos(φ) + v * double.Sin(φ);
  445.     }
  446.     public P Rot(double ψ, double φ, double θ) {
  447.       P x = new P(1, 0, 0);
  448.       P z = new P(0, 0, 1);
  449.       P xx = x.Rot(z, ψ);
  450.       P a = Rot(z, ψ);
  451.       P zz = z.Rot(xx, θ);
  452.       P b = a.Rot(xx, θ);
  453.       return b.Rot(zz, φ);
  454.     }
  455.     public static (P p, bool flag) XLin(P A, P B, P C, P D) {
  456.       double α1 = A.y - B.y, β1 = B.x - A.x, γ1 = A.y * B.x - A.x * B.y;
  457.       double α2 = C.y - D.y, β2 = D.x - C.x, γ2 = C.y * D.x - C.x * D.y;
  458.       double det = α1 * β2 - α2 * β1;
  459.       double detX = γ1 * β2 - γ2 * β1;
  460.       double detY = α1 * γ2 - α2 * γ1;
  461.       P p = new P(detX / det, detY / det);
  462.       bool flag = (A - p) * (B - p) < 0 && (C - p) * (D - p) < 0;
  463.       return (p, flag);
  464.     }
  465.  
  466.     public static class M {
  467.       static func D(func f, double h) =>
  468.        x => (f(x + h / 2) - f(x - h / 2)) / h;
  469.       static func D(func f) {
  470.         double h = 1e-3, hh = h * Math.Sqrt(2);
  471.         return x => D(f, h)(x) * 2 - D(f, hh)(x);
  472.       }
  473.       public static double Newton(func f, double x0) {
  474.         func df = D(f);
  475.         double ε1 = 1e-3, ε2 = 1e-9;
  476.         double x = x0;
  477.         int cnt = 0;
  478.         double μ = 0.5;
  479.         bool flag = false;
  480.         while (true) {
  481.           double dx = f(x) * μ / df(x);
  482.           double xx = x - dx;
  483.           if (flag) {
  484.             cnt++;
  485.             if (cnt == 3) return xx;
  486.           } else {
  487.             double λ = Math.Abs(dx / xx);
  488.             if (λ < ε2) flag = true;
  489.             else if (λ < ε1) μ = 1;
  490.           }
  491.           x = xx;
  492.         }
  493.       }
  494.     }
  495.   }
  496.   public static class Program {
  497.     static func sqr = t => t * t, sqrt = Math.Sqrt, abs = Math.Abs;
  498.     static func cube = t => t * sqr(t);
  499.     static func ln = Math.Log;
  500.     static func sin = Math.Sin, cos = Math.Cos, tg = Math.Tan;
  501.     static func asin = Math.Asin, acos = Math.Acos, atg = Math.Atan;
  502.     static func2 pwf = (x, y) => Math.Pow(x, y);
  503.     static double π = Math.PI;
  504.     static Random rnd = new Random();
  505.     static int[] First(int n) {
  506.       int[] m = new int[n];
  507.       for (int i = 0; i < n; i++) m[i] = i;
  508.       return m;
  509.     }
  510.     static int[] Next(int[] n, int N) {
  511.       int ii = -1;
  512.       for (int i = n.Length - 1; i >= 0; i--)
  513.         if (n[i] < N - n.Length + i) { ii = i; break; }
  514.       if (ii == -1) return null;
  515.       int[] nn = new int[n.Length];
  516.       for (int i = 0; i < n.Length; i++)
  517.         nn[i] = i < ii ? n[i] : i > ii ? nn[i - 1] + 1 : n[i] + 1;
  518.       return nn;
  519.     }
  520.  
  521.     public static void Main() {
  522.       {
  523.         var f = new funcN[2];
  524.         f[0] = t => {
  525.           double x = t[0], y = t[1];
  526.           return (x - 1) * ln(x) - x * y * sin(y);
  527.         };
  528.         f[1] = t => {
  529.           double x = t[0], y = t[1];
  530.           return x * y * cos(y) + x * ln(x) * sin(y) - y;
  531.         };
  532.         var t = new NLinEq(f).Solve(new double[] { 7.66, 2.22 });
  533.         Arr.Show(t, 15, 12);
  534.  
  535.         return;
  536.       }
  537.       {
  538.         funcN f = t => {
  539.           double x = t[0], y = t[1], z = t[2];
  540.           return 1 / (y + z) + 1 / (z + x) + 1 / (x + y) - 5;
  541.         };
  542.         funcN g = t => {
  543.           double x = t[0], y = t[1], z = t[2];
  544.           return x / (y + z) + y / (z + x) + z / (x + y) - 32;
  545.         };
  546.         funcN h = t => {
  547.           double x = t[0], y = t[1], z = t[2];
  548.           return y * z + z * x + x * y;
  549.         };
  550.  
  551.         funcN fx = M.D(f, 0);
  552.         funcN fy = M.D(f, 1);
  553.         funcN fz = M.D(f, 2);
  554.         funcN gx = M.D(g, 0);
  555.         funcN gy = M.D(g, 1);
  556.         funcN gz = M.D(g, 2);
  557.         funcN hx = M.D(h, 0);
  558.         funcN hy = M.D(h, 1);
  559.         funcN hz = M.D(h, 2);
  560.  
  561.         var φ = new funcN[5];
  562.         φ[0] = t => {
  563.           double x = t[0], y = t[1], z = t[2], λ = t[3], μ = t[4];
  564.           var w = new double[] { x, y, z };
  565.           return f(w);
  566.         };
  567.         φ[1] = t => {
  568.           double x = t[0], y = t[1], z = t[2], λ = t[3], μ = t[4];
  569.           var w = new double[] { x, y, z };
  570.           return g(w);
  571.         };
  572.         φ[2] = t => {
  573.           double x = t[0], y = t[1], z = t[2], λ = t[3], μ = t[4];
  574.           var w = new double[] { x, y, z };
  575.           return λ * fx(w) + μ * gx(w) - hx(w);
  576.         };
  577.         φ[3] = t => {
  578.           double x = t[0], y = t[1], z = t[2], λ = t[3], μ = t[4];
  579.           var w = new double[] { x, y, z };
  580.           return λ * fy(w) + μ * gy(w) - hy(w);
  581.         };
  582.         φ[4] = t => {
  583.           double x = t[0], y = t[1], z = t[2], λ = t[3], μ = t[4];
  584.           var w = new double[] { x, y, z };
  585.           return λ * fz(w) + μ * gz(w) - hz(w);
  586.         };
  587.         double F(double[] t) {
  588.           double s = 0;
  589.           for (int i = 0; i < 5; i++) s += sqr(φ[i](t));
  590.           return s;
  591.         }
  592.  
  593.  
  594.  
  595.  
  596.         //Console.WriteLine(τ);
  597.  
  598.         var t0 = new double[] { 0.105, 0.105, 6.72, -0.522, 0.033 };
  599.         //var rnd = new Random();
  600.         //for (int i = 0; i < t0.Length; i++)
  601.         //t0[i] = rnd.NextDouble();
  602.         //for (int i = 0; i < t0.Length; i++) t0[i] = 1;
  603.         double F0 = F(t0);
  604.         Console.WriteLine(F0);
  605.         double δ = 0.01;
  606.         int cnt = 0;
  607.         while (δ > 1e-30) {
  608.           //double F0 = F(t0);
  609.           double[] dt = M.Grad(F, t0);
  610.           double[] t = Arr.Plus(t0, dt, -δ);
  611.           double Ft = F(t);
  612.           if (Ft < F0) {
  613.             cnt++;
  614.             t0 = Arr.Copy(t);
  615.             F0 = Ft;
  616.             if (cnt == 25) {
  617.               Console.Clear();
  618.               Console.WriteLine(F0);
  619.               Arr.ShowNL(t0, 18, 15);
  620.               Console.WriteLine(h(new double[] { t0[0], t0[1], t0[2] }));
  621.               //double[] r = new double[4];
  622.               //for (int i = 0; i < 4; i++) r[i] = f[i](t0);
  623.               //Arr.ShowNL(r, 18, 15);
  624.               //cnt = 0;
  625.               //Console.WriteLine(δ);
  626.               δ *= 1.2;
  627.               cnt = 0;
  628.             }
  629.           } else {
  630.             //cnt = 0;
  631.             //Console.WriteLine(δ);
  632.             δ *= 0.7;
  633.           }
  634.         }
  635.         Console.Clear();
  636.  
  637.       }
  638.       return;
  639.  
  640.     }
  641.  
  642.  
  643.  
  644.   }
  645.  
  646.  
  647.  
  648. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement