日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
討論一下C#面向集合的擴(kuò)展

我們知道有很多數(shù)學(xué)軟件,如MatLab是面向矩陣的,而開源語言R是面向向量的,SQL是面向關(guān)系系的、APL(Array processing language)是一種一種多用途、第三代(3GL)編程語言,在向量、矩陣等各種秩的數(shù)組處理上非常簡單。SPSS,SAS等都需要大量的集合運(yùn)算。

德清ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!

本文試圖從C#本身的特性出發(fā),模擬C#面向集合的方法。

更期望C#面向集合能向MatLab, APL,R那樣直接處理集合運(yùn)算,進(jìn)入科學(xué)和工程計(jì)算領(lǐng)域,為以后的并行計(jì)算奠定基礎(chǔ)。

有一列觀測值,用List存儲,我們現(xiàn)在需要求出每一個(gè)觀測值的正弦Sin值。

用C#面向過程的語法表示如下:

 
 
 
  1. List list2;  
  2. for (int i = 0; i < list2.Count; i++)  
  3.             list2[i] = Math.Sin(list2[i]); 

求Sin值,是一個(gè)繁瑣而又重復(fù)的問題。我們希望Math.Sin(Collection c),在不改變已有代碼(不擴(kuò)展Math.Sin)的情況下,自動(dòng)處理集合,就像在MatLab里面。

 C#是面向過程的,而Matlab是面向矩陣的,SQL是面向關(guān)系代數(shù)的。關(guān)系代數(shù)和矩陣,都可以看作集合的特例。(LINQ部分加入了面向集合的特性)

面向過程,需要程序員書寫算法的每一個(gè)過程和細(xì)節(jié),指明執(zhí)行路徑,這主要表現(xiàn)在循環(huán)語句的使用上(包括for, foreach, while…)。

面向過程給了程序員最充分的自由和最大的靈活,但其固有的“底層”,導(dǎo)致了開發(fā)效率的底下,同時(shí)不利于并行計(jì)算和系統(tǒng)優(yōu)化。而在數(shù)學(xué)中,大部分計(jì)算都是基于矩陣(集合),例如圖形圖像處理,概率論,數(shù)理統(tǒng)計(jì),優(yōu)化控制等等。 所以C#難以勝任運(yùn)算集中和知識處理,人工智能設(shè)計(jì)。

由于C#實(shí)在是太優(yōu)美,是目前最藝術(shù)的語言,利用C#現(xiàn)有特性,我們可以簡單的模擬前面提出的問題    

 
 
 
  1. public static List Apply(Converter f, List l)  
  2.     {  
  3.        List list2 = new List(l);  
  4.         for (int i = 0; i < list2.Count; i++)  
  5.             list2[i] = Math.Sin(list2[i]);  
  6.         for (int i = 0; i < l.Count; i++)  
  7.         {  
  8.             list2[i] = f(l[i]);  
  9.         }  
  10.        return list2;  
  11.     } 

這樣,我們可以在Apply來處理一些關(guān)于集合處理的問題。

