Quantcast
Viewing all 224 articles
Browse latest View live

Speed of Matrix Math Solving Ax=B MKL vs Managed

@SilverShaded wrote:

Hi, i've just started to use Math.Net numerics to solve Ax=B and noticed a significant speed increase over various Matrix inversion algorithms (yes I know inverting a matrix is not the best way to solve Ax=B anyway but the code was easily available...).

Anyway now to the point, I switched it to use the MKL library, tested it and found no increase in speed at all, as far as I can tell it should be working the LinearAlgebraProvider shows the MKL is being used.

The matrices are not huge e.g. ~30x30 but are called many times a (several thousand times), they are tri-diagonal with off band elements.

Would you expect MKL to give any increase in speed for this size of matrix, or is it just too small and the overhead of calling the subroutine just cancels out any benefit?

I really want to speed up the solution time, not many options left, unless anyone has any suggestions. I understand there is a method of solving tri-diagonals with off-band elements (i have the Thompson algorithm for pure tri-diagonals (btw can this be done in .Numerics?)) but I cant find an algorithm for TDM's with off-band elements, although I have reason to believe one exists.

Any ideas/suggestions gratefully received?

Posts: 2

Participants: 1

Read full topic


Creating Matrix from CSV VB.net

@Roger_Garshol wrote:

Hi,
I'm trying to create a matrix and populating this new data structure with values i've stored in a CSV file but I keep running into problems.

My latest attempt is giving me a long error message in VS, see the code line below. The last line shows the error message i get in Visual Studio

Is there any one that can point me in the right direction on to initiate a matrix from a CSV file or know of sites which has examples of using Mathnet with VB.net?

Dim matrix As MathNet.Numerics.LinearAlgebra.Matrix(Of Double) =MathNet.Numerics.Data.Text.DelimitedReader.Read(GMatrixFile, False, ";", False)

Error 3 Overload resolution failed because no accessible 'Read' can be called with these arguments:

Posts: 4

Participants: 2

Read full topic

Any interests in element wise operations on vector/matrix with F#?

@albertp007 wrote:

Hi all, would anyone be interested in having matrix/vector support (or expose) element wise mathematical operations when using them in F#? Something like the below:

> open MathNet.Numerics.LinearAlgebra;;
> let m = matrix [[1.0; 2.0; 3.0]; [0.5; 1.1; 2.1]];;

val m : Matrix = DenseMatrix 2x3-Double
1 2 3
0.5 1.1 2.1

log m;;
val it : Matrix =
DenseMatrix 2x3-Double
0 0.693147 1.09861
-0.693147 0.0953102 0.741937

{ColumnCount = 3;
 Item = ?;
 RowCount = 2;
 Storage = MathNet.Numerics.LinearAlgebra.Storage.DenseColumnMajorMatrixStorage`1[System.Double];
 Values = [|0.0; -0.6931471806; 0.6931471806; 0.0953101798; 1.098612289;
            0.7419373447|];}

exp m;;
val it : Matrix =
DenseMatrix 2x3-Double
2.71828 7.38906 20.0855
1.64872 3.00417 8.16617

{ColumnCount = 3;
 Item = ?;
 RowCount = 2;
 Storage = MathNet.Numerics.LinearAlgebra.Storage.DenseColumnMajorMatrixStorage`1[System.Double];
 Values = [|2.718281828; 1.648721271; 7.389056099; 3.004166024;
            20.08553692; 8.166169913|];}

m .^ 2.0;;
val it : Matrix =
DenseMatrix 2x3-Double
1 4 9
0.25 1.21 4.41

