The Boost Library; Overview, Code, Applications

Postby admin » Sat Aug 22, 2009 3:29 pm

Chi-Squared Example Excel Add-in



Earlier we wrote a simple console C# example application that used a small C++/CLI wrapper to calculate the Chi-Squared distribution using the boost library. The output was then sent to an Excel sheet. The advantage was that we could combine functionality available in boost (C++) with the ease of programming in C#.



We decided to convert this example into an Excel COM add-in. Making it as COM add-in has as advantage that it runs inside Excel and thus can create the Excel sheet much faster as there are no out-of-process calls needed anymore. And the COM add-in still has the advantage that you can use fast C++ code.



Instead of making the COM add-in in C++/ATL, we decided to make the COM add-in in C# using the ?Shared Add-in? project wizard. Creating a COM add-in in C++ is difficult and error-prone. Using the C# ?Shared Add-in? project wizard is much easier.



The wizard generates a Connect class that implements the IExtensibility2 interface required for COM add-ins. The setup project that is also generated by the wizard, handles registering the add-in in the registry.

In the OnConnection() method of the generated Connect class, we add code to create a menu item that calls our callback function (In .NET this is handled by a delegate). In the OnDisconnection() method we add code that removes the menu item we created in the OnConnection() method. The menu item is in Excel 2007 available in the ?Add-In? ribbon menu.



In the callback function we added to the C# add-in we use the same C++/CLI wrapper to calculate the Chi-Squared distribution using the boost library. Then the results are written by the C# add-in directly to a new Excel sheet.



To finish this example, we wanted to create an Excel sheet were you enter all the required parameters. Then a button on the Excel sheet calls our COM add-in function. Thus our add-in must also expose a function callable by an Excel VBA macro. We do this by creating a COM interface and let the generated Connect class implement that interface. The function in the interface is now visible from Excel VBA.



So the VBA macro behind the button on the sheet passes the proper cell values to a COM add-in written in C#. The C# COM add-in then uses a tiny wrapper written in C++/CLI to call native C++ functionality.



The setup will install the Excel add-in and there is a start menu entry installed for the Excel document described above. Also a Word document is installed with this text and the source code for the Connect class. Note that it uses calls to general utility code that is not included in the document.



Note that Excel 2007 is required.
Attachments
CsExcelAddInSetup.zip
C# Excel add-in. Needs Excel 2007.
(409.72 KiB) Downloaded 1430 times
ExcelSheet.png
The Excel sheet that call the COM add-in through a VBA macro attached to the button.
ExcelSheet.png (65.36 KiB) Viewed 32546 times
admin
Site Admin
 
Posts: 237
Joined: Thu Dec 07, 2006 4:03 pm
Location: Amsterdam, The Netherlands

Postby Cuchulainn » Sun Oct 11, 2009 4:14 pm

The boost Signals library is in fact a v ery flexible implementation of the Observer patterns. Signals are publishers and slots are subscribers. Here is an example using Bind and Lambda.
Attachments
TestBindAndlambda.cpp
(1.67 KiB) Downloaded 988 times
User avatar
Cuchulainn
 
Posts: 669
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Tue Dec 22, 2009 8:10 pm

multi array example 101
Attachments
Main.cpp
(6.23 KiB) Downloaded 1039 times
User avatar
Cuchulainn
 
Posts: 669
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Sat Jan 02, 2010 1:40 pm

Updated TOC forthcoming Boost book
Attachments
BOOST_TOC.docx
(11.02 KiB) Downloaded 873 times
BOOST_TOC.doc
(23.5 KiB) Downloaded 907 times
User avatar
Cuchulainn
 
Posts: 669
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Fri Jan 15, 2010 9:59 am

Using named parameters as input arguments to avoid 'cross wiring'. This is the Parameter library.
Attachments
TestParam101.cpp
(1.08 KiB) Downloaded 830 times
User avatar
Cuchulainn
 
Posts: 669
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Mon Feb 15, 2010 2:36 pm

Somewhere in between doing it in C and a full Spirit compiler version, it might be useful to investigate the boost String Algo library (which has lots of stuff for string and text editing). In this case we take a ANY_CHAR-separated string and turn it into a vector (I leave the fopen part since that is easy). I like it because it is a one-liner and I do not want to do it myself (anymore)



If I remember properly, Spirit also has a predefined action for this operation??

#include <string>

#include <vector>

#include <iostream>



// Boost libraries

#include <boost/algorithm/string/classification.hpp>

#include <boost/algorithm/string/split.hpp>



using namespace std;

using namespace boost;



int main()

{



// Read in 'character-separated' string and extract array.

std::string sA("1,2,3,4/5/9*56");

vector<string> splitArray;

split(splitArray, sA, is_any_of(",/*"));

for (int j = 0; j < splitArray.size(); ++j)

{ // prints all the vector elements



cout << splitArray[j] << ",";

}



return 0;

}
User avatar
Cuchulainn
 
Posts: 669
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Mon Feb 15, 2010 2:43 pm

A wee bit higher level than String Algo is to use boost Tokenizer. Here is a generic extractor (in main() you will need exception handler).





typedef boost::tokenizer<boost::escaped_list_separator<char> > Tokenizer;

typedef boost::tokenizer<boost::char_separator<char> > Tokenizer2;



template <typename T>

vector<T> extract(const string& Input)

{

vector<T> result;



Tokenizer2 myToken(Input);



for (Tokenizer2::iterator it = myToken.begin(); it != myToken.end(); ++it)

{

result.push_back(boost::lexical_cast<T>(*it));

}



return result;

}





///

string s(" aa bb");

string s2("11, 23.9");

vector<double> vec;



// Convert strings to doubles, exception

try

{

vec = extract<double>(s);

}

catch(boost::bad_lexical_cast& error)

{

std::cout << error.what() << endl;

}
User avatar
Cuchulainn
 
Posts: 669
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Sat Apr 03, 2010 12:33 pm

Here is the algorithms to compute the probability of finding real roots of ax^2 + bx + c = 0 where a, b and c are N(0,1).
Attachments
EdelKost.cpp
(836 Bytes) Downloaded 874 times
User avatar
Cuchulainn
 
Posts: 669
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Fri Apr 09, 2010 9:29 pm

Code for my Quantnet.org blog
Attachments
TestExponentialApprox.cpp
(3.64 KiB) Downloaded 849 times
User avatar
Cuchulainn
 
Posts: 669
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Mon May 03, 2010 1:45 pm

Code for the Quantnet blog on multiarrays
Attachments
MyFirstMultiArray2.cpp
(2.64 KiB) Downloaded 892 times
User avatar
Cuchulainn
 
Posts: 669
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby admin » Mon Dec 27, 2010 8:14 pm

Bessel function taxonomy.
Attachments
TestBessel.cpp
(2 KiB) Downloaded 890 times
admin
Site Admin
 
Posts: 237
Joined: Thu Dec 07, 2006 4:03 pm
Location: Amsterdam, The Netherlands

Postby Cuchulainn » Thu Apr 07, 2011 1:32 pm

User avatar
Cuchulainn
 
Posts: 669
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Thu Jun 23, 2011 4:37 pm

Some high level categories (reference card, kind of)
Attachments
Figure 5.1 Essential Libraries.pdf
(4.41 KiB) Downloaded 841 times
Figure 5.2 Supporting Libraries.pdf
(4.05 KiB) Downloaded 833 times
User avatar
Cuchulainn
 
Posts: 669
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Tue Aug 30, 2011 9:06 pm

LU decomposition using uBLAS. Please feel free to kick the tyres for performance and accuracy.
Attachments
TestLUDecomposition.cpp
(8.56 KiB) Downloaded 970 times
User avatar
Cuchulainn
 
Posts: 669
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby admin » Wed Mar 14, 2012 8:10 pm

admin
Site Admin
 
Posts: 237
Joined: Thu Dec 07, 2006 4:03 pm
Location: Amsterdam, The Netherlands

PreviousNext

Return to C++

Who is online

Users browsing this forum: No registered users and 1 guest

cron