Click here to Skip to main content
13,458,837 members
Click here to Skip to main content
Add your own
alternative version


26 bookmarked
Posted 8 Jul 2003

A VB.NET class useful for basic linear algebra

, 2 Sep 2003
Rate this:
Please Sign up or sign in to vote.
Some vector and matrix manipulation routines.


An important omission in .NET is numerical computation. There are some VB libraries out there for this purpose, but because I had already converted a few Numerical Recipes routines from C to VB4 years ago, and in order to have tight integration with my Color class, I decided to upgrade these old routines to VB.NET. It was only a small effort to enhance the class with some basic matrix and vector algebra routines, especially with the possibility of overloading in VB.NET.


Some knowledge about linear algebra and numerical computing comes in handy.


Just as my Color class, this class is not written for speed, instead I hope it is clear and easy to read, and to extend (hint). Note, however that the routines translated from 'Numerical Recipes in C' are NOT very readable and shouldn't be changed (I just know they work, don't ask me how)! There are 3 basic building blocks: a scalar (single), a vector (1 dimensional array of single) and a matrix (2 dimensional array of single). Some of the features of the class are:

  • Singular value decomposition based solver, adapted from 'Numerical Recipes in C'. Can solve over-determined systems of linear equations in a least-squares sense.
  • Non-linear optimization based solver, adapted from the Nelder-Mead simplex algorithm in 'Numerical recipes in C'. Can find the parameters that minimizes a user-supplied function. See ImproveRGBTosRGBTransform in the Color class for an example of how to use this function.
  • Vector norm
  • Element-wise addition, subtraction and scaling of vectors and matrices
  • Sum and mean of elements of a vector or columns of a matrix
  • Maximum and minimum of the elements of a vector or a matrix
  • Transpose of a matrix
  • Scalar product of a vector-matrix product
  • Matrix, vector-matrix and matrix-vector product
  • Extraction of sub-matrix
  • Getting and settings row and columns of matrix using a vector
  • File IO of matrices and vectors
  • ToString for matrices and vectors

I realize that this is by no means a complete BLAS class, I just implemented what I needed. I just hope it is helpful to somebody, maybe as a starting class for more advanced functionality (Please let me now, so I can post enhancements back on the website)

Code example

Here is a piece from the Color class that actually solves 3 sets of linear equations at once (one for each R, G and B color component), resulting in a set of equations that can be used to transform color triplets from one color space to another, e.g. for calibration purposes. Note that although the set of equations is linear with regards to the unknown coefficients, it can be highly non-linear with regards to the input R, G and B values, depending on the value of iTransformType which determines the type of polynome in R, G and B that will be used, e.g. the third-order polynome 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

Improvements in current version

I have removed some bugs, and added the non-linear solver. The Nelder-Mead simplex algorithm is very robust and general, albeit slower than some gradient descent type algorithms. In order to be sure to find a global minimum, it can be restarted with a different starting simplex (a simplex is n + 1 dimensional figure, e.g. for a search in 2 dimensional space, a simplex would be a set of 3 2D points, i.e. a triangle, that encloses the potential solution).

To do

The naming of some of the routines may seem bizarre to mathematicians, I need to dig into my university linear algebra courses and find the proper names for some of the operations on vectors and matrices.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Software Developer (Senior)
Belgium Belgium
Physicist, Biomedical Engineer, Phd in engineering. Specific expertise is in medical photography and it's related image processing, and in colourimetry.

You may also be interested in...


Comments and Discussions

GeneralHelp me to solve this task Pin
jawaharraj896-Feb-12 23:16
memberjawaharraj896-Feb-12 23:16 
GeneralRe: Help me to solve this task Pin
yvdh6-Feb-12 23:33
memberyvdh6-Feb-12 23:33 
GeneralRe: Help me to solve this task Pin
jawaharraj897-Feb-12 19:45
memberjawaharraj897-Feb-12 19:45 
GeneralRe: Help me to solve this task Pin
yvdh7-Feb-12 23:40
memberyvdh7-Feb-12 23:40 
Generalwith user interface Pin
sajedxvc28-Dec-09 6:12
membersajedxvc28-Dec-09 6:12 
GeneralRe: with user interface Pin
yvdh29-Dec-09 0:37
memberyvdh29-Dec-09 0:37 
Questionhelp Pin
Crispis8216-Dec-07 23:25
memberCrispis8216-Dec-07 23:25 
GeneralNew release: the TOMNET optimization environment for .NET Pin
PER900014-Mar-07 4:20
memberPER900014-Mar-07 4:20 
GeneralRe: New release: the TOMNET optimization environment for .NET Pin
yvdh14-Mar-07 5:02
memberyvdh14-Mar-07 5:02 
GeneralProject Factors Pin
Matthew Holton8-Nov-06 19:00
memberMatthew Holton8-Nov-06 19:00 
GeneralNice Work Pin
VickyC#2-May-06 15:33
memberVickyC#2-May-06 15:33 
GeneralRe: Nice Work Pin
yvdh2-May-06 21:27
memberyvdh2-May-06 21:27 
GeneralRe: Nice Work [modified] Pin
Peter Mortensen4-Dec-08 2:28
memberPeter Mortensen4-Dec-08 2:28 
GeneralDoesn't work for me =/ Pin
rioman3-Jun-05 2:36
memberrioman3-Jun-05 2:36 
GeneralRe: Doesn't work for me =/ Pin
yvdh3-Jun-05 3:50
memberyvdh3-Jun-05 3:50 
GeneralRe: Doesn't work for me =/ Pin
rioman3-Jun-05 4:18
memberrioman3-Jun-05 4:18 
GeneralRe: Doesn't work for me =/ Pin
yvdh3-Jun-05 5:32
memberyvdh3-Jun-05 5:32 
GeneralRe: Doesn't work for me =/ Pin
rioman5-Jun-05 21:06
memberrioman5-Jun-05 21:06 
GeneralRe: Doesn't work for me =/ Pin
Anonymous4-Jun-05 3:53
sussAnonymous4-Jun-05 3:53 
GeneralAwesome Pin
Toni Danza24-Nov-03 15:28
sussToni Danza24-Nov-03 15:28 
GeneralAn improvement Pin
dog_spawn3-Sep-03 6:30
memberdog_spawn3-Sep-03 6:30 
GeneralRe: An improvement Pin
yvdh3-Sep-03 12:39
memberyvdh3-Sep-03 12:39 
GeneralRe: An improvement Pin
dog_spawn5-Sep-03 12:34
memberdog_spawn5-Sep-03 12:34 
GeneralRe: An improvement Pin
yvdh6-Sep-03 3:36
memberyvdh6-Sep-03 3:36 
Generalmissing download link Pin
valko17-Jul-03 6:19
membervalko17-Jul-03 6:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180323.1 | Last Updated 3 Sep 2003
Article Copyright 2003 by yvdh
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid