原文鏈接:https://www.codeproject.com/Articles/1160683/Functional-Programming-In-Csharp-2
https://blog.csdn.net/mzl87/article/details/100025445
目錄

通過函數表示數據
讓S是任何元素a,b,c...(例如,桌子上的書,或者歐幾里得平面的點)的集合,並讓S'是這些元素的任意子集(例如,桌子上的綠皮書,或者半徑為1的圓上以歐幾里得平面原點為中心的點)。
集合S'的特征函數S'(x)是一個函數,它將true或false與S的每個元素x相關聯。
-
S '(x) = true if x is in S'
-
S '(x) = false if x is not in S'
讓S成為桌子上的一套書,讓S'成為桌上的綠皮書。讓a和b是兩個綠色的書,讓c和d是在表中的兩個紅色的本。然后:
-
S '(a) = S'(b) = true
-
S '(c) = S'(d) = false
讓S是歐幾里德平面中的點的集合,並且讓S'在半徑為1的園中以歐幾里得平面(0,0)原點為中心的點的集合(單位圓)。讓a和b在單位圓的兩點,並讓c並且d是半徑為2的圓上以歐幾里得平面原點為中心的點。然后:
-
S '(a) = S'(b) = true
-
S '(c) = S'(d) = false
因此,任何集合S'總是可以由其特征函數表示。一個函數,它將一個元素作為參數,並返回true如果該元素在S'中,否則返回false。換句話說,可以通過C#中的謂詞來表示集合(抽象數據類型)。
Predicate<T> set;
在接下來的部分中,我們將看到如何通過C#以函數方式表示集合代數中的一些基本集合,然后我們將在集合上定義泛型二進制運算。然后,我們將在歐幾里德平面的子集上對數字應用這些操作。集合是抽象數據結構,數字的子集和歐幾里得平面的子集是抽象數據結構的表示,最后二元操作是適用於抽象數據結構的任何表示的通用邏輯。
集合
本節通過C#介紹集合代數中一些基本集的表示。
空集

讓E是空集和Empty是它的特征函數。在集合的代數中,E是沒有元素的唯一集合。因此,Empty可以定義如下:
-
Empty(x) = false if x is in E
-
Empty(x) = false if x is not in E
因此,E在C#中的表示可以定義如下:
-
public static Predicate<T> Empty<T>()
-
{
-
return x => false;
-
}
在集合的代數中,Empty表示如下:
![]()
因此,運行以下代碼:
-
Console.WriteLine( "\nEmpty set:");
-
Console.WriteLine( "Is 7 in {{}}? {0}", Empty<int>()(7));
結果如下:

全集

讓S是一集合和S'是包含所有要素的S的子集,All是其特色函數。在集合的代數中,S'是包含所有元素的完整集合。因此,All可以這樣定義:
All(x) = true if x is in S
因此,S'在C#中的表示可以定義如下:
-
public static Predicate<T> All<T>()
-
{
-
return x => true;
-
}
在集合的代數中,All表示如下:
![]()
因此,運行以下代碼:
Console.WriteLine("Is 7 in the integers set? {0}", All<int>()(7));
結果如下:

單元素集
讓E是單元素集並且Singleton是它的特征函數。在集合的代數中,E也稱為單元集合,或者1元組是具有恰好一個元素e的集合。因此,Singleton可以定義如下:
-
Singleton(x) = true if x is e
-
Singleton(x) = false if x is not e
因此,E在C#中的表示可以定義如下:
-
public static Predicate<T> Singleton<T>(T e)
-
{
-
return x => e.Equals(x);
-
}
因此,運行以下代碼:
-
Console.WriteLine( "Is 7 in the singleton {{0}}? {0}", Singleton(0)(7));
-
Console.WriteLine( "Is 7 in the singleton {{7}}? {0}", Singleton(7)(7));
結果如下:

其他集合
本節介紹整數集的子集。
偶數
讓E是一個偶數集合,並且Even是它的特征函數。在數學中,偶數是一個2的倍數。因此,Even可以定義如下:
-
Even(x) = true if x is a multiple of 2
-
Even(x) = false if x is not a multiple of 2
因此,E在C#中的表示可以定義如下:
Predicate<int> even = i => i % 2 == 0;
因此,運行以下代碼:
-
Console.WriteLine( "Is {0} even? {1}", 99, even(99));
-
Console.WriteLine( "Is {0} even? {1}", 998, even(998));
結果如下:

