示例#1
0
        public bool Equals(VectorD other)
        {
            if (object.ReferenceEquals(this, other))
            {
                return(true);
            }
            if (object.ReferenceEquals(null, other))
            {
                return(false);
            }

            var otherComponents = other._components;

            if (_components.Length != otherComponents.Length)
            {
                return(false);
            }

#if HAS_CODECONTRACTS
            Assume(_components.Length == otherComponents.Length);
#endif

            for (int dimension = 0; dimension < _components.Length; dimension++)
            {
                if (!_components[dimension].Equals(otherComponents[dimension]))
                {
                    return(false);
                }
            }

            return(true);
        }
示例#2
0
        public double GetDistanceSquared(VectorD other)
        {
            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }

            var otherComponenets = other._components;

            if (otherComponenets.Length != _components.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(other));
            }

            if (_components.Length == 0)
            {
                return(0.0);
            }

            var sum = MathEx.Square(_components[0] - otherComponenets[0]);

            for (var i = 1; i < _components.Length; i++)
            {
                sum += MathEx.Square(_components[i] - otherComponenets[i]);
            }

            return(sum);
        }
示例#3
0
        public double GetDot(VectorD right)
        {
            if (right == null)
            {
                throw new ArgumentNullException(nameof(right));
            }

            var rightComponents = right._components;

            if (_components.Length != rightComponents.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(right));
            }

            if (_components.Length == 0)
            {
                return(0.0);
            }

            var sum = _components[0] * rightComponents[0];

            for (int i = 1; i < _components.Length; i++)
            {
                sum += _components[i] * rightComponents[i];
            }

            return(sum);
        }
示例#4
0
        public VectorD(VectorD source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            _components = Clone(source._components);
        }
示例#5
0
        public VectorD GetScaled(double scalar)
        {
            var scaled           = new VectorD(_components.Length);
            var scaledComponents = scaled._components;

            for (int i = 0; i < scaledComponents.Length; i++)
            {
                scaledComponents[i] = _components[i] * scalar;
            }

            return(scaled);
        }
示例#6
0
        public VectorD GetProjected(VectorD other)
        {
            if (other.Dimensions != Dimensions)
            {
                throw new ArgumentOutOfRangeException(nameof(other));
            }

            var scalarDenominator = GetMagnitudeSquared();

            return(scalarDenominator == 0.0
                ? other
                : GetScaled(GetDot(other) / scalarDenominator));
        }
示例#7
0
        public VectorD GetNegative()
        {
            var negated = new VectorD(_components.Length);

            var negatedComponents = negated._components;

            for (var i = 0; i < _components.Length; i++)
            {
                negatedComponents[i] = -_components[i];
            }

            return(negated);
        }
示例#8
0
        public VectorD GetQuotient(double divisor)
        {
            var quotient = new VectorD(_components.Length);

            var quotientComponents = quotient._components;

            for (int i = 0; i < _components.Length; i++)
            {
                quotientComponents[i] = _components[i] / divisor;
            }

            return(quotient);
        }
示例#9
0
        public double GetAngleBetween(VectorD other)
        {
            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }
            if (other._components.Length != _components.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(other));
            }

            return(Math.Acos(
                       GetDot(other)
                       / Math.Sqrt(GetMagnitudeSquared() * other.GetMagnitudeSquared())));
        }
示例#10
0
        public static VectorD CreateUnit(int size, int dimension)
        {
            if (size <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(size));
            }
            if (dimension >= size)
            {
                throw new ArgumentOutOfRangeException(nameof(dimension));
            }

            var result = new VectorD(size);

            result.Set(dimension, 1.0);
            return(result);
        }
示例#11
0
        public void Subtract(VectorD right)
        {
            if (right == null)
            {
                throw new ArgumentNullException(nameof(right));
            }
            if (_components.Length != right._components.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(right));
            }

            var rightComponents = right._components;

            for (int i = 0; i < _components.Length; i++)
            {
                _components[i] -= rightComponents[i];
            }
        }
示例#12
0
        public VectorD GetDiffernce(VectorD right)
        {
            if (right == null)
            {
                throw new ArgumentNullException(nameof(right));
            }
            if (_components.Length != right._components.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(right));
            }

            var difference      = new VectorD(_components.Length);
            var diffComponents  = difference._components;
            var rightComponents = right._components;

            for (int i = 0; i < _components.Length; i++)
            {
                diffComponents[i] = _components[i] - rightComponents[i];
            }

            return(difference);
        }
示例#13
0
        public VectorD GetSum(VectorD right)
        {
            if (right == null)
            {
                throw new ArgumentNullException(nameof(right));
            }
            if (_components.Length != right._components.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(right));
            }

            var sum             = new VectorD(_components.Length);
            var sumComponents   = sum._components;
            var rightComponents = right._components;

            for (int i = 0; i < _components.Length; i++)
            {
                sumComponents[i] = _components[i] + rightComponents[i];
            }

            return(sum);
        }
示例#14
0
 public double GetDistance(VectorD other)
 {
     return(Math.Sqrt(GetDistanceSquared(other)));
 }