/// <summary>
        /// Multiplies the Matrix A with a vector v.
        /// A * v.
        /// </summary>
        /// <returns>The new vector.</returns>
        /// <param name="matrix">The matrix.</param>
        /// <param name="vector">The vector.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        /// <typeparam name="TRing">The underlying structure.</typeparam>
        public static IDirectSum <T, TRing> MultiplyVector <T, TRing> (
            this IMatrix <T, TRing> matrix,
            IDirectSum <T, TRing> vector)
            where TRing : IRing <T>, new()
        {
            if (matrix.ColumnDimension != vector.Dimension)
            {
                throw new IndexOutOfRangeException("The ColumnDimension of the matrix needs to equal the row dimension of the vector");
            }

            var baseStructure = new TRing();
            var vect          = vector.ReturnNewInstance(matrix.RowDimension);

            for (UInt32 i = 0; i < matrix.RowDimension; i++)
            {
                for (UInt32 j = 0; j < matrix.ColumnDimension; j++)
                {
                    vect[i] = baseStructure.Addition(
                        vect[i],
                        baseStructure.Multiplication(matrix[i, j], vector[j]));
                }
            }

            return(vect);
        }
        /// <summary>
        /// Copy the specified tuple.
        /// </summary>
        /// <returns>A copy of the tuple.</returns>
        /// <param name="tuple">The tuple.</param>
        /// <typeparam name="T">The type parameter.</typeparam>
        /// <typeparam name="TStruct">The underlying structure.</typeparam>
        public static IDirectSum <T, TStruct> Copy <T, TStruct> (this IDirectSum <T, TStruct> tuple)
            where TStruct : IStructure, new()
        {
            var vec = tuple.ReturnNewInstance(tuple.Dimension);

            for (UInt32 i = 0; i < tuple.Dimension; i++)
            {
                vec[i] = tuple[i];
            }

            return(vec);
        }
Пример #3
0
        /// <summary>
        /// Returns a vector with inverse values of the given vector.
        /// </summary>
        /// <returns>The inverse vector.</returns>
        /// <param name="tuple">The tuple.</param>
        /// <typeparam name="T">The type parameter.</typeparam>
        /// <typeparam name="TGroup">The underlying structure.</typeparam>
        public static IDirectSum <T, TGroup> InverseElement <T, TGroup> (this IDirectSum <T, TGroup> tuple)
            where TGroup : IGroup <T>, new()
        {
            var vec        = tuple.ReturnNewInstance(tuple.Dimension);
            var baseStruct = new TGroup();

            for (UInt32 i = 0; i < vec.Dimension; i++)
            {
                vec[i] = baseStruct.Inverse(tuple[i]);
            }

            return(vec);
        }
        /// <summary>
        /// Multiplies a scalar lambda with a vector v.
        /// </summary>
        /// <returns>lambda * v.</returns>
        /// <param name="tuple">The vector.</param>
        /// <param name="scalar">The scalar.</param>
        /// <typeparam name="T">The type parameter.</typeparam>
        /// <typeparam name="TRing">The underlying structure.</typeparam>
        public static IDirectSum <T, TRing> ScalarMultiply <T, TRing> (
            this IDirectSum <T, TRing> tuple,
            T scalar)
            where TRing : IRing <T>, new()
        {
            var ring = new TRing();
            var vec  = tuple.ReturnNewInstance(tuple.Dimension);

            for (UInt32 i = 0; i < tuple.Dimension; i++)
            {
                vec[i] = ring.Multiplication(scalar, tuple[i]);
            }

            return(vec);
        }
        /// <summary>
        /// Multiplies the vector power times.
        /// Multiplies the vector with itself power times.
        /// </summary>
        /// <param name="tuple">The vector.</param>
        /// <param name="power">The power.</param>
        /// <typeparam name="T">The type parameter.</typeparam>
        /// <typeparam name="TRing">The underlying structure.</typeparam>
        /// <returns>The vector^power.</returns>
        public static IDirectSum <T, TRing> Pow <T, TRing> (
            this IDirectSum <T, TRing> tuple,
            UInt32 power)
            where TRing : IRing <T>, new()
        {
            var result = tuple.ReturnNewInstance(tuple.Dimension);
            var ring   = new TRing();

            for (UInt32 i = 0; i < result.Dimension; i++)
            {
                result[i] = ring.One;
            }

            for (UInt32 i = 0; i < power; i++)
            {
                result = tuple.Multiply(result);
            }

            return(result);
        }
        /// <summary>
        /// Point multiplication of vector1 and vector2.
        /// The dimensions must agree.
        /// </summary>
        /// <param name="tuple1">The left vector.</param>
        /// <param name="tuple2">The right.</param>
        /// <typeparam name="T">The type parameter.</typeparam>
        /// <typeparam name="TRing">The underlying structure.</typeparam>
        /// <returns>The multiplication vector1 * vector2.</returns>
        public static IDirectSum <T, TRing> Multiply <T, TRing> (
            this IDirectSum <T, TRing> tuple1,
            IDirectSum <T, TRing> tuple2)
            where TRing : IRing <T>, new()
        {
            if (tuple1.Dimension != tuple2.Dimension)
            {
                // TODO throw right exception
                throw new IndexOutOfRangeException("The vector dimensions need to agree");
            }

            var ring = new TRing();

            var result = tuple1.ReturnNewInstance(tuple1.Dimension);

            for (UInt32 i = 0; i < tuple1.Dimension; i++)
            {
                result[i] = ring.Multiplication(tuple1[i], tuple2[i]);
            }

            return(result);
        }
