Advertisement
Guest User

Untitled

a guest
Dec 15th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace lab10B
  5. {
  6.    
  7.     public static class FunctionGenerationAndUsage
  8.     {
  9.         public delegate double Fun1(double a, double b);
  10.         public delegate T Fun2<T>(T a, T b);
  11.         public delegate double Fun3(double x);
  12.         public delegate double Fun4(Point2D p1, Point2D p2);
  13.         public delegate int[] Fun5(int[] tab);
  14.  
  15.         public static Fun2<double> NilpotentMinimumGeneration()
  16.         {
  17.             return delegate (double a, double b)
  18.             {
  19.                 if (a + b > 1) return Math.Min(a, b);
  20.                 else return 0;
  21.             };
  22.         }
  23.         public static List<T> ScanList<T>(Fun2<T> f, params T[] tab)
  24.         {
  25.             if (tab.Length == 0) return null;
  26.             List<T> l = new List<T>();
  27.             T element = tab[0];
  28.             l.Add(element);
  29.             for (int i=1;i<tab.Length;i++)
  30.             {
  31.                 element = f(element,tab[i]);
  32.                 l.Add(element);
  33.             }
  34.             return l;
  35.         }
  36.         public static Fun3 PolynomalFunction(Fun3 f, double[] tab)
  37.         {
  38.             return delegate (double x)
  39.             {
  40.                 double sum = 0.0, temp = 1;
  41.                 sum += temp * tab[0];
  42.                 for (int i = 1; i < tab.Length; i++)
  43.                 {
  44.                     temp *= f(x);
  45.                     sum += temp * tab[i];
  46.                 }
  47.                 return sum;
  48.             };
  49.         }
  50.         public static double Integral(double a, double b, double n, Fun3 f)
  51.         {
  52.             double sum = 0.0;
  53.             double average;
  54.             double przedzial_len = (b - a) / n;
  55.             double miejscenaosi = a;
  56.  
  57.             for (int i=0;i<n;i++)
  58.             {
  59.                 average = (f(miejscenaosi) + f(miejscenaosi + przedzial_len))/2;
  60.                 sum += przedzial_len * average;
  61.                 miejscenaosi += przedzial_len;
  62.             }
  63.             return sum;
  64.         }
  65.  
  66.         public static Fun4 ChebyshevDistance()
  67.         {
  68.             return delegate (Point2D p1, Point2D p2)
  69.             {
  70.                 double max = Math.Abs(p1.X - p2.X);
  71.                 if (Math.Abs(p1.Y - p2.Y) > max) max = Math.Abs(p1.Y - p2.Y);
  72.                 return max;
  73.             };
  74.         }
  75.  
  76.         public static Point2D SmallestEnclosingBall(List<Point2D> l, Fun4 f)
  77.         {
  78.             double min_dist = double.MaxValue;
  79.             double max_dist;
  80.             Point2D p = l[0];
  81.             //for (int i=0;i<l.Count;i++)
  82.             //{
  83.             //    for (int j = 0; j < l.Count; j++)
  84.             //        if (i != j && f(l[i], l[j]) < min_dist)
  85.             //        {
  86.             //            min_dist = f(l[i], l[j]);
  87.             //            p = l[i];
  88.             //        }
  89.             //}
  90.             foreach (Point2D punZew in l)
  91.             {
  92.                 max_dist = 0.0;
  93.                 foreach (Point2D punWew in l)
  94.                 {
  95.                     if (f(punZew, punWew) > max_dist) max_dist = f(punZew, punWew);
  96.                 }
  97.                 if (max_dist<min_dist)
  98.                 {
  99.                     min_dist = max_dist;
  100.                     p = punZew;
  101.                 }
  102.  
  103.             }
  104.             return p;
  105.         }
  106.  
  107.         public static Fun5 ReversedPascalTriangleGeneration(int a) //public delegate int[] Fun5(int[] tab);
  108.         {
  109.             return delegate (int[] tab)
  110.             {
  111.                 int[] newtab = new int[tab.Length - 1]; //
  112.  
  113.                 for (int i = 0; i < tab.Length - 1; i++)
  114.                 {
  115.                     newtab[i] = tab[i] + tab[i + 1] + a;
  116.                 }
  117.  
  118.                 return newtab;
  119.             };
  120.         }
  121.         public static void ReversedTriangleGeneration(Fun5 f,int[] tab)
  122.         {
  123.             while (tab.Length>=1)
  124.             {
  125.                 Program.PrintArray(tab);
  126.                 tab = f(tab);
  127.             }
  128.         }
  129.  
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement