//
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// 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 IStation.Numerics.Distributions;
using IStation.Numerics.LinearAlgebra.Storage;
namespace IStation.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.
///
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);
}
///