diff --git a/src/FSharp/DenseMatrix.fs b/src/FSharp/DenseMatrix.fs index e429da9c..b7638275 100644 --- a/src/FSharp/DenseMatrix.fs +++ b/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*) \ No newline at end of file + for j=0 to n-1 do A.[j, i] <- col.[j] + A \ No newline at end of file diff --git a/src/Numerics/LinearAlgebra/Double/DenseVector.cs b/src/Numerics/LinearAlgebra/Double/DenseVector.cs index 404a9f02..ebc9e39a 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseVector.cs @@ -907,7 +907,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The vector to pointwise multiply with this one. /// If the other vector is . /// If this vector and are not the same size. - 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 /// If the result vector is . /// If this vector and are not the same size. /// If this vector and are not the same size. - 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 /// The vector to pointwise divide this one by. /// If the other vector is . /// If this vector and are not the same size. - 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 /// If the result vector is . /// If this vector and are not the same size. /// If this vector and are not the same size. - 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); } } diff --git a/src/Numerics/LinearAlgebra/Double/Matrix.cs b/src/Numerics/LinearAlgebra/Double/Matrix.cs index a873ca99..82945183 100644 --- a/src/Numerics/LinearAlgebra/Double/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Double/Matrix.cs @@ -457,6 +457,64 @@ namespace MathNet.Numerics.LinearAlgebra.Double } } + /// + /// Copies the elements of a vector into a specific row. The length of the vector must match the number of columns + /// in the matrix. + /// + /// The row in which to copy into. + /// The vector to copy from. + 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]; + } + } + + /// + /// Copies the elements of a vector into a specific column. The length of the vector must match the number of rows + /// in the matrix. + /// + /// The column in which to copy into. + /// The vector to copy from. + 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]; + } + } + /// /// Returns a new matrix containing the lower triangle of this matrix. /// diff --git a/src/Numerics/LinearAlgebra/Double/SparseVector.cs b/src/Numerics/LinearAlgebra/Double/SparseVector.cs index 2e53038a..fb811533 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseVector.cs @@ -1069,7 +1069,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The vector to pointwise multiply with this one. /// If the other vector is . /// If this vector and are not the same size. - public override void PointWiseMultiply(Vector other) + public override void PointwiseMultiply(Vector other) { if (other == null) { @@ -1097,7 +1097,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// If the result vector is . /// If this vector and are not the same size. /// If this vector and are not the same size. - 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 /// The vector to pointwise divide this one by. /// If the other vector is . /// If this vector and are not the same size. - public override void PointWiseDivide(Vector other) + public override void PointwiseDivide(Vector other) { if (other == null) { @@ -1166,7 +1166,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// If the result vector is . /// If this vector and are not the same size. /// If this vector and are not the same size. - 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); } } diff --git a/src/Numerics/LinearAlgebra/Double/Vector.cs b/src/Numerics/LinearAlgebra/Double/Vector.cs index 531c09eb..6e14c1c6 100644 --- a/src/Numerics/LinearAlgebra/Double/Vector.cs +++ b/src/Numerics/LinearAlgebra/Double/Vector.cs @@ -551,7 +551,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The vector to pointwise multiply with this one. /// If the other vector is . /// If this vector and are not the same size. - public virtual void PointWiseMultiply(Vector other) + public virtual void PointwiseMultiply(Vector other) { if (other == null) { @@ -578,7 +578,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// If the result vector is . /// If this vector and are not the same size. /// If this vector and are not the same size. - 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 /// The vector to pointwise divide this one by. /// If the other vector is . /// If this vector and are not the same size. - public virtual void PointWiseDivide(Vector other) + public virtual void PointwiseDivide(Vector other) { if (other == null) { @@ -646,7 +646,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// If the result vector is . /// If this vector and are not the same size. /// If this vector and are not the same size. - 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); } } diff --git a/src/Numerics/Version.cs b/src/Numerics/Version.cs index 32362c28..d5889f72 100644 --- a/src/Numerics/Version.cs +++ b/src/Numerics/Version.cs @@ -27,10 +27,10 @@ // /* 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")] \ No newline at end of file +[assembly: AssemblyVersion("2010.06.10.1121")] +[assembly: AssemblyFileVersion("2010.06.10.1121")] \ No newline at end of file diff --git a/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs index 7f2a880d..2dba1e05 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs +++ b/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")] diff --git a/src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.cs b/src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.cs index e35a064c..cb684e4a 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.cs +++ b/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++) { diff --git a/src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs b/src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs index a28b56f8..f3e2dca8 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs +++ b/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]);