Archive for August, 2015

Using Boost Math Library in C#

How to access the functionallity in the boost libraries from the managed world is an interesting topic beeing addressed in the article. Apparantly, the cli compiler does not yet support the concurrency headers (like atomic). See This poses a problem when trying to make a managed wrapper. Another possible workaround is to build a unmanaged wrapper, in a static library, that does not expose any of the concurrency headers. Then the managed c++ wrapper assembly can link to the static library (creating a mixed mode assembly).



In this post I will share with you a lazy way to expose boost.math library to a C# project using C++/CLI. It is lazy and very wrong to do it this way, but I would like to share it with you anyway. I am doing this because of the following reasons:

  • Mixing different languages and frameworks is very difficult and error-prone. I have looked through tons of sites on ways to expose unmanaged functionality to the managed code, and apart from PInvoke, I have not found any stable and simple solution. Hopefully the step-by-step solution I am sharing may turn out to be helpful to someone one day.
  • My approach is by far not the best way to expose the Boost library to C#. But it is dead easy. I am aware of this, and if you know a better way – please let me know by contributing to this post.

View original post 790 more words

Categories: Uncategorized

The Wolf, the Goat and the Cabbage

This is the story of the Wolf, the Goat and the Cabbage:

Sailor Cat needs to bring a wolf, a goat, and a cabbage across the river.
The boat is tiny and can only carry one passenger at a time.
If he leaves the wolf and the goat alone together, the wolf will eat the goat.
If he leaves the goat and the cabbage alone together, the goat will eat the cabbage.
How can he bring all three safely across the river?

Try it out here. It is not that hard to find the solution with a bit of trial and error. Writing a program that searches for the solutions is a fun excerise. This is my result in haskell.

import Control.Monad
import Data.List

data Item = Goat | Wolf | Salad | Empty deriving (Show, Eq)

solve = mover [Goat,Wolf,Salad] [] []
        mover [] _ _ = return []
        mover left right boat = do 
                        -- unload boat
                        let left' = left ++ boat
                        -- select what to move
                        x <- left  
                        -- load boat
                        let boat' = [x]
                        let left'' = left' \\ boat'
                        -- verify that we can leave
                        guard $ compatible left''
                        -- handle next step
                        map (x:) (movel left'' right boat')

        movel [] _ _ = return []
        movel left right boat = do 
                        -- unload boat
                        let right' = right ++ boat
                        -- select what to move
                        if compatible right' then
                            -- no problem leaving all on the right side. lets just go back empty
                            map (Empty:) (mover left right' [])
                        else do
                            -- we have a problem. pick one to move back to the left
                            y <- right  
                            -- load boat
                            let boat' = [y]
                            let right'' = right' \\ boat'
                            -- verify that we can leave
                            guard $ compatible right''
                            -- handle next step
                            map (y:) (mover left right'' boat')

        compatible [Goat,Wolf] = False
        compatible [Wolf,Goat] = False
        compatible [Salad,Goat] = False
        compatible [Goat,Salad] = False
        compatible _ = True
Categories: Uncategorized