예제 #1
0
 /// <summary>
 /// Obtém a diferença entre o número complexo actual e outro número complexo.
 /// </summary>
 /// <param name="right">O outro número complexo.</param>
 /// <param name="ring">O anel responsável pelas operações.</param>
 /// <returns>O resultado da diferença.</returns>
 /// <exception cref="ArgumentNullException">Se algum dos argumentos for nulo.</exception>
 public ComplexNumber <ObjectType> Subtract(ComplexNumber <ObjectType> right, IRing <ObjectType> ring)
 {
     if (ring == null)
     {
         throw new ArgumentNullException("ring");
     }
     else if (right == null)
     {
         throw new ArgumentNullException("right");
     }
     else
     {
         var result = new ComplexNumber <ObjectType>();
         result.realPart      = ring.Add(this.realPart, ring.AdditiveInverse(right.realPart));
         result.imaginaryPart = ring.Add(this.imaginaryPart, ring.AdditiveInverse(right.imaginaryPart));
         return(result);
     }
 }
예제 #2
0
        public static R EmbedFrom <R>(this IRing <R> g, int a)
        {
            var res = g.Zero();
            var one = g.One();

            for (var i = 0; i < Math.Abs(a); i++)
            {
                res = g.Add(res, one);
            }
            if (a < 0)
            {
                res = g.Negate(res);
            }
            return(res);
        }
