public void TestVelocityOnPlane() { Frisbee.SimulationState st = new Frisbee.SimulationState { VX = 1, VY = 1, Theta = Math.PI / 4 }; Matrix<double> transformation = new SparseMatrix(new [,] { {st.CosTheta, st.SinTheta*st.SinPhi, -st.SinTheta*st.CosPhi}, {0, st.CosPhi, st.SinPhi}, {st.SinTheta, -st.CosTheta*st.SinPhi, st.CosTheta*st.CosPhi} }); SparseVector c3 = new SparseVector(transformation.Row(2)); SparseVector velocity = new SparseVector(new[] { st.VX, st.VY, st.VZ }); double velocityMagnitude = velocity.Norm(2); double velocityC3 = velocity.DotProduct(c3); Vector<double> vp = velocity.Subtract(c3.Multiply(velocityC3)); double vpMagnitude = vp.Norm(2); }
/// <summary> /// List indexing is 2000x faster than Matrix.Row() or enumeration. /// </summary> public static void SpeedOfGetRow() { SparseMatrix myMatrix = new SparseMatrix(1000, 1000); SparseVector myVector = SparseVector.OfVector(Vector.Build.Random(1000)); myVector.CoerceZero(1.8); for (int i = 0; i < 1000; i++) { myMatrix.SetRow(i, myVector); } List<Vector<double>> myList = new List<Vector<double>>(myMatrix.EnumerateRows()); Utils.StartTimer(); for (int repeat = 0; repeat < 10; repeat++) { for (int i = 0; i < 1000; i++) { double foo = myMatrix.Row(i)[0]; } } Utils.StopTimer(); Utils.StartTimer(); for (int repeat = 0; repeat < 10; repeat++) { foreach(var row in myMatrix.EnumerateRowsIndexed()) { double foo = row.Item2[0]; } } Utils.StopTimer(); Utils.StartTimer(); for (int repeat = 0; repeat < 10; repeat++) { for (int i = 0; i < 1000; i++) { double foo = myList[i][0]; } } Utils.StopTimer(); }
private List<List<int>> GetChains(SparseMatrix path) { List<List<int>> chains = new List<List<int>>(); var unusedPoints = Enumerable.Range(0, N).ToList(); int startPoint = unusedPoints.First(); do { var chain = new List<int>(); int prevPoint = startPoint, nextPoint; chain.Add(startPoint); do { nextPoint = path.Row(prevPoint).GetIndexedEnumerator().Select(p => p.Item1).First(); chain.Add(nextPoint); prevPoint = nextPoint; } while (nextPoint != startPoint); chains.Add(chain); unusedPoints = unusedPoints.Except(chain).ToList(); startPoint = unusedPoints.FirstOrDefault(); } while (unusedPoints.Count != 0); return chains; }