A delegate to a method that takes one complex as a parameter and returns a complex
Syntax
| Visual Basic (Declaration) | |
|---|
Public Delegate Function ComplexFunction1Cmp( _
ByVal value As Complex _
) As Complex |
Parameters
- value
Example
The following example uses the
ApplyFunction method in order to apply the
complex.Sqrt and
Math.Round functions to a complex matrix and it demonstrates also how to apply a custom function to the matrix's elements. The
ComplexFunction1Cmp,
ComplexFunction1Cmp1Int and
ComplexFunction2Cmp delegates are used to represent those functions.
| Visual Basic | Copy Code |
|---|
Imports System
Imports System.Numerics
Imports Bluebit.MatrixLibrary
Class Test
Public Shared Sub Main()
'Declaring a 4x4 CMatrix with random elements
Dim A As New CMatrix(4, 4)
A.FillRandom()
Console.WriteLine("CMatrix A:")
Console.WriteLine(A)
'Applying the Math.Sqrt function to A
Dim dlg1 As New ComplexFunction1Cmp(AddressOf SqrtFunction)
Dim B As CMatrix = A.ApplyFunction(dlg1)
'This can also be done without using the delegate
'Dim B As CMatrix = A.ApplyFunction(AddressOf SqrtFunction)
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 ComplexFunction1Cmp1Int(AddressOf ComplexRound)
Dim C As CMatrix = 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 ComplexFunction2Cmp(AddressOf MyCustomFunction1)
Dim D As CMatrix = A.ApplyFunction(dlg3, New Complex(0.5, 0.125))
'This can also be done without using the delegate
'Dim D As Matrix = A.ApplyFunction(AddressOf MyCustomFunction1, New complex(0.5, 0.125))
Console.WriteLine("D(i,k) = A(i,k) * A(i,k) + (0.5, 0.125i)")
Console.WriteLine(D)
Console.Read()
End Sub
Private Shared Function SqrtFunction(ByVal param1 As Complex) As Complex
Return Complex.Sqrt(param1)
End Function
Private Shared Function ComplexRound(ByVal param1 As Complex, ByVal digits As Integer) As Complex
Return New Complex(Math.Round(param1.Real, digits), Math.Round(param1.Imaginary, digits))
End Function
Private Shared Function MyCustomFunction1(ByVal param1 As Complex, ByVal param2 As Complex) As Complex
'param1 * param1 + param2
Return Complex.Add(Complex.Multiply(param1, param1), param2)
End Function
End Class
|
| C# | Copy Code |
|---|
using System;
using System.Numerics;
using Bluebit.MatrixLibrary;
class Test
{
static void Main(string[] args)
{
//Declaring a 4x4 CMatrix with random elements
CMatrix A = new CMatrix(4, 4);
A.FillRandom();
Console.WriteLine("Matrix A:");
Console.WriteLine(A);
//Applying the complex.Sqrt function to A
ComplexFunction1Cmp dlg1 = new ComplexFunction1Cmp(SqrtFunction);
CMatrix B = A.ApplyFunction(dlg1);
Console.WriteLine("CMatrix B now contains the square roots of A");
Console.WriteLine(B);
//Round the elements of matrix B to 2 decimal digits
ComplexFunction1Cmp1Int dlg2 = new ComplexFunction1Cmp1Int(ComplexRound);
CMatrix 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
ComplexFunction2Cmp dlg3 = new ComplexFunction2Cmp(MyCustomFunction1);
CMatrix D = A.ApplyFunction(dlg3, new Complex(0.5, 0.125));
Console.WriteLine("D(i,k) = A(i,k) * A(i,k) + (0.5, 0.125i)");
Console.WriteLine(D);
Console.Read();
}
static Complex SqrtFunction(Complex param1)
{
return Complex.Sqrt(param1);
}
static Complex ComplexRound(Complex param1, int digits)
{
return new Complex(Math.Round(param1.Real, digits), Math.Round(param1.Imaginary, digits));
}
static Complex MyCustomFunction1(Complex param1, Complex param2)
{
return param1 * param1 + param2;
}
}
|
| C++ | Copy Code |
|---|
#include "stdafx.h"
using namespace System;
using namespace System::Numerics;
using namespace Bluebit::MatrixLibrary;
ref class Test
{
public:
Complex static SqrtFunction(Complex param1)
{
return Complex::Sqrt(param1);
}
Complex static ComplexRound(Complex param1, int digits)
{
return Complex(Math::Round(param1.Real,digits),Math::Round(param1.Imaginary,digits));
}
Complex static MyCustomFunction1(Complex param1, Complex param2)
{
return param1 * param1 + param2;
}
};
int main(array<System::String ^> ^args)
{
//Declaring a 4x4 Matrix with random elements
CMatrix^ A = gcnew CMatrix(4, 4);
A->FillRandom();
Console::WriteLine("CMatrix A:");
Console::WriteLine(A);
//Applying the complex.Sqrt function to A
ComplexFunction1Cmp^ dlg1 = gcnew ComplexFunction1Cmp(&Test::SqrtFunction);
CMatrix^ B = A->ApplyFunction(dlg1);
Console::WriteLine("CMatrix B now contains the square roots of A");
Console::WriteLine(B);
//Round the elements of matrix B to 2 decimal digits
ComplexFunction1Cmp1Int^ dlg2 = gcnew ComplexFunction1Cmp1Int(&Test::ComplexRound);
CMatrix^ C = B->ApplyFunction(dlg2, 2);
Console::WriteLine("CMatrix C = B rounded to 2 decimal digits");
Console::WriteLine(C);
//Applying a custom function to matrix A
ComplexFunction2Cmp^ dlg3 = gcnew ComplexFunction2Cmp( &Test::MyCustomFunction1);
CMatrix^ D = A->ApplyFunction(dlg3, Complex(0.5, 0.125));
Console::WriteLine("D(i,k) = A(i,k) * A(i,k) + (0.5, 0.125i)");
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