mathnet / mathnet-numerics Goto Github PK
View Code? Open in Web Editor NEWMath.NET Numerics
Home Page: http://numerics.mathdotnet.com
License: MIT License
Math.NET Numerics
Home Page: http://numerics.mathdotnet.com
License: MIT License
Below is the Sample method from Geometric Distribution at https://github.com/mathnet/mathnet-numerics/blob/master/src/Numerics/Distributions/Discrete/Geometric.cs
private static int DoSample(Random rnd, double p)
{
return (int)Math.Ceiling(-Math.Log(rnd.NextDouble()) / p);
}
If rnd = 0 (it is possible) , then it would return -Infinity, which is acceptable in theory (as probability of rnd=0 is 0), however it might result in a problem in practical, as the range for the sampling should take values in real.
Also, shouldn't it be log(1-p)
instead of /p
?
I checked binomial, discrete uniform, both of them offer static+instance sampling method. However, it seems Geometric distribution class lacks the static sampling method.
I am wondering if it breaks the convention?
LinearAlgebraProvider has two methods PointWiseDivideArrays
and PointWiseMultiplyArrays
that can be used for DenseVector instead of directly calling CommonParallel.For
.
I've added a simple genetic optimization solver for continuous functions to my fork -
https://github.com/cuda/mathnet-numerics/tree/ga
I'm not sure of the design. I used delegates for the primary functions (pairing, mating, mutating, evaluating/cost, and convergence) so users could pass in lambdas.
Simple example using defaults (minimize x^2 where -10 < x < 10):
var solver = new ContinuousSolver((double[] x) => x[0]*x[0], ContinuousBounds.CreateBounds(1, -10, 10));
var solutions = solver.Evolve();
The solver takes in a cost function as a delegate and an IList of bounds.
Users can override the default behavior by setting the various delegates using lambdas (or they could use regular methods):
solver.Pair = (options, population) => ...
solver.Crossover = (options, bounds, ma, pa) => ...
solver.Mutate = (options, bounds, population) => ...
solver.Converge = (options, currentStatus, population) => ...
I prefixed the solver and bounds with Continuous so we could support other function types in the future, but since they are already in the Continuous namespace it might be redundant.
Any comments on the design or code would be greatly appreciated.
Thanks,
Marcus
I want to plus 2 infinite-dimensional vectors:
var sparseVector = new SparseVector(int.MaxValue) + new SparseVector(int.MaxValue);
Console.WriteLine(sparseVector.Norm(2));
and it runs forever.
Original Report: https://mathnetnumerics.codeplex.com/workitem/5692
Hi,
On the website
http://numerics.mathdotnet.com/combinatorics/
I saw there is a method to generate a combination of a list.
However I don't seem to find it in the code.
Am I overlooking something?
Best regards,
Ike
Shouldn't the sequence of the elements in source and VolatileWindow be the same (like a shift buffer)?
Actually this is what happens:
input = 1, 2, 3, 4, 5
output = 5, 4, 3, 2, 1
Instead of using the generic version in Vector an optimized version should be used that directly copies the storage array.
.Net 4.5/VS11 will come with a new project type, "Portable Class Library", that will create libraries that will work in all .Net profiles (including full .Net 4, Silverlight 4, Windows Phone 7, Tailored/Metro and Xbox 360 - hopefully also Mono?).
Might be interesting, if not just to simplify our current dual linked Net40/SL4 project approach.
After converting the Silverlight project to version 5 (RC) of Silverlight, I get the following build error in some of the LoadSolverInformationFromAssembly
method overloads in the CompositeSolver
classes:
Argument 1: cannot convert from 'System.Reflection.AssemblyName' to 'string'
The build error apparently occurs in code sections like this:
var assemblyName = new AssemblyName(assemblyFileName);
var assembly = Assembly.Load(assemblyName);
where the (public) Assembly.Load
method in SL5 does not support an AssemblyName
argument.
According to the Silverlight MSDN documentation even for SL4 (http://msdn.microsoft.com/en-us/library/x4cw969y(v=VS.95).aspx), public use of this method is advised against, as the method is claimed to throw a MethodAccessException
. It seems like this safety barrier has indeed been activated in SL5...
I have solved the problem in my local copy of mathnet-numerics by using theAssembly.Load(string)
overload instead, with the following formulation:
var assembly = Assembly.Load(assemblyName.FullName);
If I have interpreted the MSDN documentation (http://msdn.microsoft.com/en-us/library/ky3942xh(v=VS.96).aspx) correctly for this overload, I do need the full display name of the assembly, so I thought it would be safest to have the AssemblyName
object generate it for me.
I do not know if the assembly load feature is covered in the unit tests, either way I get all tests to pass after the above code modifications.
When I try to divide a Single.SparseVector with a float number I get the following exception:
"Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection."
In my opinion there is nothing illegal in this operation. The vector was obtained from a SparseMatrix:
Single.SparseVector v = mySparseMatrix.Row(index);
Hi. I think the docs should note that DescriptiveStatistics uses n-1 in the denominator for calculations of variance and standard deviation. The choice of denominator in this calculation is a point that confuses many people learning statistics. It's good to be clear.
Original Report: https://mathnetnumerics.codeplex.com/workitem/5695
The SparseMatrix.initCol function in the MathNet.Numerics.FSharp assembly incorrectly iterates over the row count instead of the column count. For example:
SparseMatrix.initCol 3 4 (fun i -> DenseVector.ofList [float i; float i; float i]);;
val it : SparseMatrix = 0,1,2,0
0,1,2,0
0,1,2,0
You can see that the fourth column is uninitialized, rather than having all 3's. Fix appears to be substituting m for n in the for loop in initCol.
DenseVector:
SparseVector:
Distributions.StudentT.Density returns NaN for large degrees of freedom. For examples
foreach(int dof in Enumerable.Range(1,500))
{
var dist = new MathNet.Numerics.Distributions.StudentT(0,1,dof);
Console.WriteLine("{0} {1}",dist,dist.Density(0));
}
StudentT(Location = 0, Scale = 1, DoF = 1) 0.318309886183791
StudentT(Location = 0, Scale = 1, DoF = 2) 0.353553390593275
StudentT(Location = 0, Scale = 1, DoF = 3) 0.367552596947861
.......
StudentT(Location = 0, Scale = 1, DoF = 337) 0.398646439335889
StudentT(Location = 0, Scale = 1, DoF = 338) 0.398647314279096
StudentT(Location = 0, Scale = 1, DoF = 339) Infinity
StudentT(Location = 0, Scale = 1, DoF = 340) NaN
StudentT(Location = 0, Scale = 1, DoF = 341) NaN
The StudentT distribution is well defined for all degrees of freedom, so this is a bug. However, for large degrees of freedom it converges to a normal distribution, so there's a workaround:
new MathNet.Numerics.Distributions.Normal(0,1).Density(0)
0.398942280401433
http://numerics.mathdotnet.com/api/MathNet.Numerics.Distributions/StudentT.htm
For Diagonal Matrices Submatrix creates a SparseMatrix and somehow tries to populate it incorrectly.
(This was from a build downloaded three weeks ago.)
using MathNet.Numerics.LinearAlgebra.Generic;
using MathNet.Numerics.LinearAlgebra.Double;
namespace TestLinearAlgebra
{
class Program
{
static void Main(string[] args)
{
var diagMatrix= new DiagonalMatrix(3);
var subM2 = diagMatrix.SubMatrix(0, 2, 1, 2); //throws Exception
var subM3 = diagMatrix.SubMatrix(0, 3, 1, 2); //throws Exception
}
}
}
System.ArgumentOutOfRangeException was unhandled
Message=Specified argument was out of the range of valid values.
Parameter name: row
..
Hi,
I found a phenomena I can not explain. For two same calculations I get different results of matrix inverse. It depends how I start the unit test in Visual Studio (Run or debug). (and it does not depend on how I build it (Release or Debug).
Here is the code to reproduce:
using MathNet.Numerics.LinearAlgebra.Single;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MathNetTestProject
{
[TestClass]
public class MathTests
{
[TestMethod]
public void MatrixInverseTest()
{
var matrix =
new DenseMatrix(
new float[,]
{
{ 2977729, 11436322, 6625426, 2285420 }, { 11436322, 46377269, 26289264, 9135869 },
{ 6625426, 26289264, 15675893, 5229745 }, { 2285420, 9135869, 5229745, 1913456 }
});
var inverseMatrix = matrix.Inverse();
using (var file = new System.IO.StreamWriter(@"C:\Temp\Test.txt", true))
{
for (int i = 0; i < inverseMatrix.RowCount; i++)
{
for (int j = 0; j < inverseMatrix.ColumnCount; j++)
{
file.WriteLine("inversematrix: " + inverseMatrix.At(i, j));
}
}
}
}
}
}
The results:
Started using Unit Test Debug:
inversematrix: 7.909061E-06
inversematrix: -8.58662E-07
inversematrix: -1.349097E-06
inversematrix: -1.659549E-06
inversematrix: -8.586639E-07
inversematrix: 7.608483E-07
inversematrix: -4.909148E-07
inversematrix: -1.265377E-06
inversematrix: -1.349096E-06
inversematrix: -4.909151E-07
inversematrix: 1.562084E-06
inversematrix: -3.141512E-07
inversematrix: -1.659542E-06
inversematrix: -1.265379E-06
inversematrix: -3.141512E-07
inversematrix: 9.404982E-06
Started using Unit Test Run:
inversematrix: 7.909059E-06
inversematrix: -8.586632E-07
inversematrix: -1.349095E-06
inversematrix: -1.65954E-06
inversematrix: -8.586637E-07
inversematrix: 7.608483E-07
inversematrix: -4.909148E-07
inversematrix: -1.265377E-06
inversematrix: -1.349094E-06
inversematrix: -4.90915E-07
inversematrix: 1.562083E-06
inversematrix: -3.14152E-07
inversematrix: -1.659544E-06
inversematrix: -1.265377E-06
inversematrix: -3.14151E-07
inversematrix: 9.404975E-06
Do you see the little difference between the two results?
Funny is, that I do not get the same behaviour if I take a double matrix (then it works perfectly).
I am not sure if this behaviour is a problem of visual studio or a problem of math.net.
But please have a look to this situation.
My environment: Visual Studio 2010 with the SP1, latest Math.Net library version taken from nuget.
Thanks!
e.g. Avogadro constant
SORTING AND SHUFFLING:
SortByColumn -- similar to excel sort
SortByRow
ShuffleRows --puts rowvectors in a matrix in a random order
ShuffleColumns --puts columnvectos in a matrix in a random order.
FINDING AND SETTING SPECIFIC ELEMENTS:
FindMask(Predicate matchCondition) find a mask matrix of 0's and 1's where matchCondition is true.
OnMaskSet(Matrix mask, T value)
OnMaskApply(Matrix mask, Func<T, T> func) can apply a functin to all elements of a Mask Matrix.
e.g. in other languages:
A= Find(myMatrix<0)
myMatrix(A)=0;
//find elemets less than 0
var lessthanZero= myMatrix.FindMask(elem => (elem < 0.0))
// set them all to 0
myMatrix.OnMaskSet(lessthanZero, 0.0)
// a similar method:
FindIndices(Predicate matchCondition)
REMOVING AND SETTING SPECIFIC ROW AND COLUM RANGES:
RemoveColumn
RemoveRow
RemoveRowandColumn
SelectColumns ..selects all rows from a specified range of columns e.g. Matrix(:,10:12) would be selectColumns(10,3)
SelectRows ..selects all rows from a specified range of Rows e.g. Matrix(10:12,:) would be selectRows(10,3)
SetColumns
SetRows
Current implementation is:
return Equals(obj as MatrixStorage<T>);
But should be:
return Equals(obj as VectorStorage<T>);
Vector.DoMultiply(scalar, result) overrides already handle these special cases. So this will remove code duplication.
Numerics.IO has already been extracted into a separate assembly. If we'd put it into a separate NuGet package, then the main packages would no longer depend on zlib.net (especially since the portable builds are now part if the same package).
Original Report: https://mathnetnumerics.codeplex.com/workitem/5670
I guess the goal of descriptive statistics is to compute a whole bunch of stats in as few passes over the data as possible. It has proven very useful to me many times; the only slightly unsatisfactory thing I feel is that it computes the Median all the time. This particular pass seems to require access to all the data - in memory - at the same time. Not sure how we could change the API to be as elegant as it currently is but we might be able to give people the option as to what level of descriptive statistics they require. Not sure if people have any thoughts?
When working with symmetric matrices there is a potential risk of misuse because element ij and element ji is the same value. Therefore if a user makes a modification on both triangles of the matrix, the result would not be what the user expects.
For example, user A have a 3x3 SymmetricDenseMatrix:
[0, 0, 0]
[, 0, 0],~, 0]
[
Since the matrix is symmetric (s)he proceeds to add 5 to (0,2), so:
[0, 0, 5]
[, 0, 0],~, 0]
[
The symmetric matrix is then expected to return 5 in both matrix[2,0] and matrix[0,2], which stands true.
On the other hand, there is a risk of adding the value twice with (for example) the intention to preserve symmetry, If add 5 to both (0,2) and (2,0) the result is:
[0, 0, 10]
[, 0, 0], ~, 0]
[
which is not what is expected. The actual cases would involve writing on both sides of the matrix for whatever reason and won't be so clear as the provided example so we have the danger of allowing silent errors which would be hard to debug.
I would like to know your opinion about how you would want to handle this in mathnet.
My opinion, and the handling I currently use, is to disallow writing on the lower triangle of the matrix (throw an exception when attempted). Only writing on the diagonal and upper triangle is allowed, so you are alerted in case of mis-use. This has caught a huge amount of bugs.
There are interbal arrays that would be more efficient for access to the non-zero values in a SparseMatrix. Somtheing alaong the lines of:
let last = coMatrix.InternalSparseRowOffsets.Length - 1
if item >= 0 && item <= last then
let (startI, endI) =
if item = last then (coMatrix.InternalSparseRowOffsets.[item], coMatrix.InternalSparseRowOffsets.[item])
else (coMatrix.InternalSparseRowOffsets.[item], coMatrix.InternalSparseRowOffsets.[item + 1] - 1)
seq {
for idx = startI to endI do
let item= coMatrix.InternalSparseColumnValues.[idx]
yield KeyValuePair(coMatrix.InternalSparseValues.[idx], item)
}
It seems the only alternative is to iterate over a Row.
The element-wise operations would allow for more efficient code.
Original Report: https://mathnetnumerics.codeplex.com/workitem/5668
The unit tests hang on the solvers (direct and iterative) and about 25 non-solver tests fail under Mono 2.10 on Ubuntu 10.10. Most are precision and culture problems, and a couple are due to differing hash code implementations.
Microsoft stopped the development of the F# PowerPack.
It would be cool if the math parts (like BigRational literals) could be ported into mathnet-numerics.
https://github.com/fsharp/powerpack/blob/master/src/FSharp.PowerPack/math/INumeric.fs
See also fsprojects/FSharpx.Extras#154
What do you think?
https://mathnetnumerics.codeplex.com/workitem/5723
The current implementation is the opposite of what it should be:
return elements < ParallelizeElements || DisableParallelization || NumberOfParallelWorkerThreads < 2;
Should be:
return elements >= ParallelizeElements && !DisableParallelization && NumberOfParallelWorkerThreads >= 2;
Original Report: https://mathnetnumerics.codeplex.com/workitem/5694
// Breakpoint before the decomposition shows the content of the first DenseMatrix in the array of dense matrices (index = [0] )
base {MathNet.Numerics.LinearAlgebra.Complex32.Matrix} = {(0.9275488, 0),(0, 0),(0, 0)(-0.9275488, 0),(0.9275488, 0),(0, 0)(0, 0),(-0.9275488, 0),(0.9275488, 0)}
UserEvd eigVR = new UserEvd(mArrayOld[0]); // halts the program
Neodym is still based on discontinued Iridium project, but contains some useful filters.
May you also port the rest of Neodym to Numerics?
It seems that some parts (e.g. some signal generators) were already ported. So completing this would completely replace Neodym.
Hope this is possible because of the different licenses of both projects.
The inplace implementations of the sparse vector arithmetic methods (i.e. DoAdd, DoSubstract) seem to be completely broken if the vectors are indeed arbitrarily sparse. Confirmed by new more extensive theory-based unit tests.
Related to gh-18.
Currently the KroneckerProduct is still operating dense on sparse matrices.
Hello, I was using MathNet library and suddenly found an issue:
DescriptiveStatistics computes kurtosis incorrectly for large series (my example is a sample of Meixner(0,Sqrt(2),0,1) of 10000 values).
The proof (as a test) and the fix is already in my branch: statistics-kurtosis
Also there is a small fix for parsing double int StatTestData for machines which have not an English locale as default
Remove Vector.Conjugate(target) overrides and move code to DoConjugate(target) method.
Levinnson Decomposition (both symmetric and non symmetric) was available in dnAnalytics 0.2 and are highly popular in signal processing. was there any special issues that it is omitted ?
Is there a reason why Precision#AlmostEqualsWithRelativeDecimalPlaces
and Precision#AlmostEqualsWithAbsoluteDecimalPlaces
calculate decimalPlaceMagnitude
as Math.Pow(10, -(decimalPlaces - 1))
rather than Math.Pow(10, -decimalPlaces)
?
This seems contrary to the documentation that:
The values are equal if the difference between the two numbers is smaller than 10^(-numberOfDecimalPlaces).
Tried to build Math.NET using Mono's xbuild (on Windows), and it failed. Maybe this isn't a mainline scenario but having some kind of build on Linux/Mac would be useful
LinearAlgebra\Factorization\Evd.cs(159,22): error CS0121: The call is ambiguous between the following methods or properties: `
MathNet.Numerics.LinearAlgebra.Generic.Matrix.operator *(MathNet.Numerics.LinearAlgebra.Generic.Matrix, MathNet.Numerics.LinearAlgebra.Generic.Matrix)
MathNet.Numerics.LinearAlgebra.Generic.Matrix.operator *(MathNet.Numerics.LinearAlgebra.Generic.Matrix, MathNet.Numerics.LinearAlgebra.Generic.Matrix)
Possibly a bug in the Mono C# compiler getting confused between the
operator *(Matrix<T≶, T)
operator *(Matrix<T>,Matrix<T>)
overloads. Even if not supported the Mono team should know about this.
Hi am using the library in a project
and find it peculiar that vector.Random(..) is not a static method, since it does not use any of this members' attributes.
i.e.:
DenseVector v= new DenseVector(1); //v's data are not needed at all..
IContinuousDistribution zeroOneDistribution = new ContinuousUniform();
DenseVector randVector= v.Random(50, zeroOneDistribution)
instead of:
IContinuousDistribution zeroOneDistribution = new ContinuousUniform();
DenseVector randVector= DenseVector.Random(50, zeroOneDistribution)
Vector.CopySubVectorTo() is using Storage.CopySubVectorTo().
Vector.SubVector() uses CommonParallel.For() what is slower.
Also Vector.SubVector() does not need to be overridden in every Vector derivative as all can be handled in the Storage implementation.
Currently Control
class only checks if (Control.DisableParallelization || Control.NumberOfParallelWorkerThreads < 2)
to decide to use parallel execution or not.
I think it should use bool Control.ParallelizeOperation(int elements)
instead, as the element count is relevant for that decision too.
Original Report: https://mathnetnumerics.codeplex.com/workitem/5686
Great thank you,
In my project I used these references: maybe they will help
http://lingpipe-blog.com/2009/07/07/welford-s-algorithm-delete-online-mean-variance-deviation/
http://www.johndcook.com/standard_deviation.html
Best,
JB
Currently sparse arithmetics allows accidential zeros (e.g. (null, 5)+(1, -5) = (1,0) instead of (1,null)). However, IEquatable<Vector>.Equals is currently implemented such that (1,0) is not equal to (1,null). This is inconsistent.
Either one of the two should be fixed:
I'm usind numerics ver 2.4.0 in my project and after runing the project and doing someparts it says find a source:ManagedLinearAlgebraProvider.Double
original location:
c:\TeamCity\buildAgent\work\d4ecde2945c804d6\src\Numerics\Algorithms\LinearAlgebra\ManagedLinearAlgebraProvider.Double.
and open a window to locate it when i press cancel the program continuous But my matices are all NAN.
and this is not the only file it wants, everytime it needs a different file...
what should I do?
Original Report: https://mathnetnumerics.codeplex.com/workitem/5698
I have a VERY large dataset (TBs in size) that I will be wanting to run against. I was testing against only 20 MBs of data and using the DescriptiveStatistics class. I noticed that performance was harsh when i used this class even if just asking for 1 or 2 types of stats. This would run the DescriptiveStatistics class over my aggregate contents in approx 104 seconds. (even just for the MEAN)
I then decided I should give the static class/ienumerable extensions a try, this shocked me in my evaluation of the exact same dataset, 69 milliseconds. (just MEAN)
I would like to suggest an update to the DescriptiveStatistics class. lazy load the statistics. this would increase performance drastically.
Thanks,
Implementing the ToString() method for SparseMatrix would improve debugging experience for large matrices, which is kinda slow at the moment. Additionally, for sparse matrices it doesn't make sense to include the 0-entries.
Maybe just display the non-zeros count ... or show the first 20 non-zero entries including indices.
Currently DenseMatrix types do not override CopyTo, resulting in a very expensive loop instead of a simple Buffer.BlockCopy. Also check other linear algebra types.
Original Report: https://mathnetnumerics.codeplex.com/workitem/5700
hello,
your current version of the Dirichlet.DensityLn function is wrong. the calculated do not match the Beta.DensityLn values when using a 2 dimensional parmaeter.
you can check the bug yourself by comparing the current Dirichlet function with the following code that works:
public double DirichletDensityLn(Dirichlet dir, double[] pi)
{
double ans = 0;
double sumalpha = 0;
double[] alpha = dir.Alpha;
for (int i = 0; i < pi.Length; i++)
{
ans += (alpha[i] - 1) * Math.Log(pi[i]) - SpecialFunctions.GammaLn(alpha[i]);
sumalpha += alpha[i];
}
ans += SpecialFunctions.GammaLn(sumalpha);
return ans;
}
best regards,
makro
CommonParallel.For breaks on empty loops (fromInclusive=0, toExclusive=0) with an ArgumentOutOfRange exception instead of just skipping the whole operation.
While we're at it, we should also consider to special-case the case of only a single item.
On http://numerics.mathdotnet.com/
under "use" is a Report link that send you to https://github.com/mathnet/mathnet-numerics/issues
in README.markdown is link Work Items and Bug Tracker that sends you to http://mathnetnumerics.codeplex.com/workitem/list/advanced
I think all the info should be consistent.
It should be only one issue tracker here on github and readme should be changed. Also there should be some information on codeplex that you should create new issues on github.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.