예제 #3
0
        /// <summary>
        /// Obtém o produto da matriz corrente com outra matriz.
        /// </summary>
        /// <param name="right">A outra matriz.</param>
        /// <param name="ring">O anel.</param>
        /// <returns>O resultado do produto.</returns>
        public ArrayMathMatrix <ObjectType> ParallelMultiply(
            ArrayMathMatrix <ObjectType> right,
            IRing <ObjectType> ring)
        {
            if (right == null)
            {
                throw new ArgumentNullException("right");
            }
            else if (ring == null)
            {
                throw new ArgumentNullException("ring");
            }
            else
            {
                var columnNumber = this.numberOfColumns;
                var lineNumber   = right.numberOfColumns;
                if (columnNumber != lineNumber)
                {
                    throw new MathematicsException("To multiply two matrices, the number of columns of the first must match the number of lines of second.");
                }
                else
                {
                    var firstDimension  = this.numberOfLines;
                    var secondDimension = right.numberOfColumns;
                    var result          = new ArrayMathMatrix <ObjectType>(
                        firstDimension,
                        secondDimension);
                    Parallel.For(0, firstDimension, i =>
                    {
                        for (int j = 0; j < secondDimension; ++j)
                        {
                            var addResult = ring.AdditiveUnity;
                            for (int k = 0; k < columnNumber; ++k)
                            {
                                var multResult = ring.Multiply(
                                    this.elements[i][k],
                                    right.elements[k][j]);
                                addResult = ring.Add(addResult, multResult);
                            }

                            result.elements[i][j] = addResult;
                        }
                    });

                    return(result);
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Efectua a combinação de vectores, colocando o resultado no primeiro.
        /// </summary>
        /// <typeparam name="T">O tipo dos objectos que constituem as entradas dos vectores.</typeparam>
        /// <param name="replacementArray">O vector a ser substituído.</param>
        /// <param name="combinationArray">O vector a ser combinado.</param>
        /// <param name="firstFactor">O primeiro factor.</param>
        /// <param name="secondFactor">O segundo factor.</param>
        /// <param name="ring">O objecto responsáel pelas operações sobre as entradas dos vectores.</param>
        private void CombineArrays <T>(
            T[] replacementArray,
            T[] combinationArray,
            T firstFactor,
            T secondFactor,
            IRing <T> ring)
        {
            var length = replacementArray.Length;

            for (int i = 0; i < length; ++i)
            {
                var firstValue  = ring.Multiply(firstFactor, replacementArray[i]);
                var secondValue = ring.Multiply(secondFactor, combinationArray[i]);
                replacementArray[i] = ring.Add(firstValue, secondValue);
            }
        }
예제 #5
0
        /// <summary>
        /// Substitui a linha especificada por uma combinação linear desta com uma outra. Por exemplo, li = a * li + b * lj, isto é,
        /// a linha i é substituída pela soma do produto de a pela linha i com o produto de b peloa linha j.
        /// </summary>
        /// <param name="i">A linha a ser substituída.</param>
        /// <param name="j">A linha a ser combinada.</param>
        /// <param name="a">O escalar a ser multiplicado pela primeira linha.</param>
        /// <param name="b">O escalar a ser multiplicado pela segunda linha.</param>
        /// <param name="ring">O objecto responsável pelas operações sobre os coeficientes.</param>
        public void CombineLines(int i, int j, int a, int b, IRing <int> ring)
        {
            var lineslength = this.bitMatrix.Length;

            if (i < 0 || i >= lineslength)
            {
                throw new ArgumentNullException("i");
            }
            else if (j < 0 || j >= lineslength)
            {
                throw new ArgumentNullException("j");
            }
            else if (ring == null)
            {
                throw new ArgumentNullException("ring");
            }
            else
            {
                var replacementLine = this.bitMatrix[i];
                if (ring.IsAdditiveUnity(a))
                {
                    if (ring.IsAdditiveUnity(b))
                    {
                        var replacementLineLenght = replacementLine.Count;
                        for (int k = 0; k < replacementLineLenght; ++k)
                        {
                            replacementLine[k] = a;
                        }
                    }
                    else if (ring.IsMultiplicativeUnity(b))
                    {
                        var combinationLine       = this.bitMatrix[j];
                        var replacementLineLenght = replacementLine.Count;
                        for (int k = 0; k < replacementLineLenght; ++k)
                        {
                            replacementLine[k] = combinationLine[k];
                        }
                    }
                    else
                    {
                        var combinationLine       = this.bitMatrix[j];
                        var replacementLineLenght = replacementLine.Count;
                        for (int k = 0; k < replacementLineLenght; ++k)
                        {
                            var value = combinationLine[k];
                            if (ring.IsAdditiveUnity(value))
                            {
                                replacementLine[k] = value;
                            }
                            else if (ring.IsMultiplicativeUnity(value))
                            {
                                replacementLine[k] = b;
                            }
                            else
                            {
                                replacementLine[k] = ring.Multiply(b, value);
                            }
                        }
                    }
                }
                else
                {
                    if (ring.IsAdditiveUnity(b))
                    {
                        if (!ring.IsMultiplicativeUnity(a))
                        {
                            var replacementLineLenght = replacementLine.Count;
                            for (int k = 0; k < replacementLineLenght; ++k)
                            {
                                var value = replacementLine[k];
                                replacementLine[k] = ring.Multiply(a, value);
                            }
                        }
                    }
                    else if (ring.IsMultiplicativeUnity(b))
                    {
                        var combinationLine       = this.bitMatrix[j];
                        var replacementLineLenght = replacementLine.Count;
                        if (ring.IsMultiplicativeUnity(a))
                        {
                            for (int k = 0; k < replacementLineLenght; ++k)
                            {
                                replacementLine[k] = ring.Add(replacementLine[k], combinationLine[k]);
                            }
                        }
                        else
                        {
                            for (int k = 0; k < replacementLineLenght; ++k)
                            {
                                var replacementValue = ring.Multiply(replacementLine[k], a);
                                replacementLine[k] = ring.Add(replacementValue, combinationLine[k]);
                            }
                        }
                    }
                    else
                    {
                        var combinationLine       = this.bitMatrix[j];
                        var replacementLineLenght = replacementLine.Count;
                        for (int k = 0; k < replacementLineLenght; ++k)
                        {
                            var replacementValue = ring.Multiply(replacementLine[k], a);
                            var combinationValue = ring.Multiply(combinationLine[k], b);
                            replacementLine[k] = ring.Add(replacementValue, combinationValue);
                        }
                    }
                }
            }
        }
예제 #6
0
        /// <summary>
        /// Substitui a linha especificada por uma combinação linear desta com uma outra. Por exemplo, li = a * li + b * lj, isto é,
        /// a linha i é substituída pela soma do produto de a pela linha i com o produto de b peloa linha j.
        /// </summary>
        /// <param name="i">A linha a ser substituída.</param>
        /// <param name="j">A linha a ser combinada.</param>
        /// <param name="a">O escalar a ser multiplicado pela primeira linha.</param>
        /// <param name="b">O escalar a ser multiplicado pela segunda linha.</param>
        /// <param name="ring">O objecto responsável pelas operações sobre os coeficientes.</param>
        public void CombineLines(long i, long j, ObjectType a, ObjectType b, IRing <ObjectType> ring)
        {
            var lineslength = this.elements.LongLength;

            if (i < 0 || i >= lineslength)
            {
                throw new ArgumentNullException("i");
            }
            else if (j < 0 || j >= lineslength)
            {
                throw new ArgumentNullException("j");
            }
            else if (a == null)
            {
                throw new ArgumentNullException("a");
            }
            else if (b == null)
            {
                throw new ArgumentNullException("b");
            }
            else if (ring == null)
            {
                throw new ArgumentNullException("ring");
            }
            else
            {
                var replacementLine = this.elements[i];
                if (ring.IsAdditiveUnity(a))
                {
                    if (ring.IsAdditiveUnity(b))
                    {
                        var replacementLineLenght = replacementLine.LongLength;
                        for (var k = 0L; k < replacementLineLenght; ++k)
                        {
                            replacementLine[k] = a;
                        }
                    }
                    else if (ring.IsMultiplicativeUnity(b))
                    {
                        var combinationLine       = this.elements[j];
                        var replacementLineLenght = replacementLine.LongLength;
                        for (int k = 0; k < replacementLineLenght; ++k)
                        {
                            replacementLine[k] = combinationLine[k];
                        }
                    }
                    else
                    {
                        var combinationLine       = this.elements[j];
                        var replacementLineLenght = replacementLine.LongLength;
                        for (var k = 0L; k < replacementLineLenght; ++k)
                        {
                            var value = combinationLine[k];
                            if (ring.IsAdditiveUnity(value))
                            {
                                replacementLine[k] = value;
                            }
                            else if (ring.IsMultiplicativeUnity(value))
                            {
                                replacementLine[k] = b;
                            }
                            else
                            {
                                replacementLine[k] = ring.Multiply(b, value);
                            }
                        }
                    }
                }
                else
                {
                    if (ring.IsAdditiveUnity(b))
                    {
                        if (!ring.IsMultiplicativeUnity(a))
                        {
                            var replacementLineLenght = replacementLine.LongLength;
                            for (var k = 0L; k < replacementLineLenght; ++k)
                            {
                                var value = replacementLine[k];
                                replacementLine[k] = ring.Multiply(a, value);
                            }
                        }
                    }
                    else if (ring.IsMultiplicativeUnity(b))
                    {
                        var combinationLine       = this.elements[j];
                        var replacementLineLenght = replacementLine.LongLength;
                        if (ring.IsMultiplicativeUnity(a))
                        {
                            for (var k = 0L; k < replacementLineLenght; ++k)
                            {
                                replacementLine[k] = ring.Add(replacementLine[k], combinationLine[k]);
                            }
                        }
                        else
                        {
                            for (var k = 0L; k < replacementLineLenght; ++k)
                            {
                                var replacementValue = ring.Multiply(replacementLine[k], a);
                                replacementLine[k] = ring.Add(replacementValue, combinationLine[k]);
                            }
                        }
                    }
                    else
                    {
                        var combinationLine       = this.elements[j];
                        var replacementLineLenght = replacementLine.LongLength;
                        for (var k = 0L; k < replacementLineLenght; ++k)
                        {
                            var replacementValue = ring.Multiply(replacementLine[k], a);
                            var combinationValue = ring.Multiply(combinationLine[k], b);
                            replacementLine[k] = ring.Add(replacementValue, combinationValue);
                        }
                    }
                }
            }
        }