下面在給出一個(gè)處理矩陣的例子:   

 
 
 
  1. public static Matrix Apply(Converter f, Matrix m)  
  2.     {  
  3.         Matrix m2 = new Matrix(m);  
  4.         for (int i = 0; i < m.Row; i++)  
  5.             for (int j = 0; j < m.Col; j++)  
  6.                 m2[i, j] = f(m2[i, j]);  
  7.         return m2;  

使用這個(gè)Apply,可以處理矩陣集合相關(guān)的計(jì)算。

矩陣定義如下:

 
 
 
  1. public class Matrix  
  2. {  
  3.     public double[,] data;  
  4.     public Matrix(int row, int col)  
  5.     {  
  6.         data = new double[row, col];  
  7.     }  
  8.     //復(fù)制構(gòu)造函數(shù)  
  9.     public Matrix(Matrix m)  
  10.     {  
  11.         data = (double[,])m.data.Clone();  
  12.     }  
  13.     public int Col  
  14.     {  
  15.         get  
  16.         {  
  17.            return data.GetLength(1);  
  18.         }  
  19.     }  
  20.     // 行數(shù)  
  21.     public int Row  
  22.     {  
  23.         get  
  24.         {  
  25.             return data.GetLength(0);  
  26.         }  
  27.    }  
  28.     //重載索引  
  29.     //存取數(shù)據(jù)成員  
  30.     public virtual double this[int row, int col]  
  31.     {  
  32.         get  
  33.         {  
  34.            return data[row, col];  
  35.         }  
  36.        set  
  37.         {  
  38.             data[row, col] = value;  
  39.        }  
  40.     }  
  41.     //維數(shù)  
  42.     public int Dimension  
  43.     {  
  44.         get { return 2; }  
  45.     }  
  46.     public string ToString(int prec)  
  47.     {  
  48.         StringBuilder sb = new StringBuilder();  
  49.         string format = "{0:F" + prec.ToString() + "} ";  
  50.        for (int i = 0; i < Row; i++)  
  51.         {  
  52.             for (int j = 0; j < Col - 1; j++)  
  53.             {  
  54.                sb.Append(string.Format(format, data[i, j]));  
  55.             }  
  56.             sb.Append(data[i, Col - 1]);  
  57.             sb.Append(""n");  
  58.        }  
  59.         return sb.ToString();  
  60.     }  

再看下面復(fù)數(shù)的例子:

 
 
 
  1. public static List Apply(Converter< Complex,double> f, List l)  
  2.     {  
  3.         List l2 = new List(l.Count);  
  4.         for (int i = 0; i < l.Count; i++)  
  5.             l2.Add(f(l[i]));  
  6.         return l2;  

 使用這個(gè)Apply,可以處理復(fù)數(shù)集合相關(guān)的許多計(jì)算。

復(fù)數(shù)類的定義如下:

 
 
 
  1. Code  
  2. public class Complex:ICloneable  
  3. {  
  4.     private double real;  
  5.     /**////   
  6.    /// 復(fù)數(shù)的實(shí)部  
  7.     ///   
  8.     public double Real  
  9.     {  
  10.         get { return real; }  
  11.         set { real = value; }  
  12.    }  
  13.     private double image;  
  14.     /**////   
  15.     /// 復(fù)數(shù)的虛部  
  16.    ///   
  17.     public double Image  
  18.     {  
  19.        get { return image; }  
  20.         set { image = value; }  
  21.     }  
  22.     /**////   
  23.     /// 默認(rèn)構(gòu)造函數(shù)  
  24.     ///   
  25.     public Complex()  
  26.        : this(0, 0)  
  27.     {  
  28.     }  
  29.     /**////   
  30.    /// 只有實(shí)部的構(gòu)造函數(shù)  
  31.     ///   
  32.     /// 實(shí)部  
  33.     public Complex(double real)  
  34.        : this(real, 0) { }  
  35.    /**////   
  36.     /// 由實(shí)部和虛部構(gòu)造  
  37.     ///   
  38.     /// 實(shí)部  
  39.    /// 虛部  
  40.     public Complex(double r, double i)  
  41.     {  
  42.         rreal = r;  
  43.         iimage = i;  
  44.     }  
  45.     /**////重載加法  
  46.    public static Complex operator +(Complex c1, Complex c2)  
  47.     {  
  48.         return new Complex(c1.real + c2.real, c1.image + c2.image);  
  49.     }  
  50.     /**////重載減法  
  51.     public static Complex operator -(Complex c1, Complex c2)  
  52.     {  
  53.         return new Complex(c1.real - c2.real, c1.image - c2.image);  
  54.     }  
  55.     /**////重載乘法  
  56.     public static Complex operator *(Complex c1, Complex c2)  
  57.     {  
  58.         return new Complex(c1.real * c2.real - c1.image * c2.image, c1.image * c2.real + c1.real * c2.image);  
  59.     }  
  60.     /**////   
  61.     /// 求復(fù)數(shù)的模  
  62.     ///   
  63.     /// 模  
  64.     public double Modul  
  65.     {  
  66.         get  
  67.         {  
  68.             return Math.Sqrt(real * real + image * image);  
  69.         }  
  70.     }  
  71.     public static double Sin(Complex c)  
  72.    {  
  73.         return c.image / c.Modul;  
  74.     }  
  75.     /**////   
  76.     /// 重載ToString方法  
  77.    ///   
  78.     /// 打印字符串  
  79.     public override string ToString()  
  80.     {  
  81.         if (Real == 0 && Image == 0)  
  82.         {  
  83.             return string.Format("{0}", 0);  
  84.        }  
  85.         if (Real == 0 && (Image != 1 && Image != -1))  
  86.         {  
  87.             return string.Format("{0} i", Image);  
  88.        }  
  89.         if (Image == 0)  
  90.         {  
  91.             return string.Format("{0}", Real);  
  92.         }  
  93.         if (Image == 1)  
  94.        {  
  95.             return string.Format("i");  
  96.         }  
  97.         if (Image == -1)  
  98.         {  
  99.  
  100.            return string.Format("- i");  
  101.         }  
  102.         if (Image < 0)  
  103.         {  
  104.            return string.Format("{0} - {1} i", Real, -Image);  
  105.         }  
  106.        return string.Format("{0} + {1} i", Real, Image);  
  107.     }  
  108.    ICloneable 成員#region ICloneable 成員  
  109.   public object Clone()  
  110.     {  
  111.         Complex c = new Complex(real, image);  
  112.         return c;  
  113.     }  
  114.    #endregion  

從前面三個(gè)例子,我們可以看出,C#面向集合有多種表示方式,有.net框架中的List,也有自定義的Matrix,同時(shí)集合的元素也是多種數(shù)據(jù)類型,有系統(tǒng)中的值類型,也有自定義的復(fù)數(shù)Complex類型。

當(dāng)然這種算法過于勉強(qiáng),顯然不是我們所需要的。

我們需要的是一個(gè)在不更改現(xiàn)有語言的情況下,不擴(kuò)充Math.Sin函數(shù)(試著想想有多少個(gè)類似的函數(shù),Cos, Tan, 我們自己定義的各種函數(shù))。系統(tǒng)自動(dòng)處理集合。也就是說,對于函數(shù)    public delegate TOutput Converter (TInput input);public T1 Func (T2 e); Func是Converter的實(shí)例。只要Func能夠處理原子類型,那么就能處理自動(dòng)所有的原子類型構(gòu)成的任意集合,而不需要程序員去寫多余的代碼。


網(wǎng)頁名稱:討論一下C#面向集合的擴(kuò)展
文章鏈接:http://m.5511xx.com/article/dhggphd.html