一个用于基本线性代数的 VB.NET 类






3.32/5 (12投票s)
2003 年 7 月 9 日
3分钟阅读

133502

750
一些向量和矩阵操作例程。
引言
.NET 中一个重要的缺失是数值计算。市面上有一些用于此目的的 VB 库,但是由于我多年前已经将一些 Numerical Recipes 例程从 C 转换为了 VB4,并且为了与我的 Color
类紧密集成,我决定将这些旧例程升级到 VB.NET。将这些旧例程升级到 VB.NET 只需要付出很小的努力,就可以用一些基本的矩阵和向量代数例程来增强这个类,尤其是在 VB.NET 中可以重载。
背景
掌握一些关于线性代数和数值计算的知识会很有帮助。
特点
就像我的 Color
类一样,这个类不是为速度而编写的,相反,我希望它清晰易懂,并且易于扩展(提示)。但是请注意,从 'Numerical Recipes in C' 翻译而来的例程并不是很容易阅读,并且不应该被更改(我只是知道它们有效,不要问我怎么做的)!有 3 个基本的构建块:标量(单精度浮点数),向量(单精度浮点数的一维数组)和矩阵(单精度浮点数的二维数组)。该类的一些特性是
- 基于奇异值分解的求解器,改编自 'Numerical Recipes in C'。可以在最小二乘意义上求解超定线性方程组。
- 基于非线性优化的求解器,改编自 'Numerical recipes in C' 中的 Nelder-Mead 单纯形算法。可以找到最小化用户提供函数的参数。有关如何使用此函数的示例,请参见
Color
类中的ImproveRGBTosRGBTransform
。 - 向量范数
- 向量和矩阵的逐元素加法、减法和缩放
- 向量或矩阵列的元素之和和平均值
- 向量或矩阵元素的**最大值和最小值**
- 矩阵的转置
- 向量-矩阵积的标量积
- 矩阵、向量-矩阵和矩阵-向量积
- 子矩阵的提取
- 使用向量获取和设置矩阵的行和列
- 矩阵和向量的文件 IO
- 矩阵和向量的
ToString
方法
我意识到这绝不是一个完整的 BLAS 类,我只是实现了我所需要的。我只是希望它对某人有帮助,也许可以作为更高级功能的起始类(请让我知道,这样我就可以将增强功能发布回网站)
代码示例
这是 Color
类的一部分,它实际上同时解决了 3 组线性方程(每个 R、G 和 B 颜色分量一组),产生了一组可用于将颜色三元组从一个颜色空间转换为另一个颜色空间的方程,例如用于校准目的。请注意,尽管关于未知系数的方程组是线性的,但根据 iTransformType
的值,它可能关于输入 R、G 和 B 值是高度非线性的,iTransformType
决定了将要使用的 R、G 和 B 的多项式类型,例如,三阶多项式 R' = a1 * R + a2 * G + a3 * B + a4 * R * G + a5 * R * B + a6 * G * B + a7 * R * G * B
。
Public Shared Sub ComputeColorSpaceTransform(ByVal sC1(,) As Single, _
ByVal sC2(,) As Single, _
ByRef sTransform(,) As Single, _
ByVal iTransformType As ColorSpaceTransform)
'Compute the polynomial transforms from R3 to R3,
'both representing LINEAR color spaces.
'This is based on a set of N color triplets in sC1, which have to
'be mapped to N color triplets in sC2.
'This means both sC1 and sC2 are N x 3 matrices.
'This also means there are actually 3 transforms to be computed,
'i.e. one for every color coordinate.
'The passed on iTransformType is equal to the number of terms
'in the transform (3 for linear,etc..),
'and can be termed the order of the transform.
'The resulting polynomial transform is stored in a 3 x M matrix,
'which can be right multiplied with
'a color N-tuple to obtain the desired color tristimulus value.
'NOTE: Usually the color transform is used to transform from RGB
'to sRGB or XYZ, allowing for device calibration
Dim iNrColors, i As Integer
iNrColors = sC1.GetUpperBound(0) + 1
Console.WriteLine("Computing " & iTransformType & _
"-term transform using " & iNrColors & " tristimulus values")
'Compute sA using the input color triplets
Dim sCNL(iTransformType - 1), sA(iNrColors - 1, _
iTransformType - 1), sC(2) As Single
For i = 0 To iNrColors - 1
Algebra.GetMatrixRow(sC1, sC, i)
' Compute higher order terms in R,G,B
ColorTripletToNTuple(sC, sCNL)
'Copy color N-tuple to sA
Algebra.SetMatrixRow(sA, sCNL, i)
Next i
Console.WriteLine("Design matrix is " & vbNewLine _
& Algebra.ToString(sA))
'Solve the 3 sets of linear equations (they are
'linear in their unknowns, even if they use
'coefficient which are non-linear functions of the
'input color triplets!)
'The resulting matrix needs to be transposed to be
'consistent with the fixed linear transform
'matrices already used in the color class
'(they all use Tf.X = Y, NOT X.Tf = Y)
Dim sX(iTransformType - 1, 2) As Single
Algebra.Solve(sA, sX, sC2)
ReDim sTransform(2, iTransformType - 1)
Algebra.Transpose(sX, sTransform)
End Sub
当前版本的改进
我修复了一些错误,并添加了非线性求解器。Nelder-Mead 单纯形算法非常稳健和通用,尽管比某些梯度下降类型的算法慢。为了确保找到全局最小值,它可以使用不同的起始单纯形(单纯形是 n + 1 维的图形,例如,对于在二维空间中的搜索,单纯形将是一组 3 个二维点,即一个三角形,它包含潜在的解决方案)。
待办
对于数学家来说,某些例程的命名可能看起来很奇怪,我需要深入研究我的大学线性代数课程,并找到某些向量和矩阵运算的正确名称。