{ColumnCount = 3;
 Item = ?;
 RowCount = 2;
 Storage = MathNet.Numerics.LinearAlgebra.Storage.DenseColumnMajorMatrixStorage`1[System.Double];
 Values = [|1.0; 0.25; 4.0; 1.21; 9.0; 4.41|];}

The last one is mimicking the octave element wise exponent operator.

All it takes to have these in F# is for the point-wise operations to be exposed as a static member function with a specific name, e.g. Log() for 'log', Exp() for 'exp' in the Matrix and Vector types. Implementation wise, PointwiseLog() and PointwiseExp() are already there but they are not yet exposed (please correct me if I am wrong). I have added some code to implement all the operators under "Math Operators" in this post: https://blogs.msdn.microsoft.com/dsyme/2008/09/01/the-f-operators-and-basic-functions/ plus exposing the existing PointwiseLog() and PointwiseExp() functions in Matrix.Operators.cs and Vector.Operators.cs.

If any one is interested in having these, I would gladly send a pull request. Or if this is actually not a very good idea for whatever reasons, please kindly share.

Thanks for reading!

Cheers,
Albert

Posts: 2

Participants: 2

Read full topic

Change 'vector' and 'matrix' builder function in F# to take sequences instead of lists

@albertp007 wrote:

Hi all, the current 'vector' and 'matrix' functions in the F# extension modules take lists as input and create a DenseVector/DenseMatrix respectively. How about we change them to take sequences like so:

let inline vector (s: seq<'T>) = DenseVector.ofSeq s

and for matrix:

let inline matrix (s: seq<#seq<'T>>) = DenseMatrix.ofRowSeq s

F# takes sequence as anything that implements the IEnumerable interface, that includes lists, arrays, sequence expressions as well. So making the above changes will not affect existing functionality but then will open a lot more possibilities like the below:

It will still work for the existing case using list.

> open MathNet.Numerics.LinearAlgebra;;
> let v0 = vector [1.0; 2.0; 3.0];;

val v0 : Vector

It will even work for vector itself because vector also implements IEnumerable.

> let v1 = vector v0;;

val v1 : Vector

v1;;
val it : Vector = seq [1.0; 2.0; 3.0]

If you have an array of floats, it will also work:

> let a0 = [|1.0; 2.0; 3.0|];;

val a0 : float [] = [|1.0; 2.0; 3.0|]

> let v2 = vector a0;;

val v2 : Vector

> v2;;

val it : Vector = seq [1.0; 2.0; 3.0]

Using a sequence expression:

> seq { for i in [0.0..5.0] -> i * 2.0 } |> vector;;
val it : Vector = seq [0.0; 2.0; 4.0; 6.0; ...]

For matrix, the existing case using lists still works:

> matrix [[1.0; 2.0; 3.0]; [0.1; 0.2; 0.3]];;
val it : Matrix =
DenseMatrix 2x3-Double
1 2 3
0.1 0.2 0.3

{ColumnCount = 3;
 Item = ?;
 RowCount = 2;
 Storage = MathNet.Numerics.LinearAlgebra.Storage.DenseColumnMajorMatrixStorage`1[System.Double];
 Values = [|1.0; 0.1; 2.0; 0.2; 3.0; 0.3|];}

Or if you like you can do array of lists and it will work:

> matrix [| [1.0; 2.0; 3.0]; [0.1; 0.2; 0.3] |];;
val it : Matrix =
DenseMatrix 2x3-Double
1 2 3
0.1 0.2 0.3

{ColumnCount = 3;
 Item = ?;
 RowCount = 2;
 Storage = MathNet.Numerics.LinearAlgebra.Storage.DenseColumnMajorMatrixStorage`1[System.Double];
 Values = [|1.0; 0.1; 2.0; 0.2; 3.0; 0.3|];}

You can also use list of arrays:

> matrix [[|1.0; 2.0; 3.0|]; [|0.1; 0.2; 0.3|]];;
val it : Matrix =
DenseMatrix 2x3-Double
1 2 3
0.1 0.2 0.3

{ColumnCount = 3;
 Item = ?;
 RowCount = 2;
 Storage = MathNet.Numerics.LinearAlgebra.Storage.DenseColumnMajorMatrixStorage`1[System.Double];
 Values = [|1.0; 0.1; 2.0; 0.2; 3.0; 0.3|];}

And then you can also plug in a list of vectors and it will work too (because, again, vector implements IEnumerable)

