Exemplo n.º 1
0
        public void TestVectorMath()
        {
            Vector v1 = new Vector(5);
            Vector v2 = new Vector(5);

            v1.SetValue(0, 1.0);
            v1.SetValue(1, 2.0);
            v1.SetValue(2, 3.0);
            v1.SetValue(3, 4.0);
            v1.SetValue(4, 5.0);

            v2.SetValue(0, 5.0);
            v2.SetValue(1, 4.0);
            v2.SetValue(2, 3.0);
            v2.SetValue(3, 2.0);
            v2.SetValue(4, 1.0);

            Vector sum = new Vector(5);
            sum.SetValue(0, 6.0);
            sum.SetValue(1, 6.0);
            sum.SetValue(2, 6.0);
            sum.SetValue(3, 6.0);
            sum.SetValue(4, 6.0);

            Assert.AreEqual(sum, v1 + v2);

            Vector diff = new Vector(5);
            diff.SetValue(0, -4.0);
            diff.SetValue(1, -2.0);
            diff.SetValue(2, 0.0);
            diff.SetValue(3, 2.0);
            diff.SetValue(4, 4.0);

            Assert.AreEqual(diff, v1 - v2);

            double dotprod = 35.0;

            Assert.AreEqual(dotprod, v1 * v2);

            Matrix m = new Matrix(2, 2);
            m.SetValue(0, 0, 1);
            m.SetValue(0, 1, 2);
            m.SetValue(1, 0, 3);
            m.SetValue(1, 1, 4);

            Vector v = new Vector(2);
            v.SetValue(0, 1);
            v.SetValue(1, 2);

            Vector result1 = new Vector(2);
            result1.SetValue(0, 5);
            result1.SetValue(1, 11);

            Vector result2 = new Vector(2);
            result2.SetValue(0, 7);
            result2.SetValue(1, 10);

            Assert.AreEqual(result1, m * v);
            Assert.AreEqual(result2, v * m);
        }
Exemplo n.º 2
0
        /* Matrix * Vector -> Vector */
        public Vector MultiplyVector(object right)
        {
            if (right.GetType() != typeof(Vector))
            {
                throw new InvalidOperationException("Can't call MultiplyVector with anything but a Vector");
            }
            Vector rightCast = (Vector)right;

            if (this.N != rightCast.Size)
            {
                throw new DimensionMismatchException("The N dimension of the matrix must equal the size of the vector");
            }

            Vector result = new Vector(this.m);

            for (int r = 0; r < this.m; r++)
            {
                for (int i = 0; i < this.n; i++)
                {
                    result.SetValue(r, result.GetValue(r) +
                                    rightCast.GetValue(i) * this.GetValue(r, i));
                }
            }
            return(result);
        }
Exemplo n.º 3
0
        /* Vector * Matrix -> Vector */
        public Vector MultiplyMatrix(object right)
        {
            if (right.GetType() != typeof(Matrix))
            {
                throw new InvalidOperationException("Can't call MultiplyMatrix with anything but a Matrix");
            }
            Matrix rightCast = (Matrix)right;

            if (this.Size != rightCast.M)
            {
                throw new DimensionMismatchException("The dimension of the vector must"
                                                     + " equal the M dimension of the matrix");
            }

            Vector result = new Vector(rightCast.N);

            for (int c = 0; c < rightCast.N; c++)
            {
                for (int i = 0; i < this.Size; i++)
                {
                    result.SetValue(c, result.GetValue(c) +
                                    this.GetValue(i) * rightCast.GetValue(i, c));
                }
            }
            return(result);
        }
Exemplo n.º 4
0
        /* Vector * Scalar -> Vector */
        public Vector MultiplyScalar(object right)
        {
            double rightCast = 0.0;

            if (right.GetType() == typeof(int) ||
                right.GetType() == typeof(float))
            {
                rightCast = Convert.ToDouble(right);
            }
            else if (right.GetType() == typeof(double))
            {
                rightCast = (double)right;
            }
            else
            {
                throw new InvalidOperationException("Scalar must be a double or convertable to a double");
            }
            Vector result = new Vector(this.Size);

            for (int i = 0; i < result.Size; i++)
            {
                result.SetValue(i, this.GetValue(i) * rightCast);
            }
            return(result);
        }
Exemplo n.º 5
0
        public void TestVectorIndexer()
        {
            Vector v = new Vector(3);
            v.SetValue(0, 1.0);
            v.SetValue(1, 2.0);
            v.SetValue(2, 3.0);

            Assert.AreEqual(v[0], 1.0);
            Assert.AreEqual(v[1], 2.0);
            Assert.AreEqual(v[2], 3.0);

            v[0] = 1.0;
            v[1] = 2.0;
            v[2] = 3.0;

            Assert.AreEqual(v[0], 1.0);
            Assert.AreEqual(v[1], 2.0);
            Assert.AreEqual(v[2], 3.0);
        }
Exemplo n.º 6
0
        /* Vector * Scalar -> Vector */
        public Vector MultiplyScalar(object right)
        {
            double rightCast = 0.0;
            if (right.GetType() == typeof(int) ||
                right.GetType() == typeof(float)) {
                rightCast = Convert.ToDouble(right);
            } else if (right.GetType() == typeof(double)) {
                rightCast = (double)right;
            } else {
                throw new InvalidOperationException("Scalar must be a double or convertable to a double");
            }
            Vector result = new Vector(this.Size);

            for (int i = 0; i < result.Size; i++) {
                result.SetValue(i, this.GetValue(i) * rightCast);
            }
            return result;
        }
Exemplo n.º 7
0
        /* Add two vectors */
        public Vector AddVector(object right)
        {
            if (!checkValidType(right)) {
                throw new InvalidOperationException("Can only add vectors with other vectors");
            }

            Vector rightCast = (Vector)right;

            if (this.Size != rightCast.Size) {
                throw new DimensionMismatchException("Vectors must be same size");
            }

            Vector result = new Vector(this.Size);
            for (int i = 0; i < this.Size; i++) {
                result.SetValue(i, this.GetValue(i) + rightCast.GetValue(i));
            }
            return result;
        }
Exemplo n.º 8
0
        /* Vector * Matrix -> Vector */
        public Vector MultiplyMatrix(object right)
        {
            if (right.GetType() != typeof(Matrix)) {
                throw new InvalidOperationException("Can't call MultiplyMatrix with anything but a Matrix");
            }
            Matrix rightCast = (Matrix)right;
            if (this.Size != rightCast.M) {
                throw new DimensionMismatchException("The dimension of the vector must"
                                     + " equal the M dimension of the matrix");
            }

            Vector result = new Vector(rightCast.N);
            for (int c = 0; c < rightCast.N; c++) {
                for (int i = 0; i < this.Size; i++) {
                    result.SetValue(c, result.GetValue(c) +
                        this.GetValue(i) * rightCast.GetValue(i, c));
                }
            }
            return result;
        }
Exemplo n.º 9
0
        /* Add two vectors */
        public Vector AddVector(object right)
        {
            if (!checkValidType(right))
            {
                throw new InvalidOperationException("Can only add vectors with other vectors");
            }

            Vector rightCast = (Vector)right;

            if (this.Size != rightCast.Size)
            {
                throw new DimensionMismatchException("Vectors must be same size");
            }

            Vector result = new Vector(this.Size);

            for (int i = 0; i < this.Size; i++)
            {
                result.SetValue(i, this.GetValue(i) + rightCast.GetValue(i));
            }
            return(result);
        }
Exemplo n.º 10
0
        /* Matrix * Vector -> Vector */
        public Vector MultiplyVector(object right)
        {
            if (right.GetType() != typeof(Vector)) {
                throw new InvalidOperationException("Can't call MultiplyVector with anything but a Vector");
            }
            Vector rightCast = (Vector)right;

            if (this.N != rightCast.Size) {
                throw new DimensionMismatchException("The N dimension of the matrix must equal the size of the vector");
            }

            Vector result = new Vector(this.m);
            for (int r = 0; r < this.m; r++) {
                for (int i = 0; i < this.n; i++) {
                    result.SetValue(r, result.GetValue(r) +
                        rightCast.GetValue(i) * this.GetValue(r, i));
                }
            }
            return result;
        }
Exemplo n.º 11
0
        public void TestVectorSize()
        {
            Vector v = new Vector(5);

            v.SetValue(0, 1.0);
            v.SetValue(1, 1.0);
            v.SetValue(2, 1.0);
            v.SetValue(3, 1.0);
            v.SetValue(4, 1.0);

            Assert.AreEqual(v.Size, 5);
        }