コード例 #1
0
        /// <summary>
        /// Sorts the vector with insertion sort method.
        /// </summary>
        /// <returns>The sort.</returns>
        /// <param name="tuple">The sorted vector is a new vector, not a reference to the argument.</param>
        /// <typeparam name="T">The type parameter.</typeparam>
        /// <typeparam name="TStruct">The underlying structure.</typeparam>
        public static IDirectSum <T, TStruct> InsertionSort <T, TStruct> (this IDirectSum <T, TStruct> tuple)
            where T : IComparable
            where TStruct : IStructure, new()
        {
            var vec = tuple.Copy();

            if (tuple.Dimension < 2)
            {
                return(vec);
            }

            for (UInt32 j = 1; j < vec.Dimension; j++)
            {
                var key = vec[j];

                // insert vector[i] into the sorted sequence vector[1..i-1]
                UInt32 i = j - 1;
                while (i >= 0 && vec[i].CompareTo(key) > 0)
                {
                    vec[i + 1] = vec[i];
                    vec[i]     = key;

                    // Security because UInt32
                    if (i == 0)
                    {
                        break;
                    }

                    i--;
                }
            }

            return(vec);
        }
コード例 #2
0
        /// <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);
        }
コード例 #3
0
        /// <summary>
        /// Sorts the vector with bubble sort algorithm.
        /// </summary>
        /// <returns>A copy of the vector (sorted).</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> BubbleSort <T, TStruct> (this IDirectSum <T, TStruct> tuple)
            where T : IComparable
            where TStruct : IStructure, new()
        {
            var vec = tuple.Copy();

            if (vec.Dimension <= 1)
            {
                return(vec);
            }

            for (var i = 0; i < vec.Dimension - 1; i++)
            {
                for (var j = vec.Dimension - 1; j > i; j--)
                {
                    if (vec[j].CompareTo(vec[j - 1]) < 0)
                    {
                        var temp = vec[j];
                        vec[j]     = vec[j - 1];
                        vec[j - 1] = temp;
                    }
                }
            }

            return(vec);
        }
コード例 #4
0
        public void SumElements_EqualsExpectedValue <T, TMonoid> (
            T expectedValue,
            TMonoid hackForGenericParameter,
            IDirectSum <T, TMonoid> tuple)
            where TMonoid : IMonoid <T>, new()
        {
            var result = tuple.SumElements();

            Assert.AreEqual(expectedValue, result);
        }
コード例 #5
0
        public void Multiply_MultiplyTwoTuples_EqualsExpectedTuple <T, TRing> (
            T hackForGenericParameter1,
            TRing hackForGenericParameter2,
            IDirectSum <T, TRing> tuple1,
            IDirectSum <T, TRing> tuple2,
            IDirectSum <T, TRing> expectedTuple)
            where TRing : IRing <T>, new()
        {
            var result = tuple1.Multiply(tuple2);

            Assert.AreEqual(expectedTuple, result);
        }
コード例 #6
0
        /// <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);
        }
コード例 #7
0
        public void Add_AddTwoTuples_EqualsExpectedTuple <T, TMonoid> (
            T hackForGenericParameter1,
            TMonoid hackForGenericParameter2,
            IDirectSum <T, TMonoid> tuple1,
            IDirectSum <T, TMonoid> tuple2,
            IDirectSum <T, TMonoid> expectedTuple)
            where TMonoid : IMonoid <T>, new()
        {
            var result = tuple1.Add(tuple2);

            Assert.AreEqual(expectedTuple, result);
        }
コード例 #8
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);
        }
コード例 #9
0
        /// <summary>
        /// Adds all the elements in the vector.
        /// </summary>
        /// <returns>The sum over all elements in the vector.</returns>
        /// <param name="vector">The vector.</param>
        /// <typeparam name="T">The type parameter.</typeparam>
        /// <typeparam name="TMonoid">The underlying structure.</typeparam>
        public static T SumElements <T, TMonoid> (this IDirectSum <T, TMonoid> vector)
            where TMonoid : IMonoid <T>, new()
        {
            var group = new TMonoid();

            var result = group.Zero;

            for (UInt32 i = 0; i < vector.Dimension; i++)   // sum over all entries
            {
                result = group.Addition(result, vector[i]);
            }

            return(result);
        }
