Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace lab10B
- {
- public static class FunctionGenerationAndUsage
- {
- public delegate double Fun1(double a, double b);
- public delegate T Fun2<T>(T a, T b);
- public delegate double Fun3(double x);
- public delegate double Fun4(Point2D p1, Point2D p2);
- public delegate int[] Fun5(int[] tab);
- public static Fun2<double> NilpotentMinimumGeneration()
- {
- return delegate (double a, double b)
- {
- if (a + b > 1) return Math.Min(a, b);
- else return 0;
- };
- }
- public static List<T> ScanList<T>(Fun2<T> f, params T[] tab)
- {
- if (tab.Length == 0) return null;
- List<T> l = new List<T>();
- T element = tab[0];
- l.Add(element);
- for (int i=1;i<tab.Length;i++)
- {
- element = f(element,tab[i]);
- l.Add(element);
- }
- return l;
- }
- public static Fun3 PolynomalFunction(Fun3 f, double[] tab)
- {
- return delegate (double x)
- {
- double sum = 0.0, temp = 1;
- sum += temp * tab[0];
- for (int i = 1; i < tab.Length; i++)
- {
- temp *= f(x);
- sum += temp * tab[i];
- }
- return sum;
- };
- }
- public static double Integral(double a, double b, double n, Fun3 f)
- {
- double sum = 0.0;
- double average;
- double przedzial_len = (b - a) / n;
- double miejscenaosi = a;
- for (int i=0;i<n;i++)
- {
- average = (f(miejscenaosi) + f(miejscenaosi + przedzial_len))/2;
- sum += przedzial_len * average;
- miejscenaosi += przedzial_len;
- }
- return sum;
- }
- public static Fun4 ChebyshevDistance()
- {
- return delegate (Point2D p1, Point2D p2)
- {
- double max = Math.Abs(p1.X - p2.X);
- if (Math.Abs(p1.Y - p2.Y) > max) max = Math.Abs(p1.Y - p2.Y);
- return max;
- };
- }
- public static Point2D SmallestEnclosingBall(List<Point2D> l, Fun4 f)
- {
- double min_dist = double.MaxValue;
- double max_dist;
- Point2D p = l[0];
- //for (int i=0;i<l.Count;i++)
- //{
- // for (int j = 0; j < l.Count; j++)
- // if (i != j && f(l[i], l[j]) < min_dist)
- // {
- // min_dist = f(l[i], l[j]);
- // p = l[i];
- // }
- //}
- foreach (Point2D punZew in l)
- {
- max_dist = 0.0;
- foreach (Point2D punWew in l)
- {
- if (f(punZew, punWew) > max_dist) max_dist = f(punZew, punWew);
- }
- if (max_dist<min_dist)
- {
- min_dist = max_dist;
- p = punZew;
- }
- }
- return p;
- }
- public static Fun5 ReversedPascalTriangleGeneration(int a) //public delegate int[] Fun5(int[] tab);
- {
- return delegate (int[] tab)
- {
- int[] newtab = new int[tab.Length - 1]; //
- for (int i = 0; i < tab.Length - 1; i++)
- {
- newtab[i] = tab[i] + tab[i + 1] + a;
- }
- return newtab;
- };
- }
- public static void ReversedTriangleGeneration(Fun5 f,int[] tab)
- {
- while (tab.Length>=1)
- {
- Program.PrintArray(tab);
- tab = f(tab);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement