Advertisement
VNM24ix

Matematika

Jun 16th, 2025
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.15 KB | Source Code | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using CSharpShellCore;
  5.  
  6. namespace Matematika {
  7.   delegate double func(double a);
  8.   delegate double func2(double a, double b);
  9.   delegate double func3(double a, double b, double c);
  10.   delegate double funcN(params double[] x);
  11.   delegate double[] funcNN(params double[] x);
  12.   delegate long ifn1(int t);
  13.   delegate long ifn(params int[] t);
  14.   public static class Program {
  15.     static double π = Math.PI;
  16.     static double e = Math.E;
  17.     static func sqr = t => t * t;
  18.     static func cube = t => t * t * t;
  19.     static func exp = t => Math.Exp(t);
  20.     static func ln = t => Math.Log(t);
  21.     static func sqrt = t => Math.Sqrt(t);
  22.     static func sin = t => Math.Sin(t);
  23.     static func arcsin = t => Math.Asin(t);
  24.     static func cos = t => Math.Cos(t);
  25.     static func arccos = t => Math.Acos(t);
  26.     static func tg = t => Math.Tan(t);
  27.     static func cosh = t => { double u = exp(t); return (u + 1 / u) / 2; };
  28.     static func sinh = t => { double u = exp(t); return (u - 1 / u) / 2; };
  29.     static func tgh => t => { double u = exp(2 * t); return (u - 1) / (u + 1); };
  30.     static func cbrt = t => Math.Cbrt(t);
  31.     static func tanh = Math.Tanh;
  32.     static func arctg = t => Math.Atan(t);
  33.     static func ctg = t => 1 / Math.Tan(t);
  34.     static func abs = t => t >= 0 ? t : -t;
  35.     static func sgn = t => Math.Sign(t);
  36.     static double pow(double x, int n) {
  37.       if (n > 0) {
  38.         double y = sqr(pow(x, n / 2));
  39.         if ((n & 1) == 1) y *= x;
  40.         return y;
  41.       } else if (n == 0) return 1;
  42.       else return 1 / pow(x, -n);
  43.     }
  44.     static double pwf(double x, double y) =>
  45.     Math.Pow(x, y);
  46.     static func D1(func F, double h) =>
  47.       x => (F(x + h / 2) - F(x - h / 2)) / h;
  48.     static func D1(func F) {
  49.       double h = 1e-3, hh = h * Math.Sqrt(2);
  50.       return x => D1(F, h)(x) * 2 - D1(F, hh)(x);
  51.     }
  52.     static func D2(func F) => D1(D1(F));
  53.     static double Newton(func f, double x0) {
  54.       double ε = 1e-10;
  55.       bool flag = false;
  56.       while (true) {
  57.         double x = x0 - f(x0) / D1(f)(x0);
  58.         //Console.WriteLine("In Root : " + x);
  59.         if (flag) return x;
  60.         else {
  61.           flag = double.Abs(1 - x / x0) < ε;
  62.           x0 = x;
  63.         }
  64.       }
  65.     }
  66.     static double Διχοτομια(func f, double a, double b) {
  67.       double ε = 1e-9;
  68.       double fa = f(a), fb = f(b);
  69.       while (true) {
  70.         double c = (a + b) / 2;
  71.         if (double.Abs(b - a) < ε) break;
  72.         double fc = f(c);
  73.         if (double.Sign(fc) == double.Sign(fa)) { a = c; fa = fc; } else if (double.Sign(fc) == double.Sign(fb)) { b = c; fb = fc; } else return c;
  74.       }
  75.       return Newton(f, (a + b) / 2);
  76.       //return (a + b) / 2;
  77.     }
  78.     static double Intg(func F, double a, double b, int n = 1) {
  79.       if (n == 1) {
  80.         double h = b - a, hh = h / 6;
  81.         double x0 = a;
  82.         double x6 = b;
  83.         double x3 = (a + b) / 2;
  84.         double x1 = a + hh;
  85.         double x2 = x3 - hh;
  86.         double x4 = x3 + hh;
  87.         double x5 = x6 - hh;
  88.         double y3 = F(x3);
  89.         double y24 = (F(x2) + F(x4)) / 2;
  90.         double y15 = (F(x1) + F(x5)) / 2;
  91.         double y06 = (F(x0) + F(x6)) / 2;
  92.         return
  93.         (y3 * 136 + y24 * 27 + y15 * 216 + y06 * 41) * h / 420;
  94.       } else {
  95.         double res = 0;
  96.         double h = (b - a) / n;
  97.         for (int i = 0; i < n; i++) {
  98.           double aa = a + h * i;
  99.           double bb = aa + h;
  100.           res += Intg(F, aa, bb);
  101.         }
  102.         return res;
  103.       }
  104.     }
  105.     static ulong GCD(ulong p, ulong q) {
  106.       if (p == 0) return q == 0 ? ulong.MaxValue : q;
  107.       else if (q == 0) return p;
  108.       else if (p < q) return GCD(q, p);
  109.       else return GCD(q, p % q);
  110.     }
  111.     static void Mods(int m, int n) {
  112.       Console.WriteLine(m + "  " + n + "\n");
  113.       List<int> list = new List<int>();
  114.       do {
  115.         list.Add(m / n);
  116.         Console.Write((m / n) + "  ");
  117.         int nn = m % n;
  118.         m = n;
  119.         n = nn;
  120.       } while (n > 0);
  121.       Console.WriteLine();
  122.       if (list.Count % 2 == 0)
  123.         list.RemoveAt(list.Count - 1);
  124.       else list[list.Count - 1]--;
  125.       int p = 1, q = 0;
  126.       for (int i = list.Count - 1; i >= 0; i--) {
  127.         int t = list[i] * p + q;
  128.         q = p;
  129.         p = t;
  130.       }
  131.       for (int i = 0; i < list.Count; i++)
  132.         Console.Write(list[i] + "  ");
  133.  
  134.     }
  135.     static double LambertW(double x) =>
  136.       Newton(t => t * exp(t) - x, 0);
  137.     static double[] LW(double x) {
  138.       if (x < -1 / e) return new double[0];
  139.       else if (x > 0)
  140.         return new double[] { LambertW(x) };
  141.       double[] res = new double[2];
  142.       double z0 = Newton(t => ln(t) + x * t, 1);
  143.       double ζ = 1 + 3 / (z0 - 1);
  144.       double z1 = Newton(t => ln(t) + x * t, ζ);
  145.       return new double[] { -ln(z0), -ln(z1) };
  146.     }
  147.     static Random rnd = new Random();
  148.     static func F(func2 f) => x => Intg(t => f(x, t), 0, 1, 100);
  149.     static double P(double p, int n) => P0(p, n) + P1(p, n);
  150.     static double Q(double p, int n) => 1 - P(p, n);
  151.     static double P0(double p, int n) => n == 2 ? p : P(p, n - 1) * p;
  152.     static double P1(double p, int n) => n == 2 ? p * (1 - p) : P0(p, n - 1) * (1 - p);
  153.     static void Main() {
  154.       {
  155.         double f(double x) => x * sin(x) / (sqr(x) + 1);
  156.         Console.WriteLine(Intg(f, -10000, 10000, 1000000));
  157.         Console.WriteLine(π / exp(1));
  158.         return;
  159.       }
  160.  
  161.       {
  162.         int N = 3;
  163.         double A(int n) => n == 1 ? 1 : (n + 1) * (A(n - 1) + 1) / (2 * n);
  164.         Console.WriteLine($"A({N}) = {A(N)}");
  165.         double S(int n) {
  166.           double s = 0;
  167.           for (int k = 1; k <= n; k++) {
  168.             s += (2 * (double)(n + 1) / k + n - k - 1) * Math.Pow(2, k);
  169.           }
  170.           return n / Math.Pow(2, n + 1) + s / Math.Pow(2, n + 2);
  171.         }
  172.         Console.WriteLine($"S({N}) = {S(N)}");
  173.  
  174.         double S1(int n) {
  175.           double s = 0;
  176.           for (int k = 1; k <= n; k++) {
  177.             s += Math.Pow(2, k) / k;
  178.           }
  179.           return s * 2 * (n + 1) / Math.Pow(2, n + 2);
  180.         }
  181.         Console.WriteLine($"S1 = {S1(N)}");
  182.         Console.WriteLine("Intg");
  183.         double intg1 = Intg(t => (pwf(t, N) - 1) / (t - 1), 0, 1.5, 100);
  184.         double intg2 = Intg(t => (pwf(t, N) - 1) / (t - 1), 1.5, 2, 100);
  185.         Console.WriteLine((intg1 + intg2) * (N + 1) / Math.Pow(2, N + 1));
  186.  
  187.         double S2(int n) {
  188.           double s = 0;
  189.           for (int k = 1; k <= n; k++) {
  190.             s += Math.Pow(2, k);
  191.           }
  192.           return s * (n - 1) / Math.Pow(2, n + 2);
  193.         }
  194.         Console.WriteLine($"S2 = {S2(N)}");
  195.  
  196.         double S3(int n) {
  197.           double s = 0;
  198.           for (int k = 1; k <= n; k++) {
  199.             s += Math.Pow(2, k) * k;
  200.           }
  201.           return -s / Math.Pow(2, n + 2);
  202.         }
  203.         Console.WriteLine($"S3 = {S3(N)}");
  204.         Console.WriteLine($"S2 + S3 = {S2(N) + S3(N)}");
  205.         Console.WriteLine((S2(N) + S3(N)) * Math.Pow(2, N + 2));
  206.         Console.WriteLine(-2 * N);
  207.         Console.WriteLine(S1(N) + (-2 * N) / Math.Pow(2, N + 2));
  208.         return;
  209.       }
  210.       {
  211.         double[] a = new double[100];
  212.         a[0] = 0; a[1] = 1;
  213.         for (int i = 1; i + 1 < a.Length; i++) {
  214.           a[i + 1] = (i + 2) * (a[i] + 1) / (2 * (i + 1));
  215.           //Console.WriteLine(a[i + 1]);
  216.  
  217.         }
  218.         double f(double x) {
  219.           double f = 0;
  220.           for (int i = a.Length - 1; i >= 0; i--)
  221.             f = f * x + a[i];
  222.           return f;
  223.         }
  224.         //Console.WriteLine(f(0.5) + "\n" D1(f)(0.5));
  225.         double b = 0.5;
  226.         //Console.WriteLine(D1(f)(b));
  227.         //Console.WriteLine(2 * f(b) / (2 - b) + 1 / sqr(1 - b));
  228.         double F(double x) =>
  229.         (x - 2 * ln(1 - x) + 1 / (1 - x) - 1) / sqr(2 - x);
  230.         /*
  231.         Console.WriteLine(D1(F)(b));
  232.         Console.WriteLine(2 * F(b) / (2 - b) + 1 / sqr(1 - b));
  233.         Console.WriteLine(F(b));
  234.         Console.WriteLine(f(b));
  235.         */
  236.         for (int i = 1; i < 100; i++) {
  237.           double x = (double)i / 100;
  238.           Console.WriteLine(x);
  239.           Console.WriteLine(f(x));
  240.           Console.WriteLine(F(x));
  241.           Console.WriteLine();
  242.         }
  243.         return;
  244.       }
  245.       {
  246.         while (true) {
  247.           double a = rnd.NextDouble();
  248.           double b = rnd.NextDouble();
  249.           double c = rnd.NextDouble();
  250.           double d = 1 / sqrt(b * c + c * a + a * b);
  251.           a *= d; b *= d; c *= d;
  252.  
  253.           double f = (sqr(a) + sqr(b) + 2) * sqr(a + b) + sqr(1 - a * b);
  254.           double g = 2 * (sqr(a) + sqr(b) + a * b + 1) * (a + b);
  255.           Console.Clear();
  256.           Console.WriteLine(a * b + b * c + c * a);
  257.           Console.WriteLine(f + "\n" + g);
  258.           if (f < g) {
  259.             Console.WriteLine(a + "\n" + b + "\n" + c);
  260.             break;
  261.           }
  262.         }
  263.         return;
  264.       }
  265.       {
  266.         double p = 1;
  267.         double A(int n) {
  268.           if (n < 2) return n;
  269.           double a = 1;
  270.           for (int k = 2; k <= n; k++)
  271.             a *= (5 * k + 1);
  272.           return (n % 2 == 1 ? 1 : -1) / a;
  273.         }
  274.         double Y(double x) {
  275.           double y = 0;
  276.           for (int i = 30; i >= 0; i--) {
  277.             y *= x;
  278.             y += A(i);
  279.           }
  280.           return y * p;
  281.         }
  282.  
  283.         //for (int i = 0; i < α.Length; i++)
  284.         double x = -7;
  285.         Console.WriteLine(Y(x));
  286.         Console.WriteLine(5 * sqr(x) * D2(Y)(x) + x * (x + 1) * D1(Y)(x));
  287.  
  288.         return;
  289.       }
  290.       {
  291.         double a = 0.71;
  292.         double f(double x) => sqr(x) * ln(sqrt(sqr(a) - sqr(x)) + a);
  293.         Console.WriteLine(Intg(f, 0, a, 1000));
  294.         double g(double x) {
  295.           double y = sqr(a) - sqr(x);
  296.           return pow(x, 4) / (y + a * sqrt(y));
  297.         }
  298.         //Console.WriteLine((cube(a) * ln(a) + Intg(g, 0, a - 0.0000001, 100000)) / 3);
  299.         //double h(double t) =>
  300.         //pow(sin(t), 4) / (1 + cos(t));
  301.  
  302.         double h(double t) =>
  303.         (1 - cos(t)) * sqr(sin(t));
  304.         //Console.WriteLine(cube(a) / 3 * (ln(a) + Intg(h, 0, π / 2, 100)));
  305.         Console.WriteLine(cube(a) / 3 * (ln(a) + π / 4 - 1d / 3));
  306.         return;
  307.       }
  308.  
  309.       {
  310.         double U1(double t, double r) => (1 + sqr(t)) / sqr(sqr(t) - sqr(r));
  311.         double U2(double t, double r) {
  312.           double a = sqr(r) - 1, b = 2 * r;
  313.           return ((a * t + b) / sqr(t - r) + (-a * t + b) / sqr(t + r)) / (4 * cube(r));
  314.         }
  315.         double U3(double t, double r) {
  316.           double a = sqr(r) - 1, b = r * (sqr(r) + 1);
  317.           double c = t - r, d = t + r;
  318.           return (a / c + b / sqr(c) - a / d + b / sqr(d)) / (4 * cube(r));
  319.         }
  320.         Console.WriteLine(U1(5, 3) + "\n" + U2(5, 3) + "\n" + U3(5, 3));
  321.         double a = 0.7;
  322.         func f = x => 1 / sqr(1 + a * cos(x));
  323.         double s1 = Intg(f, 0, 2 * π, 100);
  324.         Console.WriteLine(s1);
  325.         //func g = u => 2 / ((1 + u * u) * sqr(1 + a * (1 - u * u) / (1 + u * u)));
  326.  
  327.         func g = u => (1 + sqr(u)) / sqr(1 + a + (1 - a) * sqr(u));
  328.         double s2 = 4 * Intg(g, 0, 1000000, 100000000);
  329.  
  330.  
  331.         /*
  332.         double k = sqr(2 / (a - 1));
  333.         double b = sqrt((a + 1) / (a - 1));
  334.         func g = t => {
  335.  
  336.           return (1 + sqr(t)) / (sqr(t - b) * sqr(t + b));
  337.         };
  338.         double s2 = k * Intg(g, 0, 100000, 10000000);
  339.         */
  340.         Console.WriteLine(s2);
  341.         return;
  342.       }
  343.       {
  344.         func f = x => x == 0 ? 0 : sqr(ln(1 - x)) / x;
  345.         double a = 0, b = 1;
  346.         double ss = 0;
  347.         while (true) {
  348.           double x1 = a;
  349.           double x2 = (x1 + b) / 2;
  350.           double tmp = ss;
  351.           tmp += Intg(f, x1, x2, 1000);
  352.           if (ss == tmp) break;
  353.           else ss = tmp;
  354.           a = x2;
  355.           Console.WriteLine(ss);
  356.         }
  357.  
  358.         return;
  359.       }
  360.       {
  361.         double t = -exp(-1) + 1e-5;
  362.         Console.WriteLine(t);
  363.         Console.WriteLine("{0,18:f14}{1,18:f14}", LW(t)[0], LW(t)[0] * exp(LW(t)[0]));
  364.         Console.WriteLine("{0,18:f14}{1,18:f14}", LW(t)[1], LW(t)[1] * exp(LW(t)[1]));
  365.         return;
  366.         func f = x => 1 / (x * sqr(x - 2));
  367.         func g = D1(x => (ln(abs(x / (x - 2))) - 2 / (x - 2)) / 4);
  368.  
  369.         for (double x = -10; x <= 10; x += Math.Sqrt(2) / 7) {
  370.           //Console.WriteLine("{0,12:f9}", x);
  371.           //Console.WriteLine("{0,18:f10}{1,18:f10}", f(x), g(x));
  372.           Console.WriteLine("{0,15:f12}", f(x) / g(x));
  373.         }
  374.         return;
  375.       }
  376.       /*
  377.             func Z(double c) => x => Newton(z => Math.Abs(z / (2 - z)) * exp(z / (2 - z)) - abs(c * x), 1.9);
  378.             //Console.WriteLine(Z(a));
  379.             //return;
  380.             //func Z = x => 2 * LW(x / c)[0] / (LW(x / c)[0] + 1);
  381.             func U(double c) => x => x / Z(c)(x);
  382.             func V(double c) => x => D1(U(c))(x) + x / (4 * U(c)(x));
  383.             func Y(double c) => x => sqr(U(c)(x));
  384.             func T(double c) => x => (2 * D1(Y(c))(x) + x) / (4 * sqrt(Y(c)(x)));
  385.             double c = 2;
  386.             for (double a = 0.1; Math.Round(a, 2) <= 10; a += 0.1) {
  387.               Console.Write("{0,6:f2} {1,16:f10}", a, Z(c)(a));
  388.               Console.WriteLine("{0,16:f10}", T(c)(a));
  389.             }
  390.             return;
  391.             */
  392.       /*
  393.       double T(double x) => LW(x / c)[0];
  394.       double Z(double x) {
  395.         double t = T(x);
  396.         return 2 * t / (t - 1);
  397.       }
  398.       double U(double x) => x / Z(x);
  399.       double R(double x) => D1(U)(x) + x / (4 * U(x));
  400.       double t = T(2);
  401.       Console.WriteLine(t);
  402.       Console.WriteLine(c * t * exp(t));
  403.       Console.WriteLine(R(5));
  404.       */
  405.       /*
  406.             double U(double x) {
  407.               double w = LW(x / c)[0];
  408.               double z = 2 * w / (1 + w);
  409.               return x / z;
  410.  
  411.             }
  412.             */
  413.       /*
  414.       double U(double x) =>
  415.       x * (1 + 1 / LW(x / c)[0]) / 2;
  416.       //Console.WriteLine(U1(x));
  417.       double Y(double x) => sqr(U(x));
  418.       double R(double x) => (2 * D1(Y)(x) + x) / (4 * sqrt(Y(x)));
  419.       //Console.WriteLine(D1(U)(a) + a / (4 * U(a)));
  420.       Console.WriteLine(R(a));
  421. */
  422.     }
  423.   }
  424. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement