From c19e0c535b11e134dd33935bcf220584545c3848 Mon Sep 17 00:00:00 2001 From: Jurgen Van Gael Date: Tue, 10 Nov 2009 02:12:01 +0800 Subject: [PATCH] Implemented managed matrix routines: addition, subtraction and pointwise multiplication. --- .../ManagedLinearAlgebraProvider.cs | 114 +++++++++++++++++- 1 file changed, 110 insertions(+), 4 deletions(-) diff --git a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.cs b/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.cs index 39dca482..331d2ccc 100644 --- a/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.cs +++ b/src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.cs @@ -94,24 +94,130 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra throw new NotImplementedException(); } + /// + /// Computes the dot product between two vectors. + /// + /// The first argument of the dot product. + /// The second argument of the dot product. + /// The dot product between and . public double DotProduct(double[] x, double[] y) { - throw new NotImplementedException(); + if (y == null) + { + throw new ArgumentNullException("y"); + } + + if (x == null) + { + throw new ArgumentNullException("x"); + } + + if (y.Length != x.Length) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength); + } + + double d = 0.0; + + for (int i = 0; i < y.Length; i++) + { + d += y[i] * x[i]; + } + + return d; } + /// + /// Adds two arrays together and writes the result in a third array. + /// + /// The first argument to add. + /// The second argument to add. + /// The result to write the addition into. public void AddArrays(double[] x, double[] y, double[] result) { - throw new NotImplementedException(); + if (y == null) + { + throw new ArgumentNullException("y"); + } + + if (x == null) + { + throw new ArgumentNullException("x"); + } + + if (result == null) + { + throw new ArgumentNullException("result"); + } + + if (y.Length != x.Length || y.Length != result.Length) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength); + } + + Parallel.For(0, y.Length, i => result[i] = x[i] + y[i]); } + /// + /// Subtract two arrays and writes the result in a third array. + /// + /// The first argument to subtract. + /// The second argument to subtract. + /// The result to write the subtraction into. public void SubtractArrays(double[] x, double[] y, double[] result) { - throw new NotImplementedException(); + if (y == null) + { + throw new ArgumentNullException("y"); + } + + if (x == null) + { + throw new ArgumentNullException("x"); + } + + if (result == null) + { + throw new ArgumentNullException("result"); + } + + if (y.Length != x.Length || y.Length != result.Length) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength); + } + + Parallel.For(0, y.Length, i => result[i] = x[i] - y[i]); } + /// + /// Pointwise multiplies two arrays and writes the result in a third array. + /// + /// The first argument to pointwise multiply. + /// The second argument to pointwise multiply. + /// The result to write the pointwise multiplication into. public void PointWiseMultiplyArrays(double[] x, double[] y, double[] result) { - throw new NotImplementedException(); + if (y == null) + { + throw new ArgumentNullException("y"); + } + + if (x == null) + { + throw new ArgumentNullException("x"); + } + + if (result == null) + { + throw new ArgumentNullException("result"); + } + + if (y.Length != x.Length || y.Length != result.Length) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength); + } + + Parallel.For(0, y.Length, i => result[i] = x[i] * y[i]); } public double MatrixNorm(Norm norm, double[] matrix)