예제 #1
0
        public void CosineSimilarityTest( )
        {
            CosineSimilarity sim = new CosineSimilarity( );

            Assert.Throws <ArgumentException>(() => sim.GetSimilarityScore(p0, q4));

            double result = sim.GetSimilarityScore(p0, q0);

            Assert.AreApproximatelyEqual(result, .8, 0.00001);

            result = sim.GetSimilarityScore(p1, q1);
            Assert.AreApproximatelyEqual(result, 0.97014, 0.00001);

            result = sim.GetSimilarityScore(p2, q2);
            Assert.AreEqual(result, 0);

            result = sim.GetSimilarityScore(p3, q3);
            Assert.AreApproximatelyEqual(result, 1, 0.00001);

            result = sim.GetSimilarityScore(p4, q4);
            Assert.AreApproximatelyEqual(result, 0.96065, 0.00001);

            result = sim.GetSimilarityScore(p5, q5);
            Assert.AreApproximatelyEqual(result, 0.96897, 0.00001);
        }
예제 #2
0
        public void CanCreateComplexNumberWithModulusArgument()
        {
            var complex = Complex32.WithModulusArgument(2, (float)-Math.PI / 6);

            Assert.AreApproximatelyEqual((float)Math.Sqrt(3), complex.Real, 1e-7f, "Real part is Sqrt(3).");
            Assert.AreApproximatelyEqual(-1.0f, complex.Imaginary, 1e-7f, "Imaginary part is -1.");
        }
        public void CanSolveForRandomMatrix(int order)
        {
            var matrixA     = MatrixLoader.GenerateRandomDenseMatrix(order, order);
            var matrixACopy = matrixA.Clone();
            var factorLU    = matrixA.LU();

            var matrixB = MatrixLoader.GenerateRandomDenseMatrix(order, order);
            var matrixX = factorLU.Solve(matrixB);

            // The solution X row dimension is equal to the column dimension of A
            Assert.AreEqual(matrixA.ColumnCount, matrixX.RowCount);
            // The solution X has the same number of columns as B
            Assert.AreEqual(matrixB.ColumnCount, matrixX.ColumnCount);

            var matrixBReconstruct = matrixA * matrixX;

            // Check the reconstruction.
            for (var i = 0; i < matrixB.RowCount; i++)
            {
                for (var j = 0; j < matrixB.ColumnCount; j++)
                {
                    Assert.AreApproximatelyEqual(matrixB[i, j], matrixBReconstruct[i, j], 10e-5f);
                }
            }

            // Make sure A didn't change.
            for (var i = 0; i < matrixA.RowCount; i++)
            {
                for (var j = 0; j < matrixA.ColumnCount; j++)
                {
                    Assert.AreEqual(matrixACopy[i, j], matrixA[i, j]);
                }
            }
        }
예제 #4
0
        public IEnumerator CanRunContinues()
        {
            Debug.Log("CanRunContinues :: START");

            input.moveDirection          = new Vector2(0, 1);
            input.isRunning              = true;
            player.Movement.Acceleration = 0;
            player.Movement.CanRun       = false;

            // Skip 2 frames to make sure the player gets their speed up.
            yield return(null);

            yield return(null);

            Debug.Log("CanRunContinues :: Player velocity: " + player.Controller.velocity.z);

            Assert.AreApproximatelyEqual(3f, player.Controller.velocity.z);

            yield return(null);

            Debug.Log("CanRunContinues :: Set CanRun to true.");
            player.Movement.CanRun = true;
            // Skip 2 frames to make sure the player slows down.
            yield return(null);

            yield return(null);

            Debug.Log("CanRunContinues :: Player velocity: " + player.Controller.velocity.z);

            Assert.AreApproximatelyEqual(7f, player.Controller.velocity.z);
        }
            public void Execute(ref ModifiableJacobianHeader header, ref ModifiableContactJacobian jacobian)
            {
                // Header verification
                Assert.IsFalse(header.AngularChanged);
                Assert.AreNotEqual(header.BodyPair.BodyAIndex, header.BodyPair.BodyBIndex);
                Assert.AreEqual(header.ColliderKeys.ColliderKeyA.Value, ColliderKey.Empty.Value);
                Assert.AreEqual(header.ColliderKeys.ColliderKeyB.Value, ColliderKey.Empty.Value);
                Assert.AreEqual(header.Entities.EntityA, Bodies[header.BodyPair.BodyAIndex].Entity);
                Assert.AreEqual(header.Entities.EntityB, Bodies[header.BodyPair.BodyBIndex].Entity);
                Assert.AreEqual(header.Flags, (JacobianFlags)0);
                Assert.IsFalse(header.HasColliderKeys);
                Assert.IsFalse(header.HasMassFactors);
                Assert.IsFalse(header.HasSurfaceVelocity);
                Assert.IsFalse(header.ModifiersChanged);
                Assert.AreEqual(header.Type, JacobianType.Contact);

                // Jacobian verification
                Assert.AreApproximatelyEqual(jacobian.CoefficientOfFriction, 0.5f, 0.01f);
                Assert.IsFalse(jacobian.Modified);
                Assert.AreEqual(jacobian.NumContacts, 4);
                for (int i = 0; i < jacobian.NumContacts; i++)
                {
                    ContactJacAngAndVelToReachCp jacAng = header.GetAngularJacobian(i);
                    Assert.AreEqual(jacAng.Jac.Impulse, 0.0f);
                }
            }
예제 #6
0
        public IEnumerator CanLookAround()
        {
            Debug.Log("CanLookAround:: START");

            input.mouseInput            = new Vector2(10, -10);
            player.Camera.CanLookAround = true;

            yield return(null);

            Debug.Log("CanLookAround :: Camera rotation: " + player.Camera.CameraHead.localEulerAngles.x + " | Player rotation: " + player.transform.eulerAngles.y);

            Assert.AreApproximatelyEqual(20f, player.Camera.CameraHead.localEulerAngles.x, 0.1f);
            Assert.AreApproximatelyEqual(20f, player.transform.eulerAngles.y, 0.1f);

            // Let one more frame go before we disable CanLookAround.
            yield return(null);

            Debug.Log("CanLookAround :: Set CanLookAround to false.");
            player.Camera.CanLookAround = false;
            yield return(null);

            Debug.Log("CanLookAround :: Camera rotation: " + player.Camera.CameraHead.localEulerAngles.x + " | Player rotation: " + player.transform.eulerAngles.y);

            Assert.AreApproximatelyEqual(40f, player.Camera.CameraHead.localEulerAngles.x, 0.1f);
            Assert.AreApproximatelyEqual(40f, player.transform.eulerAngles.y, 0.1f);
        }
예제 #7
0
        public IEnumerator CameraInvertX()
        {
            Debug.Log("CameraInvertX :: START");

            input.mouseInput          = new Vector2(10, 0);
            player.Camera.InvertXAxis = true;

            yield return(null);

            Debug.Log("CameraInvertX :: Player rotation: " + player.transform.eulerAngles.y);

            Assert.AreApproximatelyEqual(340f, player.transform.eulerAngles.y, 0.1f);

            Debug.Log("CameraInvertX :: Set InvertX to false.");
            player.Camera.InvertXAxis = false;

            // Let 2 frames pass to get past the 0 rotation.
            yield return(null);

            yield return(null);

            Debug.Log("CameraInvertX :: Player rotation: " + player.transform.eulerAngles.y);

            Assert.AreApproximatelyEqual(20f, player.transform.eulerAngles.y, 0.1f);
        }
예제 #8
0
        public void CanSolveForRandomVectorWhenResultVectorGiven(int row, int column)
        {
            var matrixA     = MatrixLoader.GenerateRandomDenseMatrix(row, column);
            var matrixACopy = matrixA.Clone();
            var factorSvd   = matrixA.Svd(true);
            var vectorb     = MatrixLoader.GenerateRandomDenseVector(row);
            var vectorbCopy = vectorb.Clone();
            var resultx     = new DenseVector(column);

            factorSvd.Solve(vectorb, resultx);

            var bReconstruct = matrixA * resultx;

            // Check the reconstruction.
            for (var i = 0; i < vectorb.Count; i++)
            {
                Assert.AreApproximatelyEqual(vectorb[i].Real, bReconstruct[i].Real, 1e-3f);
                Assert.AreApproximatelyEqual(vectorb[i].Imaginary, bReconstruct[i].Imaginary, 1e-3f);
            }

            // Make sure A didn't change.
            for (var i = 0; i < matrixA.RowCount; i++)
            {
                for (var j = 0; j < matrixA.ColumnCount; j++)
                {
                    Assert.AreEqual(matrixACopy[i, j], matrixA[i, j]);
                }
            }

            // Make sure b didn't change.
            for (var i = 0; i < vectorb.Count; i++)
            {
                Assert.AreEqual(vectorbCopy[i], vectorb[i]);
            }
        }
예제 #9
0
        public void CanFactorizeRandomMatrix(int row, int column)
        {
            var matrixA   = MatrixLoader.GenerateRandomDenseMatrix(row, column);
            var factorSvd = matrixA.Svd(true);

            // Make sure the U has the right dimensions.
            Assert.AreEqual(row, factorSvd.U().RowCount);
            Assert.AreEqual(row, factorSvd.U().ColumnCount);

            // Make sure the VT has the right dimensions.
            Assert.AreEqual(column, factorSvd.VT().RowCount);
            Assert.AreEqual(column, factorSvd.VT().ColumnCount);

            // Make sure the W has the right dimensions.
            Assert.AreEqual(row, factorSvd.W().RowCount);
            Assert.AreEqual(column, factorSvd.W().ColumnCount);

            // Make sure the U*W*VT is the original matrix.
            var matrix = factorSvd.U() * factorSvd.W() * factorSvd.VT();

            for (var i = 0; i < matrix.RowCount; i++)
            {
                for (var j = 0; j < matrix.ColumnCount; j++)
                {
                    Assert.AreApproximatelyEqual(matrixA[i, j].Real, matrix[i, j].Real, 1e-3f);
                    Assert.AreApproximatelyEqual(matrixA[i, j].Imaginary, matrix[i, j].Imaginary, 1e-3f);
                }
            }
        }
        public void CanSolveForRandomVector(int row, int column)
        {
            var matrixA     = MatrixLoader.GenerateRandomUserDefinedMatrix(row, column);
            var matrixACopy = matrixA.Clone();
            var factorSvd   = matrixA.Svd(true);

            var vectorb = MatrixLoader.GenerateRandomUserDefinedVector(row);
            var resultx = factorSvd.Solve(vectorb);

            Assert.AreEqual(matrixA.ColumnCount, resultx.Count);

            var bReconstruct = matrixA * resultx;

            // Check the reconstruction.
            for (var i = 0; i < vectorb.Count; i++)
            {
                Assert.AreApproximatelyEqual(vectorb[i], bReconstruct[i], 10e-5f);
            }

            // Make sure A didn't change.
            for (var i = 0; i < matrixA.RowCount; i++)
            {
                for (var j = 0; j < matrixA.ColumnCount; j++)
                {
                    Assert.AreEqual(matrixACopy[i, j], matrixA[i, j]);
                }
            }
        }
예제 #11
0
        public void CanSolveForRandomMatrix(int row, int count)
        {
            var matrixA     = MatrixLoader.GenerateRandomDenseMatrix(row, count);
            var matrixACopy = matrixA.Clone();
            var factorSvd   = matrixA.Svd(true);

            var matrixB = MatrixLoader.GenerateRandomDenseMatrix(row, count);
            var matrixX = factorSvd.Solve(matrixB);

            // The solution X row dimension is equal to the column dimension of A
            Assert.AreEqual(matrixA.ColumnCount, matrixX.RowCount);
            // The solution X has the same number of columns as B
            Assert.AreEqual(matrixB.ColumnCount, matrixX.ColumnCount);

            var matrixBReconstruct = matrixA * matrixX;

            // Check the reconstruction.
            for (var i = 0; i < matrixB.RowCount; i++)
            {
                for (var j = 0; j < matrixB.ColumnCount; j++)
                {
                    Assert.AreApproximatelyEqual(matrixB[i, j].Real, matrixBReconstruct[i, j].Real, 1e-3f);
                    Assert.AreApproximatelyEqual(matrixB[i, j].Imaginary, matrixBReconstruct[i, j].Imaginary, 1e-3f);
                }
            }

            // Make sure A didn't change.
            for (var i = 0; i < matrixA.RowCount; i++)
            {
                for (var j = 0; j < matrixA.ColumnCount; j++)
                {
                    Assert.AreEqual(matrixACopy[i, j], matrixA[i, j]);
                }
            }
        }
예제 #12
0
        public void CaptureScreenshot_WhenBitmapIsNullAndZoomed_CapturesScaledScreenshotIntoNewBitmap()
        {
            var parameters = new CaptureParameters()
            {
                Zoom = 0.25
            };

            using (var grabber = new ScreenGrabber(parameters))
            {
                if (ScreenGrabber.CanCaptureScreenshot())
                {
                    using (Bitmap bitmap = grabber.CaptureScreenshot(null))
                    {
                        TestLog.EmbedImage("Screenshot with 0.25x zoom", bitmap);

                        Assert.Multiple(() =>
                        {
                            Assert.AreApproximatelyEqual(ScreenGrabber.GetScreenSize().Width / 2,
                                                         grabber.ScreenshotWidth, 1);
                            Assert.AreApproximatelyEqual(ScreenGrabber.GetScreenSize().Height / 2,
                                                         grabber.ScreenshotHeight, 1);
                            Assert.AreEqual(grabber.ScreenshotWidth, bitmap.Width);
                            Assert.AreEqual(grabber.ScreenshotHeight, bitmap.Height);
                        });
                    }
                }
                else
                {
                    Assert.Throws <ScreenshotNotAvailableException>(() => grabber.CaptureScreenshot(null),
                                                                    "CanCaptureScreenshot returned false so expected an exception to be thrown.");
                }
            }
        }
예제 #13
0
        public void CanSolveForRandomVector(int order)
        {
            var matrixA           = MatrixLoader.GenerateRandomUserDefinedMatrix(order, order);
            var matrixACopy       = matrixA.Clone();
            var factorGramSchmidt = matrixA.GramSchmidt();

            var vectorb = MatrixLoader.GenerateRandomUserDefinedVector(order);
            var resultx = factorGramSchmidt.Solve(vectorb);

            Assert.AreEqual(matrixA.ColumnCount, resultx.Count);

            var bReconstruct = matrixA * resultx;

            // Check the reconstruction.
            for (var i = 0; i < order; i++)
            {
                Assert.AreApproximatelyEqual(vectorb[i].Real, bReconstruct[i].Real, 1.0e-9);
                Assert.AreApproximatelyEqual(vectorb[i].Imaginary, bReconstruct[i].Imaginary, 1.0e-9);
            }

            // Make sure A didn't change.
            for (var i = 0; i < matrixA.RowCount; i++)
            {
                for (var j = 0; j < matrixA.ColumnCount; j++)
                {
                    Assert.AreEqual(matrixACopy[i, j], matrixA[i, j]);
                }
            }
        }
예제 #14
0
        public void EuclideanDistanceTest( )
        {
            EuclideanDistance dist = new EuclideanDistance( );

            Assert.Throws <ArgumentException>(() => dist.GetDistance(p0, q4));

            double result = dist.GetDistance(p0, q0);

            Assert.AreApproximatelyEqual(result, .70711, 0.00001);

            result = dist.GetDistance(p1, q1);
            Assert.AreApproximatelyEqual(result, 1.11803, 0.00001);

            result = dist.GetDistance(p2, q2);
            Assert.AreEqual(result, 0);

            result = dist.GetDistance(p3, q3);
            Assert.AreEqual(result, 0);

            result = dist.GetDistance(p4, q4);
            Assert.AreApproximatelyEqual(result, 2.39792, 0.00001);

            result = dist.GetDistance(p5, q5);
            Assert.AreApproximatelyEqual(result, 4.24264, 0.00001);
        }
예제 #15
0
        public void CanSolveForRandomMatrix(int order)
        {
            var matrixA = MatrixLoader.GenerateRandomDenseMatrix(order, order);
            var matrixB = MatrixLoader.GenerateRandomDenseMatrix(order, order);

            var monitor = new Iterator(new IIterationStopCriterium<double>[]
                                       {
                                           new IterationCountStopCriterium(1000),
                                           new ResidualStopCriterium(1e-10)
                                       });
            var solver = new MlkBiCgStab(monitor);
            var matrixX = solver.Solve(matrixA, matrixB);

            // The solution X row dimension is equal to the column dimension of A
            Assert.AreEqual(matrixA.ColumnCount, matrixX.RowCount);
            // The solution X has the same number of columns as B
            Assert.AreEqual(matrixB.ColumnCount, matrixX.ColumnCount);

            var matrixBReconstruct = matrixA * matrixX;

            // Check the reconstruction.
            for (var i = 0; i < matrixB.RowCount; i++)
            {
                for (var j = 0; j < matrixB.ColumnCount; j++)
                {
                    Assert.AreApproximatelyEqual(matrixB[i, j], matrixBReconstruct[i, j], 1.0e-7);
                }
            }
        }
            public void Execute(ref ModifiableContactHeader manifold, ref ModifiableContactPoint contact)
            {
                if (!m_Initialized)
                {
                    m_Initialized   = true;
                    CurrentManifold = manifold;
                    NumContacts     = 0;
                }

                // Header verification
                Assert.AreEqual(manifold.CustomTagsA, (byte)0);
                Assert.AreEqual(manifold.CustomTagsB, (byte)0);
                Assert.AreNotEqual(manifold.BodyIndexA, manifold.BodyIndexB);
                Assert.AreApproximatelyEqual(manifold.CoefficientOfFriction, 0.5f, 0.01f);
                Assert.AreApproximatelyEqual(manifold.CoefficientOfRestitution, 0.0f, 0.01f);
                Assert.AreEqual(manifold.ColliderKeyA.Value, ColliderKey.Empty.Value);
                Assert.AreEqual(manifold.ColliderKeyB.Value, ColliderKey.Empty.Value);
                Assert.AreEqual(manifold.EntityA, Bodies[manifold.BodyIndexA].Entity);
                Assert.AreEqual(manifold.EntityB, Bodies[manifold.BodyIndexB].Entity);
                Assert.AreEqual(manifold.JacobianFlags, (JacobianFlags)0);
                Assert.IsFalse(manifold.Modified);
                Assert.AreEqual(manifold.NumContacts, 4);

                NumContacts++;

                // Contact point verification
                Assert.IsTrue(contact.Index == NumContacts - 1);
                Assert.IsFalse(contact.Modified);

                // Save for later verification
                CurrentManifoldNumContacts[0] = CurrentManifold.NumContacts;
                CurrentManifoldNumContacts[1] = NumContacts;
            }
예제 #17
0
        public IEnumerator AirVelocity()
        {
            player.Movement.AirControl           = 0;
            player.Movement.JumpHeightMultiplier = 3;

            yield return(RunTimeScaleTest(Test(), Test()));

            IEnumerator Test()
            {
                input.moveDirection = new Vector2(0, 1);
                input.isRunning     = true;

                yield return(WaitFrames(60));

                input.isJumping = true;

                yield return(WaitFrames(60));

                input.moveDirection = new Vector2(0, -1);
                yield return(WaitFrames(30));

                Assert.AreApproximatelyEqual(player.Movement.airVelocity.x, player.Movement.groundVelocity.x);
                Assert.AreApproximatelyEqual(player.Movement.airVelocity.z, player.Movement.groundVelocity.z);

                yield return(WaitFrames(60));
            }
        }
