/// <summary> /// Initializes a new instance of the <see cref="DenseVector"/> class by /// copying the values from another. /// </summary> /// <param name="other"> /// The matrix to create the new matrix from. /// </param> public DenseVector(Vector other) : this(other.Count) { var vector = other as DenseVector; if (vector == null) { // using enumerators since they will be more efficient for copying sparse matrices // foreach (var item in other.GetIndexedEnumerator()) // { // Data[item.Key] = item.Value; // } Parallel.For(0, Count, index => this[index] = other[index]); } else { Buffer.BlockCopy(vector.Data, 0, Data, 0, Data.Length * Constants.SizeOfDouble); } }
/// <summary> /// Subtracts another vector to this vector and stores the result into the result vector. /// </summary> /// <param name="other">The vector to subtract from this one.</param> /// <param name="result">The vector to store the result of the subtraction.</param> /// <exception cref="ArgumentNullException">If the other vector is <see langword="null"/>.</exception> /// <exception cref="ArgumentNullException">If the result vector is <see langword="null"/>.</exception> /// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception> /// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception> public override void Subtract(Vector other, Vector result) { if (result == null) { throw new ArgumentNullException("result"); } if (Count != other.Count) { throw new ArgumentException("other", Resources.ArgumentVectorsSameLength); } if (Count != result.Count) { throw new ArgumentException("result", Resources.ArgumentVectorsSameLength); } if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) { var tmp = result.CreateVector(result.Count); Subtract(other, tmp); tmp.CopyTo(result); } else { CopyTo(result); result.Subtract(other); } }
/// <summary> /// Subtracts another vector from this vector. /// </summary> /// <param name="other">The vector to subtract from this one.</param> /// <exception cref="ArgumentNullException">If the other vector is <see langword="null"/>.</exception> /// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception> public override void Subtract(Vector other) { if (other == null) { throw new ArgumentNullException("other"); } if (Count != other.Count) { throw new ArgumentException("other", Resources.ArgumentVectorsSameLength); } var denseVector = other as DenseVector; if (denseVector == null) { base.Subtract(other); } else { Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -1.0, denseVector.Data); } }
/// <summary> /// Subtracts a scalar from each element of the vector and stores the result in the result vector. /// </summary> /// <param name="scalar">The scalar to subtract.</param> /// <param name="result">The vector to store the result of the subtraction.</param> /// <exception cref="ArgumentNullException">If the result vector is <see langword="null"/>.</exception> /// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception> public override void Subtract(double scalar, Vector result) { if (result == null) { throw new ArgumentNullException("result"); } if (Count != result.Count) { throw new ArgumentException("result", Resources.ArgumentVectorsSameLength); } CopyTo(result); result.Subtract(scalar); }
/// <summary> /// Copies the values of this vector into the target vector. /// </summary> /// <param name="target"> /// The vector to copy elements into. /// </param> /// <exception cref="ArgumentNullException"> /// If <paramref name="target"/> is <see langword="null"/>. /// </exception> /// <exception cref="ArgumentException"> /// If <paramref name="target"/> is not the same size as this vector. /// </exception> public override void CopyTo(Vector target) { if (target == null) { throw new ArgumentNullException("target"); } if (Count != target.Count) { throw new ArgumentException("target", Resources.ArgumentVectorsSameLength); } var otherVector = target as DenseVector; if (otherVector == null) { for (var index = 0; index < Data.Length; index++) { target[index] = Data[index]; } } else { Buffer.BlockCopy(Data, 0, otherVector.Data, 0, Data.Length * Constants.SizeOfDouble); } }
/// <summary>Конструктор из другого вектора</summary> public Vector(Vector other) : base(other) { }