Advertisement
VNM24ix

Floats

Jun 9th, 2025 (edited)
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 42.09 KB | Source Code | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. //using Float.Flt;
  6. //using Float.Cplx;
  7. //using Float.Equations;
  8. //using Float.Program;
  9. using FuncSqrt;
  10. //using FuncSqrt.Flt;
  11.  
  12. namespace FuncSqrt;
  13. delegate Flt func(Flt x);
  14. delegate Flt funcN(Flt[] x);
  15. delegate Flt func2(Flt x, Flt y);
  16. static class Arr {
  17.   static Random rnd = new Random();
  18.   public static int NZ0(uint[] a) {
  19.     int res = a.Length;
  20.     for (int i = a.Length - 1; i >= 0; i--)
  21.       if (a[i] == 0) res--;
  22.       else break;
  23.     return res;
  24.   }
  25.   public static int NZ(uint[] a) {
  26.     int n0 = NZ0(a);
  27.     if (n0 == 0) return 0;
  28.     int n1 = 4;
  29.     for (int i = 3; i >= 0; i--)
  30.       if ((a[n0 - 1] & (0xff << (i << 3))) == 0) n1--;
  31.       else break;
  32.     uint b = (a[n0 - 1] >> ((n1 - 1) << 3)) & 0xff;
  33.     int n2 = 8;
  34.     for (int i = 7; i >= 0; i--)
  35.       if ((b & (1 << i)) == 0) n2--;
  36.       else break;
  37.     return ((n0 - 1) << 5) + ((n1 - 1) << 3) + n2;
  38.   }
  39.   public static uint[] Copy(uint[] a) {
  40.     uint[] aa = new uint[a.Length];
  41.     a.CopyTo(aa, 0);
  42.     return aa;
  43.   }
  44.   public static uint[] Rand(int size) {
  45.     uint[] a = new uint[size];
  46.     for (int i = 0; i < size; i++)
  47.       for (int j = 0; j < 4; j++)
  48.         a[i] |= (uint)rnd.Next(0x100) << (j << 3);
  49.     return a;
  50.   }
  51.   static string StrB(uint x) {
  52.     string s = "";
  53.     for (int i = 31; i >= 0; i--)
  54.       s += ((x & (1u << i)) > 0 ? '1' : '0');
  55.     return s;
  56.   }
  57.   public static string StrB(uint[] a, bool nz = false) {
  58.     if (NZ0(a) == 0 && nz) return "0";
  59.     string s = "";
  60.     for (int i = a.Length - 1; i >= 0; i--)
  61.       s += StrB(a[i]);
  62.     if (nz) {
  63.       int k = 0;
  64.       for (int i = 0; i < s.Length; i++)
  65.         if (s[i] == '0') k++;
  66.         else break;
  67.       s = s.Substring(k, s.Length - k);
  68.     }
  69.     return s;
  70.   }
  71.   public static bool GetBit(uint[] a, int n) {
  72.     if (n > (a.Length << 5) - 1) return false;
  73.     int n0 = n >> 5, n1 = n & 0x1f;
  74.     return (a[n0] & (1 << n1)) > 0;
  75.   }
  76.   public static void SetBit(ref uint[] a, int n, bool f) {
  77.     int n0 = n >> 5, n1 = n & 0x1f;
  78.     if (f && n0 > a.Length - 1) {
  79.       uint[] aa = new uint[n0 + 1];
  80.       a.CopyTo(aa, 0);
  81.       a = aa;
  82.     }
  83.     a[n0] = f ? (a[n0] | (1u << n1)) : (a[n0] & ~(1u << n1));
  84.   }
  85.   public static void SetLen(ref uint[] a, int l) {
  86.     uint[] aa = new uint[l];
  87.     if (l >= a.Length) a.CopyTo(aa, 0);
  88.     else for (int i = 0; i < l; i++) aa[i] = a[i];
  89.     a = aa;
  90.   }
  91.   public static int Cmp(uint[] a, uint[] b) {
  92.     int m = Math.Max(a.Length, b.Length);
  93.     for (int i = m - 1; i >= 0; i--) {
  94.       uint aa = i < a.Length ? a[i] : 0;
  95.       uint bb = i < b.Length ? b[i] : 0;
  96.       if (aa < bb) return -1;
  97.       else if (aa > bb) return 1;
  98.     }
  99.     return 0;
  100.   }
  101.   public static uint[] ShiftL(uint[] a, int n) {
  102.     //Console.WriteLine("In ShiftL");
  103.     if (n == 0 || NZ0(a) == 0) return Copy(a);
  104.     int p = NZ(a) - 1 + n;
  105.     //Console.WriteLine($"p={p}  NZ(a)-1={NZ(a) - 1}");
  106.     int l = Math.Max((p >> 5) + 1, a.Length);
  107.     //Console.WriteLine($"l={l}  a.Length={a.Length}");
  108.     uint[] aa = new uint[l];
  109.     int n0 = n >> 5, n1 = n & 0x1f;
  110.     for (int i = Arr.NZ0(a) - 1; i >= 0; i--)
  111.       aa[i + n0] = a[i];
  112.     if (n1 > 0) {
  113.       //Console.WriteLine("In Sh");
  114.       for (int i = aa.Length - 1; i >= 0; i--) {
  115.         ulong b = ((ulong)aa[i] << 32) | (i > 0 ? aa[i - 1] : 0);
  116.         b <<= n1;
  117.         aa[i] = (uint)(b >> 32);
  118.       }
  119.       //Console.WriteLine("aa after n1");
  120.       //Console.WriteLine(Arr.StrB(aa));
  121.     }
  122.     return aa;
  123.   }
  124.   public static uint[] ShiftR(uint[] a, int n) {
  125.     if (n == 0) return Copy(a);
  126.     bool f = GetBit(a, n - 1);
  127.     int n0 = n >> 5, n1 = n & 0x1f;
  128.     uint[] aa = new uint[a.Length];
  129.     for (int i = n0; i < a.Length; i++)
  130.       aa[i - n0] = a[i];
  131.     if (n1 > 0)
  132.       for (int i = 0; i < NZ0(aa); i++) {
  133.         ulong b = aa[i] | ((ulong)(i < aa.Length - 1 ? aa[i + 1] : 0) << 32);
  134.         aa[i] = (uint)(b >> n1);
  135.       }
  136.     if (f) aa = Add(aa, new uint[] { 1 });
  137.     return aa;
  138.   }
  139.   public static uint[] Add(uint[] a, uint[] b) {
  140.     uint[] res = new uint[Math.Max(a.Length, b.Length) + 1];
  141.     uint c = 0;
  142.     for (int i = 0; i < res.Length; i++) {
  143.       ulong aa = i < a.Length ? a[i] : 0;
  144.       ulong s = aa + (i < b.Length ? b[i] : 0) + c;
  145.       res[i] = (uint)s;
  146.       c = (s > uint.MaxValue ? 1u : 0);
  147.     }
  148.     return res;
  149.   }
  150.   public static void DelZeros(ref uint[] a) => SetLen(ref a, NZ0(a));
  151.   public static void DelFirstZeros(ref uint[] a) {
  152.     int k = 0;
  153.     for (int i = 0; i < a.Length; i++)
  154.       if (a[i] == 0) k++;
  155.       else break;
  156.     uint[] aa = new uint[a.Length - k];
  157.     for (int i = 0; i < aa.Length; i++)
  158.       aa[i] = a[k + i];
  159.     a = aa;
  160.   }
  161.   public static uint[] Sub(uint[] a, uint[] b) {
  162.     uint[] res = new uint[Math.Max(a.Length, b.Length)];
  163.     uint c = 0;
  164.     for (int i = 0; i < res.Length; i++) {
  165.       ulong s = 0x100000000ul + (i < a.Length ? a[i] : 0) - (i < b.Length ? b[i] : 0) - c;
  166.       res[i] = (uint)s;
  167.       c = s > uint.MaxValue ? 0 : 1u;
  168.     }
  169.     return res;
  170.   }
  171.  
  172.   public static uint[] Mul(uint[] a, uint[] b) {
  173.     uint[] res = new uint[a.Length + b.Length];
  174.     for (int i = 0; i < NZ0(a); i++)
  175.       for (int j = 0; j < NZ0(b); j++) {
  176.         ulong w = (ulong)a[i] * b[j];
  177.         int k0 = i + j;
  178.         uint[] u = new uint[] { (uint)w, (uint)(w >> 32) };
  179.         for (int t = 0; t < 2; t++) {
  180.           int k = k0 + t;
  181.           do {
  182.             ulong v = (ulong)res[k] + u[t];
  183.             res[k++] = (uint)v;
  184.             u[t] = (uint)(v >> 32);
  185.           } while (u[t] > 0);
  186.         }
  187.       }
  188.     return res;
  189.   }
  190. }
  191. class Flt {
  192.   public static readonly int N = Program.N;
  193.   uint[] a;
  194.   public uint[] A => Arr.Copy(a);
  195.   public int exp { get; private set; }
  196.   public int sign { get; private set; }
  197.   public Flt(uint[] a, int exp, int sign) {
  198.     if (sign != 0) {
  199.       var v = Norm(a, exp);
  200.       this.a = v.a;
  201.       this.exp = v.exp;
  202.       this.sign = Arr.NZ0(this.a) == 0 ? 0 : Math.Sign(sign);
  203.     } else {
  204.       this.a = new uint[N];
  205.       this.exp = 0;
  206.       this.sign = 0;
  207.     }
  208.   }
  209.   public Flt(Flt x) : this(x.a, x.exp, x.sign) { }
  210.   public static implicit operator Flt(long n) {
  211.     if (n == 0) return Zero;
  212.     int sign = Math.Sign(n);
  213.     ulong u = (ulong)Math.Abs(n);
  214.     uint[] a = new uint[N];
  215.     a[^2] = (uint)u;
  216.     a[^1] = (uint)(u >> 32);
  217.     int d = (N << 5) - Arr.NZ(a);
  218.     int exp = 64 - d;
  219.     a = Arr.ShiftL(a, d);
  220.     return new Flt(a, exp, sign);
  221.   }
  222.   public static implicit operator Flt(double x) {
  223.     string str = x.ToString();
  224.     bool neg = str[0] == '-';
  225.     if (neg) str = str.Substring(1, str.Length - 1);
  226.     int p = str.IndexOf(',');
  227.     if (p == -1) {
  228.       long n = long.Parse(str);
  229.       return (Flt)(neg ? -n : n);
  230.     } else {
  231.       string str0 = str.Substring(0, p);
  232.       string str1 = str.Substring(p + 1, str.Length - p - 1);
  233.       Flt res = (Flt)long.Parse(str0) + (Flt)long.Parse(str1) / ((Flt)10).Pow(str1.Length);
  234.       return neg ? -res : res;
  235.     }
  236.   }
  237.   public Flt Copy => new Flt(a, exp, sign);
  238.   public static Flt[] CopyArr(Flt[] x) {
  239.     Flt[] xx = new Flt[x.Length];
  240.     for (int i = 0; i < x.Length; i++)
  241.       xx[i] = x[i].Copy;
  242.     return xx;
  243.   }
  244.   public Flt Abs => sign >= 0 ? Copy : new Flt(a, exp, 1);
  245.   public static Flt Zero => new Flt(new uint[N], 0, 0);
  246.  
  247.   public bool IsZero => sign == 0;
  248.   public static (uint[] a, int exp) Norm(uint[] a0, int exp0) {
  249.     //Console.WriteLine("In norm");
  250.     int n = Arr.NZ(a0);
  251.     if (n == 0) return (new uint[N], 0);
  252.     if (n == (N << 5)) {
  253.       //Console.WriteLine("Here");
  254.       uint[] a = Arr.Copy(a0);
  255.       Arr.SetLen(ref a, N);
  256.       return (a, exp0);
  257.     } else if (n < (N << 5)) return (Arr.ShiftL(a0, (N << 5) - n), exp0 + n - (N << 5));
  258.     else {
  259.       uint[] a = Arr.ShiftR(a0, n - (N << 5));
  260.       Arr.SetLen(ref a, N);
  261.       int exp = exp0 + n - (N << 5);
  262.       return (a, exp);
  263.     }
  264.   }
  265.   public static Flt operator *(Flt x, Flt y) {
  266.     if (x.IsZero || y.IsZero) return Zero;
  267.     uint[] z = Arr.Mul(x.A, y.A);
  268.     z = Arr.ShiftR(z, (N << 5));
  269.     //Console.WriteLine("in * before norm\n" + Arr.StrB(z));
  270.     //var v = Norm(z, x.exp + y.exp - (N << 5));
  271.     var v = Norm(z, x.exp + y.exp);
  272.     //Console.WriteLine("in * after norm\n" + Arr.StrB(v.a));
  273.     Arr.SetLen(ref v.a, N);
  274.     return new Flt(v.a, v.exp, x.sign * y.sign);
  275.   }
  276.   public bool Eq(Flt x) =>
  277.   sign == x.sign && exp == x.exp && Arr.Cmp(a, x.a) == 0;
  278.   public bool NEq(Flt x) => !Eq(x);
  279.   public Flt Pow(int n) {
  280.     if (n > 1) {
  281.       Flt x = Pow(n / 2);
  282.       Flt x2 = x * x;
  283.       return (n & 1) == 0 ? x2 : x2 * this;
  284.     } else if (n == 1) return Copy;
  285.     else if (n == 0) return 1;
  286.     else return 1 / Pow(-n);
  287.   }
  288.   public static int CmpAbs(Flt x, Flt y) {
  289.     if (x.exp != y.exp) return Math.Sign(x.exp - y.exp);
  290.     else return Arr.Cmp(x.a, y.a);
  291.   }
  292.   static int Cmp(Flt x, Flt y) {
  293.     if (x.sign != y.sign) return Math.Sign(x.sign - y.sign);
  294.     else return CmpAbs(x, y) * x.sign;
  295.   }
  296.  
  297.   public static bool operator <(Flt x, Flt y) {
  298.     if (x.sign != y.sign) return x.sign < y.sign;
  299.     else if (x.sign == 0) return false;
  300.     else {
  301.       int s = CmpAbs(x, y);
  302.       return x.sign == -s;
  303.     }
  304.   }
  305.   public static bool operator >(Flt x, Flt y) => y < x;
  306.  
  307.  
  308.   public Flt Root(int n) {
  309.     Flt x = 1;
  310.     bool f = false;
  311.     while (true) {
  312.       Flt xx = (this / x.Pow(n - 1) + x * (n - 1)) / n;
  313.       if (f) return xx;
  314.       f = x.exp == xx.exp && x.sign == xx.sign;
  315.       for (int i = x.A.Length - 1; i > 0; i--)
  316.         f &= x.A[i] == xx.A[i];
  317.       x = xx;
  318.     }
  319.   }
  320.  
  321.   public static Flt operator /(Flt x, Flt y) {
  322.     if (y.IsZero) return null;
  323.     else if (x.IsZero) return Zero;
  324.     uint[] xa = x.A;
  325.     uint[] ya = y.A;
  326.     Arr.SetLen(ref xa, N + 1);
  327.     Arr.SetLen(ref ya, N + 1);
  328.     xa = Arr.ShiftL(xa, 31);
  329.     ya = Arr.ShiftL(ya, 16);
  330.     uint[] z = new uint[1];
  331.     while (Arr.NZ(z) <= (N << 5)) {
  332.       ulong u = ((ulong)xa[^1] << 16) | (xa[^2] >> 16);
  333.       uint v = (ya[^1] << 16) | (ya[^2] >> 16);
  334.       uint w = (uint)(u / v);
  335.       uint[] yw = Arr.Mul(ya, new uint[] { w });
  336.       Arr.SetLen(ref yw, N + 1);
  337.       if (Arr.Cmp(xa, yw) < 0) {
  338.         w--;
  339.         yw = Arr.Sub(yw, ya);
  340.       }
  341.       xa = Arr.Sub(xa, yw);
  342.       xa = Arr.ShiftL(xa, 16);
  343.       z = Arr.ShiftL(z, 16);
  344.       z[0] |= w;
  345.     }
  346.     int n = (Arr.NZ0(z) << 5) - Arr.NZ(z);
  347.     //Console.WriteLine($"n={n}");
  348.     z = Arr.ShiftL(z, n);
  349.     //Console.WriteLine($"zlen={z.Length}");
  350.     uint[] zz = new uint[N];
  351.     for (int i = 0; i < N; i++)
  352.       zz[^(1 + i)] = z[^(1 + i)];
  353.     //Console.WriteLine(Arr.StrB(zz));
  354.     return new Flt(zz, x.exp - y.exp + 1 - (n & 1), x.sign * y.sign);
  355.   }
  356.  
  357.   public static Flt operator +(Flt x, Flt y) {
  358.     if (x.sign == 0) return y.Copy;
  359.     else if (y.sign == 0) return x.Copy;
  360.     else {
  361.       uint[] xa = x.A;
  362.       uint[] ya = y.A;
  363.       int expX = x.exp, expY = y.exp;
  364.       bool f = expX > expY || expX == expY && Arr.Cmp(xa, ya) >= 0;
  365.       int d = Math.Max(expX, expY) - Math.Min(expX, expY);
  366.       uint[] r;
  367.       if (f)
  368.         ya = Arr.ShiftR(ya, d);
  369.       else
  370.         xa = Arr.ShiftR(xa, d);
  371.       //Console.WriteLine("In + xa\n" + Arr.StrB(xa));
  372.       //Console.WriteLine("In + ya\n" + Arr.StrB(ya));
  373.       if (x.sign == y.sign)
  374.         r = Arr.Add(xa, ya);
  375.       else
  376.         r = f ? Arr.Sub(xa, ya) : Arr.Sub(ya, xa);
  377.       //Console.WriteLine("In + xa+ya\n" + Arr.StrB(r));
  378.       var v = Norm(r, Math.Max(x.exp, y.exp));
  379.       int sign = f ? x.sign : y.sign;
  380.       return new Flt(v.a, v.exp, sign);
  381.     }
  382.   }
  383.   public static Flt operator -(Flt x) => new Flt(x.a, x.exp, -x.sign);
  384.   public static Flt operator -(Flt x, Flt y) => x + -y;
  385.   public static explicit operator Flt(string str) {
  386.     int indP(string str) {
  387.       int i = str.IndexOf('.');
  388.       return i > -1 ? i : str.IndexOf(',');
  389.     }
  390.     Flt IntToFlt(string str) {
  391.       Flt x = 0;
  392.       for (int i = 0; i < str.Length; i++) {
  393.         x *= 10;
  394.         x += (int)str[i] - (int)'0';
  395.       }
  396.       return x;
  397.     }
  398.     Flt FracToFlt(string str) {
  399.       int p = indP(str);
  400.       if (p == -1) return IntToFlt(str);
  401.       string s0 = str.Substring(0, p);
  402.       string s1 = str.Substring(p + 1, str.Length - (p + 1));
  403.       return IntToFlt(s0) + IntToFlt(s1) / ((Flt)10).Pow(s1.Length);
  404.     }
  405.     if (str[0] == '-') return -(Flt)str.Substring(1, str.Length - 1);
  406.     else if (str[0] == '+') return (Flt)str.Substring(1, str.Length - 1);
  407.     int indE = str.IndexOf('E');
  408.     if (indE == -1) indE = str.IndexOf('e');
  409.     if (indE == -1) return FracToFlt(str);
  410.     else {
  411.       string s0 = str.Substring(0, indE);
  412.       string s1 = str.Substring(indE + 1, str.Length - (indE + 1));
  413.       return FracToFlt(s0) * ((Flt)10).Pow(int.Parse(s1));
  414.     }
  415.   }
  416.   public void ShowB(string name = "") {
  417.     if (name != "") Console.WriteLine(name + " =\n");
  418.     string s = Arr.StrB(a);
  419.     for (int i = 0; i < N; i++) {
  420.       string s1 = s.Substring((i << 5), 16);
  421.       Console.ForegroundColor = ConsoleColor.Yellow;
  422.       Console.Write(s1);
  423.       string s2 = s.Substring((i << 5) + 16, 16);
  424.       Console.ForegroundColor = ConsoleColor.Cyan;
  425.       Console.Write(s2);
  426.     }
  427.     Console.Write("\n");
  428.     Console.ForegroundColor = ConsoleColor.White;
  429.     Console.Write($"exp={exp}  sign={sign}\n\n");
  430.   }
  431.   public string Str10 => new Dcm(this).Str();
  432.   public string Str10Round(int n) => new Dcm(this).Str(n);
  433.   public void Show10(string name = "") {
  434.     if (name != "") Console.WriteLine(name + " =");
  435.     Console.WriteLine(Str10);
  436.   }
  437.   public void Show10Round(int n, string name = "") {
  438.     if (name != "") Console.WriteLine(name + " =");
  439.     string str = Str10Round(n);
  440.     if (str[0] != '-') str = " " + str;
  441.     Console.WriteLine(str);
  442.   }
  443.   public static void ShowArray(Flt[] a, int prec, string name, ConsoleColor clr = ConsoleColor.White) {
  444.     Console.ForegroundColor = clr;
  445.     for (int i = 0; i < a.Length; i++) {
  446.       if (name != "") Console.WriteLine($"{name}[{i}] = ");
  447.       a[i].Show10Round(prec);
  448.     }
  449.     Console.ForegroundColor = ConsoleColor.White;
  450.   }
  451.  
  452.  
  453.   public class Dcm {
  454.     Flt x;
  455.     public Dcm(Flt x) {
  456.       this.x = x.Copy;
  457.     }
  458.     public uint[] Int() {
  459.       if (x.exp <= 0) return new uint[0];
  460.       uint[] a0 = Arr.ShiftL(x.a, x.exp);
  461.       uint[] a = new uint[a0.Length - N];
  462.       for (int i = 0; i < a.Length; i++)
  463.         a[i] = a0[N + i];
  464.       return a;
  465.     }
  466.     public uint[] Frac() {
  467.       //Console.WriteLine("In Frac");
  468.       if (x.exp == 0) return Arr.Copy(x.a);
  469.       else if (x.exp > 0) {
  470.         //Console.WriteLine("Aaaa");
  471.         uint[] a = Arr.ShiftL(x.a, x.exp);
  472.         Arr.SetLen(ref a, N);
  473.         return a;
  474.       } else {
  475.         //Console.WriteLine("Bbbb");
  476.         int n = ((-x.exp - 1) >> 5) + 1;
  477.         //Console.WriteLine("n =" + n);
  478.         uint[] a = x.A;
  479.         Arr.SetLen(ref a, N + n);
  480.         a = Arr.ShiftL(a, (n << 5) + x.exp);
  481.         return a;
  482.       }
  483.     }
  484.     static (uint[] q, uint r) Div(uint[] a, uint b) {
  485.       int n0 = Arr.NZ0(a);
  486.       uint[] q;
  487.       uint r;
  488.       if (n0 < 3) {
  489.         ulong u = n0 == 0 ? 0 : (n0 == 1 ? a[0] : ((ulong)a[1] << 32) | a[0]);
  490.         ulong q0 = u / b;
  491.         r = (uint)(u % b);
  492.         q = (q0 > uint.MaxValue ? new uint[] { (uint)q0, (uint)(q0 >> 32) } :
  493.         new uint[] { (uint)q0 });
  494.       } else {
  495.         uint[] aa = Arr.Copy(a);
  496.         Arr.DelZeros(ref aa);
  497.         uint[] bb0 = new uint[] { b };
  498.         uint[] bb = Arr.ShiftL(bb0, Arr.NZ(aa) - Arr.NZ(bb0) + 1);
  499.         q = new uint[] { 0 };
  500.         while (Arr.Cmp(bb, bb0) == 1) {
  501.           bb = Arr.ShiftR(bb, 1);
  502.           q = Arr.ShiftL(q, 1);
  503.           if (Arr.Cmp(aa, bb) >= 0) {
  504.             aa = Arr.Sub(aa, bb);
  505.             q[0]++;
  506.           }
  507.         }
  508.         r = aa[0];
  509.       }
  510.       return (q, r);
  511.     }
  512.     static string DcmInt(uint[] a) {
  513.       var l = new List<uint>();
  514.       uint[] aa = Arr.Copy(a);
  515.       while (Arr.NZ0(aa) > 0) {
  516.         var v = Div(aa, 1000000000u);
  517.         l.Add(v.r);
  518.         aa = v.q;
  519.       }
  520.       if (l.Count == 0) return "0";
  521.       string res = "";
  522.       for (int i = l.Count - 1; i >= 0; i--) {
  523.         string s = l[i].ToString();
  524.         if (i < l.Count - 1) s = new String('0', 9 - s.Length) + s;
  525.         res += s;
  526.       }
  527.       return res;
  528.     }
  529.     public string DcmInt() => DcmInt(Int());
  530.     static string DcmFrac(uint[] a) {
  531.       //Console.WriteLine("In DcmFrac");
  532.       uint[] aa = Arr.Copy(a);
  533.       var l = new List<uint>();
  534.       while (Arr.NZ0(aa) > 0) {
  535.         Arr.DelFirstZeros(ref aa);
  536.         Arr.SetLen(ref aa, aa.Length + 1);
  537.         uint c = 0;
  538.         for (int i = 0; i < aa.Length; i++) {
  539.           ulong s = (ulong)aa[i] * 1000000000u + c;
  540.           aa[i] = (uint)s;
  541.           c = (uint)(s >> 32);
  542.         }
  543.         l.Add(aa[aa.Length - 1]);
  544.         Arr.SetLen(ref aa, aa.Length - 1);
  545.       }
  546.       string res = "";
  547.       for (int i = 0; i < l.Count; i++) {
  548.         string s = l[i].ToString();
  549.         s = new String('0', 9 - s.Length) + s;
  550.         res += s;
  551.       }
  552.       return res == "" ? "0" : res;
  553.     }
  554.     public string DcmFrac() => DcmFrac(Frac());
  555.     public string Str() {
  556.       if (x.IsZero) return "0";
  557.       int l = (int)Math.Round(N * 9.6) - 1;
  558.       string res;
  559.       string sInt = DcmInt();
  560.       string sFrac = DcmFrac();
  561.       //if (sInt.Length >= l) return sInt;
  562.       if (sInt.Length >= l) res = sInt;
  563.       else if (sInt != "0") {
  564.         if (sInt.Length + sFrac.Length <= l) res = sInt + "." + sFrac;
  565.         else res = sInt + "." + sFrac.Substring(0, l - sInt.Length);
  566.       } else {
  567.         int k = 0;
  568.         for (int i = 0; i < sFrac.Length; i++)
  569.           if (sFrac[i] != '0') break;
  570.           else k++;
  571.         //return "0." + (sFrac.Length < k + l ? sFrac : sFrac.Substring(0, k + l));
  572.         res = "0." + (sFrac.Length < k + l ? sFrac : sFrac.Substring(0, k + l));
  573.       }
  574.       return (x.sign < 0 ? "-" : "") + res;
  575.     }
  576.     public string Str(int n) {
  577.       string str = Str();
  578.       //Console.WriteLine("str=" + str);
  579.       bool neg = str[0] == '-';
  580.       //Console.WriteLine("neg=" + neg);
  581.       //Console.WriteLine(neg + " " + str[0]);
  582.       if (neg) str = str.Substring(1, str.Length - 1);
  583.       //Console.WriteLine("str0=" + str);
  584.       int p = str.IndexOf('.');
  585.       if (str.Length <= p + 1 + n) return (neg ? "-" : "") + str;//str;
  586.       bool f = str[p + 1 + n] > '4';
  587.       str = str.Substring(0, p + 1 + n);
  588.       if (!f) return (neg ? "-" : "") + str;
  589.       int k = -1;
  590.       for (int i = str.Length - 1; i >= 0; i--)
  591.         if (Char.IsDigit(str[i]) && str[i] < '9') { k = i; break; }
  592.       if (k == -1) { str = "0" + str; k = 0; }
  593.       //Console.WriteLine("str=" + str);
  594.       string str1 = str.Substring(0, k);
  595.       str1 += (char)((int)str[k] + 1);
  596.       for (int i = k + 1; i < str.Length; i++)
  597.         str1 += (str[i] == '.' ? '.' : '0');
  598.       //Console.WriteLine("check neg");
  599.       return neg ? "-" + str1 : str1;
  600.     }
  601.   }
  602.  
  603.   public class Polyn {
  604.     Flt[] a;
  605.     public int L => a.Length;
  606.     public int N => L - 1;
  607.     public Flt[] A {
  608.       get {
  609.         Flt[] aa = new Flt[L];
  610.         for (int i = 0; i < L; i++) aa[i] = a[i].Copy;
  611.         return aa;
  612.       }
  613.     }
  614.     public Flt this[int i] { get { return a[i].Copy; } set { a[i] = value.Copy; } }
  615.     public Polyn(params Flt[] a) {
  616.       this.a = new Flt[a.Length];
  617.       for (int i = 0; i < a.Length; i++)
  618.         this.a[i] = a[i].Copy;
  619.     }
  620.     //public static Polyn Zero => new Polyn(new Flt[0]);
  621.     public static Polyn operator -(Polyn p) {
  622.       Flt[] a = new Flt[p.L];
  623.       for (int i = 0; i < p.L; i++) a[i] = -p[i];
  624.       return new Polyn(a);
  625.     }
  626.     public static Polyn operator +(Polyn p1, Polyn p2) {
  627.       Flt[] a = new Flt[Math.Max(p1.L, p2.L)];
  628.       for (int i = 0; i < a.Length; i++)
  629.         a[i] = (i < p1.L ? p1[i] : 0) + (i < p2.L ? p2[i] : 0);
  630.       return new Polyn(a);
  631.     }
  632.     public static Polyn operator -(Polyn p1, Polyn p2) => p1 + -p2;
  633.     public static Polyn operator *(Polyn p, Flt μ) {
  634.       Polyn pp = new Polyn(p.a);
  635.       for (int i = 0; i < pp.L; i++) pp[i] *= μ;
  636.       return pp;
  637.     }
  638.     public static Polyn operator *(Flt μ, Polyn p) => p * μ;
  639.     public static Polyn operator *(Polyn p1, Polyn p2) {
  640.       Flt[] a = new Flt[p1.L + p2.L - 1];
  641.       for (int i = 0; i < p1.L; i++)
  642.         for (int j = 0; j < p2.L; j++) {
  643.           int k = i + j;
  644.           if (a[k] == null) a[k] = 0;
  645.           a[k] += p1[i] * p2[j];
  646.         }
  647.       return new Polyn(a);
  648.     }
  649.  
  650.     public Flt Value(Flt x) {
  651.       Flt res = 0;
  652.       for (int i = N; i >= 0; i--) {
  653.         res *= x;
  654.         res += a[i];
  655.       }
  656.       return res;
  657.     }
  658.     public Polyn D1 {
  659.       get {
  660.         if (L == 0) return new Polyn(new Flt[0]);
  661.         Flt[] aa = new Flt[N];
  662.         for (int i = 0; i < N; i++)
  663.           aa[i] = a[i + 1] * (i + 1);
  664.         return new Polyn(aa);
  665.       }
  666.     }
  667.     public Polyn D2 => D1.D1;
  668.     public Polyn Intg {
  669.       get {
  670.         Flt[] aa = new Flt[L + 1];
  671.         aa[0] = 0;
  672.         for (int i = 1; i <= L; i++)
  673.           aa[i] = a[i - 1] / i;
  674.         return new Polyn(aa);
  675.       }
  676.     }
  677.   }
  678.   static class M {
  679.     public static readonly Flt π = GetPi();
  680.     static Flt GetPi() =>
  681.       (ArcTg0((Flt)1 / 5) * 4 - ArcTg0((Flt)1 / 239)) * 4;
  682.     //ArcSin0((Flt)1 / 2) * 6;
  683.     public static readonly Flt ε0 = GetEps(-Flt.N * 16);
  684.     public static readonly Flt ε1 = GetEps(-Flt.N * 11);
  685.     public static readonly Flt ε2 = GetEps(-Flt.N * 22);
  686.     static Flt GetEps(int n) {
  687.       uint[] a = new uint[Flt.N];
  688.       Arr.SetBit(ref a, (Flt.N << 5) - 1, true);
  689.       return new Flt(a, n, 1);
  690.     }
  691.     public static readonly Flt ln2 = GetLn2();
  692.     static Flt GetLn2() => Ln0((Flt)4 / 3) - Ln0((Flt)2 / 3);
  693.     static Flt Ln0(Flt x) {
  694.       Flt y = x - 1;
  695.       Flt res = y.Copy;
  696.       Flt z = y.Copy;
  697.       int n = 1;
  698.       while (true) {
  699.         Flt t = res + (z = -z * y) / (++n);
  700.         if (t.Eq(res)) return res;
  701.         res = t;
  702.       }
  703.     }
  704.     public static Flt Ln(Flt x) {
  705.       if (x.sign != 1) return null;
  706.       Flt y = new Flt(x.A, 1, 1);
  707.       Flt z = (y - 1) / (y + 1);
  708.       return Ln0(1 + z) - Ln0(1 - z) + (x.exp - 1) * ln2;
  709.     }
  710.  
  711.     public static Flt Sin(Flt x) {
  712.       Flt t = x.Copy;
  713.       while (t.Abs > M.π)
  714.         if (t.sign == 1) t -= M.π * 2;
  715.         else t += M.π * 2;
  716.       Flt xx = t * t;
  717.       Flt y = t.Copy;
  718.       Flt s = t.Copy;
  719.       int n = 0;
  720.       //bool f = false;
  721.       while (true) {
  722.         n += 2;
  723.         y = -y * xx / (n * (n + 1));
  724.         Flt st = s + y;
  725.         if (st.Eq(s)) return st;
  726.         s = st;
  727.       }
  728.     }
  729.     public static Flt Cos(Flt x) {
  730.       Flt t = x.Copy;
  731.       while (t.Abs > M.π)
  732.         if (t.sign == 1) t -= M.π * 2;
  733.         else t += M.π * 2;
  734.       Flt xx = t * t;
  735.       Flt y = 1;
  736.       Flt s = 1;
  737.       int n = 0;
  738.       while (true) {
  739.         n += 2;
  740.         y = -y * xx / (n * (n - 1));
  741.         Flt st = s + y;
  742.         if (st.Eq(s)) break;
  743.         s = st;
  744.       }
  745.       return s;
  746.     }
  747.     public static Flt Tg(Flt x) => Sin(x) / Cos(x);
  748.     public static Flt Exp(Flt x) {
  749.       if (x.IsZero) return 1;
  750.       else if (x.sign == -1) return 1 / Exp(-x);
  751.       else {
  752.         Flt y = x.exp > 0 ? new Flt(x.A, 0, 1) : x.Copy;
  753.         Flt res = 1, z = 1;
  754.         int n = 0;
  755.         while (true) {
  756.           Flt t = res + (z *= y / ++n);
  757.           if (t.Eq(res)) break;
  758.           res = t;
  759.         }
  760.         for (int i = 0; i < x.exp; i++)
  761.           res = res.Pow(2);
  762.         return res;
  763.       }
  764.     }
  765.     public static Flt ArcTg0(Flt x) {
  766.       Flt y = x.Copy;
  767.       Flt xx = x * x;
  768.       Flt res = x.Copy;
  769.       int n = 1;
  770.       while (true) {
  771.         n += 2;
  772.         Flt t = res + (y = -y * xx) / n;
  773.         if (t.Eq(res)) break;
  774.         res = t;
  775.       }
  776.       return res;
  777.     }
  778.     public static Flt ArcTg(Flt x) {
  779.       if (x < 0) return -ArcTg(-x);
  780.       else if (x > 1) return π / 2 - ArcTg(1 / x);
  781.       else return Newton(t => Tg(t) - x, (Flt)11 / 28);
  782.     }
  783.     public static Flt ArcSin0(Flt x) =>
  784.     Newton(t => Sin(t) - x, (Flt)11 / 28);
  785.  
  786.     public static Flt Pow(Flt x, Flt y) =>
  787.     Exp(y * Ln(x));
  788.     public static func D1(func F) =>
  789.     x => (F(x + ε1 / 2) - F(x - ε1 / 2)) / ε1;
  790.     public static Flt Newton(func F, Flt x0) {
  791.       func Fx = D1(F);
  792.       int cnt = 0;
  793.       Flt x = x0.Copy;
  794.       bool f = false;
  795.       while (true) {
  796.         Flt xx = x - F(x) / Fx(x);
  797.         if (cnt == 5) return (xx + x) / 2;
  798.         f = (x - xx).Abs < ε0;
  799.         if (f) cnt++;
  800.         else cnt = 0;
  801.         x = xx;
  802.       }
  803.     }
  804.     public static Flt Intg(func F, Flt a, Flt b, int n = 1) {
  805.       if (n == 1) {
  806.         Flt h = b - a, hh = h / 6;
  807.         Flt x0 = a;
  808.         Flt x6 = b;
  809.         Flt x3 = (a + b) / 2;
  810.         Flt x1 = a + hh;
  811.         Flt x2 = x3 - hh;
  812.         Flt x4 = x3 + hh;
  813.         Flt x5 = x6 - hh;
  814.         Flt y3 = F(x3);
  815.         Flt y24 = (F(x2) + F(x4)) / 2;
  816.         Flt y15 = (F(x1) + F(x5)) / 2;
  817.         Flt y06 = (F(x0) + F(x6)) / 2;
  818.         return
  819.         (y3 * 136 + y24 * 27 + y15 * 216 + y06 * 41) * h / 420;
  820.       } else {
  821.         Flt res = 0;
  822.         Flt h = (b - a) / n;
  823.         for (int i = 0; i < n; i++) {
  824.           Flt aa = a + h * i;
  825.           Flt bb = aa + h;
  826.           res += Intg(F, aa, bb);
  827.         }
  828.         return res;
  829.       }
  830.     }
  831.     public static Flt Dihotom(func F, Flt a, Flt b, Flt ε) {
  832.       Flt aa = a.Copy, bb = b.Copy;
  833.       Flt fa = F(aa), fb = F(bb);
  834.       while ((aa - bb).Abs > ε) {
  835.         Flt c = (aa + bb) / 2, fc = F(c);
  836.         if (fc.Eq(0)) return c;
  837.         if (fc.sign == fa.sign) aa = c.Copy;
  838.         else bb = c.Copy;
  839.       }
  840.       return (fb * aa - fa * bb) / (fb - fa);
  841.     }
  842.  
  843.   }
  844.  
  845.  
  846.   /*
  847.     public static Flt[] Grad(funcN f, Flt[] x) {
  848.       var gr = new Flt[x.Length];
  849.       for (int i = 0; i < gr.Length; i++)
  850.         gr[i] = D1(f, i, x);
  851.       return gr;
  852.     }
  853.    
  854.   */
  855.   class Equations {
  856.     public static Flt D1(funcN f, Flt[] x, int n) {
  857.       Flt[] x1 = Flt.CopyArr(x);
  858.       x1[n] -= M.ε1 / 2;
  859.       Flt[] x2 = Flt.CopyArr(x);
  860.       x2[n] += M.ε1 / 2;
  861.       return (f(x2) - f(x1)) / M.ε1;
  862.     }
  863.     public static Flt[] Grad(funcN f, Flt[] x) {
  864.       var gr = new Flt[x.Length];
  865.       for (int i = 0; i < gr.Length; i++)
  866.         gr[i] = D1(f, x, i);
  867.       return gr;
  868.     }
  869.     static (Flt[,] a, Flt[] b) Mx(funcN[] f, Flt[] x) {
  870.       var a = new Flt[f.Length, f.Length];
  871.       var b = new Flt[f.Length];
  872.       for (int i = 0; i < f.Length; i++) {
  873.         b[i] = -f[i](x);
  874.         for (int j = 0; j < x.Length; j++) {
  875.           a[i, j] = D1(f[i], x, j);
  876.         }
  877.       }
  878.       return (a, b);
  879.     }
  880.     public static (Flt[] x, Flt max) Xnew(funcN[] f, Flt[] x0, double μ = 1) {
  881.       var mx = Mx(f, x0);
  882.       Flt[] dx = Matr.SolveLinEq(mx.a, mx.b);
  883.       Flt[] x = new Flt[x0.Length];
  884.       for (int i = 0; i < x.Length; i++)
  885.         x[i] = x0[i] + dx[i] * μ;
  886.       Flt max = 0;
  887.       for (int i = 0; i < dx.Length; i++)
  888.         if (dx[i].Abs > max) max = dx[i].Abs;
  889.       return (x, max);
  890.     }
  891.     public static Flt[] Solve(funcN[] f, Flt[] x0) {
  892.       double μ = 0.5;
  893.       bool s = false;
  894.       Flt[] x = Flt.CopyArr(x0);
  895.  
  896.       while (true) {
  897.         var v = Xnew(f, x, μ);
  898.         if (s) return v.x;
  899.         else if (v.max < M.ε2) s = true;
  900.         else if (v.max < 0.001) μ = 1;
  901.         x = v.x;
  902.  
  903.         ////////////////////////////
  904.         Console.Clear();
  905.         for (int i = 0; i < x.Length; i++)
  906.           x[i].Show10();
  907.         //Console.WriteLine();
  908.         //Console.ReadLine();
  909.         ////////////////////////////
  910.  
  911.       }
  912.     }
  913.     public static class Matr {
  914.       static bool IsZero(Flt x) => x.Abs < M.ε2;
  915.       static Flt[,] Inv(Flt[,] a0) {
  916.         int n = a0.GetLength(0), nn = n * 2;
  917.         Flt[,] a = new Flt[n, nn];
  918.         for (int i = 0; i < n; i++)
  919.           for (int j = 0; j < nn; j++)
  920.             a[i, j] = j < n ? a0[i, j].Copy : (j == i + n ? 1 : 0);
  921.         for (int i = 0; i < n; i++) {
  922.           if (IsZero(a[i, i])) {
  923.             int ii = i;
  924.             for (int j = i + 1; j < n; j++)
  925.               if (!IsZero(a[j, i])) { ii = j; break; }
  926.             if (ii == i) return null;
  927.             for (int j = i; j < nn; j++) {
  928.               Flt tmp = a[i, j].Copy; a[i, j] = a[ii, j].Copy; a[ii, j] = tmp;
  929.             }
  930.           }
  931.           Flt aii = a[i, i].Copy;
  932.           for (int j = i; j < nn; j++) a[i, j] /= aii;
  933.           for (int j = 0; j < n; j++)
  934.             if (j != i) {
  935.               Flt aji = a[j, i].Copy;
  936.               for (int k = i; k < nn; k++)
  937.                 a[j, k] -= aji * a[i, k];
  938.             }
  939.         }
  940.         Flt[,] aa = new Flt[n, n];
  941.         for (int i = 0; i < n; i++)
  942.           for (int j = 0; j < n; j++)
  943.             aa[i, j] = a[i, j + n].Copy;
  944.         return aa;
  945.       }
  946.       public static Flt[] Mul(Flt[,] a, Flt[] b) {
  947.         int n = b.Length;
  948.         Flt[] c = new Flt[n];
  949.         for (int i = 0; i < n; i++) {
  950.           c[i] = 0;
  951.           for (int j = 0; j < n; j++)
  952.             c[i] += a[i, j] * b[j];
  953.         }
  954.         return c;
  955.       }
  956.       public static Flt[] SolveLinEq(Flt[,] a, Flt[] b) {
  957.         Flt[,] aa = Inv(a);
  958.         if (aa == null) return null;
  959.         return Mul(aa, b);
  960.       }
  961.     }
  962.   }
  963.  
  964.   class Cplx {
  965.     Flt x, y;
  966.     public Flt X { get { return x.Copy; } private set { x = value.Copy; } }
  967.     public Flt Y { get { return y.Copy; } private set { y = value.Copy; } }
  968.     public Cplx(Flt x, Flt y) {
  969.       this.x = x;
  970.       this.y = y;
  971.     }
  972.     public Cplx(Cplx z) : this(z.x, z.y) { }
  973.     public Cplx Copy => new Cplx(this);
  974.     public Cplx(Flt x) : this(x, 0) { }
  975.     public static implicit operator Cplx(Flt x) =>
  976.     new Cplx(x);
  977.     public Flt R2 => x.Pow(2) + y.Pow(2);
  978.     public Flt R => R2.Root(2);
  979.     public static Cplx Zero => (Flt)0;
  980.     public bool IsZero => R2.exp < -(Flt.N << 32);
  981.     public static Cplx operator -(Cplx z) => new Cplx(-z.x, -z.y);
  982.     public static Cplx operator ~(Cplx z) => new Cplx(z.x, -z.y);
  983.     public static Cplx operator +(Cplx z1, Cplx z2) =>
  984.     new Cplx(z1.x + z2.x, z1.y + z2.y);
  985.     public static Cplx operator -(Cplx z1, Cplx z2) => z1 + -z2;
  986.     public static Cplx operator *(Cplx z1, Cplx z2) =>
  987.     new Cplx(z1.x * z2.x - z1.y * z2.y, z1.x * z2.y + z1.y * z2.x);
  988.     public static Cplx operator /(Cplx z, Flt a) => new Cplx(z.x / a, z.y / a);
  989.     public static Cplx operator /(Cplx z1, Cplx z2) => z1 * ~z2 / z2.R2;
  990.     public Cplx Pow(int n) {
  991.       if (n < 0) return (Flt)1 / Pow(-n);
  992.       else if (n == 0) return (Flt)1;
  993.       else if (n == 1) return Copy;
  994.       else {
  995.         int m = n / 2;
  996.         Cplx z = Pow(m);
  997.         Cplx res = z * z;
  998.         if ((n & 1) == 1) res *= this;
  999.         return res;
  1000.       }
  1001.     }
  1002.     public static Cplx[] RootsOf1(int n) {
  1003.       Cplx[] z = new Cplx[n];
  1004.       Flt φ = M.π * 2 / n;
  1005.       Cplx ζ = new Cplx(M.Cos(φ), M.Sin(φ));
  1006.       for (int i = 0; i < n; i++) z[i] = ζ.Pow(i);
  1007.       return z;
  1008.     }
  1009.     public void Show(string name = "") {
  1010.       if (name != "") Console.WriteLine(name + " =");
  1011.       Console.ForegroundColor = ConsoleColor.Red;
  1012.       x.Show10();
  1013.       if (!y.IsZero) {
  1014.         Console.ForegroundColor = ConsoleColor.Cyan;
  1015.         y.Show10();
  1016.       }
  1017.       Console.ForegroundColor = ConsoleColor.White;
  1018.     }
  1019.     public class Polyn {
  1020.       Cplx[] a;
  1021.       public Cplx this[int i] { get { return a[i].Copy; } set { a[i] = value.Copy; } }
  1022.       public int L => a.Length;
  1023.       public int N => L - 1;
  1024.       public Polyn(params Cplx[] a) {
  1025.         int k = a.Length;
  1026.         for (int i = a.Length - 1; i >= 0; i--)
  1027.           if (a[i].IsZero) k--;
  1028.           else break;
  1029.         this.a = new Cplx[k];
  1030.         for (int i = 0; i < k; i++)
  1031.           this.a[i] = a[i].IsZero ? (Flt)0 : a[i].Copy;
  1032.       }
  1033.       public static Polyn operator -(Polyn p) {
  1034.         Cplx[] a = new Cplx[p.L];
  1035.         for (int i = 0; i < p.L; i++)
  1036.           a[i] = -p[i];
  1037.         return new Polyn(a);
  1038.       }
  1039.  
  1040.       public static Polyn operator +(Polyn p1, Polyn p2) {
  1041.         Cplx[] a = new Cplx[Math.Max(p1.L, p2.L)];
  1042.         for (int i = 0; i < a.Length; i++)
  1043.           a[i] = (i < p1.L ? p1[i] : (Flt)0) + (i < p2.L ? p2[i] : (Flt)0);
  1044.         return new Polyn(a);
  1045.       }
  1046.       public static Polyn operator -(Polyn p1, Polyn p2) => p1 + -p2;
  1047.       public static Polyn operator *(Polyn p, Cplx b) {
  1048.         Cplx[] a = new Cplx[p.L];
  1049.         for (int i = 0; i < p.L; i++) a[i] *= b;
  1050.         return new Polyn(a);
  1051.       }
  1052.       public static Polyn operator *(Cplx b, Polyn p) => p * b;
  1053.       public static Polyn operator *(Polyn p1, Polyn p2) {
  1054.         Cplx[] a = new Cplx[p1.L + p2.L - 1];
  1055.         for (int i = 0; i < a.Length; i++) a[i] = Zero;
  1056.         for (int i = 0; i < p1.L; i++)
  1057.           for (int j = 0; j < p2.L; j++) {
  1058.             a[i + j] += p1[i] * p2[j];
  1059.           }
  1060.         return new Polyn(a);
  1061.       }
  1062.  
  1063.     }
  1064.   }
  1065.   public static class Program {
  1066.     public const int N = 32;
  1067.     static Random rnd = new Random();
  1068.     class Pt {
  1069.       Flt x, y;
  1070.       public Flt X { get { return x; } private set { x = value.Copy; } }
  1071.       public Flt Y { get { return y; } private set { y = value.Copy; } }
  1072.       public Pt(Flt x, Flt y) { this.x = x.Copy; this.y = y.Copy; }
  1073.       public Pt Copy => new Pt(x, y);
  1074.       public static Pt operator -(Pt p) => new Pt(-p.x, -p.y);
  1075.       public static Pt operator +(Pt p1, Pt p2) => new Pt(p1.x + p2.x, p1.y + p2.y);
  1076.       public static Pt operator -(Pt p1, Pt p2) => p1 + -p2;
  1077.       public static Pt operator *(Pt p, Flt μ) => new Pt(p.x * μ, p.y * μ);
  1078.       public static Pt operator *(Flt μ, Pt p) => p * μ;
  1079.       public static Pt operator /(Pt p, Flt μ) => new Pt(p.x / μ, p.y / μ);
  1080.       public static Flt operator *(Pt p1, Pt p2) => p1.x * p2.x + p1.y * p2.y;
  1081.       public Flt R2 => this * this;
  1082.       public Flt R => R2.Root(2);
  1083.       public Pt E => this / R;
  1084.     }
  1085.  
  1086.     static Flt.Polyn Intpol(Pt[] p) {
  1087.       Flt.Polyn res = new Polyn(new Flt[0]);
  1088.       for (int i = 0; i < p.Length; i++) {
  1089.         Flt.Polyn q = new Flt.Polyn(new Flt[] { 1 });
  1090.         Flt μ = 1;
  1091.         for (int j = 0; j < p.Length; j++)
  1092.           if (i != j) {
  1093.             q *= new Flt.Polyn(-p[j].X, 1);
  1094.             μ *= p[i].X - p[j].X;
  1095.           }
  1096.         //q *= p[i].Y / μ;
  1097.         res += q * (p[i].Y / μ);
  1098.       }
  1099.       return res;
  1100.     }
  1101.  
  1102.  
  1103.     static Flt.Polyn Pln(Flt[] prm) {
  1104.       Flt[] q = new Flt[prm.Length + 1];
  1105.       q[0] = 0; q[1] = 0;
  1106.       for (int i = 2; i < q.Length; i++) q[i] = prm[i - 1];
  1107.       return new Flt.Polyn(q);
  1108.     }
  1109.  
  1110.     static Flt I(Pt[] pt, int n) {
  1111.       Flt res = 0;
  1112.       int m = (pt.Length - 1) / n;
  1113.       for (int i = 0; i < m; i++) {
  1114.         Pt[] pt0 = new Pt[n + 1];
  1115.         // Flt.Polyn p = Parab(pt0).Intg;
  1116.         for (int j = 0; j <= n; j++) pt0[j] = pt[i * n + j];
  1117.         Flt.Polyn p = Intpol(pt0).Intg;
  1118.         res += p.Value(pt0[^1].X) - p.Value(pt0[0].X);
  1119.       }
  1120.       return res;
  1121.     }
  1122.  
  1123.  
  1124.     static Flt.Polyn Pln1(Pt[] pt) {
  1125.       var f = new funcN[pt.Length - 1];
  1126.       for (int i = 0; i < f.Length; i++) {
  1127.         int j = i;
  1128.         f[j] = t => {
  1129.           var a = new Flt[pt.Length + 1];
  1130.           a[0] = pt[0].Y.Copy;
  1131.           a[1] = 0;
  1132.           for (int k = 2; k < a.Length; k++)
  1133.             a[k] = t[k - 2].Copy;
  1134.           var pln = new Flt.Polyn(a);
  1135.           return pln.Value(pt[j + 1].X) - pt[j + 1].Y;
  1136.         };
  1137.       }
  1138.       var a0 = new Flt[f.Length];
  1139.       for (int i = 0; i < a0.Length; i++) a0[i] = 0;
  1140.       var a1 = Equations.Solve(f, a0);
  1141.       //Console.WriiteLine("OK");
  1142.       //Console.ReadLine();
  1143.       var aa = new Flt[a1.Length + 2];
  1144.       aa[0] = pt[0].Y.Copy; aa[1] = 0;
  1145.       for (int i = 2; i < aa.Length; i++) aa[i] = a1[i - 2].Copy;
  1146.       return new Flt.Polyn(aa);
  1147.     }
  1148.  
  1149.  
  1150.     public static void Main() {
  1151.       int prec = 50;
  1152.       void Show(Flt x, string name = "") =>
  1153.        x.Show10Round(prec, name);
  1154.  
  1155.  
  1156.       {
  1157.         Flt g(Flt[] a) {
  1158.           Flt res = 1;
  1159.           for (int i = a.Length - 1; i >= 0; i--)
  1160.             res = M.Pow(a[i], res);
  1161.           return res;
  1162.         }
  1163.  
  1164.         int nt = 9;
  1165.         Flt r0 = 2;
  1166.         var ft0 = new funcN[nt];
  1167.         var ft = new funcN[nt];
  1168.         for (int i = 0; i < ft.Length; i++) {
  1169.           int j = i;
  1170.           ft0[j] = t => {
  1171.             var x = new Flt[ft.Length];
  1172.             for (int k = 0; k < x.Length; k++) {
  1173.               x[k] = t[(j + k) % x.Length];
  1174.             }
  1175.             return g(x);
  1176.           };
  1177.           ft[j] = t => ft0[j](t) - (r0 + j);
  1178.         }
  1179.         /*
  1180.         Flt R(Flt[] t) {
  1181.           Flt res = 0;
  1182.           for (int i = 0; i < ft.Length; i++)
  1183.             res += ft[i](t).Pow(2);
  1184.           return res;
  1185.         }*/
  1186.         Flt R(Flt[] t) {
  1187.           Flt res = 0;
  1188.           for (int i = 0; i < ft.Length; i++) {
  1189.             var s = ft0[i](t) / (r0 + i);
  1190.             res += (s - 1).Pow(2) + (1 / s - 1).Pow(2);
  1191.           }
  1192.           return res;
  1193.         }
  1194.         /*
  1195.         var q0 = new Flt[] { 1.2, 1.2, 1.2,
  1196.         1.2, 1.2, 1.2,
  1197.         1.2, 1.2, 1.2 };
  1198.         */
  1199.         var q0 = new Flt[nt];
  1200.         /*
  1201.         for (int i = 0; i < nt; i++)
  1202.           q0[i] = 1.3;
  1203.         */
  1204.         string path = "q2.txt";
  1205.         if (File.Exists(path)) {
  1206.           var str = File.ReadAllLines(path);
  1207.           for (int i = 0; i < q0.Length; i++)
  1208.             q0[i] = (Flt)str[i];
  1209.  
  1210.         } else {
  1211.           q0[0] =
  1212.            (Flt)"1.306026805891242807652545950399";
  1213.           q0[1] =
  1214.            (Flt)"1.422496123930371511508945347115";
  1215.           q0[2] =
  1216.            (Flt)"1.481968384211597922114406899939";
  1217.           q0[3] =
  1218.            (Flt)"1.517880134636269721047143119619";
  1219.           q0[4] =
  1220.            (Flt)"1.537379894011106094871255325070";
  1221.           q0[5] =
  1222.            (Flt)"1.428813343342501327902427160487";
  1223.           q0[6] =
  1224.            (Flt)"1.298260843634120034650137509060";
  1225.           q0[7] =
  1226.            (Flt)"1.251605981176236997085156615874";
  1227.           q0[8] =
  1228.            (Flt)"3.351161933057958748208486588071";
  1229.         }
  1230.         var t = Equations.Solve(ft, q0);
  1231.         //Flt.ShowArray(t, 30, "solution");
  1232.         var res1 = new Flt[ft.Length];
  1233.         for (int i = 0; i < ft.Length; i++)
  1234.           res1[i] = ft[i](t) + r0 + i;
  1235.         Flt.ShowArray(t, 60, "q0", ConsoleColor.Yellow);
  1236.         Flt.ShowArray(res1, 60, "f", ConsoleColor.Cyan);
  1237.         return;
  1238.         //Console.WriteLine("Start");
  1239.         Flt δ0 = R(q0);
  1240.         //Console.WriteLine("δ0 OK");
  1241.         Flt hmax = 0.0005;
  1242.         Flt h = 0.00025;
  1243.         int cnt = 0;
  1244.         while (δ0 > ((Flt)10).Pow(-50)) {
  1245.           //Show(h);
  1246.           var grd = Equations.Grad(R, q0);
  1247.           var qt = new Flt[grd.Length];
  1248.           for (int i = 0; i < grd.Length; i++)
  1249.             qt[i] = q0[i] - grd[i] * h;
  1250.           var δt = R(qt);
  1251.           if (δt < δ0) {
  1252.             q0 = Flt.CopyArr(qt);
  1253.             δ0 = δt.Copy;
  1254.             cnt++;
  1255.  
  1256.             Console.Clear();
  1257.  
  1258.             Show(h);
  1259.             var res = new Flt[ft.Length];
  1260.             for (int i = 0; i < ft.Length; i++)
  1261.               res[i] = ft[i](q0) + r0 + i;
  1262.  
  1263.             Flt.ShowArray(q0, prec, "q0", ConsoleColor.Yellow);
  1264.             Flt.ShowArray(res, prec, "f", ConsoleColor.Cyan);
  1265.             Show(δ0);
  1266.             if (cnt == 5) {
  1267.               var str = new string[q0.Length];
  1268.               for (int i = 0; i < q0.Length; i++)
  1269.                 str[i] = q0[i].Str10;
  1270.               File.WriteAllLines(path, str);
  1271.               Console.WriteLine("Saved");
  1272.               if (h < hmax) h *= 1.25;
  1273.               cnt = 0;
  1274.             }
  1275.           } else {
  1276.             h *= 0.7;
  1277.             Console.ForegroundColor = ConsoleColor.Red;
  1278.             Show(h);
  1279.             Console.ForegroundColor = ConsoleColor.White;
  1280.  
  1281.             cnt = 0;
  1282.           }
  1283.         }
  1284.       }
  1285.       /*
  1286.             //Polyn p = new Polyn(1, 2, 3);
  1287.             func F0 = x => x * (x - 1) + 1;
  1288.             //func F0 = M.Exp;
  1289.             Flt FPln(Flt x, Flt[] q, Flt a) {
  1290.               Flt xx = (x - a).Pow(2);
  1291.               Flt res = 0;
  1292.               for (int i = q.Length - 1; i >= 0; i--) {
  1293.                 res *= xx;
  1294.                 res += q[i];
  1295.               }
  1296.               return res;
  1297.             }
  1298.  
  1299.             Flt x1 = -0.1, x2 = 1.1;
  1300.             Flt xm = (x1 + x2) / 2;
  1301.             int n = 13;
  1302.             Flt Δ(int i, Flt[] q, Flt x1, Flt x2) {
  1303.               //Flt x = x1 + (x2 - x1) * i / (q.Length - 1);
  1304.               Flt x = xm + (x2 - xm) * i / (q.Length - 1);
  1305.               //var pln = new Polyn(q);
  1306.               //return pln.Value(pln.Value(x)) - F0(x);
  1307.               return FPln(FPln(x, q, 0.5), q, 0.5) - F0(x);
  1308.             }
  1309.             var f = new funcN[n];
  1310.             for (int i = 0; i < n; i++) {
  1311.               int j = i;
  1312.               f[j] = q => Δ(j, q, x1, x2);
  1313.             }
  1314.  
  1315.             funcN R = q => {
  1316.               Flt r = 0;
  1317.               for (int i = 0; i < q.Length; i++) {
  1318.                 Flt δ = Δ(i, q, x1, x2);
  1319.                 r += δ * δ;
  1320.               }
  1321.               return r;
  1322.             };
  1323.             var q0 = new Flt[n];
  1324.             for (int i = 0; i < n; i++) q0[i] = 0;
  1325.             q0[0] = 0.69; q0[1] = 1.78; q0[2] = -5.9;
  1326.             var q = Equations.Solve(f, q0);
  1327.             for (int i = 0; i < q.Length; i++) {
  1328.               Show(q[i], i.ToString());
  1329.             }
  1330.             Console.WriteLine();
  1331.             //var pln = new Polyn(q);
  1332.             Flt dx = (x2 - xm) / (2 * (n - 1));
  1333.             for (int i = 0; i <= 2 * (n - 1); i++) {
  1334.               var x = xm + dx * i;
  1335.               Show(x);
  1336.               //Show(pln.Value(x));
  1337.               Show(FPln(x, q, 0.5));
  1338.               Show(FPln(FPln(x, q, 0.5), q, 0.5));
  1339.               //var t = pln.Value(pln.Value(x)) / F0(x);
  1340.               var t = FPln(FPln(x, q, 0.5), q, 0.5) / F0(x);
  1341.               Show(t);
  1342.               Console.WriteLine();
  1343.             }
  1344.             return;
  1345.             */
  1346.  
  1347.  
  1348.     }
  1349.  
  1350.   }
  1351. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement