diff --git a/src/Numerics/LinearAlgebra/Complex/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex/DenseVector.cs index 967606a4..a2a6eb1a 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseVector.cs @@ -667,53 +667,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } } - /// - /// Outer product of two vectors - /// - /// First vector - /// Second vector - /// Matrix M[i,j] = u[i]*v[j] - /// If the u vector is . - /// If the v vector is . - public static DenseMatrix OuterProduct(DenseVector u, DenseVector v) - { - if (u == null) - { - throw new ArgumentNullException("u"); - } - - if (v == null) - { - throw new ArgumentNullException("v"); - } - - var matrix = new DenseMatrix(u.Count, v.Count); - CommonParallel.For(0, u.Count, (a, b) => - { - for (int i = a; i < b; i++) - { - for (var j = 0; j < v.Count; j++) - { - matrix.At(i, j, u._values[i]*v._values[j]); - } - } - }); - return matrix; - } - - /// - /// Outer product of this and another vector. - /// - /// The vector to operate on. - /// - /// Matrix M[i,j] = this[i] * v[j]. - /// - /// - public Matrix OuterProduct(DenseVector v) - { - return OuterProduct(this, v); - } - #region Parse Functions /// diff --git a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs index 5b9dba27..d81325c3 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs @@ -794,53 +794,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } } - /// - /// Outer product of two vectors - /// - /// First vector - /// Second vector - /// Matrix M[i,j] = u[i]*v[j] - /// If the u vector is . - /// If the v vector is . - public static Matrix OuterProduct(SparseVector u, SparseVector v) - { - if (u == null) - { - throw new ArgumentNullException("u"); - } - - if (v == null) - { - throw new ArgumentNullException("v"); - } - - var matrix = new SparseMatrix(u.Count, v.Count); - for (var i = 0; i < u._storage.ValueCount; i++) - { - for (var j = 0; j < v._storage.ValueCount; j++) - { - if (u._storage.Indices[i] == v._storage.Indices[j]) - { - matrix.At(i, j, u._storage.Values[i] * v._storage.Values[j]); - } - } - } - - return matrix; - } - - /// - /// Outer product of this and another vector. - /// - /// The vector to operate on. - /// - /// Matrix M[i,j] = this[i] * v[j]. - /// - public Matrix OuterProduct(SparseVector v) - { - return OuterProduct(this, v); - } - #region Parse Functions /// diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs index 6f88ebdf..5bba0a10 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs @@ -662,53 +662,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } } - /// - /// Outer product of two vectors - /// - /// First vector - /// Second vector - /// Matrix M[i,j] = u[i]*v[j] - /// If the u vector is . - /// If the v vector is . - public static DenseMatrix OuterProduct(DenseVector u, DenseVector v) - { - if (u == null) - { - throw new ArgumentNullException("u"); - } - - if (v == null) - { - throw new ArgumentNullException("v"); - } - - var matrix = new DenseMatrix(u.Count, v.Count); - CommonParallel.For(0, u.Count, (a, b) => - { - for (int i = a; i < b; i++) - { - for (var j = 0; j < v.Count; j++) - { - matrix.At(i, j, u._values[i]*v._values[j]); - } - } - }); - return matrix; - } - - /// - /// Outer product of this and another vector. - /// - /// The vector to operate on. - /// - /// Matrix M[i,j] = this[i] * v[j]. - /// - /// - public Matrix OuterProduct(DenseVector v) - { - return OuterProduct(this, v); - } - #region Parse Functions /// diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs index 66ea720c..48c2f13c 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs @@ -789,53 +789,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } } - /// - /// Outer product of two vectors - /// - /// First vector - /// Second vector - /// Matrix M[i,j] = u[i]*v[j] - /// If the u vector is . - /// If the v vector is . - public static Matrix /*SparseMatrix*/ OuterProduct(SparseVector u, SparseVector v) - { - if (u == null) - { - throw new ArgumentNullException("u"); - } - - if (v == null) - { - throw new ArgumentNullException("v"); - } - - var matrix = new SparseMatrix(u.Count, v.Count); - for (var i = 0; i < u._storage.ValueCount; i++) - { - for (var j = 0; j < v._storage.ValueCount; j++) - { - if (u._storage.Indices[i] == v._storage.Indices[j]) - { - matrix.At(i, j, u._storage.Values[i] * v._storage.Values[j]); - } - } - } - - return matrix; - } - - /// - /// Outer product of this and another vector. - /// - /// The vector to operate on. - /// - /// Matrix M[i,j] = this[i] * v[j]. - /// - public Matrix OuterProduct(SparseVector v) - { - return OuterProduct(this, v); - } - #region Parse Functions /// diff --git a/src/Numerics/LinearAlgebra/Double/DenseVector.cs b/src/Numerics/LinearAlgebra/Double/DenseVector.cs index ab7548ef..12b96828 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseVector.cs @@ -744,53 +744,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double } } - /// - /// Outer product of two vectors - /// - /// First vector - /// Second vector - /// Matrix M[i,j] = u[i]*v[j] - /// If the u vector is . - /// If the v vector is . - public static DenseMatrix OuterProduct(DenseVector u, DenseVector v) - { - if (u == null) - { - throw new ArgumentNullException("u"); - } - - if (v == null) - { - throw new ArgumentNullException("v"); - } - - var matrix = new DenseMatrix(u.Count, v.Count); - CommonParallel.For(0, u.Count, (a, b) => - { - for (int i = a; i < b; i++) - { - for (var j = 0; j < v.Count; j++) - { - matrix.At(i, j, u._values[i]*v._values[j]); - } - } - }); - return matrix; - } - - /// - /// Outer product of this and another vector. - /// - /// The vector to operate on. - /// - /// Matrix M[i,j] = this[i] * v[j]. - /// - /// - public Matrix OuterProduct(DenseVector v) - { - return OuterProduct(this, v); - } - #region Parse Functions /// diff --git a/src/Numerics/LinearAlgebra/Double/SparseVector.cs b/src/Numerics/LinearAlgebra/Double/SparseVector.cs index d5c4761c..4b58d572 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseVector.cs @@ -836,50 +836,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double } } - /// - /// Outer product of two vectors - /// - /// First vector - /// Second vector - /// Matrix M[i,j] = u[i]*v[j] - /// If the u vector is . - /// If the v vector is . - public static Matrix OuterProduct(SparseVector u, SparseVector v) - { - if (u == null) - { - throw new ArgumentNullException("u"); - } - - if (v == null) - { - throw new ArgumentNullException("v"); - } - - var matrix = new SparseMatrix(u.Count, v.Count); - for (var i = 0; i < u._storage.ValueCount; i++) - { - for (var j = 0; j < v._storage.ValueCount; j++) - { - matrix.At(i, j, u._storage.Values[i] * v._storage.Values[j]); - } - } - - return matrix; - } - - /// - /// Outer product of this and another vector. - /// - /// The vector to operate on. - /// - /// Matrix M[i,j] = this[i] * v[j]. - /// - public Matrix OuterProduct(SparseVector v) - { - return OuterProduct(this, v); - } - #region Parse Functions /// diff --git a/src/Numerics/LinearAlgebra/Single/DenseVector.cs b/src/Numerics/LinearAlgebra/Single/DenseVector.cs index a35581f9..4b69bd9f 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseVector.cs @@ -734,53 +734,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single } } - /// - /// Outer product of two vectors - /// - /// First vector - /// Second vector - /// Matrix M[i,j] = u[i]*v[j] - /// If the u vector is . - /// If the v vector is . - public static DenseMatrix OuterProduct(DenseVector u, DenseVector v) - { - if (u == null) - { - throw new ArgumentNullException("u"); - } - - if (v == null) - { - throw new ArgumentNullException("v"); - } - - var matrix = new DenseMatrix(u.Count, v.Count); - CommonParallel.For(0, u.Count, (a, b) => - { - for (int i = a; i < b; i++) - { - for (var j = 0; j < v.Count; j++) - { - matrix.At(i, j, u._values[i]*v._values[j]); - } - } - }); - return matrix; - } - - /// - /// Outer product of this and another vector. - /// - /// The vector to operate on. - /// - /// Matrix M[i,j] = this[i] * v[j]. - /// - /// - public Matrix OuterProduct(DenseVector v) - { - return OuterProduct(this, v); - } - #region Parse Functions /// diff --git a/src/Numerics/LinearAlgebra/Single/SparseVector.cs b/src/Numerics/LinearAlgebra/Single/SparseVector.cs index 6cca3fd1..2bc0fc85 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseVector.cs @@ -837,53 +837,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single } } - /// - /// Outer product of two vectors - /// - /// First vector - /// Second vector - /// Matrix M[i,j] = u[i]*v[j] - /// If the u vector is . - /// If the v vector is . - public static Matrix OuterProduct(SparseVector u, SparseVector v) - { - if (u == null) - { - throw new ArgumentNullException("u"); - } - - if (v == null) - { - throw new ArgumentNullException("v"); - } - - var matrix = new SparseMatrix(u.Count, v.Count); - for (var i = 0; i < u._storage.ValueCount; i++) - { - for (var j = 0; j < v._storage.ValueCount; j++) - { - if (u._storage.Indices[i] == v._storage.Indices[j]) - { - matrix.At(i, j, u._storage.Values[i] * v._storage.Values[j]); - } - } - } - - return matrix; - } - - /// - /// Outer product of this and another vector. - /// - /// The vector to operate on. - /// - /// Matrix M[i,j] = this[i] * v[j]. - /// - public Matrix OuterProduct(SparseVector v) - { - return OuterProduct(this, v); - } - #region Parse Functions /// diff --git a/src/Numerics/LinearAlgebra/Vector.Arithmetic.cs b/src/Numerics/LinearAlgebra/Vector.Arithmetic.cs index a5e56b40..8cad6168 100644 --- a/src/Numerics/LinearAlgebra/Vector.Arithmetic.cs +++ b/src/Numerics/LinearAlgebra/Vector.Arithmetic.cs @@ -117,6 +117,21 @@ namespace MathNet.Numerics.LinearAlgebra /// The sum of conj(a[i])*b[i] for all i. protected abstract T DoConjugateDotProduct(Vector other); + /// + /// Computes the outer product M[i,j] = u[i]*v[j] of this and another vector and stores the result in the result matrix. + /// + /// The other vector + /// The matrix to store the result of the product. + protected void DoOuterProduct(Vector other, Matrix result) + { + var work = Build.Dense(Count); + for (var i = 0; i < Count; i++) + { + DoMultiply(other.At(i), work); + result.SetColumn(i, work); + } + } + /// /// Divides each element of the vector by a scalar and stores the result in the result vector. /// @@ -940,33 +955,34 @@ namespace MathNet.Numerics.LinearAlgebra } /// - /// Outer product of two vectors + /// Computes the outer product M[i,j] = u[i]*v[j] of this and another vector. /// - /// First vector - /// Second vector - /// Matrix M[i,j] = u[i]*v[j] - public static Matrix OuterProduct(Vector u, Vector v) + /// The other vector + public Matrix OuterProduct(Vector other) { - var matrix = Matrix.Build.SameAs(u, u.Count, v.Count); - for (var i = 0; i < u.Count; i++) - { - matrix.SetRow(i, v.Multiply(u.At(i))); - } - + var matrix = Matrix.Build.SameAs(this, Count, other.Count); + DoOuterProduct(other, matrix); return matrix; } /// - /// Outer product of this and another vector. + /// Computes the outer product M[i,j] = u[i]*v[j] of this and another vector and stores the result in the result matrix. /// - /// The vector to operate on. - /// - /// Matrix M[i,j] = this[i] * v[j]. - /// - /// - public Matrix OuterProduct(Vector v) + /// The other vector + /// The matrix to store the result of the product. + public void OuterProduct(Vector other, Matrix result) + { + if (Count != result.RowCount || other.Count != result.ColumnCount) + { + throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); + } + + DoOuterProduct(other, result); + } + + public static Matrix OuterProduct(Vector u, Vector v) { - return OuterProduct(this, v); + return u.OuterProduct(v); } ///