> matrix [v2; v2];;
val it : Matrix =
DenseMatrix 2x3-Double
1 2 3
1 2 3

{ColumnCount = 3;
 Item = ?;
 RowCount = 2;
 Storage = MathNet.Numerics.LinearAlgebra.Storage.DenseColumnMajorMatrixStorage`1[System.Double];
 Values = [|1.0; 1.0; 2.0; 2.0; 3.0; 3.0|];}

The last case using a list/seq/array of vectors to create a matrix seem very natural to me and it will save us a lot of toList conversions.

Does anybody like this? Or there might be a reason why they only take lists to begin with, please feel free to share/comment

Cheers,
Albert

Posts: 7

Participants: 2

Read full topic

Please implement tensor support

Using IPreconditioner and Iterator in BiCgStab

@hadiahameed wrote:

Hi,

I want to use BiCgStab Method to solve a system of linear equations Ax = b in which A is not symmetric. But I don't seem to understand how to instantiate IPreconditioner and Iterator.

Please help me on this.

Regards

Posts: 1

Participants: 1

Read full topic

New implementation for calculating nearest positive definite matrix using convergent series of projections

@David_D wrote:

Hello,

I worked on a feature that I couldn't find in the library. It takes as an input a non positive definite but symmetric matrix, and calculates the nearest positive definite matrix using a convergent algorithm.

The technique is described in Higham, Nick (2002) Computing the nearest correlation matrix - a problem from finance http://eprints.ma.man.ac.uk/232/01/covered/MIMS_ep2006_70.pdf

The method converges towards the nearest positive definite matrix. However due to rounding errors, the final results is never completely positive definite and the Cholesky decomposition always fails. To make it work I can "chop off" some digits from the values but this is a pretty ugly technique.

I wanted to know:
- Can a subject expert matter give me a hand with the code in order to fix these rounding issues?
- Where can this code be added to the library? In which file should the implementation be placed?

Regards,
D

Posts: 2

Participants: 2

Read full topic

Apply linear regression using Mathnet library and VB.net

@duarte wrote:

dear all, I would appreciate if someone could help me solving this problem:

I need to apply a simple linear regression analysis in visual studio using VB.net and I'm using the Math.Net library.

I created and initiated the vectors X and Y and them I call the regression function as such:

Dim regression_result = New MathNet.Numerics.LinearRegression.SimpleRegression(X,Y)

However, it gives an error in the statement "SimpleRegression" saying that: "Overload resolution failed because no 'New' is accessible"

Any thoughts for this problem?

Thanks

Posts: 10

Participants: 3

Read full topic


Referring to results coming from Math.NET Numerics from within scientific papers

@hkoestin wrote:

Dear all,

in my projects I am using Math.NET for calculation of different things for statistical evaluations of data.

End-users (physicians) are doing quite complex analysis of medial data and want to refer to the results from within their scientific papers.

The question now is: how well accepted are results from Math.NET in scientific work compared to e.g. results coming from Matlab or R?

Do you see any issue referring to this data and citing it?

Thanks a lot for your thoughts!

Harald

Posts: 1

Participants: 1

Read full topic

How to use WeightedRegression

@heering.eas wrote:

Hi! I'm new to Math.NET.
I need to use WeightedRegression.Weighted() but I don't know how to build the Predictor matrix X.
What I have is:
double[] xData
double[] yData
double[] weights (one weight factor per x value)
int order (order of polynomial fit)

From weights, I built a diagonal matrix using Matrix.Build.DenseOfDiagonalArray(weights).
How can I set the order of the polynomial fit?
What is returned by the Weighted() function?
I just want to do a weighted polynomial fit of my values.
Pls help Image may be NSFW.
Clik here to view.
:slight_smile:

Posts: 2

Participants: 1

Read full topic

Assignment "=" Operator with Matrices

@JMcCabe wrote:

It appears that the assignment operator with MathNet linear algebra is functionally different from the standard C# assignment operator. It seems with MathNet, if you say 'matrix1 = matrix2', it assigns the same reference/address to both matrices, rather than simply copying the contents of one to the other at a different reference/address/location. Hence the inclusion of a '.CopyTo' method.

Anyone know the reasoning for that? Just curious.

Thanks.

Posts: 6

Participants: 3

Read full topic

Solving for a (M x N) Matrix when N > M

@Jim_R wrote:

Hi, Newbie question here. We are trying to do Fluid Calculations and I set up a basic C# program to test out different scenarios, I'm running into an issue running a MxN matrix where N>M. For example a 4x5 Matrix. I know I am doing something wrong but I just can't figure it out.

Here is the error:
An unhandled exception of type 'System.ArgumentException' occurred in MathNet.Numerics.dll

Additional information: Matrix dimensions must agree: 4x5.

Here is the code snip:
var M = Matrix.Build;
var V = Vector.Build;

        var m = M.DenseOfArray(new double[,] { {a,b,c,cc,ccc},
                                                                     {f,g,h,hh,hhh},
                                                                     {j,k,l,ll,lll},
                                                                     {o,p,q,qq,qqq}
                                                                      });
        var v = V.Dense(new double[] { e, i, n, w });
        var x = m.Solve(v);

Here are the values I am passing:
DenseMatrix 4x5-Double
1 1 1 1 1
0.036 0.0007 0.007 0.0027 0.066
0.125 0.09 0.96 0.18 0.95
0.032 0.032 0.36 0.136 0.79

DenseVector 4-Double
8650.86
185.592
1675.66
133.2

Any help would be appreciated.
Jim

Posts: 3

Participants: 2

Read full topic

ODE Solvers Basic Usage

@JMcCabe wrote:

I've searched everywhere for some basic info/tutorial on how to implement the ODE Solvers library, and in particular the 'RungeKutta.FourthOrder' method. Aside from the info in the Class Reference (which I didn't really understand), all I found was some testing code which included some references like this:

        Func<double, double, double> ode = (t, y) => t + 2 * y * t;
        Func<double, double> sol = (t) => 0.5 * (Math.Exp(t * t) - 1);

Could some kind person provide me with a bit more information on how to use this? I'd sure appreciate it. It's incredible the wonderful stuff you find in the MathNet libraries Image may be NSFW.
Clik here to view.
:slight_smile:

Posts: 1

Participants: 1

Read full topic

Way to import structure variables from .mat file to C#

@MengGu wrote:

Is it possible to import structure variable from mat file to C#?

On the other hand, can we export a set of arrays as a structure variable in .mat format?

If possible, please give an example.

Thank you in advance!

Posts: 2

Participants: 2

Read full topic

Robust Spline or Gaussian Filters

@NMK wrote:

Is there a function within Math.Net that is similar to the Robust Spline or Gaussian Filters found in ISO/TS specifications? I've already created a function for Cardinal Splines but that does not contain "Robustness" (exclusion of outliers). I'm unfamiliar with Akima splines..Thank You!

Posts: 1

Participants: 1

Read full topic


OutOfMemoryException with Matrices

@maxmedv wrote:

Hello! I've this problem with initialisation huge matrices such as 10000x10000. Asking for help or may be you know how can i keep in memory two or three this matrices without the exception. My program is working on x86 OS, so i have near 3 Gb of memory.
This is my code:

        var abMatrix = aMatrix.TransposeAndMultiply(bMatrix);

        aMatrix.MapInplace(x => x * x);
        bMatrix=bMatrix.Transpose();
        bMatrix.MapInplace(x => x * x);

        var aParcel = aMatrix.RowSums();
        var bParcel = bMatrix.ColumnSums();

        GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
        GC.Collect();

        var rr1 = abMatrix.MapIndexed((i, j, x) => aParcel[i] + bParcel[j] - 2 * x);


        var v = rr1.EnumerateRows().Sum(x => x.Min()) / rr1.RowCount;
        var v1 = rr1.EnumerateColumns().Sum(x => x.Min()) / rr1.ColumnCount;

Thanks for help!

Posts: 1

Participants: 1

Read full topic

Weighted Multidim Regression: Fit.MultiDimWeighted

@vovjkee wrote:

Hello, guys.
I need to regress 2D weighted sample like this:

(X11, X12) -> Y1, Weight1
(X21, X22) -> Y2, Weight2
.....
(XN1, XN2) ->YN, WeightN.

into linear surface with parameters: a0 + b1 * x1 + b2 * x2

Pretty standard procedure.

When I use

double[] p = MultipleRegression.QR(
new[] { new[] { 1.0, 4.0 }, new[] { 2.0, 5.0 }, new[] { 3.0, 2.0 } },
new[] { 15.0, 20, 10 },
intercept: true);

I get:

  • p {double[3]} double[]
    [0] -1.2500000000000282
    [1] 1.2500000000000049
    [2] 3.7500000000000049

Alright. Three parameters.

But when I use weighted one,

var p = Fit.MultiDimWeighted(
new[] { new[] { 1.0, 4.0 }, new[] { 2.0, 5.0 }, new[] { 3.0, 2.0 } },
new[] { 15.0, 20, 10 }, new[] { 1.0, 2.0, 3.0 });

I get:

  • p {double[2]} double[]
    [0] 0.94827586206896386 double
    [1] 3.591954022988507 double

Only two parameters. What am I missing?

Thank you.

Posts: 2

Participants: 1

Read full topic

Schwarz-Christoffel Mapping

@DavidRutten wrote:

On page 6 of this paper, a conformal mapping from unit disc to unit square is outlined. (Further details on page 19.)

It seems to require 'incomplete Legendre elliptic integrals of the 1st kind' and 'Jacobi elliptic functions'. The only mention of Jacobi I could find in Math.NET was NumericalJacobian in Numerics.Differentiation, which I don't think is the same thing. Is there anything in Math.NET that would help me trying to convert those F and cn functions to C#? Or, if not exactly convert, maybe just approximate?

Posts: 1

Participants: 1

Read full topic

Resample log file to a different rate

@Moi_Moi wrote:

Hi,
I've a txt file containing a datas log at 171.14256985Hz and I would like to resample all the datas to a useful rate like 171Hz for example.
Could you tell me if it is possible with the lib and which function could help me ?
I work in c# whith VS2015
Thanks you

Posts: 1

Participants: 1

Read full topic

GoodnessOfFit.RSquared returning NaNs

@Vrda wrote:

Evening all,

I am new here, and new with C#. Earlier in this year I started to work on
one scientific project, and because I needed a lot of numeric I installed
package id MathNet.Numerics, version 3.15.0, targetFramework net452

and later I made an update, of course, so current package is:
package id MathNet.Numerics, version 3.17.0, targetFramework net452

Now will try to describe what problem I found regarding Curve Fitting: Linear Regression.
I used your example for: How well do the parameters fit the data?
In other words I used:
R2Matrix[i, j] = GoodnessOfFit.RSquared(xdata.Select(x => a + b * x), ydata);

inside nested FOR loop (i, j = 0, 1, ... 506), where I am creating xdata and ydata,
a 2 double vectors (x0, x1, ..., x19), (y0, y1, ..., y19)

At the end, I always finished with 33 pairs that will return NaN. ALL these pairs having
b = 0 for slope, and in ALL other cases I am having correct coefficient of determination.
When I changed an instruction in:
R2Matrix[i, j] = GoodnessOfFit.RSquared(xdata, ydata);

ALL was correct, as well as when I was checking all 33 cases with MS Excel!
Next, I made my function for calculating the coefficient of determination, and never had NaNs.

At the end, I want that someone check all this above ^^, I can write one example:
xdata ={ 0.93, 0.98, 0.98, 1.01, 0.88, 1.02, 1.02, 0.89, 0.79, 0.85, 1.05, 0.84, 0.78, 1.02, 0.99, 0.83, 0.93, 0.81, 1. 1.01}
ydata ={0, 1, 0, -1, 0, 0, -1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}

Thanx for reading!

Posts: 1

Participants: 1

Read full topic

Viewing all 224 articles
Browse latest View live