奇數
讓E是一個奇數集合並且Odd是它的特征函數。在數學中,奇數是一個不是2的倍數的數字。因此,Odd可以定義如下:
-
Odd(x) = true if x is not a multiple of 2
-
Odd(x) = false if x is a multiple of 2
因此,E在C#中的表示可以定義如下:
Predicate<int> odd = i => i % 2 == 1;
因此,運行以下代碼:
-
Console.WriteLine( "Is {0} odd? {1}", 99, odd(99));
-
Console.WriteLine( "Is {0} odd? {1}", 998, odd(998));
結果如下:

3的倍數
讓E是一個3的倍數的集合兵器MultipleOfThree是它的特征函數。在數學中,3的倍數是可被3整除的數。因此,MultipleOfThree可以定義如下:
-
MultipleOfThree(x) = true if x is divisible by 3
-
MultipleOfThree(x) = false if x is not divisible by 3
因此,E在C#中的表示可以定義如下:
Predicate<int> multipleOfThree = i => i % 3 == 0;
因此,運行以下代碼:
-
Console.WriteLine( "Is {0} a multiple of 3? {1}", 99, multipleOfThree(99));
-
Console.WriteLine( "Is {0} a multiple of 3? {1}", 998, multipleOfThree(998));
結果如下:

5的倍數
讓E是5的倍數的集合兵器MultipleOfFive是它的特征函數。在數學中,5的倍數是可被5整除的數。因此,MultipleOfFive可以定義如下:
-
MultipleOfFive(x) = true if x is divisible by 5
-
MultipleOfFive(x) = false if x is not divisible by 5
因此,E在C#中的表示可以定義如下:
Predicate<int> multipleOfFive = i => i % 5 == 0;
因此,運行以下代碼:
-
Console.WriteLine( "Is {0} a multiple of 5? {1}", 15, multipleOfFive(15));
-
Console.WriteLine( "Is {0} a multiple of 5? {1}", 998, multipleOfFive(998));
結果如下:

質數
很久以前,當我接觸Project Euler問題時,我不得不解決以下問題:
-
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13,
-
we can see that the 6th prime is 13.
-
What is the 10 001st prime number?
為了解決這個問題,我首先必須編寫一個快速算法來檢查給定的數字是否為素數。編寫算法后,我編寫了一個迭代算法,遍歷素數直到找到第10 001個素數。
讓E是素數的集合並且Prime是它的特征函數。在數學中,素數是大於1的自然數,除了1和自身之外沒有正除數。因此,Prime可以定義如下:
-
Prime(x) = true if x is prime
-
Prime(x) = false if x is not prime
因此,E在C#中的表示可以定義如下:
Predicate<int> prime = IsPrime;
IsPrime是檢查給定數字是否為素數的方法。
-
static bool IsPrime(int i)
-
{
-
if (i == 1) return false; // 1 is not prime
-
if (i < 4) return true; // 2 and 3 are primes
-
if ((i >> 1) * 2 == i) return false; // multiples of 2 are not prime
-
if (i < 9) return true; // 5 and 7 are primes
-
if (i % 3 == 0) return false; // multiples of 3 are not primes
-
-
// If a divisor less than or equal to sqrt(i) is found
-
// then i is not prime
-
int sqrt = (int)Math.Sqrt(i);
-
for (int d = 5; d <= sqrt; d += 6)
-
{
-
if (i % d == 0) return false;
-
if (i % (d + 2) == 0) return false;
-
}
-
-
// Otherwise i is prime
-
return true;
-
}
因此,運行下面的代碼來解決我們的問題:
-
int p = Primes(prime).Skip(10000).First();
-
Console.WriteLine( "The 10 001st prime number is {0}", p);
其中Primes定義如下:
-
static IEnumerable <int> Primes(Predicate<int> prime)
-
{
-
yield return 2;
-
-
int p = 3;
-
while (true)
-
{
-
if (prime(p)) yield return p;
-
p += 2;
-
}
-
}
結果如下:

二元操作
本節介紹了從給定集合和操作集合構造新集合的幾個基本操作。下面是集合代數中的維恩圖。

並集

