Recommendation for C# Matrix Library
Math.NET Numerics is very nice, if it supports the operations you want. The older Math.Net Iridium still supports more options. Also, dnAnalytics is quite nice, but no longer being developed. (It, as well as Iridium, are being merged into Math.NET Numerics.)
On the commercial side, there are some very good, robust options. The Extreme Optimization Numerical Libraries work very well. The Visual Numerics library also works very well (although with a royalty-based distribution...).
Matrix Library for .NET
Edit:
It wasn't quite there last time I evaluated it, but there has been quite a bit of activity, so you should also consider the (free and open source) Math.NET Numerics.
In looking now, it seems they've finished their new version, and have added sparse matrix support, as well as other nice goodies.
If you want more robust support, you unfortunately really need to get into commercial packages for .NET atm.
There are two very feature-rich packages, both of which support matrices very well. Extreme Numerics works great, and has some very nice features. I've also heard very good things about the IMSL Visual Numerics math libraries.
Good library for 3D math in C#?
Microsoft.Xna.Framework (ship this XNA) could do the work.
The XNA Framework Math library has multiple basic geometric types that can be used to manipulate objects in 2D or 3D space. The primitive objects in this library represent the data required to represent a geometric object or an operation on that object. Each geometric type has a number of mathematical operations that are supported for the type.
Vector
The XNA Framework provides the Vector2, Vector3 and Vector4 classes for representing and manipulating vectors. A vector is typically used to represent a direction and magnitude. However, in the XNA framework it might also be used to store a coordinate or some other data type with the same storage requirements.
Each vector class has methods for performing standard vector operations such as:
- Dot product
- Cross product
- Normalization
- Transformation
- Linear, Cubic, Catmull-Rom, or Hermite spline interpolation.
Matrices
The XNA Framework provides the Matrix class for transformation of geometry. The Matrix class uses a row major order to address matrices, which means that the row is specified before the column when describing an element of a two-dimensional matrix. The Matrix class provides methods for performing standard matrix operations such as calculating the determinate or inverse of a matrix, in addition to helper methods for creating scale, translation, and rotation matrices.
Quaternions
The XNA Framework provides the Quaternion structure to represent and calculate the efficient rotation about a vector around a specified angle.
What is a good free (open source) BLAS/LAPACK library for .net (C#)?
AMD's ACML is a free download, but it is binary only, not open source, and native code, not .NET.
Performance is generally superior to the Netlib.org code, and generally roughly the same as Intel's MKL -- which is not free IIRC.
The download includes one sample that demonstrates how to bind it to C#. Not any different from calling any other C or C++ library from C#.
Library implements BLAS, LAPACK, FFTs, and RNGs.
http://developer.amd.com/cpu/Libraries/acml/downloads/pages/default.aspx
EDIT TO RESPOND TO COMMENT:
On an Intel CPU, AMD's ACML will perform approximately as well as Intel's MKL, but it depends on the algorithm, matrix sizes, number of cores, memory topology and speed, etc. etc. etc. Your mileage may vary. The only way to tell for sure is to run your own benchmark. In some cases, ACML is faster than MKL even on Itel hardware.
Either one will be significantly faster than any "naive" implementation for large matrixes. Both are architected to use multiple threads on multicore processors, and have hand-tweaked assembly language kernels and a lot of tuning for the cache behaviours on various machines.
For small matrixes, performance is generally a don't-care, since any modern cpu can solve a small matix in just a few milliseconds, even using the simplest code. In that case, you're only using a library to avoid writing and debugging code that has been written hundreds of times already.
Matrix must be positive definite (Math.Net C# library)
I fixed this issue by switching on the fly for the different equations to see which one returned the correct answers and didn't throw this exception. Here is my solution to this problem which I hope helps someone else out.
public Vector<double> CalculateWithQR(Matrix<double> x, Vector<double> y)
{
Vector<double> result = null;
try
{
result = MultipleRegression.QR(x, y);
// check for NaN and infinity
for (int i = 0; i < result.Count; i++)
{
var value = result.ElementAt(i);
if (Double.IsNaN(value) || Double.IsInfinity(value))
{
return null;
}
}
}
catch (Exception ex)
{
}
return result;
}
public Vector<double> CalculateWithNormal(Matrix<double> x, Vector<double> y)
{
Vector<double> result = null;
try
{
result = MultipleRegression.NormalEquations(x, y);
// check for NaN and infinity
for (int i = 0; i < result.Count; i++)
{
var value = result.ElementAt(i);
if (Double.IsNaN(value) || Double.IsInfinity(value))
{
return null;
}
}
}
catch (Exception ex)
{
}
return result;
}
public Vector<double> CalculateWithSVD(Matrix<double> x, Vector<double> y)
{
Vector<double> result = null;
try
{
result = MultipleRegression.Svd(x, y);
// check for NaN and infinity
for (int i = 0; i < result.Count; i++)
{
var value = result.ElementAt(i);
if (Double.IsNaN(value) || Double.IsInfinity(value))
{
return null;
}
}
}
catch (Exception ex)
{
}
return result;
}
public Vector<double> FindBestMRSolution(Matrix<double> x, Vector<double> y)
{
Vector<double> result = null;
try
{
result = CalculateWithNormal(x, y);
if (result != null)
{
return result;
}
else
{
result = CalculateWithSVD(x, y);
if (result != null)
{
return result;
}
else
{
result = CalculateWithQR(x, y);
if (result != null)
{
return result;
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
return result;
}
C# Algebra Linear Library
See:
- http://www.meta-numerics.net/
- http://www.mathdotnet.com/
- http://linearalgebra.codeplex.com/
They are open source too!
Creating a 3x3 matrix with user input numbers C#
I will add a while loop and use double.TryParse to validate user's input. Usin BWHazel's code:
const int MATRIX_ROWS = 3;
const int MATRIX_COLUMNS = 3;
double[,] matrix = new double[MATRIX_ROWS, MATRIX_COLUMNS];
for (int i = 0; i < MATRIX_ROWS; i++)
{
for (int j = 0; j < MATRIX_COLUMNS; j++)
{
double input;
Console.Write("Enter value for ({0},{1}): ", i, j);
while (!double.TryParse(Console.ReadLine(), out input)
{
Console.Write("Enter correct value for ({0},{1}): ", i, j);
}
matrix[i,j] = input
}
}
To get the totals for all rows you can use following snippet:
for (int i = 0; i < MATRIX_ROWS; i++)
{
// The some for each row
double sum = 0.0;
for (int j = 0; j < MATRIX_COLUMNS; j++)
{
sum += matrix[i,j];
}
Console.WriteLine(string.format("The sum for row {0} is: {1}", i, sum));
}
Related Topics
How to Reduce Memory Consumption of PDFptable with Many Cells
How to Add Data Table with Legend Keys to a Ms Chart in C#
Why Is Addrange Faster Than Using a Foreach Loop
Multi Threading C# Application with SQL Server Database Calls
System.Threading.Timer in C# It Seems to Be Not Working. It Runs Very Fast Every 3 Second
How to Get Mx Records for a Dns Name with System.Net.Dns
Using Moq to Determine If a Method Is Called
Serializing/Deserializing with Memory Stream
Translate Perl Regular Expressions to .Net
Creating a SQL Server Table from a C# Datatable
JSONconvert.Serializeobject Always Return {} in Xamarinforms
What's the Fastest Way to Bulk Insert a Lot of Data in SQL Server (C# Client)
How to Debug a Single Thread in Visual Studio
How to Read and Write Id3 Tags to an Mp3 in C#
Visual Studio 2015 Is Extremely Slow
Datetime2' Error When Using Entity Framework in VS 2010 .Net 4.0