Page 1 of 1

MathNet.Numerics

PostPosted: Wed Oct 17, 2012 4:40 pm
by yledu
I've just discovered Cobra, and I currently mostly use C, Python, Julia and Matlab for my computational work. I find it quite hard to do simple things with Cobra, mostly because I miss the mathematical apparatus and tend to get lost in the underscores, var, get, share and all that. But it's fun hacking through the thickets.

So, I do understand the benefits of quality coding put forward by Cobra, and I kind of feel it's right, but I also wanted to test some simple code to see how fast Cobra was on numerics; I use Mono 2.11.4 on Mac OS 10.7.5, and 2.10.8.1 on Linux Ubuntu 12.04.

I read that Cobra could use .NET/MONO libraries, so I tried to do something with MathNet.Numerics (which is quite complete and very well documented); I downloaded the binaried (dll, XML and pdb) and put those in the same directory as my program, and wrote some simple programs, like drawing normally distributed random numbers, after having a look at how MathNet does it in C#: https://github.com/mathnet/mathnet-numerics/blob/master/src/Examples/ContinuousDistributions/NormalDistribution.cs.

However, I didn't manage to make the following C# code snipped work in Cobra, it fails on 'take' :

Code: Select all
var normal = Normal.WithMeanPrecision(1, .2);
normal.RandomSource = new MersenneTwister();
var samples = normal.Samples().Take(1000000).ToArray();


which I (badly) translated to:

normal = Distributions.Normal.withMeanPrecision(1, 0.2)
normal.randomSource = MathNet.Numerics.Random.MersenneTwister()
samples = normal.samples.take(1000000).toArray


The 'take' part fails:

Code: Select all
randomNet.cobra(35): error: Cannot find a definition for "take" in "normal.samples" whose type is "IEnumerable<of float>?".


I also don't understand how to put myself in the right namespace and avoid typing 'MathNet.Numerics.', I tried @ref but it fails (deprecated?), and 'use' doesn't help for that (the compiler still mentions ambiguity of Random)

Also, ss there iome more algorithmic stuff somewhere in the same spirit as the sorting algorithms in the howto?

Once on the right track, I'll code the Julia microbenchmarks in Cobra and see what comes out.

Best,

Yann

Re: MathNet.Numerics

PostPosted: Wed Oct 17, 2012 8:34 pm
by jaegs
Take is a LINQ method right? C# extension methods and supporting LINQ is still somewhat of a todo in Cobra.

Re: MathNet.Numerics

PostPosted: Wed Oct 17, 2012 8:49 pm
by Charles
-- Re: .take and other extension methods, it's high on my to-do list, but not done. Here is an implementation you can slap in your code for now (untested):

extend IEnumerable<of T>

def take(count as int) as T*
require count >= 0
i = 0
for item in this
if i == count, break
yield item
i += 1


-- Re: namespaces, I think you want this:

use MathNet.Numerics


-- Re: additional algo samples, no. Your main sources for Cobra code are:

http://cobra-language.com/how-to/
http://cobra-language.com/samples/
http://cobra-language.com/trac/cobra/wi ... edProjects
http://cobra-language.com/trac/cobra/br ... obra/trunk

-- I prefer to write "1000000" as "1_000_000". Same number, but easier on the eyes, especially when looking back at code at a later date.