A delegate to a method that takes one
double and one
integer parameter and returns a
double.
Syntax
Example
The following example uses the
ApplyFunction method in order to apply the
Math.Sqrt and
Math.Round functions to a matrix and it demonstrates also how to apply a custom function to the matrix's elements. The
DoubleFunction1Dbl,
DoubleFunction1Dbl1Int and
DoubleFunction2Dbl delegates are used to represent those functions.
| Visual Basic | Copy Code |
|---|
Imports System
Imports Bluebit.MatrixLibrary
Class Test
Public Shared Sub Main()
'Declaring a 4x4 Matrix with random elements
Dim A As New Matrix(4, 4)
A.FillRandom()
Console.WriteLine("Matrix A:")
Console.WriteLine(A)
'Applying the Math.Sqrt function to A
Dim dlg1 As New DoubleFunction1Dbl(AddressOf Math.Sqrt)
Dim B As Matrix = A.ApplyFunction(dlg1)
'This can also be done without using the delegate
'Dim B As Matrix = A.ApplyFunction(AddressOf Math.Sqrt)
Console.WriteLine("Matrix B now contains the square roots of A")
Console.WriteLine(B)
'Round the elements of matrix B to 2 decimal digits
Dim dlg2 As New DoubleFunction1Dbl1Int(AddressOf Math.Round)
Dim C As Matrix = B.ApplyFunction(dlg2, 2)
Console.WriteLine("Matrix C = B rounded to 2 decimal digits")
Console.WriteLine(C)
'Applying a custom function to matrix A
Dim dlg3 As New DoubleFunction2Dbl(AddressOf MyCustomFunction1)
Dim D As Matrix = A.ApplyFunction(dlg3, 1.527)
'This can also be done without using the delegate
'Dim D As Matrix = A.ApplyFunction(AddressOf MyCustomFunction1, 1.527)
Console.WriteLine("D(i,k) = A(i,k) * A(i,k) + 1.527")
Console.WriteLine(C)
Console.Read()
End Sub
Private Shared Function MyCustomFunction1(ByVal param1 As Double, ByVal param2 As Double) As Double
Return param1 * param1 + param2
End Function
End Class
|
| C# | Copy Code |
|---|
using System;
using Bluebit.MatrixLibrary;
class Test
{
static void Main(string[] args)
{
//Declaring a 4x4 Matrix with random elements
Matrix A = new Matrix(4, 4);
A.FillRandom();
Console.WriteLine("Matrix A:");
Console.WriteLine(A);
//Applying the Math.Sqrt function to A
DoubleFunction1Dbl dlg1 = new DoubleFunction1Dbl(Math.Sqrt);
Matrix B = A.ApplyFunction(dlg1);
Console.WriteLine("Matrix B now contains the square roots of A");
Console.WriteLine(B);
//Round the elements of matrix B to 2 decimal digits
DoubleFunction1Dbl1Int dlg2 = new DoubleFunction1Dbl1Int(Math.Round);
Matrix C = B.ApplyFunction(dlg2, 2);
Console.WriteLine("Matrix C = B rounded to 2 decimal digits");
Console.WriteLine(C);
//Applying a custom function to matrix A
DoubleFunction2Dbl dlg3 = new DoubleFunction2Dbl(MyCustomFunction1);
Matrix D = A.ApplyFunction(dlg3, 1.527);
Console.WriteLine("D(i,k) = A(i,k) * A(i,k) + 1.527");
Console.WriteLine(D);
Console.Read();
}
static double MyCustomFunction1(double param1, double param2)
{
return param1 * param1 + param2;
}
}
|
| C++ | Copy Code |
|---|
#include "stdafx.h"
using namespace System;
using namespace Bluebit::MatrixLibrary;
ref class Test
{
public:
double static MyCustomFunction1(double param1, double param2)
{
return param1 * param1 + param2;
}
};
int main(array<System::String ^> ^args)
{
//Declaring a 4x4 Matrix with random elements
Matrix^ A = gcnew Matrix(4, 4);
A->FillRandom();
Console::WriteLine("Matrix A:");
Console::WriteLine(A);
//Applying the Math.Sqrt function to A
DoubleFunction1Dbl^ dlg1 = gcnew DoubleFunction1Dbl(&Math::Sqrt);
Matrix^ B = A->ApplyFunction(dlg1);
Console::WriteLine("Matrix B now contains the square roots of A");
Console::WriteLine(B);
//Round the elements of matrix B to 2 decimal digits
DoubleFunction1Dbl1Int^ dlg2 = gcnew DoubleFunction1Dbl1Int(&Math::Round);
Matrix^ C = B->ApplyFunction(dlg2, 2);
Console::WriteLine("Matrix C = B rounded to 2 decimal digits");
Console::WriteLine(C);
//Applying a custom function to matrix A
DoubleFunction2Dbl^ dlg3 = gcnew DoubleFunction2Dbl(&Test::MyCustomFunction1);
Matrix^ D = A->ApplyFunction(dlg3, 1.527);
Console::WriteLine("D(i,k) = A(i,k) * A(i,k) + 1.527");
Console::WriteLine(D);
Console::Read();
return 0;
}
|
Requirements
Target Platforms: Windows 2000, Windows XP, Windows Server 2003 family, Windows Vista, Windows Server 2008 family, Windows 7
See Also