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);
        }
        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);
        }
        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);
        }
        public VectorD(VectorD source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            _components = Clone(source._components);
        }
        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);
        }
        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));
        }
        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);
        }
        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);
        }
        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())));
        }
        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);
        }
        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];
            }
        }
        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);
        }
        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);
        }
 public double GetDistance(VectorD other)
 {
     return(Math.Sqrt(GetDistanceSquared(other)));
 }