Extending finaquant's .NET libraries with ILNumerics

General topics that don't fit into other forum categories

Extending finaquant's .NET libraries with ILNumerics

Postby finaquant » 19 Dec 2013, 22:58

ILNumerics is a .NET library for math including functions for statistics, machine learning and data visualization.

ILNumerics doesn't have the Table Functions of finaquant libraries finaquant® protos (non-commercial) or finaquant® calcs (commercial, and finaquant libraries don't have many functions of ILNumerics.

So it makes sense to combine finaquant with ILNumerics if you need Table Functions, plus many other matrix/vector functions of ILNumerics that are not included in finaquant libraries.

You may find below some C#/.NET code examples that illustrate:
  1. How to convert an ILNumerics matrix to a finaquant matrix, and vice versa
  2. How to write extend matrix functions of finaquant using methods in ILNumerics

It is quite easy to install ILNumerics with its NuGet package into a Visual Studio file (see Quick Start). Additionally, you will need two assembly references, FinaquantCalcs and CryptoLicensing for finaquant® calcs.

Image

Converting matrices from ILNumerics to Finaquant, and vice versa
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. // create a matrix A, give values explicitely
  2. ILArray<double> A = array<double>(
  3.         new double[] { 1, 1, 1, 1, 1, 2, 3, 4, 1, 3, 6, 10, 1, 4, 10, 20 }, 4, 4);
  4.  
  5. // use a creation function for B & C
  6. ILArray<double> B = counter(4, 2);
  7.  
  8. // use a function of the base class: ILMath.linsolve
  9. ILArray<double> R = linsolve(A, B);
  10.  
  11. // A.ToString() gives formated output
  12. Console.Out.WriteLine("A: " + Environment.NewLine + A.ToString());
  13. Console.Out.WriteLine("B: " + Environment.NewLine + B.ToString());
  14. Console.Out.WriteLine("R such that A * R = B: " + Environment.NewLine + R.ToString());
  15.  
  16. // check result:
  17. // uses norm, multiply, eps and binary operators
  18. if (norm(multiply(A, R) - B) <= eps)
  19. {
  20.     Console.Out.WriteLine("Result ok");
  21. }
  22. else
  23. {
  24.     Console.Out.WriteLine("Result false");
  25. }
  26.  
  27. // conversion: ILNumerics --> Finaquant
  28. KeyMatrix R_FQ = ConvertILNumToFQ(R);
  29. KeyMatrix B_FQ = ConvertILNumToFQ(B);
  30. KeyMatrix C_FQ = ConvertILNumToFQ(C);
  31.  
  32. Console.Out.WriteLine("R_FQ = \n" + R_FQ);
  33. Console.Out.WriteLine("B_FQ = \n" + B_FQ);
  34.  
  35. // conversion: Finaquant --> ILNumerics
  36. Console.Out.WriteLine("R_ILNum = \n" + ConvertFQtoILNum(R_FQ));
  37. Console.Out.WriteLine("B_ILNum = \n" + ConvertFQtoILNum(B_FQ));
GeSHi ©


Conversion methods:
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. /// Convert matrix from Finaquant to ILNumerics type
  2. public static ILArray<double> ConvertFQtoILNum(KeyMatrix M)
  3. {
  4.     // check input parameter
  5.     if (M == null || M.IsEmpty)
  6.         throw new Exception("ConvertFQtoILNum: Null-valued or empty input matrix M!");
  7.     try
  8.     {
  9.         // get row and column size
  10.         int RowCount = M.nRows;
  11.         int ColumnCount = M.nCols;
  12.  
  13.         // convert matrix type from Finaquant to IlNumerics
  14.         return array<double>(KeyMatrix.MatrixToVector(M, RowColDirection.ColByCol).toArray, RowCount, ColumnCount);
  15.     }
  16.     catch (Exception ex)
  17.     {
  18.         throw new Exception("ConvertFQtoILNum: " + ex.Message);
  19.     }
  20. }
  21. /// Convert matrix from ILNumerics to Finaquant type
  22. public static KeyMatrix ConvertILNumToFQ(ILArray<double> M)
  23. {
  24.     // check input parameter
  25.     if (M == null || M.IsEmpty)
  26.         throw new Exception("ConvertILNumToFQ: Null-valued or empty input matrix M!");
  27.     if (! M.IsMatrix)
  28.         throw new Exception("ConvertILNumToFQ: Input array M must be a matrix!");
  29.     if (M.S.NumberOfDimensions != 2)
  30.         throw new Exception("ConvertILNumToFQ: Input array M must be a 2-dimensional matrix!");
  31.     try
  32.     {
  33.         // get row and column size
  34.         int RowCount = M.S.ToIntArray()[0];
  35.         int ColumnCount = M.S.ToIntArray()[1];
  36.  
  37.         // convert matrix type from IlNumerics to Finaquant
  38.         return OneDimArrayToMatrix(M.ToArray(), RowCount, ColumnCount);
  39.     }
  40.     catch (Exception ex)
  41.     {
  42.         throw new Exception("ConvertILNumToFQ: " + ex.Message);
  43.     }
  44. }
  45. /// Convert 1-dimensional array to 2-dimensional KeyMatrix
  46. private static KeyMatrix OneDimArrayToMatrix(double[] Array, int RowCount, int ColumnCount)
  47. {
  48.     // PARAMETER CHECKS
  49.     if (Array == null || Array.Length == 0)
  50.         throw new Exception("OneDimArrayToMatrix: Null or empty input array!\n");
  51.  
  52.     if (RowCount < 1 || ColumnCount < 1)
  53.         throw new Exception("OneDimArrayToMatrix: Zero or negative row/column counts!\n");
  54.  
  55.     if (RowCount * ColumnCount != Array.Length)
  56.         throw new Exception("OneDimArrayToMatrix: Improper array size w.r.t. given row and column counts!\n");
  57.  
  58.     // construct matrix
  59.     KeyMatrix M = KeyMatrix.CreateConstantMatrix(RowCount, ColumnCount);
  60.  
  61.     for (int i = 0; i < ColumnCount; i++)
  62.     {
  63.         for (int j = 0; j < RowCount; j++)
  64.         {
  65.             M[j, i] = Array[i * RowCount + j];
  66.         }
  67.     }
  68.     return M;
  69. }
GeSHi ©


Writing an extension method for finaquant's KeyMatrix type
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. /// Extensions methods for KeyMatrix object, based on methods of ILNumerics
  2. public static class ILNum_KeyMatrix_Extensions
  3. {
  4.     /// Hyperbolic sine of all matrix elements
  5.     public static KeyMatrix ILNum_Sinh(this KeyMatrix M)
  6.     {
  7.         try
  8.         {
  9.             ILArray<double> M_ILNum = Test.ConvertFQtoILNum(M);
  10.  
  11.             return Test.ConvertILNumToFQ(ILMath.sinh(M_ILNum));
  12.         }
  13.         catch (Exception ex)
  14.         {
  15.             throw new Exception("ILNum_Sinh: " + ex.Message);
  16.         }
  17.     }
  18. }
GeSHi ©


Calling the extension method:
Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. // call extension method of KeyMatrix
  2. Console.Out.WriteLine("Sinh(B_FQ) = \n" + B_FQ.ILNum_Sinh());
GeSHi ©
User avatar
finaquant
 
Posts: 61
Joined: 16 Sep 2013, 18:37

Return to General

Who is online

Users browsing this forum: No registered users and 2 guests

cron