CodePlexProject Hosting for Open Source Software

Here’s an example from the MIConvexHull project, where StarMath is used to perform a number of vector operations.

What is required is to find the normal vector for the (hyper-)plane that passes through the provided vertices.

private static double[] findNormalVector(IList<IVertexConvHull> vertices)

{

double[] normal;

if the dimension is 3, then we can simply use StarMath’s multiplyCross (crossProduct) to find an orthogonal vector to the 2 vector created in the plane by the 3 points.

if (dimension == 3)

normal = StarMath.multiplyCross(StarMath.subtract(vertices[1].coordinates, vertices[0].coordinates),

StarMath.subtract(vertices[2].coordinates, vertices[1].coordinates));

If the number of dimensions is something else, n, then we will have to solve a system of equations. What is the vector, n which is orthogonal to the n vertices? Here we use StarMath’s solve routine to solve the
**A**x = b set of linear equations. **A** is simply comprised of vertices’ coordinate values, and we are seeking values of n (or shown as x in the typical
**A**x = b) that produce the same value.

else

{

var b = new double[dimension];

for (var i = 0; i < dimension; i++) b[i] = 1.0;

In this case that same value is set to 1, as all values are 1. The values of A are created using StarMath’s SetRow function.

var A = new double[dimension, dimension];

for (var i = 0; i < dimension; i++)

StarMath.SetRow(i, A, vertices[i].coordinates);

Finally, the normal vector is produced by the solve function. For additional mathematical simplicity, this is normalized so that it has magnitude of 1. This is done by using StarMath’s normalize function.

normal = StarMath.solve(A, b);

}

return StarMath.normalize(normal);

}

Here is the entire function shown again.

private static double[] findNormalVector(IList<IVertexConvHull> vertices)

{

double[] normal;

if (dimension == 3)

normal = StarMath.multiplyCross(StarMath.subtract(vertices[1].coordinates, vertices[0].coordinates),

StarMath.subtract(vertices[2].coordinates, vertices[1].coordinates));

else

{

var b = new double[dimension];

for (var i = 0; i < dimension; i++) b[i] = 1.0;

var A = new double[dimension, dimension];

for (var i = 0; i < dimension; i++)

StarMath.SetRow(i, A, vertices[i].coordinates);

normal = StarMath.solve(A, b);

}

return StarMath.normalize(normal);

}

Last edited Oct 18, 2010 at 4:26 PM by mattica, version 2