Exemplo n.º 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));
      */
 }
Exemplo n.º 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));
        }
Exemplo n.º 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));
        }
Exemplo n.º 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
                       ));
        }
Exemplo n.º 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
                       ));
        }
Exemplo n.º 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));
 }
Exemplo n.º 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
                );
        }
Exemplo n.º 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);
        }
Exemplo n.º 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));
 }
Exemplo n.º 10
0
 public static Term EuclidianDistance(TVec one, TVec two)
 {
     return(Power(EuclidianDistanceSqr(one, two), 0.5));
 }
Exemplo n.º 11
0
 public static Term EuclidianDistanceSqr(TVec one, TVec two)
 {
     return((one - two).NormSquared);
 }
Exemplo n.º 12
0
 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));
 }
Exemplo n.º 13
0
 public static Term Gaussian(TVec args, TVec mean, double variance)
 {
     return(Exp(((args - mean).NormSquared) * (-0.5 / variance)));
 }
Exemplo n.º 14
0
 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)));
 }