本文整理汇总了C#中MathNet.Numerics.LinearAlgebra.Double.DenseVector.DotProduct方法的典型用法代码示例。如果您正苦于以下问题:C# DenseVector.DotProduct方法的具体用法?C# DenseVector.DotProduct怎么用?C# DenseVector.DotProduct使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MathNet.Numerics.LinearAlgebra.Double.DenseVector
的用法示例。
在下文中一共展示了DenseVector.DotProduct方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetRealVertices
private static void SetRealVertices(Workspace workspace)
{
Vector<double> fittedPlaneVector = GeometryHelper.FitPlaneToPoints(workspace.PointCloud.ToArray());
if (fittedPlaneVector == null)
{
return;
}
Point3D projectedPoint = GeometryHelper.ProjectPoint3DToPlane(workspace.PointCloud.First(), fittedPlaneVector);
Vector<double> planeNormal = new DenseVector(new[] { fittedPlaneVector[0], fittedPlaneVector[1], fittedPlaneVector[2] });
Point3D[] vertices3D = workspace.Vertices3D;
Point[] vertices = workspace.Vertices.ToArray();
for (int i = 0; i < vertices.Length; i++)
{
Vector<double> pointOnPlane = new DenseVector(new[] { projectedPoint.X, projectedPoint.Y, projectedPoint.Z });
Vector<double> pointOnLine = new DenseVector(new double[] { vertices3D[i].X, vertices3D[i].Y, vertices3D[i].Z });
double d = (pointOnPlane.Subtract(pointOnLine)).DotProduct(planeNormal) / (pointOnLine.DotProduct(planeNormal));
Vector<double> intersection = pointOnLine + pointOnLine.Multiply(d);
workspace.FittedVertices[i] = new Point3D(intersection[0], intersection[1], intersection[2]);
}
workspace.PlaneVector = fittedPlaneVector;
}
示例2: Run
/// <summary>
/// Run example.
/// </summary>
/// <seealso cref="http://en.wikipedia.org/wiki/Euclidean_vector#Scalar_multiplication">Multiply vector by scalar</seealso>
/// <seealso cref="http://en.wikipedia.org/wiki/Euclidean_vector#Dot_product">Multiply vector by vector (compute the dot product between two vectors)</seealso>
/// <seealso cref="http://en.wikipedia.org/wiki/Euclidean_vector#Addition_and_subtraction">Vector addition and subtraction</seealso>
/// <seealso cref="http://en.wikipedia.org/wiki/Outer_product">Outer Product of two vectors</seealso>
public void Run()
{
// Initialize IFormatProvider to print matrix/vector data
var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone();
formatProvider.TextInfo.ListSeparator = " ";
// Create vector "X"
var vectorX = new DenseVector(new[] { 1.0, 2.0, 3.0, 4.0, 5.0 });
Console.WriteLine(@"Vector X");
Console.WriteLine(vectorX.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// Create vector "Y"
var vectorY = new DenseVector(new[] { 5.0, 4.0, 3.0, 2.0, 1.0 });
Console.WriteLine(@"Vector Y");
Console.WriteLine(vectorY.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// Multiply vector by scalar
// 1. Using Multiply method and getting result into different vector instance
var resultV = vectorX.Multiply(3.0);
Console.WriteLine(@"Multiply vector by scalar using method Multiply. (result = X.Multiply(3.0))");
Console.WriteLine(resultV.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 2. Using operator "*"
resultV = 3.0 * vectorX;
Console.WriteLine(@"Multiply vector by scalar using operator *. (result = 3.0 * X)");
Console.WriteLine(resultV.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 3. Using Multiply method and updating vector itself
vectorX.Multiply(3.0, vectorX);
Console.WriteLine(@"Multiply vector by scalar using method Multiply. (X.Multiply(3.0, X))");
Console.WriteLine(vectorX.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// Multiply vector by vector (compute the dot product between two vectors)
// 1. Using operator "*"
var dotProduct = vectorX * vectorY;
Console.WriteLine(@"Dot product between two vectors using operator *. (result = X * Y)");
Console.WriteLine(dotProduct);
Console.WriteLine();
// 2. Using DotProduct method and getting result into different vector instance
dotProduct = vectorX.DotProduct(vectorY);
Console.WriteLine(@"Dot product between two vectors using method DotProduct. (result = X.DotProduct(Y))");
Console.WriteLine(dotProduct.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// Pointwise multiplies vector with another vector
// 1. Using PointwiseMultiply method and getting result into different vector instance
resultV = vectorX.PointwiseMultiply(vectorY);
Console.WriteLine(@"Pointwise multiplies vector with another vector using method PointwiseMultiply. (result = X.PointwiseMultiply(Y))");
Console.WriteLine(resultV.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 2. Using PointwiseMultiply method and updating vector itself
vectorX.PointwiseMultiply(vectorY, vectorX);
Console.WriteLine(@"Pointwise multiplies vector with another vector using method PointwiseMultiply. (X.PointwiseMultiply(Y, X))");
Console.WriteLine(vectorX.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// Pointwise divide vector with another vector
// 1. Using PointwiseDivide method and getting result into different vector instance
resultV = vectorX.PointwiseDivide(vectorY);
Console.WriteLine(@"Pointwise divide vector with another vector using method PointwiseDivide. (result = X.PointwiseDivide(Y))");
Console.WriteLine(resultV.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 2. Using PointwiseDivide method and updating vector itself
vectorX.PointwiseDivide(vectorY, vectorX);
Console.WriteLine(@"Pointwise divide vector with another vector using method PointwiseDivide. (X.PointwiseDivide(Y, X))");
Console.WriteLine(vectorX.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// Addition
// 1. Using operator "+"
resultV = vectorX + vectorY;
Console.WriteLine(@"Add vectors using operator +. (result = X + Y)");
Console.WriteLine(resultV.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 2. Using Add method and getting result into different vector instance
resultV = vectorX.Add(vectorY);
Console.WriteLine(@"Add vectors using method Add. (result = X.Add(Y))");
Console.WriteLine(resultV.ToString("#0.00\t", formatProvider));
Console.WriteLine();
// 3. Using Add method and updating vector itself
vectorX.Add(vectorY, vectorX);
Console.WriteLine(@"Add vectors using method Add. (X.Add(Y, X))");
Console.WriteLine(vectorX.ToString("#0.00\t", formatProvider));
//.........这里部分代码省略.........
示例3: SetRealVertices
public void SetRealVertices(Workspace workspace)
{
Vector<double> fittedPlaneVector = GeometryHelper.FitPlaneToPoints(workspace.PointCloud.ToArray());
if (fittedPlaneVector == null)
{
return;
}
Point3D projectedPoint = GeometryHelper.ProjectPoint3DToPlane(workspace.PointCloud.First(), fittedPlaneVector);
Vector<double> planeNormal = new DenseVector(new[] { fittedPlaneVector[0], fittedPlaneVector[1], fittedPlaneVector[2] });
CameraSpacePoint[] csps = { new CameraSpacePoint() };
Point[] vertices = workspace.Vertices.ToArray();
for (int i = 0; i < vertices.Length; i++)
{
Point vertex = vertices[i];
kinectStreamer.CoordinateMapper.MapDepthPointsToCameraSpace(
new[] {
new DepthSpacePoint {
X = (float)vertex.X,
Y = (float)vertex.Y
}
},
new ushort[] { 1 }, csps);
Vector<double> pointOnPlane = new DenseVector(new[] { projectedPoint.X, projectedPoint.Y, projectedPoint.Z });
Vector<double> pointOnLine = new DenseVector(new double[] { csps[0].X, csps[0].Y, csps[0].Z });
double d = (pointOnPlane.Subtract(pointOnLine)).DotProduct(planeNormal) / (pointOnLine.DotProduct(planeNormal));
Vector<double> intersection = pointOnLine + pointOnLine.Multiply(d);
workspace.FittedVertices[i] = new Point3D(intersection[0], intersection[1], intersection[2]);
}
workspace.PlaneVector = fittedPlaneVector;
}
示例4: RunPLAvsSVM
static Tuple<double, double> RunPLAvsSVM(int experiments, int points)
{
const int TEST_POINTS = 10000;
Random rnd = new Random();
long svmWins = 0, svCount = 0;
for (int i = 1; i <= experiments; i++)
{
//pick a random line y = a * x + b
double x1 = rnd.NextDouble(), y1 = rnd.NextDouble(), x2 = rnd.NextDouble(), y2 = rnd.NextDouble();
var Wf = new DenseVector(3);
Wf[0] = 1;
Wf[1] = (y1 - y2) / (x1 * y2 - y1 * x2);
Wf[2] = (x2 - x1) / (x1 * y2 - y1 * x2);
Func<MathNet.Numerics.LinearAlgebra.Generic.Vector<double>, int> f = x => Wf.DotProduct(x) >= 0 ? 1 : -1;
//generate training set of N random points
var X = new DenseMatrix(points, 3);
do
for (int j = 0; j < points; j++)
{
X[j, 0] = 1;
X[j, 1] = rnd.NextDouble() * 2 - 1;
X[j, 2] = rnd.NextDouble() * 2 - 1;
}
while (Enumerable.Range(0, X.RowCount).All(j => f(X.Row(0)) == f(X.Row(j))));
var W = new DenseVector(3);
Func<MathNet.Numerics.LinearAlgebra.Generic.Vector<double>, int> h = x => W.DotProduct(x) >= 0 ? 1 : -1;
//run Perceptron
int k = 1;
while (Enumerable.Range(0, points).Any(j => h(X.Row(j)) != f(X.Row(j))))
{
//find all misclasified points
int[] M = Enumerable.Range(0, points).Where(j => h(X.Row(j)) != f(X.Row(j))).ToArray();
int m = M[rnd.Next(0, M.Length)];
int sign = f(X.Row(m));
W[0] += sign;
W[1] += sign * X[m, 1];
W[2] += sign * X[m, 2];
k++;
}
//calculate P[f(Xtest) != h(Xtest)]
DenseVector Xtest = new DenseVector(3);
Xtest[0] = 1;
int matches = 0;
for (int j = 0; j < TEST_POINTS; j++)
{
Xtest[1] = rnd.NextDouble() * 2 - 1;
Xtest[2] = rnd.NextDouble() * 2 - 1;
if (f(Xtest) == h(Xtest)) matches++;
}
double Ppla = (matches + 0.0) / TEST_POINTS;
//Run SVM
var prob = new svm_problem() {
x = Enumerable.Range(0, points).Select(j =>
new svm_node[] {
new svm_node() { index = 0, value = X[j, 1] },
new svm_node() { index = 1, value = X[j, 2] } }).ToArray(),
y = Enumerable.Range(0, points).Select(j => (double)f(X.Row(j))).ToArray(),
l = points };
var model = svm.svm_train(prob, new svm_parameter()
{
svm_type = (int)SvmType.C_SVC,
kernel_type = (int)KernelType.LINEAR,
C = 1000000,
eps = 0.001,
shrinking = 0
});
//calculate P[f(Xtest) != h_svm(Xtest)]
svm_node[] Xsvm = new svm_node[] {
new svm_node() { index = 0, value = 1.0 },
new svm_node() { index = 1, value = 1.0 } };
matches = 0;
for (int j = 0; j < TEST_POINTS; j++)
{
Xtest[1] = rnd.NextDouble() * 2 - 1;
Xsvm[0].value = Xtest[1];
Xtest[2] = rnd.NextDouble() * 2 - 1;
Xsvm[1].value = Xtest[2];
if (f(Xtest) == (svm.svm_predict(model, Xsvm) > 0 ? 1 : -1)) matches++;
}
double Psvm = (matches + 0.0) / TEST_POINTS;
svCount += model.l;
if (Psvm >= Ppla) svmWins++;
}
return Tuple.Create((svmWins + 0.0) / experiments, (svCount + 0.0) / experiments);
}
示例5: ProcessElasticCollisionWith
/// <summary>
/// Calculates the resulting velocities when colliding in a perfectly
/// elastic manner with another object.
/// </summary>
/// <param name="otherObject">The other object that this one is colliding with.</param>
protected void ProcessElasticCollisionWith(DEEPKinectObjectBaseClass otherObject)
{
/* We use the definition from Wikipedia for how to handle the collision of
* two moving objects in vector form. See: en.wikipedia.org/wiki/Elastic_collision
*
* To be specific, we use instructions from www.vobarian.com/collisions/2dcollisions2.pdf */
/* Here are the known quanitities of the situation. */
DenseVector p1 = this.GetPosition();
DenseVector p2 = otherObject.GetPosition();
DenseVector v1 = this.velocity;
DenseVector v2 = otherObject.velocity;
double m1 = this.mass;
double m2 = otherObject.mass;
/* First we create a unit normal and tangent vector. */
DenseVector n = p2 - p1;
DenseVector un = n / n.Norm(2d);
DenseVector ut = new DenseVector(new double[] { -un[1], un[0] });
/* Here we find the normal and tangential components of the velocities. */
double v1n = un.DotProduct(v1);
double v1t = ut.DotProduct(v1);
double v2n = un.DotProduct(v2);
double v2t = ut.DotProduct(v2);
/* We then apply 1-D elastic collision dynamics in the normal direction to the
* line of collision.
* Note that there is NO CHANGE in the tangential components of the velocity. */
double post_v1n = (v1n * (m1 - m2) + 2 * m2 * v2n) / (m1 + m2);
double post_v2n = (v2n * (m2 - m1) + 2 * m1 * v1n) / (m1 + m2);
/* Now we convert the scalar normal/tangential velocities into vectors pointing
* in the appropriate directions. */
DenseVector vPost_v1n = post_v1n * un;
DenseVector vPost_v1t = v1t * ut;
DenseVector vPost_v2n = post_v2n * un;
DenseVector vPost_v2t = v2t * ut;
/* Calculate the post-collision velocity by adding the normal/tangential velocities
* together. */
DenseVector v1FinalVelocity = vPost_v1n + vPost_v1t;
DenseVector v2FinalVelocity = vPost_v2n + vPost_v2t;
/* Set the object's velocity to the post-collision velocity. */
this.velocity = v1FinalVelocity;
otherObject.velocity = v2FinalVelocity;
}
示例6: PseudoLikelihood
private double PseudoLikelihood(DenseVector wtest, DenseVector vtest)
{
double first_term = 0;
for(int m = 0; m < TrainingInputs.Count; m++)
{
for(int horz = 0; horz < TrainingInputs[m].XSites; horz++)
{
for(int vert = 0; vert < TrainingInputs[m].YSites; vert++)
{
int x = (int)(TrainingOutputs[m][horz,vert])*2 - 1;
//h_i(y):
DenseVector h = Transformer.Transform(TrainingInputs[m][horz,vert]);
first_term += MathWrapper.Log (MathWrapper.Sigma( x * wtest.DotProduct(h) ) );
foreach(Tuple<int,int> j in TrainingInputs[m].GetNeighbors(horz,vert))
{
int jx = (int)(TrainingOutputs[m][j.Item1,j.Item2])*2 - 1;
DenseVector mu;
if(ImageData.IsEarlier(horz,vert,j.Item1,j.Item2))mu = Crosser.Cross(TrainingInputs[m][horz,vert],TrainingInputs[m][j.Item1,j.Item2]);
else mu = Crosser.Cross(TrainingInputs[m][j.Item1,j.Item2], TrainingInputs[m][horz,vert]);
first_term += x * jx * vtest.DotProduct(mu);
}
double z = 0;
//Sum over possible x_i
for(int tempx = -1; tempx <= 1; tempx += 2)
{
double logofcoeff = MathWrapper.Log(MathWrapper.Sigma(tempx * wtest.DotProduct(h)));
//sum over the neighbors
foreach(Tuple<int,int> j in TrainingInputs[m].GetNeighbors(horz,vert))
{
int jx = (int)(TrainingOutputs[m][j.Item1,j.Item2])*2 - 1;
DenseVector mu;
if(ImageData.IsEarlier(horz,vert,j.Item1,j.Item2))mu = Crosser.Cross(TrainingInputs[m][horz,vert],TrainingInputs[m][j.Item1,j.Item2]);
else mu = Crosser.Cross(TrainingInputs[m][j.Item1,j.Item2], TrainingInputs[m][horz,vert]);
logofcoeff += tempx * jx * vtest.DotProduct(mu);
}
z += MathWrapper.Exp (logofcoeff);
}
first_term -= MathWrapper.Log(z);
}
}
}
return first_term - vtest.DotProduct(vtest)/(2*Math.Pow(Tau,2d));
}