예제 #18
0
        public void CalculateCollisionProbability(double expectedProbability, IEnumerable <int> hashes)
        {
            var    store             = new HashStore(hashes);
            double actualProbability = store.Result.CollisionProbability;

            Assert.AreApproximatelyEqual(expectedProbability, actualProbability, 0.0001);
        }
예제 #19
0
        public IEnumerator CanJump()
        {
            Debug.Log("CanJump :: START");

            input.isJumping = true;

            // Skip a frame to let the simulation play a bit.
            yield return(null);

            Debug.Log("CanJump :: Player velocity: " + player.Controller.velocity.y);

            Assert.AreApproximatelyEqual(8.944f, player.Controller.velocity.y, 0.1f);

            while (player.Movement.IsJumping || !player.Movement.IsGrounded)
            {
                yield return(null);
            }

            Debug.Log("CanJump :: Set CanJump to false.");
            player.Movement.CanJump = false;
            yield return(new WaitForSecondsRealtime(0.3f)); // Give plenty of time for the player to settle.

            input.isJumping = true;

            yield return(null);

            Debug.Log("CanJump :: Player velocity: " + player.Controller.velocity.y);

            Assert.AreApproximatelyEqual(0, player.Controller.velocity.y, 0.1f);
        }
예제 #20
0
        public void CalculateCollisionProbabilityWithHighLoad()
        {
            var    store             = new HashStore(GenerateHashLoad(Enumerable.Range(0, 10000), 5000));
            double actualProbability = store.Result.CollisionProbability;

            Assert.AreApproximatelyEqual(9.998E-5, actualProbability, 1E-5);
        }
예제 #21
0
        public IEnumerator CameraInvertY()
        {
            Debug.Log("CameraInvertY :: START");

            input.mouseInput          = new Vector2(0, -10);
            player.Camera.InvertYAxis = true;

            yield return(null);

            Debug.Log("CameraInvertY :: Camera rotation: " + player.Camera.CameraHead.localEulerAngles.x);

            Assert.AreApproximatelyEqual(340f, player.Camera.CameraHead.localEulerAngles.x, 0.1f);

            Debug.Log("CameraInvertY :: Set InvertY to false.");
            player.Camera.InvertYAxis = false;

            // Let 2 frames pass to get past the 0 rotation.
            yield return(null);

            yield return(null);

            Debug.Log("CameraInvertY :: Camera rotation: " + player.Camera.CameraHead.localEulerAngles.x);

            Assert.AreApproximatelyEqual(20f, player.Camera.CameraHead.localEulerAngles.x, 0.1f);
        }
예제 #22
0
        public void DistributionDeviationProbability(double expectedProbability, IEnumerable <int> hashes)
        {
            var    store  = new HashStore(hashes);
            double actual = store.Result.UniformDistributionDeviationProbability;

            Assert.AreApproximatelyEqual(expectedProbability, actual, 0.000001);
        }
예제 #23
0
        public IEnumerator CanMoveStops()
        {
            Debug.Log("CanMoveStops :: START");

            input.moveDirection          = new Vector2(0, 1);
            player.Movement.Acceleration = 0;

            // Skip 2 frames to make sure the player gets their speed up.
            yield return(null);

            yield return(null);

            Debug.Log("CanMoveStops :: Player velocity: " + player.Controller.velocity.z);

            Assert.AreApproximatelyEqual(3f, player.Controller.velocity.z);

            yield return(null);

            Debug.Log("CanMoveStops :: Set CanMoveAround to false.");
            player.Movement.CanMoveAround = false;

            // Skip 2 frames to make sure the player slows down.
            yield return(null);

            yield return(null);

            Debug.Log("CanMoveStops :: Player velocity: " + player.Controller.velocity.z);

            Assert.AreApproximatelyEqual(0f, player.Controller.velocity.z);
        }
        public void CanFactorizeRandomMatrix(int order)
        {
            var matrixX = MatrixLoader.GenerateRandomPositiveDefiniteUserDefinedMatrix(order);
            var chol    = matrixX.Cholesky();
            var factorC = chol.Factor;

            // Make sure the Cholesky factor has the right dimensions.
            Assert.AreEqual(order, factorC.RowCount);
            Assert.AreEqual(order, factorC.ColumnCount);

            // Make sure the Cholesky factor is lower triangular.
            for (var i = 0; i < factorC.RowCount; i++)
            {
                for (var j = i + 1; j < factorC.ColumnCount; j++)
                {
                    Assert.AreEqual(0.0, factorC[i, j]);
                }
            }

            // Make sure the cholesky factor times it's transpose is the original matrix.
            var matrixXfromC = factorC * factorC.Transpose();

            for (var i = 0; i < matrixXfromC.RowCount; i++)
            {
                for (var j = 0; j < matrixXfromC.ColumnCount; j++)
                {
                    Assert.AreApproximatelyEqual(matrixX[i, j], matrixXfromC[i, j], 1.0e-11);
                }
            }
        }
예제 #25
0
        public void CanSolveForRandomVectorAndSymmetricMatrixWhenResultVectorGiven(int order)
        {
            var matrixA     = MatrixLoader.GenerateRandomPositiveDefiniteUserDefinedMatrix(order);
            var matrixACopy = matrixA.Clone();
            var factorEvd   = matrixA.Evd();
            var vectorb     = MatrixLoader.GenerateRandomUserDefinedVector(order);
            var vectorbCopy = vectorb.Clone();
            var resultx     = new UserDefinedVector(order);

            factorEvd.Solve(vectorb, resultx);

            var bReconstruct = matrixA * resultx;

            // Check the reconstruction.
            for (var i = 0; i < vectorb.Count; i++)
            {
                Assert.AreApproximatelyEqual(vectorb[i], bReconstruct[i], 1.0e-10);
            }

            // Make sure A didn't change.
            for (var i = 0; i < matrixA.RowCount; i++)
            {
                for (var j = 0; j < matrixA.ColumnCount; j++)
                {
                    Assert.AreEqual(matrixACopy[i, j], matrixA[i, j]);
                }
            }

            // Make sure b didn't change.
            for (var i = 0; i < vectorb.Count; i++)
            {
                Assert.AreEqual(vectorbCopy[i], vectorb[i]);
            }
        }
        public void CanSolveForRandomVector(int order)
        {
            var matrixA     = MatrixLoader.GenerateRandomPositiveDefiniteUserDefinedMatrix(order);
            var matrixACopy = matrixA.Clone();
            var chol        = matrixA.Cholesky();
            var b           = MatrixLoader.GenerateRandomUserDefinedVector(order);
            var x           = chol.Solve(b);

            Assert.AreEqual(b.Count, x.Count);

            var bReconstruct = matrixA * x;

            // Check the reconstruction.
            for (var i = 0; i < order; i++)
            {
                Assert.AreApproximatelyEqual(b[i], bReconstruct[i], 1.0e-11);
            }

            // Make sure A didn't change.
            for (var i = 0; i < matrixA.RowCount; i++)
            {
                for (var j = 0; j < matrixA.ColumnCount; j++)
                {
                    Assert.AreEqual(matrixACopy[i, j], matrixA[i, j]);
                }
            }
        }
        public void CanSolveForRandomVector(int order)
        {
            var matrixA     = MatrixLoader.GenerateRandomDenseMatrix(order, order);
            var matrixACopy = matrixA.Clone();
            var factorLU    = matrixA.LU();

            var vectorb = MatrixLoader.GenerateRandomDenseVector(order);
            var resultx = factorLU.Solve(vectorb);

            Assert.AreEqual(matrixA.ColumnCount, resultx.Count);

            var bReconstruct = matrixA * resultx;

            // Check the reconstruction.
            for (var i = 0; i < order; i++)
            {
                Assert.AreApproximatelyEqual(vectorb[i], bReconstruct[i], 10e-5f);
            }

            // Make sure A didn't change.
            for (var i = 0; i < matrixA.RowCount; i++)
            {
                for (var j = 0; j < matrixA.ColumnCount; j++)
                {
                    Assert.AreEqual(matrixACopy[i, j], matrixA[i, j]);
                }
            }
        }
        public void CanSolveForRandomMatrix(int row, int col)
        {
            var matrixA     = MatrixLoader.GenerateRandomPositiveDefiniteUserDefinedMatrix(row);
            var matrixACopy = matrixA.Clone();
            var chol        = matrixA.Cholesky();
            var matrixB     = MatrixLoader.GenerateRandomUserDefinedMatrix(row, col);
            var matrixX     = chol.Solve(matrixB);

            Assert.AreEqual(matrixB.RowCount, matrixX.RowCount);
            Assert.AreEqual(matrixB.ColumnCount, matrixX.ColumnCount);

            var matrixBReconstruct = matrixA * matrixX;

            // Check the reconstruction.
            for (var i = 0; i < matrixB.RowCount; i++)
            {
                for (var j = 0; j < matrixB.ColumnCount; j++)
                {
                    Assert.AreApproximatelyEqual(matrixB[i, j], matrixBReconstruct[i, j], 1.0e-11);
                }
            }

            // Make sure A didn't change.
            for (var i = 0; i < matrixA.RowCount; i++)
            {
                for (var j = 0; j < matrixA.ColumnCount; j++)
                {
                    Assert.AreEqual(matrixACopy[i, j], matrixA[i, j]);
                }
            }
        }
        public void CanInverse(int order)
        {
            var matrixA     = MatrixLoader.GenerateRandomDenseMatrix(order, order);
            var matrixACopy = matrixA.Clone();
            var factorLU    = matrixA.LU();

            var matrixAInverse = factorLU.Inverse();

            // The inverse dimension is equal A
            Assert.AreEqual(matrixAInverse.RowCount, matrixAInverse.RowCount);
            Assert.AreEqual(matrixAInverse.ColumnCount, matrixAInverse.ColumnCount);

            var matrixIdentity = matrixA * matrixAInverse;

            // Make sure A didn't change.
            for (var i = 0; i < matrixA.RowCount; i++)
            {
                for (var j = 0; j < matrixA.ColumnCount; j++)
                {
                    Assert.AreEqual(matrixACopy[i, j], matrixA[i, j]);
                }
            }

            // Check if multiplication of A and AI produced identity matrix.
            for (var i = 0; i < matrixIdentity.RowCount; i++)
            {
                Assert.AreApproximatelyEqual(matrixIdentity[i, i], 1.0f, 10e-5f);
            }
        }
예제 #30
0
        public void CosineDistanceTest( )
        {
            CosineDistance dist = new CosineDistance( );

            Assert.Throws <ArgumentException>(() => dist.GetDistance(p0, q4));

            double result = dist.GetDistance(p0, q0);

            Assert.AreApproximatelyEqual(result, .2, 0.00001);

            result = dist.GetDistance(p1, q1);
            Assert.AreApproximatelyEqual(result, 0.029857, 0.00001);

            result = dist.GetDistance(p2, q2);
            Assert.AreEqual(result, 1);

            result = dist.GetDistance(p3, q3);
            Assert.AreApproximatelyEqual(result, 0, 0.00001);

            result = dist.GetDistance(p4, q4);
            Assert.AreApproximatelyEqual(result, 0.039354, 0.00001);

            result = dist.GetDistance(p5, q5);
            Assert.AreApproximatelyEqual(result, 0.031026, 0.00001);
        }