Exemple #1
0
        public Term GetDeterminant()
        {
            Contract.Requires(RowsCount == ColsCount);

            if (RowsCount > 2) // General case
            {
                // get minor determinants (recursively)
                var minorDets = new Term[RowsCount];
                for (int i = 0; i < minorDets.Length; ++i)
                {
                    minorDets[i] = GetMinor(i, 0).GetDeterminant();
                }

                // negate every second determinant
                for (int i = 1; i < minorDets.Length; i += 2)
                {
                    minorDets[i] = -1 * minorDets[i];
                }

                return(TermBuilder.Sum(minorDets));
            }
            else if (RowsCount == 2)
            {
                return(rows[0][0] * rows[1][1] - rows[1][0] * rows[0][1]); // 2D determinant
            }
            else  // RowsCount == 1
            {
                return(rows[0][0]);  // 1D determinant - simply the term value
            }
        }
Exemple #2
0
            public SubstitutionResult Visit(Sum sum)
            {
                var summmandResults = sum.Terms.Select(x => x.Accept(this)).ToArray();

                var    nonConstants = new List <Term>();
                double sumValue     = 0;

                foreach (var summandResult in summmandResults)
                {
                    double value;
                    if (TryGetConstant(summandResult, out value))
                    {
                        sumValue += value;
                    }
                    else
                    {
                        nonConstants.Add(summandResult.Term);
                    }
                }

                if (nonConstants.Count == 0) // all are constants
                {
                    return(CreateResult(sumValue));
                }
                else
                {
                    var newSummands = nonConstants.Concat(Enumerable.Repeat(TermBuilder.Constant(sumValue), 1));
                    return(CreateResult(TermBuilder.Sum(newSummands)));
                }
            }
        /// <summary>
        /// Generates a term that gets smaller as the given 2D point fits a 3D points set projection.
        /// </summary>
        /// <param name="pointsSet">A representation for the 3D points set</param>
        /// <param name="point">The 2D point</param>
        /// <returns>The term that measures fitness of <paramref name="point"/> being on the 2D projection of the set specified by <paramref name="pointsSet"/></returns>
        public static Term Compute(CircleFeatureCurve pointsSet, Point point)
        {
            // here we explicitly assume that the view vector is (0, 0, 1) or (0, 0, -1)
            var x_ = point.X;
            var y_ = point.Y;

            var cx = pointsSet.Center.X;
            var cy = pointsSet.Center.Y;
            var cz = pointsSet.Center.Z;

            var nx = pointsSet.Normal.X;
            var ny = pointsSet.Normal.Y;
            var nz = pointsSet.Normal.Z;

            var r = pointsSet.Radius;

            var dx = cx - x_;
            var dy = cy + y_;

            var lhs = TermBuilder.Sum(
                TermBuilder.Power(dx * nz, 2),
                TermBuilder.Power(dy * nz, 2),
                TermBuilder.Power(dx * nx + dy * ny, 2));
            var rhs = TermBuilder.Power(r * nz, 2);

            return(TermBuilder.Power(lhs - rhs, 2));
        }
Exemple #4
0
 public void SumValidationContract()
 {
     Assert.Throws <ArgumentNullException>(() => TermBuilder.Sum(null));
     Assert.Throws <ArgumentNullException>(() => TermBuilder.Sum(x, null));
     Assert.Throws <ArgumentNullException>(() => TermBuilder.Sum(null, x));
     Assert.Throws <ArgumentNullException>(() => TermBuilder.Sum(x, y, null));
     Assert.Throws <ArgumentException>(() => TermBuilder.Sum(x, y, Vec(x, null)));
     Assert.Throws <ArgumentException>(() => TermBuilder.Sum(Vec(x, null)));
 }
Exemple #5
0
        public static Term DiffSquared(Term[] left, Term[] right)
        {
            Contract.Requires(left != null);
            Contract.Requires(right != null);
            Contract.Requires(left.Length == right.Length);

            var toSum = from i in System.Linq.Enumerable.Range(0, left.Length)
                        select TermBuilder.Power(left[i] - right[i], 2);

            return(TermBuilder.Sum(toSum));
        }
Exemple #6
0
        public static Term SoftMin(Term[] terms, double exponent = 6)
        {
            Contract.Requires(terms != null);
            Contract.Requires(terms.Length > 0);
            Contract.Requires(Contract.ForAll(terms, term => term != null));
            Contract.Requires(exponent > 1);
            Contract.Ensures(Contract.Result <Term>() != null);

            var powers = terms.Select(term => TermBuilder.Power(term, -exponent));

            return(TermBuilder.Power(TermBuilder.Sum(powers), -1 / exponent));
        }
Exemple #7
0
        private static ICompiledTerm ConstructTerm(Coefficient[][] coefficients, Variable[] variables)
        {
            var squareTerms =
                from i in Enumerable.Range(0, coefficients.Length)
                let termCoefficients = coefficients[i]
                                       let sumTerms = from j in Enumerable.Range(0, termCoefficients.Length)
                                                      select termCoefficients[j].Value * variables[termCoefficients[j].Index]
                                                      let sum = TermBuilder.Sum(sumTerms)
                                                                select TermBuilder.Power(sum, 2);

            var finalTerm = TermBuilder.Sum(squareTerms);
            var compiled  = finalTerm.Compile(variables);

            return(compiled);
        }
Exemple #8
0
        public static Term SafeSum(IEnumerable <Term> terms)
        {
            Contract.Requires(terms != null);
            Contract.Requires(Contract.ForAll(terms, term => term != null));
            Contract.Ensures(Contract.Result <Term>() != null);

            terms = terms.Where(term => !(term is Zero));

            if (!terms.Any())
            {
                return(0);
            }
            else if (!terms.Skip(1).Any())
            {
                return(terms.First());
            }
            else
            {
                return(TermBuilder.Sum(terms));
            }
        }