Data Analysis Mathematics Linear Algebra Statistics
New Version 8.1!

Supports .NET 6.0. Try it for free with our fully functional 60-day trial version. QuickStart Samples

# Vector Operations QuickStart Sample (Visual Basic)

Illustrates how to perform operations on Vector objects, including construction, element access, arithmetic operations in Visual Basic.

```Option Infer On

' The Vector class resides in the Extreme.Mathematics.LinearAlgebra namespace.
Imports Extreme.Mathematics.LinearAlgebra
' The delegate classes reside in the Extreme.Mathematics
' namespace.
Imports Extreme.Mathematics

Namespace Extreme.Numerics.QuickStart.VB
' Illustrates operations on Vector objects from the
' Extreme.Mathematics.LinearAlgebra namespace of the Extreme Optimization
' Numerical Libraries for .NET.
Module VectorOperations

Sub Main()
' For details on the basic workings of Vector
' objects, including constructing, copying and
' cloning vectors, see the BasicVectors QuickStart
' Sample.
'
' Let's create some vectors to work with.
Dim v1 = Vector.Create(New Double() {1, 2, 3, 4, 5})
Dim v2 = Vector.Create(New Double() {1, -2, 3, -4, 5})
Dim v3 = Vector.Create(New Double() {3, 2, 1, 0, -1})
' This one will hold results.
Dim v As Vector(Of Double)

'
' Vector Arithmetic
'
' The Vector class defines static methods for
Console.WriteLine("v1 = {0:F4}", v1)
Console.WriteLine("v2 = {0:F4}", v2)
Console.WriteLine("Basic arithmetic:")
v = Vector.Negate(v1)
Console.WriteLine("-v1 = {0:F4}", v)
Console.WriteLine("v1 + v2 = {0:F4}", v)
v = Vector.Subtract(v1, v2)
Console.WriteLine("v1 - v2 = {0:F4}", v)
' Vectors can only be multiplied or divided by
' a real number. For dot products, see the
' DotProduct method.
v = Vector.Multiply(5, v1)
Console.WriteLine("5 * v1 = {0:F4}", v)

' You can also apply these methods to Vector objects.
' In this case, they change the first operand.
Console.WriteLine("v3 = {0:F4}", v3)
' Note that this is different from the += operator!
' The += operator creates a new Vector object,
' whereas the Add method above does not.
Console.WriteLine("v3+v1 -> v3 = {0:F4}", v3)
' This method is overloaded so you can directly
Console.WriteLine("v3-2v1 -> v3 = {0:F4}", v3)
Console.WriteLine()

'
' Norms, dot products, etc.
'
Console.WriteLine("Norms, dot products, etc.")
' The dot product is calculated in one of two ways:
' Using the static DotProduct method:
Dim a As Double = Vector.DotProduct(v1, v2)
' Or using the DotProduct method on one of the two
' vectors:
Dim b As Double = v1.DotProduct(v2)
Console.WriteLine("DotProduct(v1, v2) = {0} = {0:F4}", _
a, b)
' The Norm method returns the standard two norm
' of a Vector:
a = v1.Norm()
Console.WriteLine("|v1| = {0}", a)
' .the Norm method is overloaded to allow other norms,
' including the one-norm:
a = v1.Norm(1)
Console.WriteLine("one norm(v1) = {0}", a)
' ...the positive infinity norm, which returns the
' absolute value of the largest component:
a = v1.Norm(Double.PositiveInfinity)
Console.WriteLine("+inf norm(v1) = {0}", a)
' ...the negative infinity norm, which returns the
' absolute value of the smallest component:
a = v1.Norm(Double.NegativeInfinity)
Console.WriteLine("-inf norm(v1) = {0}", a)
' ...and even the zero norm, which simply returns
' the number of components of the vector:
a = v1.Norm(0)
Console.WriteLine("zero-norm(v1) = {0}", a)
' You can get the square of the two norm with the
' NormSquared method.
a = v1.NormSquared()
Console.WriteLine("|v1|^2 = {0}", a)
Console.WriteLine()

'
' Largest and smallest elements
'
' The Vector class defines methods to find the
' largest or smallest element or its index.
Console.WriteLine("v2 = {0:F4}", v2)
' The Max method returns the largest element:
Console.WriteLine("Max(v2) = {0}", v2.Max())
' The AbsoluteMax method returns the element with
' the largest absolute value.
Console.WriteLine("Absolute max(v2) = {0}", _
v2.AbsoluteMax())
' The Min method returns the smallest element:
Console.WriteLine("Min(v2) = {0}", v2.Min())
' The AbsoluteMin method returns the element with
' the smallest absolute value.
Console.WriteLine("Absolute min(v2) = {0}", _
v2.AbsoluteMin())
' Each of these methods has an equivalent method
' that returns the zero-based index of the element
' instead of its value, for example:
Console.WriteLine("Index of Min(v2) = {0}", _
v2.MinIndex())

' Finally, the Map method lets you apply
' an arbitrary function to each element of the
' vector: