forked from tsai/mathnet-numerics
10 changed files with 659 additions and 2 deletions
@ -0,0 +1,127 @@ |
|||
// <copyright file="DenseMatrix.fs" company="Math.NET"> |
|||
// Math.NET Numerics, part of the Math.NET Project |
|||
// http://mathnet.opensourcedotnet.info |
|||
// |
|||
// Copyright (c) 2009 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> |
|||
|
|||
namespace MathNet.Numerics.LinearAlgebra.Double |
|||
|
|||
open MathNet.Numerics.LinearAlgebra |
|||
|
|||
/// A module which implements functional dense vector operations. |
|||
module DenseMatrix = |
|||
|
|||
/// Initialize a matrix by calling a construction function for every element. |
|||
let inline init (n: int) (m: int) f = |
|||
let A = new DenseMatrix(n,m) |
|||
for i=0 to n-1 do |
|||
for j=0 to m-1 do |
|||
A.[i,j] <- f i j |
|||
A |
|||
|
|||
/// Create the identity matrix. |
|||
let inline identity (n:int) = |
|||
let A = new DenseMatrix(n, n, 0.0) |
|||
for i=0 to n-1 do |
|||
A.[i,i] <- 1.0 |
|||
A |
|||
|
|||
/// Create a matrix from a list of float lists. Every list in the master list specifies a row. |
|||
let inline of_list (fll: float list list) = |
|||
let n = List.length fll |
|||
let m = List.length (List.hd fll) |
|||
let A = DenseMatrix(n,m) |
|||
fll |> List.iteri (fun i fl -> |
|||
if (List.length fl) <> m then failwith "Each subrow must be of the same length." else |
|||
List.iteri (fun j f -> A.[i,j] <- f) fl) |
|||
A |
|||
|
|||
/// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a row. |
|||
let inline of_seq (fss: #seq<#seq<float>>) = |
|||
let n = Seq.length fss |
|||
let m = Seq.length (Seq.hd fss) |
|||
let A = DenseMatrix(n,m) |
|||
fss |> Seq.iteri (fun i fs -> |
|||
if (Seq.length fs) <> m then failwith "Each subrow must be of the same length." else |
|||
Seq.iteri (fun j f -> A.[i,j] <- f) fs) |
|||
A |
|||
|
|||
/// Create a matrix from a 2D array of floating point numbers. |
|||
let inline of_array2 (arr: float[,]) = new DenseMatrix(arr) |
|||
|
|||
/// Create a matrix with the given entries. |
|||
let inline init_dense (n: int) (m: int) (es: #seq<int * int * float>) = |
|||
let A = new DenseMatrix(n,m) |
|||
Seq.iter (fun (i,j,f) -> A.[i,j] <- f) es |
|||
A |
|||
|
|||
/// Create a square matrix with constant diagonal entries. |
|||
let inline constDiag (n: int) (f: float) = |
|||
let A = new DenseMatrix(n,n) |
|||
for i=0 to n-1 do |
|||
A.[i,i] <- f |
|||
A |
|||
|
|||
/// Create a square matrix with the vector elements on the diagonal. |
|||
let inline diag (v: #Vector) = |
|||
let n = v.Count |
|||
let A = new DenseMatrix(n,n) |
|||
for i=0 to n-1 do |
|||
A.[i,i] <- v.Item(i) |
|||
A |
|||
|
|||
(* |
|||
|
|||
/// Initialize a matrix by calling a construction function for every row. |
|||
let inline init_row (n: int) (m: int) (f: int -> #Vector) = |
|||
let A = new DenseMatrix(n,m) |
|||
for i=0 to n-1 do |
|||
let row = f i |
|||
if row.Count <> m then failwith "Row generator does not create rows of the appropriate size." |
|||
A.SetRow(i, row) |
|||
A |
|||
|
|||
/// Initialize a matrix by calling a construction function for every column. |
|||
let inline init_col (n: int) (m: int) (f: int -> #Vector) = |
|||
let A = new DenseMatrix(n,m) |
|||
for i=0 to m-1 do |
|||
let col = f i |
|||
if col.Count <> n then failwith "Column generator does not create columns of the appropriate size." |
|||
A.SetColumn(i, col) |
|||
A |
|||
|
|||
/// Create a 1xn dimensional matrix from a row vector. |
|||
let inline of_rowvector (v: #Vector) = |
|||
let n = v.Count |
|||
let A = new DenseMatrix(1, n) |
|||
A.SetRow(0, v) |
|||
A |
|||
|
|||
/// Create an nx1 dimensional matrix from a column vector. |
|||
let inline of_vector (v: #Vector) = |
|||
let n = v.Count |
|||
let A = new DenseMatrix(n, 1) |
|||
A.SetColumn(0, v) |
|||
A*) |
|||
@ -0,0 +1,164 @@ |
|||
// <copyright file="DenseMatrix.cs" company="Math.NET">
|
|||
// Math.NET Numerics, part of the Math.NET Project
|
|||
// http://mathnet.opensourcedotnet.info
|
|||
//
|
|||
// Copyright (c) 2009 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>
|
|||
|
|||
namespace MathNet.Numerics.LinearAlgebra.Double |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Globalization; |
|||
using Algorithms; |
|||
using Algorithms.LinearAlgebra; |
|||
using NumberTheory; |
|||
using Properties; |
|||
using Threading; |
|||
|
|||
/// <summary>
|
|||
/// A Matrix class with dense storage.
|
|||
/// </summary>
|
|||
public class DenseMatrix : Matrix |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DenseMatrix"/> class. This matrix is square with a given size.
|
|||
/// </summary>
|
|||
/// <param name="order">the size of the square matrix.</param>
|
|||
/// <exception cref="ArgumentException">
|
|||
/// If <paramref name="size"/> is less than one.
|
|||
/// </exception>
|
|||
public DenseMatrix(int order) |
|||
: base(order) |
|||
{ |
|||
Data = new double[order*order]; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DenseMatrix"/> class.
|
|||
/// </summary>
|
|||
/// <param name="rows">
|
|||
/// The number of rows.
|
|||
/// </param>
|
|||
/// <param name="columns">
|
|||
/// The number of columns.
|
|||
/// </param>
|
|||
public DenseMatrix(int rows, int columns) |
|||
: base(rows, columns) |
|||
{ |
|||
Data = new double[rows * columns]; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DenseMatrix"/> class with all entries set to a particular value.
|
|||
/// </summary>
|
|||
/// <param name="rows">
|
|||
/// The number of rows.
|
|||
/// </param>
|
|||
/// <param name="columns">
|
|||
/// The number of columns.
|
|||
/// </param
|
|||
/// <param name="value">The value which we assign to each element of the matrix.</param>
|
|||
public DenseMatrix(int rows, int columns, double value) |
|||
: base(rows, columns) |
|||
{ |
|||
Data = new double[rows * columns]; |
|||
for (int i = 0; i < Data.Length; i++) |
|||
{ |
|||
Data[i] = value; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DenseMatrix"/> class from a 2D array.
|
|||
/// </summary>
|
|||
/// <param name="array">The 2D array to create this matrix from.</param>
|
|||
public DenseMatrix(double[,] array) |
|||
: base(array.GetLength(0), array.GetLength(1)) |
|||
{ |
|||
throw new NotImplementedException(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the matrix's data.
|
|||
/// </summary>
|
|||
/// <value>The matrix's data.</value>
|
|||
internal double[] Data |
|||
{ |
|||
get; |
|||
private set; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Creates a <strong>DenseMatrix</strong> for the given number of rows and columns.
|
|||
/// </summary>
|
|||
/// <param name="numberOfRows">
|
|||
/// The number of rows.
|
|||
/// </param>
|
|||
/// <param name="numberOfColumns">
|
|||
/// The number of columns.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// A <strong>DenseMatrix</strong> with the given dimensions.
|
|||
/// </returns>
|
|||
public override Matrix CreateMatrix(int numberOfRows, int numberOfColumns) |
|||
{ |
|||
return new DenseMatrix(numberOfRows, numberOfColumns); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Retrieves the requested element without range checking.
|
|||
/// </summary>
|
|||
/// <param name="row">
|
|||
/// The row of the element.
|
|||
/// </param>
|
|||
/// <param name="column">
|
|||
/// The column of the element.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// The requested element.
|
|||
/// </returns>
|
|||
public override double At(int row, int column) |
|||
{ |
|||
return Data[column * RowCount + row]; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Sets the value of the given element.
|
|||
/// </summary>
|
|||
/// <param name="row">
|
|||
/// The row of the element.
|
|||
/// </param>
|
|||
/// <param name="column">
|
|||
/// The column of the element.
|
|||
/// </param>
|
|||
/// <param name="value">
|
|||
/// The value to set the element to.
|
|||
/// </param>
|
|||
public override void At(int row, int column, double value) |
|||
{ |
|||
Data[column * RowCount + row] = value; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,52 @@ |
|||
using System.Collections.Generic; |
|||
using MbUnit.Framework; |
|||
|
|||
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double |
|||
{ |
|||
using LinearAlgebra.Double; |
|||
|
|||
public class DenseMatrixTests : MatrixTests |
|||
{ |
|||
protected override Matrix CreateMatrix(int rows, int columns) |
|||
{ |
|||
return new DenseMatrix(rows, columns); |
|||
} |
|||
|
|||
protected override Matrix CreateMatrix(double[,] data) |
|||
{ |
|||
return new DenseMatrix(data); |
|||
} |
|||
|
|||
[Test] |
|||
[Row("Singular3x3")] |
|||
[Row("Singular3x3")] |
|||
[Row("Square3x3")] |
|||
[Row("Square4x4")] |
|||
[Row("Tall3x2")] |
|||
[Row("Wide2x3")] |
|||
public void CanCreateMatrixFromArray(string name) |
|||
{ |
|||
var matrix = new DenseMatrix(testData[name]); |
|||
for (var i = 0; i < testData[name].GetLength(0); i++) |
|||
{ |
|||
for (var j = 0; j < testData[name].GetLength(0); j++) |
|||
{ |
|||
Assert.AreEqual(testData[name][i,j], matrix[i,j]); |
|||
} |
|||
} |
|||
} |
|||
|
|||
[Test] |
|||
public void CanCreateMatrixWithUniformValues() |
|||
{ |
|||
var matrix = new DenseMatrix(10, 10, 10.0); |
|||
for (var i = 0; i < matrix.RowCount; i++) |
|||
{ |
|||
for (var j = 0; j < matrix.ColumnCount; j++) |
|||
{ |
|||
Assert.AreEqual(matrix[i, j], 10.0); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,162 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Globalization; |
|||
using MathNet.Numerics.LinearAlgebra.Double; |
|||
using MbUnit.Framework; |
|||
|
|||
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double |
|||
{ |
|||
public abstract partial class MatrixTests |
|||
{ |
|||
protected Dictionary<string, double[,]> testData; |
|||
protected Dictionary<string, Matrix> testMatrices; |
|||
|
|||
protected abstract Matrix CreateMatrix(int rows, int columns); |
|||
protected abstract Matrix CreateMatrix(double[,] data); |
|||
|
|||
[SetUp] |
|||
public void SetupDistributions() |
|||
{ |
|||
testData.Add("Singular3x3", new double[,] { { 1, 1, 2 }, { 1, 1, 2 }, { 1, 1, 2 } }); |
|||
testData.Add("Square3x3", new double[,] { { -1.1, -2.2, -3.3 }, { 0, 1.1, 2.2 }, { -4.4, 5.5, 6.6 } }); |
|||
testData.Add("Square4x4", new double[,] { { -1.1, -2.2, -3.3, -4.4 }, { 0, 1.1, 2.2, 3.3 }, { -4.4, 5.5, 6.6, -7.7 } }); |
|||
testData.Add("Tall3x2", new double[,] { { -1.1, -2.2 }, { 0, 1.1 }, { -4.4, 5.5 } }); |
|||
testData.Add("Wide2x3", new double[,] { { -1.1, -2.2, -3.3 }, { 0, 1.1, 2.2 } }); |
|||
|
|||
foreach(var name in testData.Keys) |
|||
{ |
|||
testMatrices.Add(name, CreateMatrix(testData[name])); |
|||
} |
|||
} |
|||
|
|||
[Test] |
|||
[Row("Singular3x3")] |
|||
[Row("Square3x3")] |
|||
[Row("Square4x4")] |
|||
[Row("Tall3x2")] |
|||
[Row("Wide2x3")] |
|||
[MultipleAsserts] |
|||
public void CanCloneMatrix(string name) |
|||
{ |
|||
var matrix = CreateMatrix(testData[name]); |
|||
var clone = matrix.Clone(); |
|||
|
|||
Assert.AreNotSame(matrix, clone); |
|||
Assert.AreEqual(matrix.RowCount, clone.RowCount); |
|||
Assert.AreEqual(matrix.ColumnCount, clone.ColumnCount); |
|||
for (var i = 0; i < matrix.RowCount; i++) |
|||
{ |
|||
for (var j = 0; j < matrix.RowCount; j++) |
|||
{ |
|||
Assert.AreEqual(matrix[i,j], clone[i,j]); |
|||
} |
|||
} |
|||
} |
|||
|
|||
[Test] |
|||
[Row("Singular3x3")] |
|||
[Row("Square3x3")] |
|||
[Row("Square4x4")] |
|||
[Row("Tall3x2")] |
|||
[Row("Wide2x3")] |
|||
[MultipleAsserts] |
|||
public void CanCloneMatrixUsingICloneable(string name) |
|||
{ |
|||
var matrix = CreateMatrix(testData[name]); |
|||
var clone = (Matrix)((ICloneable)matrix).Clone(); |
|||
|
|||
Assert.AreNotSame(matrix, clone); |
|||
Assert.AreEqual(matrix.RowCount, clone.RowCount); |
|||
Assert.AreEqual(matrix.ColumnCount, clone.ColumnCount); |
|||
for (var i = 0; i < matrix.RowCount; i++) |
|||
{ |
|||
for (var j = 0; j < matrix.RowCount; j++) |
|||
{ |
|||
Assert.AreEqual(matrix[i, j], clone[i, j]); |
|||
} |
|||
} |
|||
} |
|||
|
|||
[Test] |
|||
[Ignore] |
|||
public void CanConvertVectorToString() |
|||
{ |
|||
} |
|||
|
|||
[Test] |
|||
public void CanCreateMatrix() |
|||
{ |
|||
var expected = CreateMatrix(5, 6); |
|||
var actual = expected.CreateMatrix(5, 6); |
|||
Assert.AreEqual(expected.GetType(), actual.GetType(), "Matrices are same type."); |
|||
} |
|||
|
|||
[Test] |
|||
[Row("Singular3x3")] |
|||
[Row("Square3x3")] |
|||
[Row("Square4x4")] |
|||
[Row("Tall3x2")] |
|||
[Row("Wide2x3")] |
|||
[MultipleAsserts] |
|||
public void CanEquateMatrices(string name) |
|||
{ |
|||
var matrix1 = CreateMatrix(testData[name]); |
|||
var matrix2 = CreateMatrix(testData[name]); |
|||
var matrix3 = CreateMatrix(testData[name].GetLength(0), testData[name].GetLength(1)); |
|||
Assert.IsTrue(matrix1.Equals(matrix1)); |
|||
Assert.IsTrue(matrix1.Equals(matrix2)); |
|||
Assert.IsFalse(matrix1.Equals(matrix3)); |
|||
Assert.IsFalse(matrix1.Equals(null)); |
|||
} |
|||
|
|||
[Test] |
|||
[Row(0, 2)] |
|||
[Row(-1, 1)] |
|||
[ExpectedArgumentException] |
|||
public void ThrowsArgumentExceptionIfSizeIsNotPositive(int rows, int columns) |
|||
{ |
|||
var A = CreateMatrix(rows, columns); |
|||
} |
|||
|
|||
[Test] |
|||
[Row("Singular3x3")] |
|||
[Row("Square3x3")] |
|||
[Row("Square4x4")] |
|||
[Row("Tall3x2")] |
|||
[Row("Wide2x3")] |
|||
public void TestingForEqualityWithNonMatrixReturnsFalse(string name) |
|||
{ |
|||
var matrix = CreateMatrix(testData[name]); |
|||
Assert.IsFalse(matrix.Equals(2)); |
|||
} |
|||
|
|||
[Test] |
|||
[Row("Singular3x3")] |
|||
[Row("Square3x3")] |
|||
[Row("Square4x4")] |
|||
[Row("Tall3x2")] |
|||
[Row("Wide2x3")] |
|||
public void CanTestForEqualityUsingObjectEquals(string name) |
|||
{ |
|||
var matrix1 = CreateMatrix(testData[name]); |
|||
var matrix2 = CreateMatrix(testData[name]); |
|||
Assert.IsTrue(matrix1.Equals((object)matrix2)); |
|||
} |
|||
|
|||
[Test] |
|||
[Row(-1, 1, "Singular3x3")] |
|||
[Row(1, -1, "Singular3x3")] |
|||
[Row(4, 2, "Square3x3")] |
|||
[ExpectedException(typeof(ArgumentOutOfRangeException))] |
|||
public void RangeCheckFails(int i, int j, string name) |
|||
{ |
|||
var d = testMatrices[name][i, j]; |
|||
} |
|||
|
|||
[Test] |
|||
[Ignore] |
|||
public void MatrixGetHashCode() |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue