Browse Source

LA: extend Build.Diagonal for diagonal matrices

pull/184/head
Christoph Ruegg 13 years ago
parent
commit
64a641e8ca
  1. 2
      src/Numerics/Fit.cs
  2. 60
      src/Numerics/LinearAlgebra/Builder.cs
  3. 2
      src/Numerics/LinearRegression/WeightedRegression.cs

2
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<double>.Build.Dense(x.Length, order + 1, (i, j) => Math.Pow(x[i], j));
return WeightedRegression.Weighted(design, Vector<double>.Build.Dense(y), Matrix<double>.Build.Diagonal(w.Length, w.Length, w)).ToArray();
return WeightedRegression.Weighted(design, Vector<double>.Build.Dense(y), Matrix<double>.Build.Diagonal(w)).ToArray();
}
/// <summary>

60
src/Numerics/LinearAlgebra/Builder.cs

@ -1147,6 +1147,17 @@ namespace MathNet.Numerics.LinearAlgebra
return Diagonal(new DiagonalMatrixStorage<T>(rows, columns, storage));
}
/// <summary>
/// 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.
/// </summary>
/// <seealso href="http://en.wikipedia.org/wiki/Row-major_order"/>
public Matrix<T> Diagonal(T[] storage)
{
return Diagonal(new DiagonalMatrixStorage<T>(storage.Length, storage.Length, storage));
}
/// <summary>
/// Create a new diagonal matrix and initialize each diagonal value to the same provided value.
/// </summary>
@ -1180,6 +1191,55 @@ namespace MathNet.Numerics.LinearAlgebra
return Diagonal(DiagonalMatrixStorage<T>.OfInit(order, order, i => One));
}
/// <summary>
/// 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.
/// </summary>
public Matrix<T> DiagonalOfDiagonalVector(Vector<T> diagonal)
{
var m = Diagonal(diagonal.Count, diagonal.Count);
m.SetDiagonal(diagonal);
return m;
}
/// <summary>
/// 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.
/// </summary>
public Matrix<T> DiagonalOfDiagonalVector(int rows, int columns, Vector<T> diagonal)
{
var m = Diagonal(rows, columns);
m.SetDiagonal(diagonal);
return m;
}
/// <summary>
/// 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.
/// </summary>
public Matrix<T> DiagonalOfDiagonalArray(T[] diagonal)
{
var m = Diagonal(diagonal.Length, diagonal.Length);
m.SetDiagonal(diagonal);
return m;
}
/// <summary>
/// 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.
/// </summary>
public Matrix<T> DiagonalOfDiagonalArray(int rows, int columns, T[] diagonal)
{
var m = Diagonal(rows, columns);
m.SetDiagonal(diagonal);
return m;
}
public abstract IIterationStopCriterium<T>[] IterativeSolverStopCriteria(int maxIterations = 1000);
}

2
src/Numerics/LinearRegression/WeightedRegression.cs

@ -65,7 +65,7 @@ namespace MathNet.Numerics.LinearRegression
predictor = predictor.InsertColumn(0, Vector<T>.Build.Dense(predictor.RowCount, Vector<T>.One));
}
var response = Vector<T>.Build.Dense(y);
var weights = Matrix<T>.Build.Diagonal(w.Length, w.Length, w);
var weights = Matrix<T>.Build.Diagonal(w);
return predictor.TransposeThisAndMultiply(weights*predictor).Cholesky().Solve(predictor.TransposeThisAndMultiply(weights*response)).ToArray();
}

Loading…
Cancel
Save