Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections.Generic;
- using CSharpShellCore;
- namespace Matematika {
- delegate double func(double a);
- delegate double func2(double a, double b);
- delegate double func3(double a, double b, double c);
- delegate double funcN(params double[] x);
- delegate double[] funcNN(params double[] x);
- delegate long ifn1(int t);
- delegate long ifn(params int[] t);
- public static class Program {
- static double π = Math.PI;
- static double e = Math.E;
- static func sqr = t => t * t;
- static func cube = t => t * t * t;
- static func exp = t => Math.Exp(t);
- static func ln = t => Math.Log(t);
- static func sqrt = t => Math.Sqrt(t);
- static func sin = t => Math.Sin(t);
- static func arcsin = t => Math.Asin(t);
- static func cos = t => Math.Cos(t);
- static func arccos = t => Math.Acos(t);
- static func tg = t => Math.Tan(t);
- static func cosh = t => { double u = exp(t); return (u + 1 / u) / 2; };
- static func sinh = t => { double u = exp(t); return (u - 1 / u) / 2; };
- static func tgh => t => { double u = exp(2 * t); return (u - 1) / (u + 1); };
- static func cbrt = t => Math.Cbrt(t);
- static func tanh = Math.Tanh;
- static func arctg = t => Math.Atan(t);
- static func ctg = t => 1 / Math.Tan(t);
- static func abs = t => t >= 0 ? t : -t;
- static func sgn = t => Math.Sign(t);
- static double pow(double x, int n) {
- if (n > 0) {
- double y = sqr(pow(x, n / 2));
- if ((n & 1) == 1) y *= x;
- return y;
- } else if (n == 0) return 1;
- else return 1 / pow(x, -n);
- }
- static double pwf(double x, double y) =>
- Math.Pow(x, y);
- static func D1(func F, double h) =>
- x => (F(x + h / 2) - F(x - h / 2)) / h;
- static func D1(func F) {
- double h = 1e-3, hh = h * Math.Sqrt(2);
- return x => D1(F, h)(x) * 2 - D1(F, hh)(x);
- }
- static func D2(func F) => D1(D1(F));
- static double Newton(func f, double x0) {
- double ε = 1e-10;
- bool flag = false;
- while (true) {
- double x = x0 - f(x0) / D1(f)(x0);
- //Console.WriteLine("In Root : " + x);
- if (flag) return x;
- else {
- flag = double.Abs(1 - x / x0) < ε;
- x0 = x;
- }
- }
- }
- static double Διχοτομια(func f, double a, double b) {
- double ε = 1e-9;
- double fa = f(a), fb = f(b);
- while (true) {
- double c = (a + b) / 2;
- if (double.Abs(b - a) < ε) break;
- double fc = f(c);
- 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;
- }
- return Newton(f, (a + b) / 2);
- //return (a + b) / 2;
- }
- static double Intg(func F, double a, double b, int n = 1) {
- if (n == 1) {
- double h = b - a, hh = h / 6;
- double x0 = a;
- double x6 = b;
- double x3 = (a + b) / 2;
- double x1 = a + hh;
- double x2 = x3 - hh;
- double x4 = x3 + hh;
- double x5 = x6 - hh;
- double y3 = F(x3);
- double y24 = (F(x2) + F(x4)) / 2;
- double y15 = (F(x1) + F(x5)) / 2;
- double y06 = (F(x0) + F(x6)) / 2;
- return
- (y3 * 136 + y24 * 27 + y15 * 216 + y06 * 41) * h / 420;
- } else {
- double res = 0;
- double h = (b - a) / n;
- for (int i = 0; i < n; i++) {
- double aa = a + h * i;
- double bb = aa + h;
- res += Intg(F, aa, bb);
- }
- return res;
- }
- }
- static ulong GCD(ulong p, ulong q) {
- if (p == 0) return q == 0 ? ulong.MaxValue : q;
- else if (q == 0) return p;
- else if (p < q) return GCD(q, p);
- else return GCD(q, p % q);
- }
- static void Mods(int m, int n) {
- Console.WriteLine(m + " " + n + "\n");
- List<int> list = new List<int>();
- do {
- list.Add(m / n);
- Console.Write((m / n) + " ");
- int nn = m % n;
- m = n;
- n = nn;
- } while (n > 0);
- Console.WriteLine();
- if (list.Count % 2 == 0)
- list.RemoveAt(list.Count - 1);
- else list[list.Count - 1]--;
- int p = 1, q = 0;
- for (int i = list.Count - 1; i >= 0; i--) {
- int t = list[i] * p + q;
- q = p;
- p = t;
- }
- for (int i = 0; i < list.Count; i++)
- Console.Write(list[i] + " ");
- }
- static double LambertW(double x) =>
- Newton(t => t * exp(t) - x, 0);
- static double[] LW(double x) {
- if (x < -1 / e) return new double[0];
- else if (x > 0)
- return new double[] { LambertW(x) };
- double[] res = new double[2];
- double z0 = Newton(t => ln(t) + x * t, 1);
- double ζ = 1 + 3 / (z0 - 1);
- double z1 = Newton(t => ln(t) + x * t, ζ);
- return new double[] { -ln(z0), -ln(z1) };
- }
- static Random rnd = new Random();
- static func F(func2 f) => x => Intg(t => f(x, t), 0, 1, 100);
- static double P(double p, int n) => P0(p, n) + P1(p, n);
- static double Q(double p, int n) => 1 - P(p, n);
- static double P0(double p, int n) => n == 2 ? p : P(p, n - 1) * p;
- static double P1(double p, int n) => n == 2 ? p * (1 - p) : P0(p, n - 1) * (1 - p);
- static void Main() {
- {
- double f(double x) => x * sin(x) / (sqr(x) + 1);
- Console.WriteLine(Intg(f, -10000, 10000, 1000000));
- Console.WriteLine(π / exp(1));
- return;
- }
- {
- int N = 3;
- double A(int n) => n == 1 ? 1 : (n + 1) * (A(n - 1) + 1) / (2 * n);
- Console.WriteLine($"A({N}) = {A(N)}");
- double S(int n) {
- double s = 0;
- for (int k = 1; k <= n; k++) {
- s += (2 * (double)(n + 1) / k + n - k - 1) * Math.Pow(2, k);
- }
- return n / Math.Pow(2, n + 1) + s / Math.Pow(2, n + 2);
- }
- Console.WriteLine($"S({N}) = {S(N)}");
- double S1(int n) {
- double s = 0;
- for (int k = 1; k <= n; k++) {
- s += Math.Pow(2, k) / k;
- }
- return s * 2 * (n + 1) / Math.Pow(2, n + 2);
- }
- Console.WriteLine($"S1 = {S1(N)}");
- Console.WriteLine("Intg");
- double intg1 = Intg(t => (pwf(t, N) - 1) / (t - 1), 0, 1.5, 100);
- double intg2 = Intg(t => (pwf(t, N) - 1) / (t - 1), 1.5, 2, 100);
- Console.WriteLine((intg1 + intg2) * (N + 1) / Math.Pow(2, N + 1));
- double S2(int n) {
- double s = 0;
- for (int k = 1; k <= n; k++) {
- s += Math.Pow(2, k);
- }
- return s * (n - 1) / Math.Pow(2, n + 2);
- }
- Console.WriteLine($"S2 = {S2(N)}");
- double S3(int n) {
- double s = 0;
- for (int k = 1; k <= n; k++) {
- s += Math.Pow(2, k) * k;
- }
- return -s / Math.Pow(2, n + 2);
- }
- Console.WriteLine($"S3 = {S3(N)}");
- Console.WriteLine($"S2 + S3 = {S2(N) + S3(N)}");
- Console.WriteLine((S2(N) + S3(N)) * Math.Pow(2, N + 2));
- Console.WriteLine(-2 * N);
- Console.WriteLine(S1(N) + (-2 * N) / Math.Pow(2, N + 2));
- return;
- }
- {
- double[] a = new double[100];
- a[0] = 0; a[1] = 1;
- for (int i = 1; i + 1 < a.Length; i++) {
- a[i + 1] = (i + 2) * (a[i] + 1) / (2 * (i + 1));
- //Console.WriteLine(a[i + 1]);
- }
- double f(double x) {
- double f = 0;
- for (int i = a.Length - 1; i >= 0; i--)
- f = f * x + a[i];
- return f;
- }
- //Console.WriteLine(f(0.5) + "\n" D1(f)(0.5));
- double b = 0.5;
- //Console.WriteLine(D1(f)(b));
- //Console.WriteLine(2 * f(b) / (2 - b) + 1 / sqr(1 - b));
- double F(double x) =>
- (x - 2 * ln(1 - x) + 1 / (1 - x) - 1) / sqr(2 - x);
- /*
- Console.WriteLine(D1(F)(b));
- Console.WriteLine(2 * F(b) / (2 - b) + 1 / sqr(1 - b));
- Console.WriteLine(F(b));
- Console.WriteLine(f(b));
- */
- for (int i = 1; i < 100; i++) {
- double x = (double)i / 100;
- Console.WriteLine(x);
- Console.WriteLine(f(x));
- Console.WriteLine(F(x));
- Console.WriteLine();
- }
- return;
- }
- {
- while (true) {
- double a = rnd.NextDouble();
- double b = rnd.NextDouble();
- double c = rnd.NextDouble();
- double d = 1 / sqrt(b * c + c * a + a * b);
- a *= d; b *= d; c *= d;
- double f = (sqr(a) + sqr(b) + 2) * sqr(a + b) + sqr(1 - a * b);
- double g = 2 * (sqr(a) + sqr(b) + a * b + 1) * (a + b);
- Console.Clear();
- Console.WriteLine(a * b + b * c + c * a);
- Console.WriteLine(f + "\n" + g);
- if (f < g) {
- Console.WriteLine(a + "\n" + b + "\n" + c);
- break;
- }
- }
- return;
- }
- {
- double p = 1;
- double A(int n) {
- if (n < 2) return n;
- double a = 1;
- for (int k = 2; k <= n; k++)
- a *= (5 * k + 1);
- return (n % 2 == 1 ? 1 : -1) / a;
- }
- double Y(double x) {
- double y = 0;
- for (int i = 30; i >= 0; i--) {
- y *= x;
- y += A(i);
- }
- return y * p;
- }
- //for (int i = 0; i < α.Length; i++)
- double x = -7;
- Console.WriteLine(Y(x));
- Console.WriteLine(5 * sqr(x) * D2(Y)(x) + x * (x + 1) * D1(Y)(x));
- return;
- }
- {
- double a = 0.71;
- double f(double x) => sqr(x) * ln(sqrt(sqr(a) - sqr(x)) + a);
- Console.WriteLine(Intg(f, 0, a, 1000));
- double g(double x) {
- double y = sqr(a) - sqr(x);
- return pow(x, 4) / (y + a * sqrt(y));
- }
- //Console.WriteLine((cube(a) * ln(a) + Intg(g, 0, a - 0.0000001, 100000)) / 3);
- //double h(double t) =>
- //pow(sin(t), 4) / (1 + cos(t));
- double h(double t) =>
- (1 - cos(t)) * sqr(sin(t));
- //Console.WriteLine(cube(a) / 3 * (ln(a) + Intg(h, 0, π / 2, 100)));
- Console.WriteLine(cube(a) / 3 * (ln(a) + π / 4 - 1d / 3));
- return;
- }
- {
- double U1(double t, double r) => (1 + sqr(t)) / sqr(sqr(t) - sqr(r));
- double U2(double t, double r) {
- double a = sqr(r) - 1, b = 2 * r;
- return ((a * t + b) / sqr(t - r) + (-a * t + b) / sqr(t + r)) / (4 * cube(r));
- }
- double U3(double t, double r) {
- double a = sqr(r) - 1, b = r * (sqr(r) + 1);
- double c = t - r, d = t + r;
- return (a / c + b / sqr(c) - a / d + b / sqr(d)) / (4 * cube(r));
- }
- Console.WriteLine(U1(5, 3) + "\n" + U2(5, 3) + "\n" + U3(5, 3));
- double a = 0.7;
- func f = x => 1 / sqr(1 + a * cos(x));
- double s1 = Intg(f, 0, 2 * π, 100);
- Console.WriteLine(s1);
- //func g = u => 2 / ((1 + u * u) * sqr(1 + a * (1 - u * u) / (1 + u * u)));
- func g = u => (1 + sqr(u)) / sqr(1 + a + (1 - a) * sqr(u));
- double s2 = 4 * Intg(g, 0, 1000000, 100000000);
- /*
- double k = sqr(2 / (a - 1));
- double b = sqrt((a + 1) / (a - 1));
- func g = t => {
- return (1 + sqr(t)) / (sqr(t - b) * sqr(t + b));
- };
- double s2 = k * Intg(g, 0, 100000, 10000000);
- */
- Console.WriteLine(s2);
- return;
- }
- {
- func f = x => x == 0 ? 0 : sqr(ln(1 - x)) / x;
- double a = 0, b = 1;
- double ss = 0;
- while (true) {
- double x1 = a;
- double x2 = (x1 + b) / 2;
- double tmp = ss;
- tmp += Intg(f, x1, x2, 1000);
- if (ss == tmp) break;
- else ss = tmp;
- a = x2;
- Console.WriteLine(ss);
- }
- return;
- }
- {
- double t = -exp(-1) + 1e-5;
- Console.WriteLine(t);
- Console.WriteLine("{0,18:f14}{1,18:f14}", LW(t)[0], LW(t)[0] * exp(LW(t)[0]));
- Console.WriteLine("{0,18:f14}{1,18:f14}", LW(t)[1], LW(t)[1] * exp(LW(t)[1]));
- return;
- func f = x => 1 / (x * sqr(x - 2));
- func g = D1(x => (ln(abs(x / (x - 2))) - 2 / (x - 2)) / 4);
- for (double x = -10; x <= 10; x += Math.Sqrt(2) / 7) {
- //Console.WriteLine("{0,12:f9}", x);
- //Console.WriteLine("{0,18:f10}{1,18:f10}", f(x), g(x));
- Console.WriteLine("{0,15:f12}", f(x) / g(x));
- }
- return;
- }
- /*
- func Z(double c) => x => Newton(z => Math.Abs(z / (2 - z)) * exp(z / (2 - z)) - abs(c * x), 1.9);
- //Console.WriteLine(Z(a));
- //return;
- //func Z = x => 2 * LW(x / c)[0] / (LW(x / c)[0] + 1);
- func U(double c) => x => x / Z(c)(x);
- func V(double c) => x => D1(U(c))(x) + x / (4 * U(c)(x));
- func Y(double c) => x => sqr(U(c)(x));
- func T(double c) => x => (2 * D1(Y(c))(x) + x) / (4 * sqrt(Y(c)(x)));
- double c = 2;
- for (double a = 0.1; Math.Round(a, 2) <= 10; a += 0.1) {
- Console.Write("{0,6:f2} {1,16:f10}", a, Z(c)(a));
- Console.WriteLine("{0,16:f10}", T(c)(a));
- }
- return;
- */
- /*
- double T(double x) => LW(x / c)[0];
- double Z(double x) {
- double t = T(x);
- return 2 * t / (t - 1);
- }
- double U(double x) => x / Z(x);
- double R(double x) => D1(U)(x) + x / (4 * U(x));
- double t = T(2);
- Console.WriteLine(t);
- Console.WriteLine(c * t * exp(t));
- Console.WriteLine(R(5));
- */
- /*
- double U(double x) {
- double w = LW(x / c)[0];
- double z = 2 * w / (1 + w);
- return x / z;
- }
- */
- /*
- double U(double x) =>
- x * (1 + 1 / LW(x / c)[0]) / 2;
- //Console.WriteLine(U1(x));
- double Y(double x) => sqr(U(x));
- double R(double x) => (2 * D1(Y)(x) + x) / (4 * sqrt(Y(x)));
- //Console.WriteLine(D1(U)(a) + a / (4 * U(a)));
- Console.WriteLine(R(a));
- */
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement