public void Statistics_DicModeAlgTest() { var target = new DicModeAlgorithm <int>(); var source = new IntegerSequence(); for (var i = 0; i < 1000; ++i) { source.Clear(); source.Add(0, i); var innerActual = target.Run(source); Assert.AreEqual(source.Count, innerActual.Count); innerActual.Sort(); for (var j = 0; j < source.Count; ++j) { Assert.AreEqual(source[j], innerActual[j]); } } var arraySource = new int[] { 1, 3, 2, 2, 1, 3, 3, 3, 3, 4, 2 }; var expected = 3; var actual = target.Run(arraySource); Assert.AreEqual(1, actual.Count); Assert.AreEqual(expected, actual[0]); }
public void Statistics_DicMedianAlgTest() { var target = new DicMedianAlgorithm <int>(); var source = new IntegerSequence(); var expected = 1; for (var i = 1; i < 1000; i += 2) { source.Clear(); source.Add(1, i); var actual = target.Run(source); Assert.AreEqual(expected, actual.Item1); Assert.AreEqual(expected, actual.Item2); ++expected; } expected = 1; for (var i = 2; i < 1000; i += 2) { source.Clear(); source.Add(1, i); var actual = target.Run(source); Assert.AreEqual(expected++, actual.Item1); Assert.AreEqual(expected, actual.Item2); } var arraySource = new int[] { 1, 3, 2, 2, 1, 3, 3, 3, 3, 4, 2 }; expected = 3; var outerActual = target.Run(arraySource); Assert.AreEqual(expected, outerActual.Item1); Assert.AreEqual(expected, outerActual.Item2); arraySource = new int[] { 1, 3, 2, 2, 1, 3, 3, 3, 4, 2 }; expected = 2; outerActual = target.Run(arraySource); Assert.AreEqual(expected++, outerActual.Item1); Assert.AreEqual(expected, outerActual.Item2); }
/// <summary> /// Determina o polinómio característico de uma matriz. /// </summary> /// <param name="data">A matriz.</param> /// <returns>O polinómio característico.</returns> public UnivariatePolynomialNormalForm <ElementType> Run(ISquareMathMatrix <ElementType> data) { if (data == null) { return(new UnivariatePolynomialNormalForm <ElementType>(this.variableName)); } else { var lines = data.GetLength(0); if (lines == 0) { return(new UnivariatePolynomialNormalForm <ElementType>(this.variableName)); } else if (lines == 1) { var entry = data[0, 0]; var result = new UnivariatePolynomialNormalForm <ElementType>( this.ring.MultiplicativeUnity, 1, this.variableName, this.ring); result = result.Add(this.ring.AdditiveInverse(entry), 0, this.ring); return(result); } else if (lines == 2) { var variablePol = new UnivariatePolynomialNormalForm <ElementType>( this.ring.MultiplicativeUnity, 1, this.variableName, this.ring); var firstDiagonalElement = variablePol.Add( this.ring.AdditiveInverse(data[0, 0]), this.ring); var secondDiagonalElement = variablePol.Add( this.ring.AdditiveInverse(data[1, 1]), this.ring); var result = firstDiagonalElement.Multiply(secondDiagonalElement, this.ring); result = result.Add( this.ring.AdditiveInverse(this.ring.Multiply(data[0, 1], data[1, 0])), this.ring); return(result); } else { var matrixFactory = new ArrayMathMatrixFactory <ElementType>(); var matrixMultiplicator = new MatrixMultiplicationOperation <ElementType>( matrixFactory, this.ring, this.ring); var subMatrixSequence = new IntegerSequence(); var singleValueSequence = new IntegerSequence(); IMatrix <ElementType> multiplicationMatrix = new ArrayMathMatrix <ElementType>( lines + 1, lines, this.ring.AdditiveUnity); subMatrixSequence.Add(1, lines - 1); singleValueSequence.Add(0); this.FillMultiplicationMatrix( data, data[0, 0], subMatrixSequence, singleValueSequence, matrixMultiplicator, multiplicationMatrix); var currentDimension = 1; while (currentDimension < lines - 1) { subMatrixSequence.Clear(); singleValueSequence.Clear(); subMatrixSequence.Add(currentDimension + 1, lines - 1); singleValueSequence.Add(currentDimension); var otherLines = lines - currentDimension; var otherMultiplicationMatrix = new ArrayMathMatrix <ElementType>( otherLines + 1, otherLines, this.ring.AdditiveUnity); this.FillMultiplicationMatrix( data, data[currentDimension, currentDimension], subMatrixSequence, singleValueSequence, matrixMultiplicator, otherMultiplicationMatrix); multiplicationMatrix = matrixMultiplicator.Multiply( multiplicationMatrix, otherMultiplicationMatrix); ++currentDimension; } var lastOtherMultiplicationMatrix = new ArrayMathMatrix <ElementType>( 2, 1, this.ring.AdditiveUnity); lastOtherMultiplicationMatrix[0, 0] = this.ring.MultiplicativeUnity; lastOtherMultiplicationMatrix[1, 0] = this.ring.AdditiveInverse(data[currentDimension, currentDimension]); multiplicationMatrix = matrixMultiplicator.Multiply( multiplicationMatrix, lastOtherMultiplicationMatrix); var result = new UnivariatePolynomialNormalForm <ElementType>( multiplicationMatrix[0, 0], lines, this.variableName, this.ring); for (int i = 1; i <= lines; ++i) { result = result.Add(multiplicationMatrix[i, 0], lines - i, this.ring); } return(result); } } }