Пример #7
0
        /// <summary>
        /// Add the specified vector1 and vector2.
        /// </summary>
        /// <param name="tuple1">The left vector.</param>
        /// <param name="tuple2">The right vector.</param>
        /// <typeparam name="T">The type parameter.</typeparam>
        /// <typeparam name="TMonoid">The underlying structure.</typeparam>
        /// <returns>vector1 + vector2.</returns>
        public static IDirectSum <T, TMonoid> Add <T, TMonoid> (
            this IDirectSum <T, TMonoid> tuple1,
            IDirectSum <T, TMonoid> tuple2)
            where TMonoid : IMonoid <T>, new()
        {
            var group = new TMonoid();

            // Todo throw right exception
            if (tuple1.Dimension != tuple2.Dimension)
            {
                throw new IndexOutOfRangeException("The dimension of the two vectors do not agree");
            }

            var result = tuple1.ReturnNewInstance(tuple1.Dimension);

            for (UInt32 i = 0; i < tuple1.Dimension; i++)
            {
                result[i] = group.Addition(tuple1[i], tuple2[i]);
            }

            return(result);
        }
Пример #8
0
        /// <summary>
        /// Increases the vector Dimension by additionalDimensions.
        /// The values of the parameters are default(T).
        /// </summary>
        /// <param name="tuple">The tuple.</param>
        /// <param name="additionalDimensions">Additional dimensions.</param>
        /// <typeparam name="T">The type parameter.</typeparam>
        /// <typeparam name="TMonoid">The underlying structure.</typeparam>
        /// <returns>A new vector with dimension dimension(original vector) + additionalDimension.
        /// The first values are the values of the original vector. The other values are the zero elements of the group
        /// associated with T.
        /// </returns>
        public static IDirectSum <T, TMonoid> Injection <T, TMonoid> (
            this IDirectSum <T, TMonoid> tuple,
            UInt32 additionalDimensions)
            where TMonoid : IMonoid <T>, new()
        {
            var newDimension = tuple.Dimension + additionalDimensions;
            var group        = new TMonoid();

            var vec = tuple.ReturnNewInstance(tuple.Dimension + additionalDimensions);

            for (UInt32 i = 0; i < newDimension; i++)
            {
                if (i < tuple.Dimension)
                {
                    vec[i] = tuple[i];
                    continue;
                }

                vec[i] = group.Zero;
            }

            return(vec);
        }