diff --git a/src/Numerics/Fit.cs b/src/Numerics/Fit.cs index 14ddce5c..1669a883 100644 --- a/src/Numerics/Fit.cs +++ b/src/Numerics/Fit.cs @@ -106,7 +106,7 @@ namespace MathNet.Numerics public static double[] PolynomialWeighted(double[] x, double[] y, double[] w, int order) { var design = Matrix.Build.Dense(x.Length, order + 1, (i, j) => Math.Pow(x[i], j)); - return WeightedRegression.Weighted(design, Vector.Build.Dense(y), Matrix.Build.Diagonal(w.Length, w.Length, w)).ToArray(); + return WeightedRegression.Weighted(design, Vector.Build.Dense(y), Matrix.Build.Diagonal(w)).ToArray(); } /// diff --git a/src/Numerics/LinearAlgebra/Builder.cs b/src/Numerics/LinearAlgebra/Builder.cs index e167164e..811dd961 100644 --- a/src/Numerics/LinearAlgebra/Builder.cs +++ b/src/Numerics/LinearAlgebra/Builder.cs @@ -1147,6 +1147,17 @@ namespace MathNet.Numerics.LinearAlgebra return Diagonal(new DiagonalMatrixStorage(rows, columns, storage)); } + /// + /// Create a new square diagonal matrix directly binding to a raw array. + /// The array is assumed to represent the diagonal values and is used directly without copying. + /// Very efficient, but changes to the array and the matrix will affect each other. + /// + /// + public Matrix Diagonal(T[] storage) + { + return Diagonal(new DiagonalMatrixStorage(storage.Length, storage.Length, storage)); + } + /// /// Create a new diagonal matrix and initialize each diagonal value to the same provided value. /// @@ -1180,6 +1191,55 @@ namespace MathNet.Numerics.LinearAlgebra return Diagonal(DiagonalMatrixStorage.OfInit(order, order, i => One)); } + + /// + /// Create a new diagonal matrix with the diagonal as a copy of the given vector. + /// This new matrix will be independent from the vector. + /// A new memory block will be allocated for storing the matrix. + /// + public Matrix DiagonalOfDiagonalVector(Vector diagonal) + { + var m = Diagonal(diagonal.Count, diagonal.Count); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new diagonal matrix with the diagonal as a copy of the given vector. + /// This new matrix will be independent from the vector. + /// A new memory block will be allocated for storing the matrix. + /// + public Matrix DiagonalOfDiagonalVector(int rows, int columns, Vector diagonal) + { + var m = Diagonal(rows, columns); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new diagonal matrix with the diagonal as a copy of the given array. + /// This new matrix will be independent from the array. + /// A new memory block will be allocated for storing the matrix. + /// + public Matrix DiagonalOfDiagonalArray(T[] diagonal) + { + var m = Diagonal(diagonal.Length, diagonal.Length); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new diagonal matrix with the diagonal as a copy of the given array. + /// This new matrix will be independent from the array. + /// A new memory block will be allocated for storing the matrix. + /// + public Matrix DiagonalOfDiagonalArray(int rows, int columns, T[] diagonal) + { + var m = Diagonal(rows, columns); + m.SetDiagonal(diagonal); + return m; + } + public abstract IIterationStopCriterium[] IterativeSolverStopCriteria(int maxIterations = 1000); } diff --git a/src/Numerics/LinearRegression/WeightedRegression.cs b/src/Numerics/LinearRegression/WeightedRegression.cs index 693a9f99..9d7ec732 100644 --- a/src/Numerics/LinearRegression/WeightedRegression.cs +++ b/src/Numerics/LinearRegression/WeightedRegression.cs @@ -65,7 +65,7 @@ namespace MathNet.Numerics.LinearRegression predictor = predictor.InsertColumn(0, Vector.Build.Dense(predictor.RowCount, Vector.One)); } var response = Vector.Build.Dense(y); - var weights = Matrix.Build.Diagonal(w.Length, w.Length, w); + var weights = Matrix.Build.Diagonal(w); return predictor.TransposeThisAndMultiply(weights*predictor).Cholesky().Solve(predictor.TransposeThisAndMultiply(weights*response)).ToArray(); }