/// <summary> /// Instancia um novo objecto do tipo /// <see cref="DecompositionFactorizationAlgorithm{NumberType, DegreeType}"/>. /// </summary> /// <param name="productDecompAlg"> /// O algoritmo resposnável pela factorização de um número em dois factores. /// </param> /// <param name="unitaryDegree">O grau unitário.</param> /// <param name="degreeMonoid">O monóide responsável pelas operações sobre os graus.</param> /// <param name="numberRing">O anel responsável pelas operações sobre os números.</param> /// <exception cref="ArgumentNullException"> /// Se pelo menos um dos argumentos for nulo. /// </exception> public DecompositionFactorizationAlgorithm( IAlgorithm <NumberType, Tuple <NumberType, NumberType> > productDecompAlg, DegreeType unitaryDegree, IMonoid <DegreeType> degreeMonoid, IRing <NumberType> numberRing) { if (numberRing == null) { throw new ArgumentNullException("numberRing"); } else if (degreeMonoid == null) { throw new ArgumentNullException("degreeMonoid"); } else if (unitaryDegree == null) { throw new ArgumentNullException("uniartyDegree"); } else if (productDecompAlg == null) { throw new ArgumentNullException("productDecompAlg"); } else { this.numberRing = numberRing; this.degreeMonoid = degreeMonoid; this.unitaryDegree = unitaryDegree; this.productDecompAlg = productDecompAlg; } }
public void Int32Test() { IMonoid <int> addInt32 = 0.Monoid((a, b) => a + b); Assert.AreEqual(0, addInt32.Unit); Assert.AreEqual(1 + 2, addInt32.Binary(1, 2)); // Monoid law 1: Unit Binary m == m Assert.AreEqual(1, addInt32.Binary(addInt32.Unit, 1)); // Monoid law 2: m Binary Unit == m Assert.AreEqual(1, addInt32.Binary(1, addInt32.Unit)); // Monoid law 3: (m1 Binary m2) Binary m3 == m1 Binary (m2 Binary m3) Assert.AreEqual(addInt32.Binary(addInt32.Binary(1, 2), 3), addInt32.Binary(1, addInt32.Binary(2, 3))); IMonoid <int> multiplyInt32 = 1.Monoid((a, b) => a * b); Assert.AreEqual(1, multiplyInt32.Unit); Assert.AreEqual(1 * 2, multiplyInt32.Binary(1, 2)); // Monoid law 1: Unit Binary m == m Assert.AreEqual(2, multiplyInt32.Binary(multiplyInt32.Unit, 2)); // Monoid law 2: m Binary Unit == m Assert.AreEqual(2, multiplyInt32.Binary(2, multiplyInt32.Unit)); // Monoid law 3: (m1 Binary m2) Binary m3 == m1 Binary (m2 Binary m3) Assert.AreEqual( multiplyInt32.Binary(multiplyInt32.Binary(1, 2), 3), multiplyInt32.Binary(1, multiplyInt32.Binary(2, 3))); }
public override IMonoid <TA> MAppend(IMonoid <TA> a1, IMonoid <TA> a2) { var listMonoid1 = (ListMonoid <TA>)a1; var listMonoid2 = (ListMonoid <TA>)a2; return(new ListMonoid <TA>(listMonoid1.List.Concat(listMonoid2.List))); }
/// <summary> /// Addition of leftFunction and rightFunction. /// The component addition is defined in calculator on T. /// </summary> /// <param name="monoid">The calculator.</param> /// <param name="leftFunction">Left function.</param> /// <param name="rightFunction">Right function.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> /// <returns>The Addition of the left function with the right function.</returns> public static Func <T, T> Addition <T> ( this IMonoid <T> monoid, Func <T, T> leftFunction, Func <T, T> rightFunction) { return((x) => monoid.Addition(leftFunction(x), rightFunction(x))); }
public Sum(A value, IMonoid <A> monoid) { this.value = value; this.IsNonempty = true; this.combiner = monoid.combine; this.equator = monoid.eq; }
/// <summary> /// Creates a new writer from a result. /// </summary> /// <param name="result">The result to produce.</param> public Writer(TValue result) { var neutral = Algebra.Monoid.NeutralUnsafe <TOutput, TMonoid>(); State = neutral; Result = result; Monoid = new Monoid <TOutput>(neutral, Algebra.Monoid.OpUnsafe <TOutput, TMonoid>()); }
/// <summary> /// The m append. /// </summary> /// <param name="_"> /// The _. /// </param> /// <returns> /// The <see cref="IMonoid"/>. /// </returns> public IMonoid <IRuleDataConstraint> MAppend(IMonoid <IRuleDataConstraint> _) { if (_ == null) { return(this); } var constraints = this.Constraints.Concat(_.Case.Constraints).ToArray(); return(new RuleDataConstraint(this.Rule, this.RuleParameterId, constraints)); }
/// <summary> /// Verifica se o número complexo actual é o número complexo nulo. /// </summary> /// <param name="monoid">O monóide responsável pelas operações.</param> /// <returns>Verdadeiro caso o número seja zero e falso caso contrário.</returns> /// <exception cref="ArgumentNullException">Se o monóide for nulo.</exception> public bool IsZero(IMonoid <ObjectType> monoid) { if (monoid == null) { throw new ArgumentNullException("monoid"); } else { return(monoid.IsAdditiveUnity(this.realPart) && monoid.IsAdditiveUnity(this.imaginaryPart)); } }
public void Addition_OfTwoAdditionFunctions_PointsAreEqual <T> ( T value1, T value2, T expectedResult, IMonoid <T> monoid) { Func <T, T, T> func1 = monoid.Addition; Func <T, T, T> func2 = monoid.Addition; var calc = monoid.Addition(func1, func2); Assert.NotNull(calc); Assert.AreEqual(expectedResult, calc(value1, value2)); }
public void ZeroFunction_PointsEqualsMock <T> ( T value, IMonoid <T> monoid) { Func <T, T> functionMock = Substitute.For <Func <T, T> >(); functionMock(value).Returns(value); var testFunction = monoid.Addition( monoid.ZeroFunction(), functionMock); Assert.AreEqual(functionMock(value), testFunction(value)); }
/// <summary> /// Instancia um novo objecto do tipo <see cref="ZeroVector{CoeffType}"/>. /// </summary> /// <param name="length">O tamanho do vector.</param> /// <param name="monoid">O monóide responsável pelas operações sobre os coeficientes.</param> /// <exception cref="ArgumentNullException">Se o monóide for nulo.</exception> /// <exception cref="ArgumentException">Se o comprimento do vector for negativo.</exception> public ZeroVector(int length, IMonoid <CoeffType> monoid) { if (monoid == null) { throw new ArgumentNullException("monoid"); } else if (length < 0) { throw new ArgumentException("The vector length must be non-negative."); } else { this.monoid = monoid; this.length = length; } }
public void ClockTest() { // Stolen from: http://channel9.msdn.com/Shows/Going+Deep/Brian-Beckman-Dont-fear-the-Monads IMonoid <int> clock = 12.Monoid((a, b) => (a + b) % 12); Assert.AreEqual(12, clock.Unit); Assert.AreEqual((7 + 10) % 12, clock.Binary(7, 10)); // Monoid law 1: Unit Binary m == m Assert.AreEqual(111 % 12, clock.Binary(clock.Unit, 111)); // Monoid law 2: m Binary Unit == m Assert.AreEqual(111 % 12, clock.Binary(111, clock.Unit)); // Monoid law 3: (m1 Binary m2) Binary m3 == m1 Binary (m2 Binary m3) Assert.AreEqual(clock.Binary(clock.Binary(11, 22), 33), clock.Binary(11, clock.Binary(22, 33))); }
public static T Join <T>(this IMonoid <T> monoid, IArray <T> array) { switch (array.Size) { case 0: return(monoid.Null); case 1: return(array[0]); default: var midpoint = array.Size / 2; var left = array.Range(0, midpoint); var right = array.Range(midpoint, array.Size); return(monoid.Join(monoid.Join(left), monoid.Join(right))); } }
public void StringTest() { IMonoid <string> concatString = string.Empty.Monoid(string.Concat); Assert.AreEqual(string.Empty, concatString.Unit); Assert.AreEqual("ab", concatString.Binary("a", "b")); // Monoid law 1: Unit Binary m == m Assert.AreEqual("ab", concatString.Binary(concatString.Unit, "ab")); // Monoid law 2: m Binary Unit == m Assert.AreEqual("ab", concatString.Binary("ab", concatString.Unit)); // Monoid law 3: (m1 Binary m2) Binary m3 == m1 Binary (m2 Binary m3) Assert.AreEqual( concatString.Binary(concatString.Binary("a", "b"), "c"), concatString.Binary("a", concatString.Binary("b", "c"))); }
// O(N) public BinaryIndexedTree(IEnumerable <T> collection, IMonoid <T> monoid) { N = collection.Count(); Monoid = monoid; tree = new T[N + 1]; for (var i = 0; i < N; i++) { tree[i + 1] = collection.ElementAt(i); } for (var i = 1; i < N; i++) { var j = i + (i & -i); if (j < N) { tree[j] = monoid.Mappend(tree[j], tree[i]); } } }
/// <summary> /// Determina se o vector actual é zero relativamente a um monóide. /// </summary> /// <param name="monoid">O monóide.</param> /// <returns>Verdadeiro caso o vector seja nulo e falso caso contrário.</returns> /// <exception cref="ArgumentNullException">Se o monóide fornecido for nulo.</exception> public bool IsNull(IMonoid <CoeffType> monoid) { if (monoid == null) { throw new ArgumentNullException("monoid"); } else { for (int i = 0; i < this.vectorEntries.Length; ++i) { if (!monoid.IsAdditiveUnity(this.vectorEntries[i])) { return(false); } } return(true); } }
/// <summary> /// Determina se o vector actual é zero relativamente a um monóide. /// </summary> /// <param name="monoid">O monóide.</param> /// <returns>Verdadeiro caso o vector seja nulo e falso caso contrário.</returns> /// <exception cref="ArgumentNullException">Se o monóide fornecido for nulo.</exception> public bool IsNull(IMonoid <CoeffType> monoid) { if (monoid == null) { throw new ArgumentNullException("monoid"); } else { for (int i = 0; i < this.matrix.GetLength(0); ++i) { if (!monoid.IsAdditiveUnity(this.matrix[i, this.columnNumber])) { return(false); } } return(true); } }
public static IMonoid <T> Nullable <T>(this IMonoid <T> monoid) where T : class { return(new Monoid <T>( @null: null, joinF: (left, right) => { if (left == null) { return right; } if (right == null) { return left; } return monoid.Join(left, right); })); }
public void EnumerableTest() { IMonoid <IEnumerable <int> > concatEnumerable = Enumerable.Empty <int>().Monoid((a, b) => a.Concat(b)); Assert.IsFalse(concatEnumerable.Unit.Any()); int[] x = new[] { 0, 1, 2 }; int[] y = new[] { 3, 4, 5 }; EnumerableAssert.AreSequentialEqual(concatEnumerable.Binary(x, y), x.Concat(y)); // Monoid law 1: Unit Binary m == m EnumerableAssert.AreSequentialEqual(concatEnumerable.Binary(concatEnumerable.Unit, x), x); // Monoid law 2: m Binary Unit == m EnumerableAssert.AreSequentialEqual(concatEnumerable.Binary(x, concatEnumerable.Unit), x); // Monoid law 3: (m1 Binary m2) Binary m3 == m1 Binary (m2 Binary m3) EnumerableAssert.AreSequentialEqual( concatEnumerable.Binary(concatEnumerable.Binary(x, y), x), concatEnumerable.Binary(x, concatEnumerable.Binary(y, x))); }
/// <summary> /// Instancia um novo objecto do tipo <see cref="ZeroMatrix{ElementType}"/>. /// </summary> /// <param name="lines">O número de linhas da matriz.</param> /// <param name="columns">O número de colunas da matriz.</param> /// <param name="monoid">O monóide responsável pelas operações sobre os coeficientes.</param> /// <exception cref="ArgumentNullException">Se o monóide for nulo.</exception> /// <exception cref="ArgumentException"> /// Se o número de linhas ou o número de colunas for negativo. /// </exception> public ZeroMatrix(int lines, int columns, IMonoid <ElementType> monoid) { if (monoid == null) { throw new ArgumentNullException("monoid"); } else if (lines < 0) { throw new ArgumentException("Parameter lines must be non-negative."); } else if (columns < 0) { throw new ArgumentException("Parameter columns must be non-negative."); } else { this.monoid = monoid; this.linesNumber = lines; this.columnsNumber = columns; } }
public void BooleanTest() { IMonoid <bool> orBoolean = false.Monoid((a, b) => a || b); Assert.IsFalse(orBoolean.Unit); Assert.AreEqual(true || false, orBoolean.Binary(true, false)); // Monoid law 1: Unit Binary m == m Assert.AreEqual(true, orBoolean.Binary(orBoolean.Unit, true)); Assert.AreEqual(false, orBoolean.Binary(orBoolean.Unit, false)); // Monoid law 2: m Binary Unit == m Assert.AreEqual(true, orBoolean.Binary(true, orBoolean.Unit)); Assert.AreEqual(false, orBoolean.Binary(false, orBoolean.Unit)); // Monoid law 3: (m1 Binary m2) Binary m3 == m1 Binary (m2 Binary m3) Assert.AreEqual( orBoolean.Binary(orBoolean.Binary(true, false), true), orBoolean.Binary(true, orBoolean.Binary(false, true))); IMonoid <bool> andBoolean = true.Monoid((a, b) => a && b); Assert.IsTrue(andBoolean.Unit); Assert.AreEqual(true && false, andBoolean.Binary(true, false)); // Monoid law 1: Unit Binary m == m Assert.AreEqual(true, andBoolean.Binary(andBoolean.Unit, true)); Assert.AreEqual(false, andBoolean.Binary(andBoolean.Unit, false)); // Monoid law 2: m Binary Unit == m Assert.AreEqual(true, andBoolean.Binary(true, andBoolean.Unit)); Assert.AreEqual(false, andBoolean.Binary(false, andBoolean.Unit)); // Monoid law 3: (m1 Binary m2) Binary m3 == m1 Binary (m2 Binary m3) Assert.AreEqual( andBoolean.Binary(andBoolean.Binary(true, false), true), andBoolean.Binary(true, andBoolean.Binary(false, true))); }
public static IMonoid <Nullable <TSource> > MonoidOfNullable <TSource> (this IMonoid <TSource> monoid) => new Monoid <Nullable <TSource> >( new Nullable <TSource>(), (a, b) => new Nullable <TSource>(() => { if (a.HasValue && b.HasValue) { return(Tuple.Create(true, monoid.Binary(a.Value, b.Value))); } if (a.HasValue) { return(Tuple.Create(true, a.Value)); } if (b.HasValue) { return(Tuple.Create(true, b.Value)); } return(Tuple.Create(false, default(TSource))); }));
public void NullableTest() { IMonoid <int> addInt32 = 0.Monoid((a, b) => a + b); IMonoid <Nullable <int> > addNullable = addInt32.MonoidOfNullable(); Assert.IsFalse(addNullable.Unit.HasValue); Assert.AreEqual(addInt32.Binary(1, 2), addNullable.Binary(1.Nullable(), 2.Nullable()).Value); Assert.AreEqual(1, addNullable.Binary(1.Nullable(), new Nullable <int>()).Value); Assert.AreEqual(2, addNullable.Binary(new Nullable <int>(), 2.Nullable()).Value); Assert.IsFalse(addNullable.Binary(new Nullable <int>(), new Nullable <int>()).HasValue); // Monoid law 1: Unit Binary m == m Assert.AreEqual(1, addNullable.Binary(addNullable.Unit, 1.Nullable()).Value); // Monoid law 2: m Binary Unit == m Assert.AreEqual(1, addNullable.Binary(1.Nullable(), addNullable.Unit).Value); // Monoid law 3: (m1 Binary m2) Binary m3 == m1 Binary (m2 Binary m3) Nullable <int> left = addNullable.Binary(addNullable.Binary(1.Nullable(), 2.Nullable()), 3.Nullable()); Nullable <int> right = addNullable.Binary(1.Nullable(), addNullable.Binary(2.Nullable(), 3.Nullable())); Assert.AreEqual(left.Value, right.Value); }
/// <summary> /// Averigua se se trata de um vector nulo. /// </summary> /// <param name="monoid">O monóide responsável pela identificação do zero.</param> /// <returns>Veradeiro caso o vector seja nulo e falso caso contrário.</returns> /// <exception cref="ArgumentNullException">Se o argumento for nulo.</exception> public bool IsNull(IMonoid <CoeffType> monoid) { if (monoid == null) { throw new ArgumentNullException("monoid"); } else { if (monoid.IsAdditiveUnity(this.defaultValue)) { if (this.vectorEntries.Count > 0) { return(false); } } else { if (this.vectorEntries.Count < this.length) { return(false); } else { foreach (var itemKvp in this.vectorEntries) { if (!monoid.IsAdditiveUnity(itemKvp.Value)) { return(false); } } } } return(true); } }
/// <inheritdoc /> public T1 FoldMap <T1>(IMonoid <T1> monoid, Func <T, T1> f) => f(Value);
public TreapNode(T value, IMonoid <T> monoid) : base(value) { aggregate = value; this.monoid = monoid; }
/// <summary> /// The m append. /// </summary> /// <param name="_"> /// The _. /// </param> /// <returns> /// The <see cref="IMonoid"/>. /// </returns> public IMonoid <IRuleDataConstraint> MAppend(IMonoid <IRuleDataConstraint> _) { return(_); }
public static A Extract <A>(this Maybe <A> maybe, IMonoid <A> m) { return(maybe.ValueOr(() => m.Zero)); }
public static A Extract <A>(this IEnumerable <A> enumerable, IMonoid <A> m) { return(enumerable.MaybeFirst().ValueOr(() => m.Zero)); }
public static A Extract <X, A>(this Validation <X, A> validation, IMonoid <A> m) { return(validation.SuccessOr(() => m.Zero)); }