Browse Source

Small incremental changes.

Added F# DenseMatrix.initRow and DenseMatrix.initCol
Renamed PointWiseXXX to PointwiseXXX
Added Matrix.SetRow and Matrix.SetCol
la-knuth
Jurgen Van Gael 17 years ago
parent
commit
510fa2c4c1
  1. 26
      src/FSharp/DenseMatrix.fs
  2. 20
      src/Numerics/LinearAlgebra/Double/DenseVector.cs
  3. 58
      src/Numerics/LinearAlgebra/Double/Matrix.cs
  4. 16
      src/Numerics/LinearAlgebra/Double/SparseVector.cs
  5. 16
      src/Numerics/LinearAlgebra/Double/Vector.cs
  6. 6
      src/Numerics/Version.cs
  7. 52
      src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs
  8. 4
      src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.cs
  9. 36
      src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs

26
src/FSharp/DenseMatrix.fs

@ -91,37 +91,21 @@ module DenseMatrix =
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 inline initRow (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)
for j=0 to m-1 do A.[i, j] <- row.[j]
A
/// Initialize a matrix by calling a construction function for every column.
let inline init_col (n: int) (m: int) (f: int -> #Vector) =
let inline initCol (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*)
for j=0 to n-1 do A.[j, i] <- col.[j]
A

20
src/Numerics/LinearAlgebra/Double/DenseVector.cs

@ -907,7 +907,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="other">The vector to pointwise multiply with this one.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override void PointWiseMultiply(Vector other)
public override void PointwiseMultiply(Vector other)
{
if (other == null)
{
@ -923,7 +923,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (denseVector == null)
{
base.PointWiseMultiply(other);
base.PointwiseMultiply(other);
}
else
{
@ -943,7 +943,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void PointWiseMultiply(Vector other, Vector result)
public override void PointwiseMultiply(Vector other, Vector result)
{
if (result == null)
{
@ -968,13 +968,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{
var tmp = result.CreateVector(result.Count);
this.PointWiseMultiply(other, tmp);
this.PointwiseMultiply(other, tmp);
tmp.CopyTo(result);
}
else
{
this.CopyTo(result);
result.PointWiseMultiply(other);
result.PointwiseMultiply(other);
}
}
@ -984,7 +984,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="other">The vector to pointwise divide this one by.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override void PointWiseDivide(Vector other)
public override void PointwiseDivide(Vector other)
{
if (other == null)
{
@ -1000,7 +1000,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (denseVector == null)
{
base.PointWiseMultiply(other);
base.PointwiseMultiply(other);
}
else
{
@ -1020,7 +1020,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void PointWiseDivide(Vector other, Vector result)
public override void PointwiseDivide(Vector other, Vector result)
{
if (result == null)
{
@ -1045,13 +1045,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{
var tmp = result.CreateVector(result.Count);
this.PointWiseDivide(other, tmp);
this.PointwiseDivide(other, tmp);
tmp.CopyTo(result);
}
else
{
this.CopyTo(result);
result.PointWiseDivide(other);
result.PointwiseDivide(other);
}
}

58
src/Numerics/LinearAlgebra/Double/Matrix.cs

@ -457,6 +457,64 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
}
/// <summary>
/// Copies the elements of a vector into a specific row. The length of the vector must match the number of columns
/// in the matrix.
/// </summary>
/// <param name="r">The row in which to copy <paramref name="v"/> into.</param>
/// <param name="v">The vector to copy from.</param>
public virtual void SetRow(int r, Vector v)
{
if (r < 0 || r >= RowCount)
{
throw new ArgumentException(Resources.ArgumentMatrixIndexOutOfRange, "r");
}
if (v == null)
{
throw new ArgumentNullException("v");
}
if (v.Count != ColumnCount)
{
throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixSameColumnDimension, "v");
}
for (int i = 0; i < v.Count; i++)
{
this[r, i] = v[i];
}
}
/// <summary>
/// Copies the elements of a vector into a specific column. The length of the vector must match the number of rows
/// in the matrix.
/// </summary>
/// <param name="c">The column in which to copy <paramref name="v"/> into.</param>
/// <param name="v">The vector to copy from.</param>
public virtual void SetColumn(int c, Vector v)
{
if (c < 0 || c >= RowCount)
{
throw new ArgumentException(Resources.ArgumentMatrixIndexOutOfRange, "r");
}
if (v == null)
{
throw new ArgumentNullException("v");
}
if (v.Count != ColumnCount)
{
throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixSameRowDimension, "v");
}
for (int i = 0; i < v.Count; i++)
{
this[i, c] = v[i];
}
}
/// <summary>
/// Returns a new matrix containing the lower triangle of this matrix.
/// </summary>

16
src/Numerics/LinearAlgebra/Double/SparseVector.cs

@ -1069,7 +1069,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="other">The vector to pointwise multiply with this one.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override void PointWiseMultiply(Vector other)
public override void PointwiseMultiply(Vector other)
{
if (other == null)
{
@ -1097,7 +1097,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void PointWiseMultiply(Vector other, Vector result)
public override void PointwiseMultiply(Vector other, Vector result)
{
if (result == null)
{
@ -1122,13 +1122,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{
var tmp = result.CreateVector(result.Count);
this.PointWiseMultiply(other, tmp);
this.PointwiseMultiply(other, tmp);
tmp.CopyTo(result);
}
else
{
this.CopyTo(result);
result.PointWiseMultiply(other);
result.PointwiseMultiply(other);
}
}
@ -1138,7 +1138,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="other">The vector to pointwise divide this one by.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override void PointWiseDivide(Vector other)
public override void PointwiseDivide(Vector other)
{
if (other == null)
{
@ -1166,7 +1166,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void PointWiseDivide(Vector other, Vector result)
public override void PointwiseDivide(Vector other, Vector result)
{
if (result == null)
{
@ -1191,13 +1191,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{
var tmp = result.CreateVector(result.Count);
this.PointWiseDivide(other, tmp);
this.PointwiseDivide(other, tmp);
tmp.CopyTo(result);
}
else
{
this.CopyTo(result);
result.PointWiseDivide(other);
result.PointwiseDivide(other);
}
}

16
src/Numerics/LinearAlgebra/Double/Vector.cs

@ -551,7 +551,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="other">The vector to pointwise multiply with this one.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public virtual void PointWiseMultiply(Vector other)
public virtual void PointwiseMultiply(Vector other)
{
if (other == null)
{
@ -578,7 +578,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public virtual void PointWiseMultiply(Vector other, Vector result)
public virtual void PointwiseMultiply(Vector other, Vector result)
{
if (result == null)
{
@ -603,13 +603,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{
var tmp = result.CreateVector(result.Count);
this.PointWiseMultiply(other, tmp);
this.PointwiseMultiply(other, tmp);
tmp.CopyTo(result);
}
else
{
this.CopyTo(result);
result.PointWiseMultiply(other);
result.PointwiseMultiply(other);
}
}
@ -619,7 +619,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="other">The vector to pointwise divide this one by.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public virtual void PointWiseDivide(Vector other)
public virtual void PointwiseDivide(Vector other)
{
if (other == null)
{
@ -646,7 +646,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public virtual void PointWiseDivide(Vector other, Vector result)
public virtual void PointwiseDivide(Vector other, Vector result)
{
if (result == null)
{
@ -671,13 +671,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{
var tmp = result.CreateVector(result.Count);
this.PointWiseDivide(other, tmp);
this.PointwiseDivide(other, tmp);
tmp.CopyTo(result);
}
else
{
this.CopyTo(result);
result.PointWiseDivide(other);
result.PointwiseDivide(other);
}
}

6
src/Numerics/Version.cs

@ -27,10 +27,10 @@
// </copyright>
/* This file is automatically generated - do not modify it. Change Version.tt instead.
Last generated on UTC 2010-06-09 08:16:54Z
Last generated on UTC 2010-06-10 18:41:04Z
*/
using System.Reflection;
[assembly: AssemblyVersion("2010.06.9.496")]
[assembly: AssemblyFileVersion("2010.06.9.496")]
[assembly: AssemblyVersion("2010.06.10.1121")]
[assembly: AssemblyFileVersion("2010.06.10.1121")]

52
src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs

@ -458,6 +458,58 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
matrix.GetColumn(0, 0, matrix.RowCount, result);
}
[Test]
[Row(0, "Singular3x3")]
[Row(1, "Singular3x3")]
[Row(2, "Singular3x3")]
[Row(2, "Square3x3")]
public void CanSetRow(int rowIndex, string name)
{
var matrix = (Matrix) testMatrices[name].Clone();
matrix.SetRow(rowIndex, new DenseVector(matrix.ColumnCount, 0.0));
for (int i = 0; i < matrix.RowCount; i++)
{
for (int j = 0; j < matrix.ColumnCount; j++)
{
if (i == rowIndex)
{
Assert.AreEqual(0.0, matrix[i, j]);
}
else
{
Assert.AreEqual(testMatrices[name][i, j], matrix[i, j]);
}
}
}
}
[Test]
[Row(0, "Singular3x3")]
[Row(1, "Singular3x3")]
[Row(2, "Singular3x3")]
[Row(2, "Square3x3")]
public void CanSetColumn(int colIndex, string name)
{
var matrix = (Matrix)testMatrices[name].Clone();
matrix.SetColumn(colIndex, new DenseVector(matrix.ColumnCount, 0.0));
for (int i = 0; i < matrix.RowCount; i++)
{
for (int j = 0; j < matrix.ColumnCount; j++)
{
if (j == colIndex)
{
Assert.AreEqual(0.0, matrix[i, j]);
}
else
{
Assert.AreEqual(testMatrices[name][i, j], matrix[i, j]);
}
}
}
}
[Test]
[Row("Singular3x3")]
[Row("Square3x3")]

4
src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.cs

@ -400,14 +400,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
}
[Test]
public void PointWiseMultiplySparseVector()
public void PointwiseMultiplySparseVector()
{
var zeroArray = new[] { 0.0, 1.0, 0.0, 1.0, 0.0 };
var vector1 = new SparseVector(this._data);
var vector2 = new SparseVector(zeroArray);
var result = new SparseVector(vector1.Count);
vector1.PointWiseMultiply(vector2, result);
vector1.PointwiseMultiply(vector2, result);
for (var i = 0; i < vector1.Count; i++)
{

36
src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs

@ -724,12 +724,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
}
[Test]
public void PointWiseMultiply()
public void PointwiseMultiply()
{
var vector1 = this.CreateVector(this._data);
var vector2 = vector1.Clone();
var result = CreateVector(vector1.Count);
vector1.PointWiseMultiply(vector2, result);
vector1.PointwiseMultiply(vector2, result);
for (var i = 0; i < vector1.Count; i++)
{
Assert.AreEqual(this._data[i] * this._data[i], result[i]);
@ -738,32 +738,32 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test]
[ExpectedArgumentNullException]
public void PointWiseMultiplyWithOtherNullShouldThrowException()
public void PointwiseMultiplyWithOtherNullShouldThrowException()
{
var vector1 = this.CreateVector(this._data);
Vector vector2 = null;
var result = CreateVector(vector1.Count);
vector1.PointWiseMultiply(vector2, result);
vector1.PointwiseMultiply(vector2, result);
}
[Test]
[ExpectedArgumentNullException]
public void PointWiseMultiplyWithResultNullShouldThrowException()
public void PointwiseMultiplyWithResultNullShouldThrowException()
{
var vector1 = this.CreateVector(this._data);
var vector2 = vector1.Clone();
Vector result = null;
vector1.PointWiseMultiply(vector2, result);
vector1.PointwiseMultiply(vector2, result);
}
[Test]
[ExpectedArgumentException]
public void PointWiseMultiplyWithInvalidResultLengthShouldThrowException()
public void PointwiseMultiplyWithInvalidResultLengthShouldThrowException()
{
var vector1 = this.CreateVector(this._data);
var vector2 = vector1.Clone();
var result = this.CreateVector(vector1.Count + 1);
vector1.PointWiseMultiply(vector2, result);
vector1.PointwiseMultiply(vector2, result);
}
[Test]
@ -771,7 +771,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
{
var vector1 = this.CreateVector(this._data);
var vector2 = vector1.Clone();
vector1.PointWiseMultiply(vector2);
vector1.PointwiseMultiply(vector2);
for (var i = 0; i < vector1.Count; i++)
{
Assert.AreEqual(this._data[i] * this._data[i], vector1[i]);
@ -784,7 +784,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
var vector1 = this.CreateVector(this._data);
var vector2 = vector1.Clone();
var result = CreateVector(vector1.Count);
vector1.PointWiseDivide(vector2, result);
vector1.PointwiseDivide(vector2, result);
for (var i = 0; i < vector1.Count; i++)
{
Assert.AreEqual(this._data[i] / this._data[i], result[i]);
@ -793,40 +793,40 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test]
[ExpectedArgumentNullException]
public void PointWiseDivideWithOtherNullShouldThrowException()
public void PointwiseDivideWithOtherNullShouldThrowException()
{
var vector1 = this.CreateVector(this._data);
Vector vector2 = null;
var result = CreateVector(vector1.Count);
vector1.PointWiseDivide(vector2, result);
vector1.PointwiseDivide(vector2, result);
}
[Test]
[ExpectedArgumentNullException]
public void PointWiseDivideWithResultNullShouldThrowException()
public void PointwiseDivideWithResultNullShouldThrowException()
{
var vector1 = this.CreateVector(this._data);
var vector2 = vector1.Clone();
Vector result = null;
vector1.PointWiseDivide(vector2, result);
vector1.PointwiseDivide(vector2, result);
}
[Test]
[ExpectedArgumentException]
public void PointWiseDivideWithInvalidResultLengthShouldThrowException()
public void PointwiseDivideWithInvalidResultLengthShouldThrowException()
{
var vector1 = this.CreateVector(this._data);
var vector2 = vector1.Clone();
var result = this.CreateVector(vector1.Count + 1);
vector1.PointWiseDivide(vector2, result);
vector1.PointwiseDivide(vector2, result);
}
[Test]
public void PointWiseDivideWithResult()
public void PointwiseDivideWithResult()
{
var vector1 = this.CreateVector(this._data);
var vector2 = vector1.Clone();
vector1.PointWiseDivide(vector2);
vector1.PointwiseDivide(vector2);
for (var i = 0; i < vector1.Count; i++)
{
Assert.AreEqual(this._data[i] / this._data[i], vector1[i]);

Loading…
Cancel
Save