讓E和F兩個集合。E和F的並集,用E u F表示的是E或F的所有元素的集合。
讓Union成為並操作。因此,可以在C#中實現如下Union操作:
-
public static Predicate<T> Union<T>(this Predicate<T> e, Predicate<T> f)
-
{
-
return x => e(x) || f(x);
-
}
如您所見,Union是一組特征函數的擴展函數。所有操作將被定義為集合的特征函數上的擴展函數。從而,運行以下代碼:
Console.WriteLine("Is 7 in the union of Even and Odd Integers Set? {0}", Even.Union(Odd)(7));
結果如下:

交集

讓E和F兩個集合。E和F的交集,由E n F表示的是所有元素的集合,他們是E和F的成員。
讓Intersection成為交叉操作。因此,可以在C#中實現如下Intersection操作:
-
public static Predicate<T> Intersection<T>(this Predicate<T> e, Predicate<T> f)
-
{
-
return x => e(x) && f(x);
-
}
如您所見,Intersection是一組特征函數的擴展函數。從而,運行以下代碼:
-
Predicate< int> multiplesOfThreeAndFive = multipleOfThree.Intersection(multipleOfFive);
-
Console.WriteLine( "Is 15 a multiple of 3 and 5? {0}", multiplesOfThreeAndFive(15));
-
Console.WriteLine( "Is 10 a multiple of 3 and 5? {0}", multiplesOfThreeAndFive(10));
結果如下:

笛卡爾積

讓E和F兩個集合。E和F的笛卡兒積,用E × F表示的是所有有序對(e, f) 集合,因此e是E的成員,f是F的成員。
讓CartesianProduct成為笛卡爾積操作。因此,CartesianProduct可以在C#中實現如下操作:
-
public static Func<T1, T2, bool> CartesianProduct<T1, T2>(this Predicate<T1> e, Predicate<T2> f)
-
{
-
return (x, y) => e(x) && f(y);
-
}
如您所見,CartesianProduct是一組特征函數的擴展函數。從而,運行以下代碼:
-
Func< int, int, bool> cartesianProduct = multipleOfThree.CartesianProduct(multipleOfFive);
-
Console.WriteLine( "Is (9, 15) in MultipleOfThree x MultipleOfFive? {0}", cartesianProduct(9, 15));
結果如下:

差集

讓E和F兩個集合。F中E的差集,用E \ F表示的是所有元素的集合,其中的元素是E的成員但不是F的成員。
讓Complement成為差集操作。因此,Complement可以在C#中實現如下操作:
-
public static Predicate<T> Complement<T>(this Predicate<T> e, Predicate<T> f)
-
{
-
return x => e(x) && !f(x);
-
}
如您所見,Complement是一個集合的特征函數的擴展方法。從而,運行以下代碼:
-
Console.WriteLine( "Is 15 in MultipleOfThree \\ MultipleOfFive set? {0}",
-
multipleOfThree.Complement(multipleOfFive)( 15));
-
Console.WriteLine( "Is 9 in MultipleOfThree \\ MultipleOfFive set? {0}",
-
multipleOfThree.Complement(multipleOfFive)( 9));
結果如下:

對等差分

讓E和F兩個集合。E和F的對等差分,用E ▲ F表示的是所有元素的集合,其中的元素是E和F的成員,但不是E和F的交集成員。
讓SymmetricDifference成為對等差分操作。因此,SymmetricDifference可以在C#中以兩種方式實現操作。一個簡單的方法是使用並和差操作,如下:
-
public static Predicate<T> SymmetricDifferenceWithoutXor<T>(this Predicate<T> e, Predicate<T> f)
-
{
-
return Union(e.Complement(f), f.Complement(e));
-
}
另一種方法是使用XOR二進制操作如下:
-
public static Predicate<T> SymmetricDifferenceWithXor<T>(this Predicate<T> e, Predicate<T> f)
-
{
-
return x => e(x) ^ f(x);
-
}
如您所見,SymmetricDifferenceWithoutXor和SymmetricDifferenceWithXor是集合的特征函數的擴展方法。從而,運行以下代碼:
-
// SymmetricDifference without XOR
-
Console.WriteLine( "\nSymmetricDifference without XOR:");
-
Predicate< int> sdWithoutXor = prime.SymmetricDifferenceWithoutXor(even);
-
Console.WriteLine
-
( "Is 2 in the symetric difference of prime and even Sets? {0}", sdWithoutXor(2));
-
Console.WriteLine
-
( "Is 4 in the symetric difference of prime and even Sets? {0}", sdWithoutXor(4));
-
Console.WriteLine
-
( "Is 7 in the symetric difference of prime and even Sets? {0}", sdWithoutXor(7));
-
-
// SymmetricDifference with XOR
-
Console.WriteLine( "\nSymmetricDifference with XOR:");
-
Predicate< int> sdWithXor = prime.SymmetricDifferenceWithXor(even);
-
Console.WriteLine( "Is 2 in the symetric difference of prime and even Sets? {0}", sdWithXor(2));
-
Console.WriteLine( "Is 4 in the symetric difference of prime and even Sets? {0}", sdWithXor(4));
-
Console.WriteLine( "Is 7 in the symetric difference of prime and even Sets? {0}", sdWithXor(7));
結果如下:

其他操作
本節介紹集合上其他有用的二進制操作。
包含
讓Contains是檢查元素是否在集合中的操作。此操作是一個集合的特征函數的擴展函數,它將元素作為參數,如果元素在集合中則返回true,否則返回false。
因此,此操作在C#中定義如下:
-
public static bool Contains<T>(this Predicate<T> e, T x)
-
{
-
return e(x);
-
}
因此,運行以下代碼:
-
Console.WriteLine( "Is 7 in the singleton {{0}}? {0}", Singleton(0).Contains(7));
-
Console.WriteLine( "Is 7 in the singleton {{7}}? {0}", Singleton(7).Contains(7));
結果如下:

加
讓Add是將一個元素添加到集合中的操作。此操作是一個集合的特征函數的擴展函數,它將元素作為參數並將其添加到集合中。
因此,此操作在C#中定義如下:
-
public static Predicate<T> Add<T>(this Predicate<T> s, T e)
-
{
-
return x => x.Equals(e) || s(x);
-
}
因此,運行以下代碼:
-
Console.WriteLine( "Is 7 in {{0, 7}}? {0}", Singleton(0).Add(7)(7));
-
Console.WriteLine( "Is 0 in {{1, 0}}? {0}", Singleton(1).Add(0)(0));
-
Console.WriteLine( "Is 7 in {{19, 0}}? {0}", Singleton(19).Add(0)(7));
結果如下:

刪除
讓Remove是從集合中刪除元素的操作。此操作是對集合的特征函數的擴展函數,該函數將元素作為參數並將其從集合中移除。
因此,此操作在C#中定義如下:
-
public static Predicate<T> Remove<T>(this Predicate<T> s, T e)
-
{
-
return x => !x.Equals(e) && s(x);
-
}
因此,運行以下代碼:
-
Console.WriteLine( "Is 7 in {{}}? {0}", Singleton(0).Remove(0)(7));
-
Console.WriteLine( "Is 0 in {{}}? {0}", Singleton(7).Remove(7)(0));
結果如下:

對於那些想要更進一步的人
您可以通過函數式編程看到我們在C#中使用集合代數是多么容易。在前面的部分中顯示了最基本的定義。但是,如果你想進一步,你可以考慮:
- 關系集
- 抽象代數,如單倍體,群,場,環,K-矢量空間等
- 包含排除原則
- 羅素的悖論
- 康托爾的悖論
- 雙向量空間
- 定理和推論
歐幾里得平面
在上一節中,集合的基本概念是在C#中實現的。在本節中,我們將練習在平面點集(歐幾里德平面)上實現的概念。
繪制磁盤

磁盤是由圓圈限定的平面的子集。有兩種類型的磁盤。封閉的磁盤是包含構成其邊界的圓的點的磁盤,而打開的磁盤是不包含構成其邊界的圓的點的磁盤。
在本節中,我們將設置特征函數的閉盤,並在WPF應用程序繪制。
要設置特征函數,首先需要一個計算平面中兩點之間歐氏距離的函數。該函數實現如下:
-
public static double EuclidianDistance(Point point1, Point point2)
-
{
-
return Math.Sqrt(Math.Pow(point1.X - point2.X, 2) + Math.Pow(point1.Y - point2.Y, 2));
-
}
其中Point是在System.Windows命名空間中定義的struct。這個公式是基於畢達哥拉斯定理。

其中c是歐幾里德距離,a²是(point1.X - point2.X)²和b²是(point1.Y - point2.Y)²。
讓Disk是特征函數的閉盤。在集合的代數中,實數集中的閉盤的定義如下:
![]()
其中a和b是中心和R半徑的坐標。
因此,Disk在C#中的實現如下:
-
public static Predicate<Point> Disk(Point center, double radius)
-
{
-
return p => EuclidianDistance(center, p) <= radius;
-
}
為了查看集合,我決定實現一個在歐幾里得平面中繪制集合的函數Draw。我選擇了WPF,因此使用System.Windows.Controls.Image作為畫布和Bitmap作為上下文。
因此,我通過Draw方法建立了下面說明的歐幾里德平面。

以下是該方法的實現:
-
public static void Draw(this Predicate<Point> set, Image plan)
-
{
-
Drawing.Bitmap bitmap = new Drawing.Bitmap((int)plan.Width, (int)plan.Height);
-
-
//
-
// Graph drawing
-
//
-
double semiWidth = plan.Width / 2;
-
double semiHeight = plan.Height / 2;
-
-
double xMin = -semiWidth;
-
double xMax = +semiWidth;
-
double yMin = -semiHeight;
-
double yMax = +semiHeight;
-
-
for (int x = 0; x < bitmap.Height; x++)
-
{
-
double xp = xMin + x * (xMax - xMin) / plan.Width;
-
-
for (int y = 0; y < bitmap.Width; y++)
-
{
-
double yp = yMax - y * (yMax - yMin) / plan.Height;
-
-
if (set(new Point(xp, yp)))
-
{
-
bitmap.SetPixel(x, y, Drawing.Color.Black);
-
}
-
}
-
}
-
-
plan.Source = Imaging.CreateBitmapSourceFromHBitmap(
-
bitmap.GetHbitmap(),
-
IntPtr.Zero,
-
System.Windows.Int32Rect.Empty,
-
BitmapSizeOptions.FromWidthAndHeight(bitmap.Width, bitmap.Height));
-
-
}
在Draw方法中,創建具有與歐幾里德平面容器相同的寬度和相同高度的bitmap 。然后,如果bitmap每個像素點(x,y)都屬於set,則將其替換為一個黑點。xMin,xMax,yMin和yMax是在上方圖中所示的歐幾里得平面的邊界值。
如您所見,Draw是一組點的特征函數的擴展函數。因此,運行以下代碼:
Plan.Disk(new Point(0, 0), 20).Draw(plan);
結果如下:

繪制水平和垂直半平面
水平或垂直半平面或者是平面分割歐幾里得空間的兩個子集之一。水平半平面是兩個子集的任意一個子集,其中一個平面通過與Y軸垂直的線將歐幾里得空間分割成兩個子集。垂直半平面是兩個子集的任意一個子集,其中一個平面通過與x軸垂直的線將歐幾里得空間分割成兩個子集。
在本節中,我們將設置水平和垂直半平面的特征函數,在WPF應用程序中繪制它們,看看如果我們將它們與磁盤子集組合,我們可以做些什么。
讓HorizontalHalfPlane是水平半平面的特征函數。HorizontalHalfPlanE在C#中的實現如下:
-
public static Predicate<Point> HorizontalHalfPlane(double y, bool lowerThan)
-
{
-
return p => lowerThan ? p.Y <= y : p.Y >= y;
-
}
因此,運行以下代碼:
Plan.HorizontalHalfPlane(0, true).Draw(plan);
結果如下:

讓VerticalHalfPlane是垂直半平面的特征函數。VerticalHalfPlanE在C#中的實現如下:
-
public static Predicate<Point> VerticalHalfPlane(double x, bool lowerThan)
-
{
-
return p => lowerThan ? p.X <= x : p.X >= x;
-
}
因此,運行以下代碼:
Plan.VerticalHalfPlane(0, false).Draw(plan);
結果如下:

在本文的第一部分中,我們在集合上設置了基本的二元操作。因此,通過組合disk和half-plane的交集,我們可以繪制半磁盤子集。
因此,運行以下示例:
Plan.VerticalHalfPlane(0, false).Intersection(Plan.Disk(new Point(0, 0), 20)).Draw(plan);
結果如下:

函數
本節介紹歐幾里德平面上的集合的函數。
轉變

