/// <summary> /// 指定精度的求导 /// </summary> /// <param name="f"></param> /// <param name="x"></param> /// <param name="Precision"></param> /// <returns></returns> static public double DerivativeByPrecision(Func11 f, double x, double Precision) { double h = 0.125, d, dd, u; if (f(x) == double.NaN) { return(double.NaN); //是否在定义域内,若不在定义域内,则返回NaN } d = (f(x + h) - f(x)) / h; while (h > 0.000000000001) { h /= 2; dd = (f(x + h) - f(x)) / h; u = dd - d; if (u < 0) { u = -u; } if (u < Precision) { return(dd); } d = dd; } return(double.NaN); }
/// <summary> /// 求f的导函数 /// </summary> /// <param name="f"></param> /// <returns></returns> static public Func11 DerivativeAuto(Func11 f) { return(delegate(double x) { return DerivativeAuto(f, x); }); }
/// <summary> /// 指定步长的求导,不会判断导函数是否存在 /// </summary> /// <param name="f">一元数量函数f</param> /// <param name="x">求x处的导</param> /// <param name="Step">求导步长</param> /// <returns></returns> static public double DerivativeByStep(Func11 f, double x, double Step) { return((f(x + Step) - f(x)) / Step); }
public void DownloadFile(Func21 onDataRead,Func01 onComplete,Func11 onError) { try { string methodLine = string.Format("GET {0} HTTP/1.1", this.UrlAddress.FilePath); string hostLine = string.Format("Host: {0}", this.UrlAddress.Host); string connectionLine = "Connection: Close"; var requestArray = new string[] { methodLine, hostLine, connectionLine, "", "", }; var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp); socket.Connect(this.UrlAddress.Host, this.UrlAddress.Port); var request = string.Join("\r\n", requestArray); var requestB = Encoding.Default.GetBytes(request); socket.Send(requestB); int readBytes = 0; byte[] buffer = new byte[1024]; readBytes = socket.Receive(buffer); if (onDataRead != null) { onDataRead(buffer, readBytes); } while (readBytes > 0) { if (onDataRead != null) { onDataRead(buffer, readBytes); } readBytes = socket.Receive(buffer); } socket.Close(); if (onComplete != null) { onComplete(); } } catch (Exception ex) { if (onError != null) { onError(ex); } } }