/// <summary> /// Constructs a term representing the inner product of two vectors. /// </summary> /// <param name="left">The first vector of the inner product</param> /// <param name="right">The second vector of the inner product</param> /// <returns>A term representing the inner product of <paramref name="left"/> and <paramref name="right"/>.</returns> public static Term InnerProduct(TVec left, TVec right) { Guard.NotNull(left, nameof(left)); Guard.NotNull(right, nameof(right)); Guard.MustHold(left.Dimension == right.Dimension, "left and right must be of the same dimension"); var products = from i in Enumerable.Range(0, left.Dimension) select left.terms[i] * right.terms[i]; return(TermBuilder.Sum(products)); }
/// <summary> /// Constructs a term representing the inner product of two vectors. /// </summary> /// <param name="left">The first vector of the inner product</param> /// <param name="right">The second vector of the inner product</param> /// <returns>A term representing the inner product of <paramref name="left"/> and <paramref name="right"/>.</returns> public static Term InnerProduct(TVec left, TVec right) { Contract.Requires(left != null); Contract.Requires(right != null); Contract.Requires(left.Dimension == right.Dimension); Contract.Ensures(Contract.Result <Term>() != null); var products = from i in System.Linq.Enumerable.Range(0, left.Dimension) select left.terms[i] * right.terms[i]; return(TermBuilder.Sum(products)); }
/// <summary> /// Constructs a sum of the two given terms. /// </summary> /// <param name="left">First term in the sum</param> /// <param name="right">Second term in the sum</param> /// <returns>A term representing the sum of <paramref name="left"/> and <paramref name="right"/>.</returns> public static Term operator+(Term left, Term right) { if (left is Zero && right is Zero) { return(new Zero()); } else if (left is Zero) { return(right); } else if (right is Zero) { return(left); } else { return(TermBuilder.Sum(left, right)); } }
/// <summary> /// Constructs a fraction term of the two given terms. /// </summary> /// <param name="numerator">The numerator of the fraction. That is, the "top" part.</param> /// <param name="denominator">The denominator of the fraction. That is, the "bottom" part.</param> /// <returns>A term representing the fraction <paramref name="numerator"/> over <paramref name="denominator"/>.</returns> public static Term operator/(Term numerator, Term denominator) { return(TermBuilder.Product(numerator, TermBuilder.Power(denominator, -1))); }
/// <summary> /// Constructs a product term of the two given terms. /// </summary> /// <param name="left">The first term in the product</param> /// <param name="right">The second term in the product</param> /// <returns>A term representing the product of <paramref name="left"/> and <paramref name="right"/>.</returns> public static Term operator*(Term left, Term right) { return(TermBuilder.Product(left, right)); }
/// <summary> /// Constructs a sum of the two given terms. /// </summary> /// <param name="left">First term in the sum</param> /// <param name="right">Second term in the sum</param> /// <returns>A term representing the sum of <paramref name="left"/> and <paramref name="right"/>.</returns> public static Term operator+(Term left, Term right) { return(TermBuilder.Sum(left, right)); }