NMath是一个适用于所有.NET语言,如C#、Visual Basic、F#和.NET的数学库,它包含了.NET平台上的面向对象数字计算的基础类。我们将以连载的形式向大家介绍NMath的实用教程。本节展示在NMatht中简单的将一元和二元函数应用于向量元素的便捷方法。
NMath提供了将一元和二元函数应用于向量元素的便捷方法,这些方法中的每一个都采用函数委托。Apply() 方法将会返回到一个新的向量,内容是给定的函数向量中的每个元素的结果。Transform() 方法是通过将给定的函数应用于其每个元素来修改矢量对象。
例,假设MyFunc 是一个带双精度并返回double的函数:
代码示例-C#
var v = new DoubleVector ( 10, 0, -1 );
// Construct a delegate for MyFunc
Func MyFuncDelegate =
new Func( MyFunc );
// Construct a new vector whose values are the result of applying
// MyFunc to the values in vector v. v remains unchanged.DoubleVector w = v.Apply( MyFuncDelegate );
// Transform the contents of v.
v.Transform( MyFuncDelegate );
v == w; // true!
代码示例-vb
Dim V As New DoubleVector(10, 0, -1)
' Construct a delegate for MyFunc
Dim MyFuncDelegate As New Func(Of Double, Double)(AddressOf MyFunc)
' Construct a new vector whose values are the result of applying
' MyFunc to the values in vector v. v remains unchanged.
Dim W As DoubleVector = V.Apply(MyFuncDelegate)
' Transform the contents of v.
V.Transform(MyFuncDelegate)
V = W ' true!
NMath能够为NMathFunctions类中的许多常用数学函数提供委托。