讓Translate是轉變在平面上的點的函數。在歐幾里德幾何中,Translate 是一個將給定點在指定方向上移動恆定距離的函數。因此,C#中的實現如下:
-
public static Func<Point, Point> Translate(double deltax, double deltay)
-
{
-
return p => new Point(p.X + deltax, p.Y + deltay);
-
}
其中(deltax, deltay)是轉變的常量向量。
讓TranslateSet是轉化平面中集合的函數。此函數在C#中簡單實現如下:
-
public static Predicate<Point> TranslateSet(this Predicate<Point> set,
-
double deltax, double deltay)
-
{
-
return x => set(Translate(-deltax, -deltay)(x));
-
}
TranslateSet是集合上的擴展函數。它以deltax作為參數,deltax是第一個歐幾里得維度中的delta距離,deltay是第二個歐幾里得維度中的delta距離。如果點P(x,y)在集合S中被轉變,則其坐標將變為(x',y')=(x + delatx,y + deltay)。因此,點(X ' - delatx,Y' - DELTAY)將始終屬於集合S。在集合代數中,TranslateSet稱為同構,換句話說,所有轉變的集合形成轉變組T,其與空間本身同構。這解釋了函數的主要邏輯。
因此,在我們的WPF應用程序中運行以下代碼:
TranslateDiskAnimation();
其中TranslateDiskAnimation描述如下:
-
private const double Delta = 50;
-
private double _diskDeltay;
-
private readonly Predicate<Point> _disk = Plan.Disk(new Point(0, -170), 80);
-
-
private void TranslateDiskAnimation()
-
{
-
DispatcherTimer diskTimer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 0, 1, 0) };
-
diskTimer.Tick += TranslateTimer_Tick;
-
diskTimer.Start();
-
}
-
-
private void TranslateTimer_Tick(object sender, EventArgs e)
-
{
-
_diskDeltay = _diskDeltay <= plan.Height ? _diskDeltay + Delta : Delta;
-
Predicate<Point> translatedDisk = _diskDeltay <= plan.Height ?
-
_disk.TranslateSet( 0, _diskDeltay) : _disk;
-
translatedDisk.Draw(plan);
-
}
結果如下:

相似擴大

讓Scale是將任何點M發送到另一個點N的函數,這樣段SN與SM在同一條線上,但按系數lambda進行縮放。在集合的代數中,Scale表述如下:
![]()
因此,C#中的實現如下:
-
public static Func<Point, Point> Scale
-
( double deltax, double deltay, double lambdax, double lambday)
-
{
-
return p => new Point(lambdax * p.X + deltax, lambday * p.Y + deltay);
-
}
其中(deltax, deltay)是轉變的常量向量,(lambdax, lambday)是?向量。
讓ScaleSet是對計划中的集合上應用相似擴大的函數。此函數在C#中簡單實現如下:
-
public static Predicate<Point> ScaleSet(this Predicate<Point> set,
-
double deltax, double deltay, double lambdax, double lambday)
-
{
-
return x => set(Scale(-deltax / lambdax, -deltay / lambday, 1 / lambdax, 1 / lambday)(x));
-
}
ScaleSet是集合上的擴展函數。它以deltax作為參數,deltax是第一個歐幾里得維度中的delta距離deltay是第二個歐幾里得維度中的delta距離,以及(lambdax, lambday)是常數因子向量?如果點P(x,y)在集合S中通過ScaleSet轉換,則其坐標將更改為(x',y')=(lambdax*x+delatx,lambday*y+deltay)。因此,點((x’-delatx)/lambdax,(y’-deltay)/lambday)將始終屬於集合S,如果?當然,與向量0不同。在集合代數中,ScaleSet稱為同構,換句話說,所有同構的集合形成同構群H,同構於空間本身。這解釋了函數的主要邏輯。
因此,在我們的WPF應用程序中運行以下代碼:
ScaleDiskAnimation();
其中ScaleDiskAnimation描述如下:
-
private const double Delta = 50;
-
private double _lambdaFactor = 1;
-
private double _diskScaleDeltay;
-
private readonly Predicate<Point> _disk2 = Plan.Disk(new Point(0, -230), 20);
-
-
private void ScaleDiskAnimation()
-
{
-
DispatcherTimer scaleTimer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 0, 1, 0) };
-
scaleTimer.Tick += ScaleTimer_Tick;
-
scaleTimer.Start();
-
}
-
-
private void ScaleTimer_Tick(object sender, EventArgs e)
-
{
-
_diskScaleDeltay = _diskScaleDeltay <= plan.Height ? _diskScaleDeltay + Delta : Delta;
-
_lambdaFactor = _diskScaleDeltay <= plan.Height ? _lambdaFactor + 0.5 : 1;
-
Predicate<Point> scaledDisk = _diskScaleDeltay <= plan.Height
-
? _disk2.ScaleSet( 0, _diskScaleDeltay, _lambdaFactor, 1)
-
: _disk2;
-
scaledDisk.Draw(plan);
-
}
結果如下:

旋轉

讓Rotation是以theta角度旋轉一點的函數。在矩陣代數中,Rotation表述如下:

其中(x',y')是旋轉后點的坐標,x'和y'的公式如下:

這個公式的演示非常簡單。看看這個旋轉。

演示如下:

因此,C#中的實現如下:
-
public static Func<Point, Point> Rotate(double theta)
-
{
-
return p => new Point(p.X * Math.Cos(theta) - p.Y * Math.Sin(theta),
-
p.X * Math.Cos(theta) + p.Y * Math.Sin(theta));
-
}
讓RotateSet是在平面的集合上應用的帶角度旋轉的函數?此函數在C#中簡單實現如下。
-
public static Predicate<Point> RotateSet(this Predicate<Point> set, double theta)
-
{
-
return p => set(Rotate(-theta)(p));
-
}
RotateSet是集合上的擴展函數。它以theta作為參數,即旋轉角度。如果點P(x,y)通過集合S中的RotateSet進行變換,則其坐標將變為(x', y') = (x * cos(?) - y * sin(?), x * cos(?) + y * sin(?))。因此,點(x' * cos(?) + y' * sin(?), x' * cos(?) - y' * sin(?))將永遠屬於集合S。在集合的代數中,RotateSet稱為同構,換句話說,所有旋轉的集合形成旋轉組R,其與空間本身同構。這解釋了該函數的主要邏輯。
因此,在我們的WPF應用程序中運行以下代碼:
RotateHalfPlaneAnimation();
其中RotateHalfPlaneAnimation描述如下:
-
private double _theta;
-
private const double TwoPi = 2 * Math.PI;
-
private const double HalfPi = Math.PI / 2;
-
private readonly Predicate<Point> _halfPlane = Plan.VerticalHalfPlane(220, false);
-
-
private void RotateHalfPlaneAnimation()
-
{
-
DispatcherTimer rotateTimer = new DispatcherTimer
-
{ Interval = new TimeSpan(0, 0, 0, 1, 0) };
-
rotateTimer.Tick += RotateTimer_Tick;
-
rotateTimer.Start();
-
}
-
-
private void RotateTimer_Tick(object sender, EventArgs e)
-
{
-
_halfPlane.RotateSet(_theta).Draw(plan);
-
_theta += HalfPi;
-
_theta = _theta % TwoPi;
-
}
結果如下:

對於那些想要更進一步的人
很簡單,不是嗎?對於那些想要更進一步的人,你可以探索這些:
- 橢圓
- 三維歐氏空間
- 橢圓體
- 拋物面
- 雙曲面
- 球面諧波
- 超橢球體
- 妊神星
- 同形體
- 焦平面
分形

分形是具有通常超過其拓撲維度並且可能落在整數之間的分形維數的集合。例如,Mandelbrot集是由一系列復數二次多項式定義的分形:
Pc(z) = z^2 + c
其中c是復數。Mandelbrot分形被定義為所有點的集合c,使得上述序列不逃逸到無窮大。在集合的代數中,這表達如下:
![]()
Mandelbrot集如上圖所示。
分形(抽象數據類型)總是可以在C#中表示如下:
Func<Complex, Complex> fractal;
復數和繪圖
為了能夠繪制分形,我需要操縱復數。因此,我使用了Meta.numerics庫。我還需要一個實用程序來繪制Bitmap中的復數,因此我使用了類ColorMap和ClorTriplet類。
牛頓分形
我創建了一個牛頓分形(抽象數據類型表示)P(z) = z^3 - 2*z + 2,可在下面找到。
-
public static Func<Complex, Complex> NewtonFractal()
-
{
-
return z => z * z * z - 2 * z + 2;
-
}
為了能夠繪制復數,我需要更新Draw函數。因此,我創建了一個使用ColorMap和ClorTriplet類的Draw函數的重載。下面是C#中的實現。
-
public static void Draw(this Func<Complex, Complex> fractal, Image plan)
-
{
-
var bitmap = new Bitmap((int) plan.Width, (int) plan.Height);
-
-
const double reMin = -3.0;
-
const double reMax = +3.0;
-
const double imMin = -3.0;
-
const double imMax = +3.0;
-
-
for (int x = 0; x < plan.Width; x++)
-
{
-
double re = reMin + x*(reMax - reMin)/plan.Width;
-
for (int y = 0; y < plan.Height; y++)
-
{
-
double im = imMax - y*(imMax - imMin)/plan.Height;
-
-
var z = new Complex(re, im);
-
Complex fz = fractal(z);
-
-
if (Double.IsInfinity(fz.Re) || Double.IsNaN(fz.Re) || Double.IsInfinity(fz.Im) ||
-
Double.IsNaN(fz.Im))
-
{
-
continue;
-
}
-
-
ColorTriplet hsv = ColorMap.ComplexToHsv(fz);
-
-
ColorTriplet rgb = ColorMap.HsvToRgb(hsv);
-
var r = (int) Math.Truncate(255.0*rgb.X);
-
var g = (int) Math.Truncate(255.0*rgb.Y);
-
var b = (int) Math.Truncate(255.0*rgb.Z);
-
Color color = Color.FromArgb(r, g, b);
-
-
bitmap.SetPixel(x, y, color);
-
}
-
}
-
-
plan.Source = Imaging.CreateBitmapSourceFromHBitmap(
-
bitmap.GetHbitmap(),
-
IntPtr.Zero,
-
Int32Rect.Empty,
-
BitmapSizeOptions.FromWidthAndHeight(bitmap.Width, bitmap.Height));
-
}
因此,運行以下代碼:
Plan.NewtonFractal().Draw(plan);
結果如下:

對於那些想要更進一步的人
對於那些想要更進一步的人,你可以探索這些:
- Mandelbrot分形
- 朱莉婭分形
- 其他牛頓分形
- 其他分形
延遲簡介
在本節中,我們將看到如何從.NET Framework 3.5版開始創建一個Lazy類型。
延遲評估是一種評估策略,它將表達式的評估延遲到需要它的值,並且還避免重復評估。與其他非嚴格的評估策略(如按名稱調用)相比,共享可以通過指數因子減少某些函數的運行時間。下面列出了延遲評估的好處。
- 通過避免不必要的計算以及評估復合表達式的錯誤條件來提高性能
- 構造潛在無限數據結構的能力:我們可以輕松地創建一個無限的整數集,例如通過一個函數(參見集合部分中素數的例子)
- 將控制流(結構)定義為抽象而不是基元的能力
我們來看看下面的代碼:
-
public class MyLazy<T>
-
{
-
-
-
private readonly Func<T> _f;
-
private bool _hasValue;
-
private T _value;
-
-
-
-
-
-
public MyLazy(Func<T> f)
-
{
-
_f = f;
-
}
-
-
-
-
-
-
//
-
// Use objects of type MyLazy<T> as objects of type T
-
// through implicit keyword
-
//
-
public static implicit operator T(MyLazy<T> lazy)
-
{
-
if (!lazy._hasValue)
-
{
-
lazy._value = lazy._f();
-
lazy._hasValue = true;
-
}
-
-
return lazy._value;
-
}
-
-
-
}
MyLazy<T>是一個包含以下字段的泛型類:
- _f:延遲評估的函數,返回T類型值
- _value:T類型的值(凍結值)
- _hasValue:一個布爾值,指示是否已計算該值
為了使用類型MyLazy<T>的對象作為類型T的對象,使用implicit關鍵字。評估在類型鑄造時完成,此操作稱為解凍。
因此,運行以下代碼:
-
var myLazyRandom = new MyLazy<double>(GetRandomNumber);
-
double myRandomX = myLazyRandom;
-
Console.WriteLine( "\n Random with MyLazy<double>: {0}", myRandomX);
其中GetRandomNumber返回隨機double如下:
-
static double GetRandomNumber()
-
{
-
Random r = new Random();
-
return r.NextDouble();
-
}
給出以下輸出:

.NET Framework 4引入了一個用於延遲評估的類System.Lazy<T>。此類通過屬性Value返回值。運行以下代碼:
-
var lazyRandom = new Lazy<double>(GetRandomNumber);
-
double randomX = lazyRandom;
給出編譯錯誤,因為類型Lazy<T>與類型double不同。
要使用類System.Lazy<T>的值,必須按如下方式使用該屬性Value:
-
var lazyRandom = new Lazy<double>(GetRandomNumber);
-
double randomX = lazyRandom.Value;
-
Console.WriteLine( "\n Random with System.Lazy<double>.Value: {0}", randomX);
輸出如下:

.NET Framework 4還為延遲評估推出了ThreadLocal和LazyInitializer。
