# VB Apply Transform Example

← All NMath Code Examples

```Imports System

Imports CenterSpace.NMath.Core

Namespace CenterSpace.NMath.Core.Examples.VisualBasic

' A .NET example in Visual Basic showing how to use the matrix and vector Apply() and Transform()
' methods.
Module ApplyTransformExample

Sub Main()

Dim length As Integer = 4

Console.WriteLine()

' Create a vector containing 0, PI/2, PI, 3PI/2, 2PI
Dim v As New DoubleVector(length, 0, Math.PI / 2.0)
Console.WriteLine("v = {0}", v.ToString())

' Use the Apply method to create a new vector containing the
' cosines of the elements of v.
Dim cosV As DoubleVector = v.Apply(NMathFunctions.CosFunc)

' We should get cosV = [1 0 -1 0]
Console.WriteLine("cosV = {0}", cosV.ToString())

' Hmmm. Where there is supposed to zeros there are numbers
' on the order of 10^-16 and 10^-16 - which are zero, within
' machine precision.  Lets use the vector method Transform to
' round these values to what they are supposed to be.
Dim roundFunction As Func(Of Double, Double) = AddressOf Math.Round
cosV.Transform(roundFunction)
Console.WriteLine("cosV rounded = {0}", cosV.ToString())   ' [1 0 -1 0]

' The apply and transform functions are overloaded to take binary
' function delegates.
Dim baseValues As New FloatComplexMatrix(2, 2, New FloatComplex(1), New FloatComplex(1))
Dim exponents As New FloatComplexMatrix(2, 2, New FloatComplex(1), New FloatComplex(1))

' baseValues = | (1,0) (3,0) |
'              | (2,0) (4,0) |
'
' exponents =  | (1,0) (3,0) |
'              | (2,0) (4,0) |
Console.WriteLine("baseValues = {0}", baseValues.ToString())
Console.WriteLine("exponents = {0}", exponents.ToString())

Dim powerMatrix As FloatComplexMatrix = baseValues.Apply(NMathFunctions.FloatComplexPowFunc, exponents)

' powerMatrix = | (1,0)^(1,0) (3,0)^(3,0) | = | (1,0) (27,0) |
'               | (2,0)^(2,0) (4,0)^(4,0) | = | (4,0) (256,0) |
Console.WriteLine("baseValues^exponents = {0}", powerMatrix.ToString())

Dim Sums As FloatComplexVector = baseValues.ApplyColumns(NMathFunctions.FloatComplexSumFunc)
Console.WriteLine("sums of baseValues columns = {0}", Sums.ToString())

Console.WriteLine()
Console.WriteLine("Press Enter Key")