help with porting code from Math.NET Iridium to Math.NET Numerics

Jul 1, 2013 at 1:56 PM
Edited Jul 1, 2013 at 1:59 PM
hi, I am a developer and need to port some code from Math.NET Iridium to Math.NET Numerics.

I have some knowledge of maths, but have not really used it for some time.

Perhaps someone could have a look at the following, to see if it makes sense.

or is there a guide on how the Math.NET Numerics API compares to Math.NET Iridium.

note: I need to use double, so to use Complex64 I need to use the Math.NET Numerics portable solution.

any tips would be greatly appreciated !

1a) old code:

var cft = ComplexFourierTransformation(TransformationConvention.Matlab);
Complex[] array = {}; //... numbers
cft.TransformForward(array);

1b) replace with:

Complex[] array = {}; //... numbers
Transform.FourierForward(array);

2a) old code:

var cft = ComplexFourierTransformation(TransformationConvention.Matlab);
Complex[] array = {}; //... numbers
cft.TransformBackward(array);

2b) replace with:

Complex[] array = {}; //... numbers
Transform.FourierInverse(array);

3a) old code:

double real = 1;
double imag = 2;
var cpx = new Complex( real, imag );
double real2 = cpx.Real;
double imag2 = cpx.Imag;

3b) replace with:

double real = 1;
double imag = 2;
var cpx = new Complex64( real, imag );
double real2 = cpx.Real;
double imag2 = cpx.Imaginary;
thanks !
Sean
Coordinator
Jul 2, 2013 at 12:15 AM
Hi Sean

Looks quite good already.

The original code specified MATLAB conventions. Assuming you want the same behavior, you should declare that in Math.NET Numerics as well. Both FourierForward and FourierInverse have overloads that accept a FourierOptions parameter. FourierOptions.Matlab is the equivalent one.

Then, regarding complex number types: We generally use the existing double-precision type Complex from the System.Numerics assembly from the official .Net Base Class Libraries. However, System.Numerics does not contain an equivalent single-precision type, that's why we provide our own Complex32 type. Unfortunately System.Numerics is not available in one of the portable library configurations at all, that's why in the portable library we also provide a replacement Complex type as well - but not in the full library.

We do not recommend to use the portable library in scenarios where the full library is supported. But you may need to add a reference to System.Numerics to get the Complex type in .Net 4 projects.

Not sure where the Complex64 type you mentioned comes from? There is no such type in Math.NET Numerics.

Thanks,
Christoph
Marked as answer by cdrnet on 10/3/2013 at 3:35 PM
Jul 2, 2013 at 8:41 AM
thank you Christoph, this is very helpful.

regarding Complex64 - yes sorry I was looking at the name of the file Complex64.cs :)
Jul 22, 2013 at 5:13 PM
Edited Jul 22, 2013 at 5:14 PM
hi Christoph

I finally got on to coding this task :)

There is 1 more case:

4a) old code:
points = new List<double> { start.XValue, middle.XValue, end.XValue };
values = new List<double> { start.YValues[0], middle.YValues[0], end.YValues[0] };

var method = Interpolation.CreatePolynomial(points, values);
var result = method.Interpolate(verticalX);
4b) replace with:
points = new List<double> { start.XValue, middle.XValue, end.XValue };
values = new List<double> { start.YValues[0], middle.YValues[0], end.YValues[0] };

var interpolate = new NevillePolynomialInterpolation(points, values);
var result = interpolate.Interpolate(verticalX);
note: I chose NevillePolynomialInterpolation to replace Interpolation.CreatePolynomial() because of a comment on Interpolation.CreatePolynomial() mentions 'Neville':

_"Create a polynomial (neville) interpolation based on arbitrary points."


note: I tried to take the unit tests for NevillePolynomialInterpolation and apply them to the old code Interpolation.CreatePolynomial() to check that the results would be the same. However, these tests are failing:
Constructor_SamplePointsNotUnique_Throws
InterpolationFitsAtArbitraryPointsWithMapleTest
InterpolationFitsAtSamplePointsTest
so when I saw the tests failing - I thought I better double check on this forum, if this replacement makes sense.

Does this replacement look correct ? ( using NevillePolynomialInterpolation to replace Interpolation.CreatePolynomial() )


thanks !

Sean
Jul 23, 2013 at 9:47 AM
hi

just in case this helps anyone:

if I take the old unit test from Math.NET Iridium:
file: InterpolationTest.cs
test: TestInterpolationMethod_NevillePolynomial

code:
  NumericAssert.AreAlmostEqual(.57225000000000000000, method.Differentiate(0.1, out dx, out d2x);, 1e-15, "B 0.1");
this PASSES in Math.NET Iridium, as you would expect.

however, if we port code to Math.NET Numerics (using replacement 4b) above) then the above Assert will fail.
This seems to be because the return value is now the first differential, instead of the interpolated value.

quick fix in test code: check the correct value (the interpolated value):
method.Differentiate(0.1, out dx, out d2x);
NumericAssert.AreAlmostEqual(.57225000000000000000, dx, 1e-15, "B 0.1");