From 1d7ce24d3e687c2ad3a152d748b5bbe3bdaf3dfb Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Thu, 14 May 2015 13:35:30 +0200 Subject: [PATCH] LA: Static CreateMatrix/Vector: simpler to use than builders and with better type inferrence --- src/Numerics/LinearAlgebra/Builder.cs | 2 - src/Numerics/LinearAlgebra/CreateMatrix.cs | 954 +++++++++++++++++++++ src/Numerics/LinearAlgebra/CreateVector.cs | 313 +++++++ src/Numerics/Numerics.csproj | 2 + 4 files changed, 1269 insertions(+), 2 deletions(-) create mode 100644 src/Numerics/LinearAlgebra/CreateMatrix.cs create mode 100644 src/Numerics/LinearAlgebra/CreateVector.cs diff --git a/src/Numerics/LinearAlgebra/Builder.cs b/src/Numerics/LinearAlgebra/Builder.cs index 46b1c9eb..8a6fdad2 100644 --- a/src/Numerics/LinearAlgebra/Builder.cs +++ b/src/Numerics/LinearAlgebra/Builder.cs @@ -1206,7 +1206,6 @@ namespace MathNet.Numerics.LinearAlgebra /// 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(int rows, int columns, T[] storage) { return Diagonal(new DiagonalMatrixStorage(rows, columns, storage)); @@ -1217,7 +1216,6 @@ namespace MathNet.Numerics.LinearAlgebra /// 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)); diff --git a/src/Numerics/LinearAlgebra/CreateMatrix.cs b/src/Numerics/LinearAlgebra/CreateMatrix.cs new file mode 100644 index 00000000..e6ee3495 --- /dev/null +++ b/src/Numerics/LinearAlgebra/CreateMatrix.cs @@ -0,0 +1,954 @@ +// +// Math.NET Numerics, part of the Math.NET Project +// http://numerics.mathdotnet.com +// http://github.com/mathnet/mathnet-numerics +// http://mathnetnumerics.codeplex.com +// +// Copyright (c) 2009-2015 Math.NET +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +using System; +using System.Collections.Generic; +using MathNet.Numerics.Distributions; +using MathNet.Numerics.LinearAlgebra.Storage; + +namespace MathNet.Numerics.LinearAlgebra +{ + public static class CreateMatrix + { + /// + /// Create a new matrix straight from an initialized matrix storage instance. + /// If you have an instance of a discrete storage type instead, use their direct methods instead. + /// + public static Matrix WithStorage(MatrixStorage storage) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.OfStorage(storage); + } + + /// + /// Create a new matrix with the same kind of the provided example. + /// + public static Matrix SameAs(Matrix example, int rows, int columns, bool fullyMutable = false) + where T : struct, IEquatable, IFormattable + where TU : struct, IEquatable, IFormattable + { + return Matrix.Build.SameAs(example, rows, columns, fullyMutable); + } + + /// + /// Create a new matrix with the same kind and dimensions of the provided example. + /// + public static Matrix SameAs(Matrix example) + where T : struct, IEquatable, IFormattable + where TU : struct, IEquatable, IFormattable + { + return Matrix.Build.SameAs(example); + } + + /// + /// Create a new matrix with the same kind of the provided example. + /// + public static Matrix SameAs(Vector example, int rows, int columns) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SameAs(example, rows, columns); + } + + /// + /// Create a new matrix with a type that can represent and is closest to both provided samples. + /// + public static Matrix SameAs(Matrix example, Matrix otherExample, int rows, int columns, bool fullyMutable = false) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SameAs(example, otherExample, rows, columns, fullyMutable); + } + + /// + /// Create a new matrix with a type that can represent and is closest to both provided samples and the dimensions of example. + /// + public static Matrix SameAs(Matrix example, Matrix otherExample) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SameAs(example, otherExample); + } + + /// + /// Create a new dense matrix with values sampled from the provided random distribution. + /// + public static Matrix Random(int rows, int columns, IContinuousDistribution distribution) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.Random(rows, columns, distribution); + } + + /// + /// Create a new dense matrix with values sampled from the standard distribution with a system random source. + /// + public static Matrix Random(int rows, int columns) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.Random(rows, columns); + } + + /// + /// Create a new dense matrix with values sampled from the standard distribution with a system random source. + /// + public static Matrix Random(int rows, int columns, int seed) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.Random(rows, columns, seed); + } + + /// + /// Create a new positive definite dense matrix where each value is the product + /// of two samples from the provided random distribution. + /// + public static Matrix RandomPositiveDefinite(int order, IContinuousDistribution distribution) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.RandomPositiveDefinite(order, distribution); + } + + /// + /// Create a new positive definite dense matrix where each value is the product + /// of two samples from the standard distribution. + /// + public static Matrix RandomPositiveDefinite(int order) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.RandomPositiveDefinite(order); + } + + /// + /// Create a new positive definite dense matrix where each value is the product + /// of two samples from the provided random distribution. + /// + public static Matrix RandomPositiveDefinite(int order, int seed) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.RandomPositiveDefinite(order, seed); + } + + /// + /// Create a new dense matrix straight from an initialized matrix storage instance. + /// The storage is used directly without copying. + /// Intended for advanced scenarios where you're working directly with + /// storage for performance or interop reasons. + /// + public static Matrix Dense(DenseColumnMajorMatrixStorage storage) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.Dense(storage); + } + + /// + /// Create a new dense matrix with the given number of rows and columns. + /// All cells of the matrix will be initialized to zero. + /// Zero-length matrices are not supported. + /// + public static Matrix Dense(int rows, int columns) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.Dense(rows, columns); + } + + /// + /// Create a new dense matrix with the given number of rows and columns directly binding to a raw array. + /// The array is assumed to be in column-major order (column by column) and is used directly without copying. + /// Very efficient, but changes to the array and the matrix will affect each other. + /// + /// + public static Matrix Dense(int rows, int columns, T[] storage) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.Dense(rows, columns, storage); + } + + /// + /// Create a new dense matrix and initialize each value to the same provided value. + /// + public static Matrix Dense(int rows, int columns, T value) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.Dense(rows, columns, value); + } + + /// + /// Create a new dense matrix and initialize each value using the provided init function. + /// + public static Matrix Dense(int rows, int columns, Func init) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.Dense(rows, columns, init); + } + + /// + /// Create a new diagonal dense matrix and initialize each diagonal value to the same provided value. + /// + public static Matrix DenseDiagonal(int rows, int columns, T value) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DenseDiagonal(rows, columns, value); + } + + /// + /// Create a new diagonal dense matrix and initialize each diagonal value to the same provided value. + /// + public static Matrix DenseDiagonal(int order, T value) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DenseDiagonal(order, value); + } + + /// + /// Create a new diagonal dense matrix and initialize each diagonal value using the provided init function. + /// + public static Matrix DenseDiagonal(int rows, int columns, Func init) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DenseDiagonal(rows, columns, init); + } + + /// + /// Create a new diagonal dense identity matrix with a one-diagonal. + /// + public static Matrix DenseIdentity(int rows, int columns) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DenseIdentity(rows, columns); + } + + /// + /// Create a new diagonal dense identity matrix with a one-diagonal. + /// + public static Matrix DenseIdentity(int order) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DenseIdentity(order); + } + + /// + /// Create a new dense matrix as a copy of the given other matrix. + /// This new matrix will be independent from the other matrix. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix DenseOfMatrix(Matrix matrix) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DenseOfMatrix(matrix); + } + + /// + /// Create a new dense matrix as a copy of the given two-dimensional array. + /// This new matrix will be independent from the provided array. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix DenseOfArray(T[,] array) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DenseOfArray(array); + } + + /// + /// Create a new dense matrix as a copy of the given indexed enumerable. + /// Keys must be provided at most once, zero is assumed if a key is omitted. + /// This new matrix will be independent from the enumerable. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix DenseOfIndexed(int rows, int columns, IEnumerable> enumerable) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DenseOfIndexed(rows, columns, enumerable); + } + + /// + /// Create a new dense matrix as a copy of the given enumerable. + /// The enumerable is assumed to be in column-major order (column by column). + /// This new matrix will be independent from the enumerable. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix DenseOfColumnMajor(int rows, int columns, IEnumerable columnMajor) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DenseOfColumnMajor(rows, columns, columnMajor); + } + + /// + /// Create a new dense matrix as a copy of the given enumerable of enumerable columns. + /// Each enumerable in the master enumerable specifies a column. + /// This new matrix will be independent from the enumerables. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix DenseOfColumns(IEnumerable> data) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DenseOfColumns(data); + } + + /// + /// Create a new dense matrix as a copy of the given enumerable of enumerable columns. + /// Each enumerable in the master enumerable specifies a column. + /// This new matrix will be independent from the enumerables. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix DenseOfColumns(int rows, int columns, IEnumerable> data) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DenseOfColumns(rows, columns, data); + } + + /// + /// Create a new dense matrix of T as a copy of the given column arrays. + /// This new matrix will be independent from the arrays. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix DenseOfColumnArrays(params T[][] columns) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DenseOfColumnArrays(columns); + } + + /// + /// Create a new dense matrix of T as a copy of the given column arrays. + /// This new matrix will be independent from the arrays. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix DenseOfColumnArrays(IEnumerable columns) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DenseOfColumnArrays(columns); + } + + /// + /// Create a new dense matrix as a copy of the given column vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix DenseOfColumnVectors(params Vector[] columns) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DenseOfColumnVectors(columns); + } + + /// + /// Create a new dense matrix as a copy of the given column vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix DenseOfColumnVectors(IEnumerable> columns) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DenseOfColumnVectors(columns); + } + + /// + /// Create a new dense matrix as a copy of the given enumerable of enumerable rows. + /// Each enumerable in the master enumerable specifies a row. + /// This new matrix will be independent from the enumerables. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix DenseOfRows(IEnumerable> data) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DenseOfRows(data); + } + + /// + /// Create a new dense matrix as a copy of the given enumerable of enumerable rows. + /// Each enumerable in the master enumerable specifies a row. + /// This new matrix will be independent from the enumerables. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix DenseOfRows(int rows, int columns, IEnumerable> data) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DenseOfRows(rows, columns, data); + } + + /// + /// Create a new dense matrix of T as a copy of the given row arrays. + /// This new matrix will be independent from the arrays. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix DenseOfRowArrays(params T[][] rows) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DenseOfRowArrays(rows); + } + + /// + /// Create a new dense matrix of T as a copy of the given row arrays. + /// This new matrix will be independent from the arrays. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix DenseOfRowArrays(IEnumerable rows) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DenseOfRowArrays(rows); + } + + /// + /// Create a new dense matrix as a copy of the given row vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix DenseOfRowVectors(params Vector[] rows) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DenseOfRowVectors(rows); + } + + /// + /// Create a new dense matrix as a copy of the given row vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix DenseOfRowVectors(IEnumerable> rows) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DenseOfRowVectors(rows); + } + + /// + /// Create a new dense 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 static Matrix DenseOfDiagonalVector(Vector diagonal) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DenseOfDiagonalVector(diagonal); + } + + /// + /// Create a new dense 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 static Matrix DenseOfDiagonalVector(int rows, int columns, Vector diagonal) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DenseOfDiagonalVector(rows, columns, diagonal); + } + + /// + /// Create a new dense 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 static Matrix DenseOfDiagonalArray(T[] diagonal) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DenseOfDiagonalArray(diagonal); + } + + /// + /// Create a new dense 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 static Matrix DenseOfDiagonalArray(int rows, int columns, T[] diagonal) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DenseOfDiagonalArray(rows, columns, diagonal); + } + + /// + /// Create a new dense matrix from a 2D array of existing matrices. + /// The matrices in the array are not required to be dense already. + /// If the matrices do not align properly, they are placed on the top left + /// corner of their cell with the remaining fields left zero. + /// + public static Matrix DenseOfMatrixArray(Matrix[,] matrices) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DenseOfMatrixArray(matrices); + } + + /// + /// Create a new sparse matrix straight from an initialized matrix storage instance. + /// The storage is used directly without copying. + /// Intended for advanced scenarios where you're working directly with + /// storage for performance or interop reasons. + /// + public static Matrix Sparse(SparseCompressedRowMatrixStorage storage) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.Sparse(storage); + } + + /// + /// Create a sparse matrix of T with the given number of rows and columns. + /// + /// The number of rows. + /// The number of columns. + public static Matrix Sparse(int rows, int columns) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.Sparse(rows, columns); + } + + /// + /// Create a new sparse matrix and initialize each value to the same provided value. + /// + public static Matrix Sparse(int rows, int columns, T value) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.Sparse(rows, columns, value); + } + + /// + /// Create a new sparse matrix and initialize each value using the provided init function. + /// + public static Matrix Sparse(int rows, int columns, Func init) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.Sparse(rows, columns, init); + } + + /// + /// Create a new diagonal sparse matrix and initialize each diagonal value to the same provided value. + /// + public static Matrix SparseDiagonal(int rows, int columns, T value) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseDiagonal(rows, columns, value); + } + + /// + /// Create a new diagonal sparse matrix and initialize each diagonal value to the same provided value. + /// + public static Matrix SparseDiagonal(int order, T value) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseDiagonal(order, value); + } + + /// + /// Create a new diagonal sparse matrix and initialize each diagonal value using the provided init function. + /// + public static Matrix SparseDiagonal(int rows, int columns, Func init) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseDiagonal(rows, columns, init); + } + + /// + /// Create a new diagonal dense identity matrix with a one-diagonal. + /// + public static Matrix SparseIdentity(int rows, int columns) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseIdentity(rows, columns); + } + + /// + /// Create a new diagonal dense identity matrix with a one-diagonal. + /// + public static Matrix SparseIdentity(int order) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseIdentity(order); + } + + /// + /// Create a new sparse matrix as a copy of the given other matrix. + /// This new matrix will be independent from the other matrix. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix SparseOfMatrix(Matrix matrix) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseOfMatrix(matrix); + } + + /// + /// Create a new sparse matrix as a copy of the given two-dimensional array. + /// This new matrix will be independent from the provided array. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix SparseOfArray(T[,] array) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseOfArray(array); + } + + /// + /// Create a new sparse matrix as a copy of the given indexed enumerable. + /// Keys must be provided at most once, zero is assumed if a key is omitted. + /// This new matrix will be independent from the enumerable. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix SparseOfIndexed(int rows, int columns, IEnumerable> enumerable) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseOfIndexed(rows, columns, enumerable); + } + + /// + /// Create a new sparse matrix as a copy of the given enumerable. + /// The enumerable is assumed to be in row-major order (row by row). + /// This new matrix will be independent from the enumerable. + /// A new memory block will be allocated for storing the vector. + /// + /// + public static Matrix SparseOfRowMajor(int rows, int columns, IEnumerable rowMajor) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseOfRowMajor(rows, columns, rowMajor); + } + + /// + /// Create a new sparse matrix with the given number of rows and columns as a copy of the given array. + /// The array is assumed to be in column-major order (column by column). + /// This new matrix will be independent from the provided array. + /// A new memory block will be allocated for storing the matrix. + /// + /// + public static Matrix SparseOfColumnMajor(int rows, int columns, IList columnMajor) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseOfColumnMajor(rows, columns, columnMajor); + } + + /// + /// Create a new sparse matrix as a copy of the given enumerable of enumerable columns. + /// Each enumerable in the master enumerable specifies a column. + /// This new matrix will be independent from the enumerables. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix SparseOfColumns(IEnumerable> data) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseOfColumns(data); + } + + /// + /// Create a new sparse matrix as a copy of the given enumerable of enumerable columns. + /// Each enumerable in the master enumerable specifies a column. + /// This new matrix will be independent from the enumerables. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix SparseOfColumns(int rows, int columns, IEnumerable> data) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseOfColumns(rows, columns, data); + } + + /// + /// Create a new sparse matrix as a copy of the given column arrays. + /// This new matrix will be independent from the arrays. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix SparseOfColumnArrays(params T[][] columns) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseOfColumnArrays(columns); + } + + /// + /// Create a new sparse matrix as a copy of the given column arrays. + /// This new matrix will be independent from the arrays. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix SparseOfColumnArrays(IEnumerable columns) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseOfColumnArrays(columns); + } + + /// + /// Create a new sparse matrix as a copy of the given column vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix SparseOfColumnVectors(params Vector[] columns) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseOfColumnVectors(columns); + } + + /// + /// Create a new sparse matrix as a copy of the given column vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix SparseOfColumnVectors(IEnumerable> columns) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseOfColumnVectors(columns); + } + + /// + /// Create a new sparse matrix as a copy of the given enumerable of enumerable rows. + /// Each enumerable in the master enumerable specifies a row. + /// This new matrix will be independent from the enumerables. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix SparseOfRows(IEnumerable> data) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseOfRows(data); + } + + /// + /// Create a new sparse matrix as a copy of the given enumerable of enumerable rows. + /// Each enumerable in the master enumerable specifies a row. + /// This new matrix will be independent from the enumerables. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix SparseOfRows(int rows, int columns, IEnumerable> data) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseOfRows(rows, columns, data); + } + + /// + /// Create a new sparse matrix as a copy of the given row arrays. + /// This new matrix will be independent from the arrays. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix SparseOfRowArrays(params T[][] rows) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseOfRowArrays(rows); + } + + /// + /// Create a new sparse matrix as a copy of the given row arrays. + /// This new matrix will be independent from the arrays. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix SparseOfRowArrays(IEnumerable rows) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseOfRowArrays(rows); + } + + /// + /// Create a new sparse matrix as a copy of the given row vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix SparseOfRowVectors(params Vector[] rows) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseOfRowVectors(rows); + } + + /// + /// Create a new sparse matrix as a copy of the given row vectors. + /// This new matrix will be independent from the vectors. + /// A new memory block will be allocated for storing the matrix. + /// + public static Matrix SparseOfRowVectors(IEnumerable> rows) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseOfRowVectors(rows); + } + + /// + /// Create a new sparse 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 static Matrix SparseOfDiagonalVector(Vector diagonal) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseOfDiagonalVector(diagonal); + } + + /// + /// Create a new sparse 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 static Matrix SparseOfDiagonalVector(int rows, int columns, Vector diagonal) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseOfDiagonalVector(rows, columns, diagonal); + } + + /// + /// Create a new sparse 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 static Matrix SparseOfDiagonalArray(T[] diagonal) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseOfDiagonalArray(diagonal); + } + + /// + /// Create a new sparse 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 static Matrix SparseOfDiagonalArray(int rows, int columns, T[] diagonal) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseOfDiagonalArray(rows, columns, diagonal); + } + + /// + /// Create a new sparse matrix from a 2D array of existing matrices. + /// The matrices in the array are not required to be sparse already. + /// If the matrices do not align properly, they are placed on the top left + /// corner of their cell with the remaining fields left zero. + /// + public static Matrix SparseOfMatrixArray(Matrix[,] matrices) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.SparseOfMatrixArray(matrices); + } + + /// + /// Create a new diagonal matrix straight from an initialized matrix storage instance. + /// The storage is used directly without copying. + /// Intended for advanced scenarios where you're working directly with + /// storage for performance or interop reasons. + /// + public static Matrix Diagonal(DiagonalMatrixStorage storage) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.Diagonal(storage); + } + + /// + /// Create a new diagonal matrix with the given number of rows and columns. + /// All cells of the matrix will be initialized to zero. + /// Zero-length matrices are not supported. + /// + public static Matrix Diagonal(int rows, int columns) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.Diagonal(rows, columns); + } + + /// + /// Create a new diagonal matrix with the given number of rows and columns 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 static Matrix Diagonal(int rows, int columns, T[] storage) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.Diagonal(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 static Matrix Diagonal(T[] storage) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.Diagonal(storage); + } + + /// + /// Create a new diagonal matrix and initialize each diagonal value to the same provided value. + /// + public static Matrix Diagonal(int rows, int columns, T value) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.Diagonal(rows, columns, value); + } + + /// + /// Create a new diagonal matrix and initialize each diagonal value using the provided init function. + /// + public static Matrix Diagonal(int rows, int columns, Func init) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.Diagonal(rows, columns, init); + } + + /// + /// Create a new diagonal identity matrix with a one-diagonal. + /// + public static Matrix DiagonalIdentity(int rows, int columns) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DiagonalIdentity(rows, columns); + } + + /// + /// Create a new diagonal identity matrix with a one-diagonal. + /// + public static Matrix DiagonalIdentity(int order) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DiagonalIdentity(order); + } + + + /// + /// 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 static Matrix DiagonalOfDiagonalVector(Vector diagonal) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DiagonalOfDiagonalVector(diagonal); + } + + /// + /// 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 static Matrix DiagonalOfDiagonalVector(int rows, int columns, Vector diagonal) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DiagonalOfDiagonalVector(rows, columns, diagonal); + } + + /// + /// 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 static Matrix DiagonalOfDiagonalArray(T[] diagonal) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DiagonalOfDiagonalArray(diagonal); + } + + /// + /// 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 static Matrix DiagonalOfDiagonalArray(int rows, int columns, T[] diagonal) + where T : struct, IEquatable, IFormattable + { + return Matrix.Build.DiagonalOfDiagonalArray(rows, columns, diagonal); + } + } +} \ No newline at end of file diff --git a/src/Numerics/LinearAlgebra/CreateVector.cs b/src/Numerics/LinearAlgebra/CreateVector.cs new file mode 100644 index 00000000..982dafff --- /dev/null +++ b/src/Numerics/LinearAlgebra/CreateVector.cs @@ -0,0 +1,313 @@ +// +// Math.NET Numerics, part of the Math.NET Project +// http://numerics.mathdotnet.com +// http://github.com/mathnet/mathnet-numerics +// http://mathnetnumerics.codeplex.com +// +// Copyright (c) 2009-2015 Math.NET +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +using System; +using System.Collections.Generic; +using MathNet.Numerics.Distributions; +using MathNet.Numerics.LinearAlgebra.Storage; + +namespace MathNet.Numerics.LinearAlgebra +{ + public static class CreateVector + { + /// + /// Create a new vector straight from an initialized matrix storage instance. + /// If you have an instance of a discrete storage type instead, use their direct methods instead. + /// + public static Vector WithStorage(VectorStorage storage) + where T : struct, IEquatable, IFormattable + { + return Vector.Build.OfStorage(storage); + } + + /// + /// Create a new vector with the same kind of the provided example. + /// + public static Vector SameAs(Vector example, int length) + where T : struct, IEquatable, IFormattable + where TU : struct, IEquatable, IFormattable + { + return Vector.Build.SameAs(example, length); + } + + /// + /// Create a new vector with the same kind and dimension of the provided example. + /// + public static Vector SameAs(Vector example) + where T : struct, IEquatable, IFormattable + where TU : struct, IEquatable, IFormattable + { + return Vector.Build.SameAs(example); + } + + /// + /// Create a new vector with the same kind of the provided example. + /// + public static Vector SameAs(Matrix example, int length) + where T : struct, IEquatable, IFormattable + where TU : struct, IEquatable, IFormattable + { + return Vector.Build.SameAs(example, length); + } + + /// + /// Create a new vector with a type that can represent and is closest to both provided samples. + /// + public static Vector SameAs(Vector example, Vector otherExample, int length) + where T : struct, IEquatable, IFormattable + { + return Vector.Build.SameAs(example, otherExample, length); + } + + /// + /// Create a new vector with a type that can represent and is closest to both provided samples and the dimensions of example. + /// + public static Vector SameAs(Vector example, Vector otherExample) + where T : struct, IEquatable, IFormattable + { + return Vector.Build.SameAs(example, otherExample); + } + + /// + /// Create a new vector with a type that can represent and is closest to both provided samples. + /// + public static Vector SameAs(Matrix matrix, Vector vector, int length) + where T : struct, IEquatable, IFormattable + { + return Vector.Build.SameAs(matrix, vector, length); + } + + /// + /// Create a new dense vector with values sampled from the provided random distribution. + /// + public static Vector Random(int length, IContinuousDistribution distribution) + where T : struct, IEquatable, IFormattable + { + return Vector.Build.Random(length, distribution); + } + + /// + /// Create a new dense vector with values sampled from the standard distribution with a system random source. + /// + public static Vector Random(int length) + where T : struct, IEquatable, IFormattable + { + return Vector.Build.Random(length); + } + + /// + /// Create a new dense vector with values sampled from the standard distribution with a system random source. + /// + public static Vector Random(int length, int seed) + where T : struct, IEquatable, IFormattable + { + return Vector.Build.Random(length, seed); + } + + /// + /// Create a new dense vector straight from an initialized vector storage instance. + /// The storage is used directly without copying. + /// Intended for advanced scenarios where you're working directly with + /// storage for performance or interop reasons. + /// + public static Vector Dense(DenseVectorStorage storage) + where T : struct, IEquatable, IFormattable + { + return Vector.Build.Dense(storage); + } + + /// + /// Create a dense vector of T with the given size. + /// + /// The size of the vector. + public static Vector Dense(int size) + where T : struct, IEquatable, IFormattable + { + return Vector.Build.Dense(size); + } + + /// + /// Create a dense vector of T that is directly bound to the specified array. + /// + public static Vector Dense(T[] array) + where T : struct, IEquatable, IFormattable + { + return Vector.Build.Dense(array); + } + + /// + /// Create a new dense vector and initialize each value using the provided value. + /// + public static Vector Dense(int length, T value) + where T : struct, IEquatable, IFormattable + { + return Vector.Build.Dense(length, value); + } + + /// + /// Create a new dense vector and initialize each value using the provided init function. + /// + public static Vector Dense(int length, Func init) + where T : struct, IEquatable, IFormattable + { + return Vector.Build.Dense(length, init); + } + + /// + /// Create a new dense vector as a copy of the given other vector. + /// This new vector will be independent from the other vector. + /// A new memory block will be allocated for storing the vector. + /// + public static Vector DenseOfVector(Vector vector) + where T : struct, IEquatable, IFormattable + { + return Vector.Build.DenseOfVector(vector); + } + + /// + /// Create a new dense vector as a copy of the given array. + /// This new vector will be independent from the array. + /// A new memory block will be allocated for storing the vector. + /// + public static Vector DenseOfArray(T[] array) + where T : struct, IEquatable, IFormattable + { + return Vector.Build.DenseOfArray(array); + } + + /// + /// Create a new dense vector as a copy of the given enumerable. + /// This new vector will be independent from the enumerable. + /// A new memory block will be allocated for storing the vector. + /// + public static Vector DenseOfEnumerable(IEnumerable enumerable) + where T : struct, IEquatable, IFormattable + { + return Vector.Build.DenseOfEnumerable(enumerable); + } + + /// + /// Create a new dense vector as a copy of the given indexed enumerable. + /// Keys must be provided at most once, zero is assumed if a key is omitted. + /// This new vector will be independent from the enumerable. + /// A new memory block will be allocated for storing the vector. + /// + public static Vector DenseOfIndexed(int length, IEnumerable> enumerable) + where T : struct, IEquatable, IFormattable + { + return Vector.Build.DenseOfIndexed(length, enumerable); + } + + /// + /// Create a new sparse vector straight from an initialized vector storage instance. + /// The storage is used directly without copying. + /// Intended for advanced scenarios where you're working directly with + /// storage for performance or interop reasons. + /// + public static Vector Sparse(SparseVectorStorage storage) + where T : struct, IEquatable, IFormattable + { + return Vector.Build.Sparse(storage); + } + + /// + /// Create a sparse vector of T with the given size. + /// + /// The size of the vector. + public static Vector Sparse(int size) + where T : struct, IEquatable, IFormattable + { + return Vector.Build.Sparse(size); + } + + /// + /// Create a new sparse vector and initialize each value using the provided value. + /// + public static Vector Sparse(int length, T value) + where T : struct, IEquatable, IFormattable + { + return Vector.Build.Sparse(length, value); + } + + /// + /// Create a new sparse vector and initialize each value using the provided init function. + /// + public static Vector Sparse(int length, Func init) + where T : struct, IEquatable, IFormattable + { + return Vector.Build.Sparse(length, init); + } + + /// + /// Create a new sparse vector as a copy of the given other vector. + /// This new vector will be independent from the other vector. + /// A new memory block will be allocated for storing the vector. + /// + public static Vector SparseOfVector(Vector vector) + where T : struct, IEquatable, IFormattable + { + return Vector.Build.SparseOfVector(vector); + } + + /// + /// Create a new sparse vector as a copy of the given array. + /// This new vector will be independent from the array. + /// A new memory block will be allocated for storing the vector. + /// + public static Vector SparseOfArray(T[] array) + where T : struct, IEquatable, IFormattable + { + return Vector.Build.SparseOfArray(array); + } + + /// + /// Create a new sparse vector as a copy of the given enumerable. + /// This new vector will be independent from the enumerable. + /// A new memory block will be allocated for storing the vector. + /// + public static Vector SparseOfEnumerable(IEnumerable enumerable) + where T : struct, IEquatable, IFormattable + { + return Vector.Build.SparseOfEnumerable(enumerable); + } + + /// + /// Create a new sparse vector as a copy of the given indexed enumerable. + /// Keys must be provided at most once, zero is assumed if a key is omitted. + /// This new vector will be independent from the enumerable. + /// A new memory block will be allocated for storing the vector. + /// + public static Vector SparseOfIndexed(int length, IEnumerable> enumerable) + where T : struct, IEquatable, IFormattable + { + return Vector.Build.SparseOfIndexed(length, enumerable); + } + } +} diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj index a5a42d96..941675c9 100644 --- a/src/Numerics/Numerics.csproj +++ b/src/Numerics/Numerics.csproj @@ -97,8 +97,10 @@ + +