Browse Source

LA: Static CreateMatrix/Vector: simpler to use than builders and with better type inferrence

pull/308/merge
Christoph Ruegg 11 years ago
parent
commit
1d7ce24d3e
  1. 2
      src/Numerics/LinearAlgebra/Builder.cs
  2. 954
      src/Numerics/LinearAlgebra/CreateMatrix.cs
  3. 313
      src/Numerics/LinearAlgebra/CreateVector.cs
  4. 2
      src/Numerics/Numerics.csproj

2
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.
/// </summary>
/// <seealso href="http://en.wikipedia.org/wiki/Row-major_order"/>
public Matrix<T> Diagonal(int rows, int columns, T[] storage)
{
return Diagonal(new DiagonalMatrixStorage<T>(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.
/// </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));

954
src/Numerics/LinearAlgebra/CreateMatrix.cs

@ -0,0 +1,954 @@
// <copyright file="CreateMatrix.cs" company="Math.NET">
// 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.
// </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra.Storage;
namespace MathNet.Numerics.LinearAlgebra
{
public static class CreateMatrix
{
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> WithStorage<T>(MatrixStorage<T> storage)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.OfStorage(storage);
}
/// <summary>
/// Create a new matrix with the same kind of the provided example.
/// </summary>
public static Matrix<T> SameAs<T,TU>(Matrix<TU> example, int rows, int columns, bool fullyMutable = false)
where T : struct, IEquatable<T>, IFormattable
where TU : struct, IEquatable<TU>, IFormattable
{
return Matrix<T>.Build.SameAs(example, rows, columns, fullyMutable);
}
/// <summary>
/// Create a new matrix with the same kind and dimensions of the provided example.
/// </summary>
public static Matrix<T> SameAs<T,TU>(Matrix<TU> example)
where T : struct, IEquatable<T>, IFormattable
where TU : struct, IEquatable<TU>, IFormattable
{
return Matrix<T>.Build.SameAs(example);
}
/// <summary>
/// Create a new matrix with the same kind of the provided example.
/// </summary>
public static Matrix<T> SameAs<T>(Vector<T> example, int rows, int columns)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SameAs(example, rows, columns);
}
/// <summary>
/// Create a new matrix with a type that can represent and is closest to both provided samples.
/// </summary>
public static Matrix<T> SameAs<T>(Matrix<T> example, Matrix<T> otherExample, int rows, int columns, bool fullyMutable = false)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SameAs(example, otherExample, rows, columns, fullyMutable);
}
/// <summary>
/// Create a new matrix with a type that can represent and is closest to both provided samples and the dimensions of example.
/// </summary>
public static Matrix<T> SameAs<T>(Matrix<T> example, Matrix<T> otherExample)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SameAs(example, otherExample);
}
/// <summary>
/// Create a new dense matrix with values sampled from the provided random distribution.
/// </summary>
public static Matrix<T> Random<T>(int rows, int columns, IContinuousDistribution distribution)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.Random(rows, columns, distribution);
}
/// <summary>
/// Create a new dense matrix with values sampled from the standard distribution with a system random source.
/// </summary>
public static Matrix<T> Random<T>(int rows, int columns)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.Random(rows, columns);
}
/// <summary>
/// Create a new dense matrix with values sampled from the standard distribution with a system random source.
/// </summary>
public static Matrix<T> Random<T>(int rows, int columns, int seed)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.Random(rows, columns, seed);
}
/// <summary>
/// Create a new positive definite dense matrix where each value is the product
/// of two samples from the provided random distribution.
/// </summary>
public static Matrix<T> RandomPositiveDefinite<T>(int order, IContinuousDistribution distribution)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.RandomPositiveDefinite(order, distribution);
}
/// <summary>
/// Create a new positive definite dense matrix where each value is the product
/// of two samples from the standard distribution.
/// </summary>
public static Matrix<T> RandomPositiveDefinite<T>(int order)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.RandomPositiveDefinite(order);
}
/// <summary>
/// Create a new positive definite dense matrix where each value is the product
/// of two samples from the provided random distribution.
/// </summary>
public static Matrix<T> RandomPositiveDefinite<T>(int order, int seed)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.RandomPositiveDefinite(order, seed);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> Dense<T>(DenseColumnMajorMatrixStorage<T> storage)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.Dense(storage);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> Dense<T>(int rows, int columns)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.Dense(rows, columns);
}
/// <summary>
/// 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.
/// </summary>
/// <seealso href="http://en.wikipedia.org/wiki/Row-major_order"/>
public static Matrix<T> Dense<T>(int rows, int columns, T[] storage)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.Dense(rows, columns, storage);
}
/// <summary>
/// Create a new dense matrix and initialize each value to the same provided value.
/// </summary>
public static Matrix<T> Dense<T>(int rows, int columns, T value)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.Dense(rows, columns, value);
}
/// <summary>
/// Create a new dense matrix and initialize each value using the provided init function.
/// </summary>
public static Matrix<T> Dense<T>(int rows, int columns, Func<int, int, T> init)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.Dense(rows, columns, init);
}
/// <summary>
/// Create a new diagonal dense matrix and initialize each diagonal value to the same provided value.
/// </summary>
public static Matrix<T> DenseDiagonal<T>(int rows, int columns, T value)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DenseDiagonal(rows, columns, value);
}
/// <summary>
/// Create a new diagonal dense matrix and initialize each diagonal value to the same provided value.
/// </summary>
public static Matrix<T> DenseDiagonal<T>(int order, T value)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DenseDiagonal(order, value);
}
/// <summary>
/// Create a new diagonal dense matrix and initialize each diagonal value using the provided init function.
/// </summary>
public static Matrix<T> DenseDiagonal<T>(int rows, int columns, Func<int, T> init)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DenseDiagonal(rows, columns, init);
}
/// <summary>
/// Create a new diagonal dense identity matrix with a one-diagonal.
/// </summary>
public static Matrix<T> DenseIdentity<T>(int rows, int columns)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DenseIdentity(rows, columns);
}
/// <summary>
/// Create a new diagonal dense identity matrix with a one-diagonal.
/// </summary>
public static Matrix<T> DenseIdentity<T>(int order)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DenseIdentity(order);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> DenseOfMatrix<T>(Matrix<T> matrix)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DenseOfMatrix(matrix);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> DenseOfArray<T>(T[,] array)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DenseOfArray(array);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> DenseOfIndexed<T>(int rows, int columns, IEnumerable<Tuple<int, int, T>> enumerable)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DenseOfIndexed(rows, columns, enumerable);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> DenseOfColumnMajor<T>(int rows, int columns, IEnumerable<T> columnMajor)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DenseOfColumnMajor(rows, columns, columnMajor);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> DenseOfColumns<T>(IEnumerable<IEnumerable<T>> data)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DenseOfColumns(data);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> DenseOfColumns<T>(int rows, int columns, IEnumerable<IEnumerable<T>> data)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DenseOfColumns(rows, columns, data);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> DenseOfColumnArrays<T>(params T[][] columns)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DenseOfColumnArrays(columns);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> DenseOfColumnArrays<T>(IEnumerable<T[]> columns)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DenseOfColumnArrays(columns);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> DenseOfColumnVectors<T>(params Vector<T>[] columns)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DenseOfColumnVectors(columns);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> DenseOfColumnVectors<T>(IEnumerable<Vector<T>> columns)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DenseOfColumnVectors(columns);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> DenseOfRows<T>(IEnumerable<IEnumerable<T>> data)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DenseOfRows(data);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> DenseOfRows<T>(int rows, int columns, IEnumerable<IEnumerable<T>> data)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DenseOfRows(rows, columns, data);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> DenseOfRowArrays<T>(params T[][] rows)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DenseOfRowArrays(rows);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> DenseOfRowArrays<T>(IEnumerable<T[]> rows)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DenseOfRowArrays(rows);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> DenseOfRowVectors<T>(params Vector<T>[] rows)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DenseOfRowVectors(rows);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> DenseOfRowVectors<T>(IEnumerable<Vector<T>> rows)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DenseOfRowVectors(rows);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> DenseOfDiagonalVector<T>(Vector<T> diagonal)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DenseOfDiagonalVector(diagonal);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> DenseOfDiagonalVector<T>(int rows, int columns, Vector<T> diagonal)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DenseOfDiagonalVector(rows, columns, diagonal);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> DenseOfDiagonalArray<T>(T[] diagonal)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DenseOfDiagonalArray(diagonal);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> DenseOfDiagonalArray<T>(int rows, int columns, T[] diagonal)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DenseOfDiagonalArray(rows, columns, diagonal);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> DenseOfMatrixArray<T>(Matrix<T>[,] matrices)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DenseOfMatrixArray(matrices);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> Sparse<T>(SparseCompressedRowMatrixStorage<T> storage)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.Sparse(storage);
}
/// <summary>
/// Create a sparse matrix of T with the given number of rows and columns.
/// </summary>
/// <param name="rows">The number of rows.</param>
/// <param name="columns">The number of columns.</param>
public static Matrix<T> Sparse<T>(int rows, int columns)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.Sparse(rows, columns);
}
/// <summary>
/// Create a new sparse matrix and initialize each value to the same provided value.
/// </summary>
public static Matrix<T> Sparse<T>(int rows, int columns, T value)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.Sparse(rows, columns, value);
}
/// <summary>
/// Create a new sparse matrix and initialize each value using the provided init function.
/// </summary>
public static Matrix<T> Sparse<T>(int rows, int columns, Func<int, int, T> init)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.Sparse(rows, columns, init);
}
/// <summary>
/// Create a new diagonal sparse matrix and initialize each diagonal value to the same provided value.
/// </summary>
public static Matrix<T> SparseDiagonal<T>(int rows, int columns, T value)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseDiagonal(rows, columns, value);
}
/// <summary>
/// Create a new diagonal sparse matrix and initialize each diagonal value to the same provided value.
/// </summary>
public static Matrix<T> SparseDiagonal<T>(int order, T value)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseDiagonal(order, value);
}
/// <summary>
/// Create a new diagonal sparse matrix and initialize each diagonal value using the provided init function.
/// </summary>
public static Matrix<T> SparseDiagonal<T>(int rows, int columns, Func<int, T> init)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseDiagonal(rows, columns, init);
}
/// <summary>
/// Create a new diagonal dense identity matrix with a one-diagonal.
/// </summary>
public static Matrix<T> SparseIdentity<T>(int rows, int columns)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseIdentity(rows, columns);
}
/// <summary>
/// Create a new diagonal dense identity matrix with a one-diagonal.
/// </summary>
public static Matrix<T> SparseIdentity<T>(int order)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseIdentity(order);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> SparseOfMatrix<T>(Matrix<T> matrix)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseOfMatrix(matrix);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> SparseOfArray<T>(T[,] array)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseOfArray(array);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> SparseOfIndexed<T>(int rows, int columns, IEnumerable<Tuple<int, int, T>> enumerable)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseOfIndexed(rows, columns, enumerable);
}
/// <summary>
/// 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.
/// </summary>
/// <seealso href="http://en.wikipedia.org/wiki/Row-major_order"/>
public static Matrix<T> SparseOfRowMajor<T>(int rows, int columns, IEnumerable<T> rowMajor)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseOfRowMajor(rows, columns, rowMajor);
}
/// <summary>
/// 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.
/// </summary>
/// <seealso href="http://en.wikipedia.org/wiki/Row-major_order"/>
public static Matrix<T> SparseOfColumnMajor<T>(int rows, int columns, IList<T> columnMajor)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseOfColumnMajor(rows, columns, columnMajor);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> SparseOfColumns<T>(IEnumerable<IEnumerable<T>> data)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseOfColumns(data);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> SparseOfColumns<T>(int rows, int columns, IEnumerable<IEnumerable<T>> data)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseOfColumns(rows, columns, data);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> SparseOfColumnArrays<T>(params T[][] columns)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseOfColumnArrays(columns);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> SparseOfColumnArrays<T>(IEnumerable<T[]> columns)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseOfColumnArrays(columns);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> SparseOfColumnVectors<T>(params Vector<T>[] columns)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseOfColumnVectors(columns);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> SparseOfColumnVectors<T>(IEnumerable<Vector<T>> columns)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseOfColumnVectors(columns);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> SparseOfRows<T>(IEnumerable<IEnumerable<T>> data)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseOfRows(data);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> SparseOfRows<T>(int rows, int columns, IEnumerable<IEnumerable<T>> data)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseOfRows(rows, columns, data);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> SparseOfRowArrays<T>(params T[][] rows)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseOfRowArrays(rows);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> SparseOfRowArrays<T>(IEnumerable<T[]> rows)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseOfRowArrays(rows);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> SparseOfRowVectors<T>(params Vector<T>[] rows)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseOfRowVectors(rows);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> SparseOfRowVectors<T>(IEnumerable<Vector<T>> rows)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseOfRowVectors(rows);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> SparseOfDiagonalVector<T>(Vector<T> diagonal)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseOfDiagonalVector(diagonal);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> SparseOfDiagonalVector<T>(int rows, int columns, Vector<T> diagonal)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseOfDiagonalVector(rows, columns, diagonal);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> SparseOfDiagonalArray<T>(T[] diagonal)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseOfDiagonalArray(diagonal);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> SparseOfDiagonalArray<T>(int rows, int columns, T[] diagonal)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseOfDiagonalArray(rows, columns, diagonal);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> SparseOfMatrixArray<T>(Matrix<T>[,] matrices)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.SparseOfMatrixArray(matrices);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> Diagonal<T>(DiagonalMatrixStorage<T> storage)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.Diagonal(storage);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> Diagonal<T>(int rows, int columns)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.Diagonal(rows, columns);
}
/// <summary>
/// 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.
/// </summary>
public static Matrix<T> Diagonal<T>(int rows, int columns, T[] storage)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.Diagonal(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>
public static Matrix<T> Diagonal<T>(T[] storage)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.Diagonal(storage);
}
/// <summary>
/// Create a new diagonal matrix and initialize each diagonal value to the same provided value.
/// </summary>
public static Matrix<T> Diagonal<T>(int rows, int columns, T value)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.Diagonal(rows, columns, value);
}
/// <summary>
/// Create a new diagonal matrix and initialize each diagonal value using the provided init function.
/// </summary>
public static Matrix<T> Diagonal<T>(int rows, int columns, Func<int, T> init)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.Diagonal(rows, columns, init);
}
/// <summary>
/// Create a new diagonal identity matrix with a one-diagonal.
/// </summary>
public static Matrix<T> DiagonalIdentity<T>(int rows, int columns)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DiagonalIdentity(rows, columns);
}
/// <summary>
/// Create a new diagonal identity matrix with a one-diagonal.
/// </summary>
public static Matrix<T> DiagonalIdentity<T>(int order)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DiagonalIdentity(order);
}
/// <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 static Matrix<T> DiagonalOfDiagonalVector<T>(Vector<T> diagonal)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DiagonalOfDiagonalVector(diagonal);
}
/// <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 static Matrix<T> DiagonalOfDiagonalVector<T>(int rows, int columns, Vector<T> diagonal)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DiagonalOfDiagonalVector(rows, columns, diagonal);
}
/// <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 static Matrix<T> DiagonalOfDiagonalArray<T>(T[] diagonal)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DiagonalOfDiagonalArray(diagonal);
}
/// <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 static Matrix<T> DiagonalOfDiagonalArray<T>(int rows, int columns, T[] diagonal)
where T : struct, IEquatable<T>, IFormattable
{
return Matrix<T>.Build.DiagonalOfDiagonalArray(rows, columns, diagonal);
}
}
}

313
src/Numerics/LinearAlgebra/CreateVector.cs

@ -0,0 +1,313 @@
// <copyright file="CreateVector.cs" company="Math.NET">
// 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.
// </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra.Storage;
namespace MathNet.Numerics.LinearAlgebra
{
public static class CreateVector
{
/// <summary>
/// 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.
/// </summary>
public static Vector<T> WithStorage<T>(VectorStorage<T> storage)
where T : struct, IEquatable<T>, IFormattable
{
return Vector<T>.Build.OfStorage(storage);
}
/// <summary>
/// Create a new vector with the same kind of the provided example.
/// </summary>
public static Vector<T> SameAs<T,TU>(Vector<TU> example, int length)
where T : struct, IEquatable<T>, IFormattable
where TU : struct, IEquatable<TU>, IFormattable
{
return Vector<T>.Build.SameAs(example, length);
}
/// <summary>
/// Create a new vector with the same kind and dimension of the provided example.
/// </summary>
public static Vector<T> SameAs<T,TU>(Vector<TU> example)
where T : struct, IEquatable<T>, IFormattable
where TU : struct, IEquatable<TU>, IFormattable
{
return Vector<T>.Build.SameAs(example);
}
/// <summary>
/// Create a new vector with the same kind of the provided example.
/// </summary>
public static Vector<T> SameAs<T,TU>(Matrix<TU> example, int length)
where T : struct, IEquatable<T>, IFormattable
where TU : struct, IEquatable<TU>, IFormattable
{
return Vector<T>.Build.SameAs(example, length);
}
/// <summary>
/// Create a new vector with a type that can represent and is closest to both provided samples.
/// </summary>
public static Vector<T> SameAs<T>(Vector<T> example, Vector<T> otherExample, int length)
where T : struct, IEquatable<T>, IFormattable
{
return Vector<T>.Build.SameAs(example, otherExample, length);
}
/// <summary>
/// Create a new vector with a type that can represent and is closest to both provided samples and the dimensions of example.
/// </summary>
public static Vector<T> SameAs<T>(Vector<T> example, Vector<T> otherExample)
where T : struct, IEquatable<T>, IFormattable
{
return Vector<T>.Build.SameAs(example, otherExample);
}
/// <summary>
/// Create a new vector with a type that can represent and is closest to both provided samples.
/// </summary>
public static Vector<T> SameAs<T>(Matrix<T> matrix, Vector<T> vector, int length)
where T : struct, IEquatable<T>, IFormattable
{
return Vector<T>.Build.SameAs(matrix, vector, length);
}
/// <summary>
/// Create a new dense vector with values sampled from the provided random distribution.
/// </summary>
public static Vector<T> Random<T>(int length, IContinuousDistribution distribution)
where T : struct, IEquatable<T>, IFormattable
{
return Vector<T>.Build.Random(length, distribution);
}
/// <summary>
/// Create a new dense vector with values sampled from the standard distribution with a system random source.
/// </summary>
public static Vector<T> Random<T>(int length)
where T : struct, IEquatable<T>, IFormattable
{
return Vector<T>.Build.Random(length);
}
/// <summary>
/// Create a new dense vector with values sampled from the standard distribution with a system random source.
/// </summary>
public static Vector<T> Random<T>(int length, int seed)
where T : struct, IEquatable<T>, IFormattable
{
return Vector<T>.Build.Random(length, seed);
}
/// <summary>
/// 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.
/// </summary>
public static Vector<T> Dense<T>(DenseVectorStorage<T> storage)
where T : struct, IEquatable<T>, IFormattable
{
return Vector<T>.Build.Dense(storage);
}
/// <summary>
/// Create a dense vector of T with the given size.
/// </summary>
/// <param name="size">The size of the vector.</param>
public static Vector<T> Dense<T>(int size)
where T : struct, IEquatable<T>, IFormattable
{
return Vector<T>.Build.Dense(size);
}
/// <summary>
/// Create a dense vector of T that is directly bound to the specified array.
/// </summary>
public static Vector<T> Dense<T>(T[] array)
where T : struct, IEquatable<T>, IFormattable
{
return Vector<T>.Build.Dense(array);
}
/// <summary>
/// Create a new dense vector and initialize each value using the provided value.
/// </summary>
public static Vector<T> Dense<T>(int length, T value)
where T : struct, IEquatable<T>, IFormattable
{
return Vector<T>.Build.Dense(length, value);
}
/// <summary>
/// Create a new dense vector and initialize each value using the provided init function.
/// </summary>
public static Vector<T> Dense<T>(int length, Func<int, T> init)
where T : struct, IEquatable<T>, IFormattable
{
return Vector<T>.Build.Dense(length, init);
}
/// <summary>
/// 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.
/// </summary>
public static Vector<T> DenseOfVector<T>(Vector<T> vector)
where T : struct, IEquatable<T>, IFormattable
{
return Vector<T>.Build.DenseOfVector(vector);
}
/// <summary>
/// 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.
/// </summary>
public static Vector<T> DenseOfArray<T>(T[] array)
where T : struct, IEquatable<T>, IFormattable
{
return Vector<T>.Build.DenseOfArray(array);
}
/// <summary>
/// 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.
/// </summary>
public static Vector<T> DenseOfEnumerable<T>(IEnumerable<T> enumerable)
where T : struct, IEquatable<T>, IFormattable
{
return Vector<T>.Build.DenseOfEnumerable(enumerable);
}
/// <summary>
/// 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.
/// </summary>
public static Vector<T> DenseOfIndexed<T>(int length, IEnumerable<Tuple<int, T>> enumerable)
where T : struct, IEquatable<T>, IFormattable
{
return Vector<T>.Build.DenseOfIndexed(length, enumerable);
}
/// <summary>
/// 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.
/// </summary>
public static Vector<T> Sparse<T>(SparseVectorStorage<T> storage)
where T : struct, IEquatable<T>, IFormattable
{
return Vector<T>.Build.Sparse(storage);
}
/// <summary>
/// Create a sparse vector of T with the given size.
/// </summary>
/// <param name="size">The size of the vector.</param>
public static Vector<T> Sparse<T>(int size)
where T : struct, IEquatable<T>, IFormattable
{
return Vector<T>.Build.Sparse(size);
}
/// <summary>
/// Create a new sparse vector and initialize each value using the provided value.
/// </summary>
public static Vector<T> Sparse<T>(int length, T value)
where T : struct, IEquatable<T>, IFormattable
{
return Vector<T>.Build.Sparse(length, value);
}
/// <summary>
/// Create a new sparse vector and initialize each value using the provided init function.
/// </summary>
public static Vector<T> Sparse<T>(int length, Func<int, T> init)
where T : struct, IEquatable<T>, IFormattable
{
return Vector<T>.Build.Sparse(length, init);
}
/// <summary>
/// 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.
/// </summary>
public static Vector<T> SparseOfVector<T>(Vector<T> vector)
where T : struct, IEquatable<T>, IFormattable
{
return Vector<T>.Build.SparseOfVector(vector);
}
/// <summary>
/// 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.
/// </summary>
public static Vector<T> SparseOfArray<T>(T[] array)
where T : struct, IEquatable<T>, IFormattable
{
return Vector<T>.Build.SparseOfArray(array);
}
/// <summary>
/// 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.
/// </summary>
public static Vector<T> SparseOfEnumerable<T>(IEnumerable<T> enumerable)
where T : struct, IEquatable<T>, IFormattable
{
return Vector<T>.Build.SparseOfEnumerable(enumerable);
}
/// <summary>
/// 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.
/// </summary>
public static Vector<T> SparseOfIndexed<T>(int length, IEnumerable<Tuple<int, T>> enumerable)
where T : struct, IEquatable<T>, IFormattable
{
return Vector<T>.Build.SparseOfIndexed(length, enumerable);
}
}
}

2
src/Numerics/Numerics.csproj

@ -97,8 +97,10 @@
<Compile Include="Interpolation\QuadraticSpline.cs" />
<Compile Include="Interpolation\StepInterpolation.cs" />
<Compile Include="Interpolation\TransformedInterpolation.cs" />
<Compile Include="LinearAlgebra\CreateMatrix.cs" />
<Compile Include="LinearAlgebra\Options.cs" />
<Compile Include="LinearAlgebra\Solvers\DelegateStopCriterion.cs" />
<Compile Include="LinearAlgebra\CreateVector.cs" />
<Compile Include="LinearRegression\Options.cs" />
<Compile Include="Precision.Comparison.cs" />
<Compile Include="Precision.Equality.cs" />

Loading…
Cancel
Save