Exemple #1
0
 /// <summary>
 /// Constructs a new instance of the <see cref="TVec"/> class using another vector's components.
 /// </summary>
 /// <param name="first">A vector containing the first vector components to use.</param>
 /// <param name="rest">More vector components to add in addition to the components in <paramref name="first"/></param>
 public TVec(TVec first, params Term[] rest)
     : this(first.terms.Concat(rest ?? System.Linq.Enumerable.Empty <Term>()))
 {
     /*Contract.Requires(first != null);
      * Contract.Requires(Contract.ForAll(rest, term => term != null));
      */
 }
Exemple #2
0
        /// <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));
        }
Exemple #3
0
        /// <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));
        }
Exemple #4
0
        /// <summary>
        /// Constructs a 3D cross-product vector given two 3D vectors.
        /// </summary>
        /// <param name="left">The left cross-product term</param>
        /// <param name="right">The right cross product term</param>
        /// <returns>A vector representing the cross product of <paramref name="left"/> and <paramref name="right"/></returns>
        public static TVec CrossProduct(TVec left, TVec right)
        {
            Guard.NotNull(left, nameof(left));
            Guard.NotNull(right, nameof(right));
            Guard.MustHold(left.Dimension == 3 && right.Dimension == 3,
                           "vectors must be three dimensional");

            return(new TVec(
                       left.Y * right.Z - left.Z * right.Y,
                       left.Z * right.X - left.X * right.Z,
                       left.X * right.Y - left.Y * right.X
                       ));
        }
Exemple #5
0
        /// <summary>
        /// Constructs a 3D cross-product vector given two 3D vectors.
        /// </summary>
        /// <param name="left">The left cross-product term</param>
        /// <param name="right">The right cross product term</param>
        /// <returns>A vector representing the cross product of <paramref name="left"/> and <paramref name="right"/></returns>
        public static TVec CrossProduct(TVec left, TVec right)
        {
            Contract.Requires(left != null);
            Contract.Requires(right != null);
            Contract.Requires(left.Dimension == 3);
            Contract.Requires(right.Dimension == 3);
            Contract.Ensures(Contract.Result <TVec>().Dimension == 3);

            return(new TVec(
                       left.Y * right.Z - left.Z * right.Y,
                       left.Z * right.X - left.X * right.Z,
                       left.X * right.Y - left.Y * right.X
                       ));
        }
Exemple #6
0
 /// <summary>
 /// Constructs a new instance of the <see cref="TVec"/> class using another vector's components.
 /// </summary>
 /// <param name="first">A vector containing the first vector components to use.</param>
 /// <param name="rest">More vector components to add in addition to the components in <paramref name="first"/></param>
 public TVec(TVec first, params Term[] rest)
     : this(first.terms.Concat(rest ?? System.Linq.Enumerable.Empty<Term>()))
 {
     Contract.Requires(first != null);
     Contract.Requires(Contract.ForAll(rest, term => term != null));
 }
Exemple #7
0
        /// <summary>
        /// Constructs a 3D cross-product vector given two 3D vectors.
        /// </summary>
        /// <param name="left">The left cross-product term</param>
        /// <param name="right">The right cross product term</param>
        /// <returns>A vector representing the cross product of <paramref name="left"/> and <paramref name="right"/></returns>
        public static TVec CrossProduct(TVec left, TVec right)
        {
            Contract.Requires(left != null);
            Contract.Requires(right != null);
            Contract.Requires(left.Dimension == 3);
            Contract.Requires(right.Dimension == 3);
            Contract.Ensures(Contract.Result<TVec>().Dimension == 3);

            return new TVec(
                left.Y * right.Z - left.Z * right.Y,
                left.Z * right.X - left.X * right.Z,
                left.X * right.Y - left.Y * right.X
                );
        }
Exemple #8
0
        /// <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);
        }
Exemple #9
0
 /// <summary>
 /// Constructs a new instance of the <see cref="TVec"/> class using another vector's components.
 /// </summary>
 /// <param name="first">A vector containing the first vector components to use.</param>
 /// <param name="rest">More vector components to add in addition to the components in <paramref name="first"/></param>
 public TVec(TVec first, params Term[] rest)
     : this(first.terms.Concat(rest ?? Enumerable.Empty <Term>()))
 {
     Guard.ItemsNotNull(rest, nameof(rest));
 }
 public static Term EuclidianDistance(TVec one, TVec two)
 {
     return(Power(EuclidianDistanceSqr(one, two), 0.5));
 }
 public static Term EuclidianDistanceSqr(TVec one, TVec two)
 {
     return((one - two).NormSquared);
 }
 public static Term BoundedRectangle(TVec arg, TVec leftLower, TVec rightUpper, double steepness)
 {
     return(BoundedValue(arg.X, leftLower.X, rightUpper.X, steepness) & BoundedValue(arg.Y, leftLower.Y, rightUpper.Y, steepness));
 }
 public static Term Gaussian(TVec args, TVec mean, double variance)
 {
     return(Exp(((args - mean).NormSquared) * (-0.5 / variance)));
 }
 public static Term NormalDistribution(TVec args, TVec mean, double variance)
 {
     return(Exp(((args - mean).NormSquared) * (-0.5 / variance)) * (1 / Math.Sqrt(2.0 * Math.PI * variance)));
 }