コード例 #10
0
        /// <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);
        }
コード例 #11
0
        public void Copy_IsNewInstance_VectorsAreEqual <T, TStruct> (
            T hackForGenericParameter1,
            TStruct hackForGenericParameter2,
            IDirectSum <T, TStruct> tuple)
            where TStruct : IStructure, new()
        {
            var tupleCopy = tuple.Copy();

            // the references and the type should not be the same.
            Assert.IsFalse(object.ReferenceEquals(tuple, tupleCopy));

            // The values should be the same.
            Assert.AreEqual(tuple.Dimension, tupleCopy.Dimension);

            Assert.AreEqual(tuple, tupleCopy);
        }
コード例 #12
0
        public void InverseElement <T, TGroup> (
            T hackForGenericParameter,
            TGroup underlyingGroup,
            IDirectSum <T, TGroup> tuple)
            where TGroup : IGroup <T>, new()
        {
            var inverseElement = tuple.InverseElement();

            Assert.IsNotNull(inverseElement);

            Assert.AreEqual(tuple.Dimension, inverseElement.Dimension);

            for (uint i = 0; i < tuple.Dimension; i++)
            {
                var expectedInverse = underlyingGroup.Inverse(tuple [i]);
                Assert.AreEqual(expectedInverse, inverseElement [i]);
            }
        }
コード例 #13
0
        public void Injection_InjectNewDimension_DimensionEqualsExpected <T, TMonoid> (
            T hackForGenericParameter1,
            TMonoid monoid,
            IDirectSum <T, TMonoid> tuple,
            uint additionalDimensions)
            where TMonoid : IMonoid <T>, new()
        {
            var result          = tuple.Injection(additionalDimensions);
            var resultDimension = result.Dimension;

            var tupleDimension = tuple.Dimension;

            var expectedResultDimension = tupleDimension + additionalDimensions;

            Assert.AreEqual(expectedResultDimension, resultDimension);

            Assert.AreEqual(monoid.Zero, result [resultDimension - 1]);
        }
コード例 #14
0
        /// <summary>
        /// Calculates the scalar product of two vectors.
        /// </summary>
        /// <returns>The product.</returns>
        /// <param name="vector1">The left vector.</param>
        /// <param name="vector2">The right vector.</param>
        /// <typeparam name="T">The type parameter.</typeparam>
        /// <typeparam name="TStruct">The underlying structure.</typeparam>
        public static T ScalarProduct <T, TStruct> (
            this IDirectSum <T, TStruct> vector1,
            IDirectSum <T, TStruct> vector2)
            where TStruct : IRing <T>, new()
        {
            var ring = new TStruct();

            var result = ring.Zero;

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

            return(result);
        }
コード例 #15
0
        /// <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);
        }
コード例 #16
0
        /// <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);
        }
コード例 #17
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);
        }
コード例 #18
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);
        }
コード例 #19
0
        public void BubbleSort_Sort_AreEqual <T, TStruct> (
            T hackForGenericParameter1,
            TStruct hackForGenericParameter2,
            IDirectSum <T, TStruct> tupleToSort,
            List <T> list)
            where T : IComparable
            where TStruct : IStructure, new()
        {
            list.Sort();

            var sortedVector = tupleToSort.BubbleSort();

            // the references and the type should not be the same.
            Assert.IsFalse(object.ReferenceEquals(sortedVector, tupleToSort));

            // The values should be the same.
            Assert.AreEqual(tupleToSort.Dimension, sortedVector.Dimension);
            Assert.AreEqual(list.Count, sortedVector.Dimension);

            for (uint i = 0; i < list.Count; i++)
            {
                Assert.AreEqual(list[(int)i], sortedVector[i]);
            }
        }