Class Vector
A 1-D array used in linear algebra: QuantLib's Array, renamed. It implements the concept of a vector as used
in linear algebra, and is not meant to be used as a container.
[WorkingType]
public sealed class Vector : IEquatable<Vector>, IEnumerable<double>, IEnumerable
- Inheritance
-
Vector
- Implements
- Inherited Members
Remarks
A working type: mutable, owned by one computation and not thread-safe, as an array is. The indexer sets elements,
and the compound assignments (v += w, v *= x, ...) change the vector in place, so every reference to
it sees the change; the binary and unary operators return new vectors. Clone() is the deep copy, where
QuantLib copies an Array by value. v += x and the other operations with a scalar apply it to every
element; v *= w and the other operations with two vectors work element by element, and need vectors of the
same size.
A collection expression builds one, as QuantLib's initializer list does: Vector v = [1, 2, 3];.
QuantLib's iterators are AsSpan() and GetEnumerator(), and its reverse iterators
(rbegin(), rend()) indexed loops run backwards.
Constructors
Vector()
Initializes a new instance of the Vector class with size 0.
public Vector()
Vector(int)
Initializes a new instance of the Vector class with the given size.
public Vector(int size)
Parameters
sizeintThe size.
Remarks
The elements are zero, where QuantLib leaves them uninitialized.
Exceptions
- ArgumentOutOfRangeException
The size is negative.
Vector(int, double)
Initializes a new instance of the Vector class, every element set to a value.
public Vector(int size, double value)
Parameters
Exceptions
- ArgumentOutOfRangeException
The size is negative.
Vector(int, double, double)
Initializes a new instance of the Vector class filled as a₀ = value,
aᵢ = aᵢ₋₁ + increment.
public Vector(int size, double value, double increment)
Parameters
sizeintThe size.
valuedoubleThe first element.
incrementdoubleThe increment between successive elements, added one at a time.
Exceptions
- ArgumentOutOfRangeException
The size is negative.
Vector(ReadOnlySpan<double>)
Initializes a new instance of the Vector class from a sequence, which it copies.
public Vector(ReadOnlySpan<double> data)
Parameters
dataReadOnlySpan<double>The elements.
Properties
Empty
Gets a value indicating whether the vector is empty.
public bool Empty { get; }
Property Value
this[int]
Gets or sets the element at the given index.
public double this[int i] { get; set; }
Parameters
iintThe index.
Property Value
- double
The element.
Size
Gets the size of the vector.
public int Size { get; }
Property Value
Methods
AsSpan()
A span over the elements, through which an algorithm reads and writes them: QuantLib's begin() and
end(). It reads this vector until the next Resize(int) or Swap(Vector).
public Span<double> AsSpan()
Returns
At(int)
The element at the given index, checked: QuantLib's at, which can also set it.
public ref double At(int i)
Parameters
iintThe index.
Returns
- double
A reference to the element.
Exceptions
- ArgumentOutOfRangeException
The index is out of range.
Back()
The last element, which can also be set.
public ref double Back()
Returns
- double
A reference to the last element.
Exceptions
- InvalidOperationException
The vector is empty, where QuantLib reads past its end.
Clone()
A deep copy: QuantLib's copy constructor and assignment.
public Vector Clone()
Returns
- Vector
A new vector with the same elements.
Create(ReadOnlySpan<double>)
Builds a vector from a collection expression's elements, which it copies.
public static Vector Create(ReadOnlySpan<double> values)
Parameters
valuesReadOnlySpan<double>The elements.
Returns
- Vector
The vector.
Equals(Vector?)
Whether another vector has the same size and equal elements, each compared as Equals(double)
does: a NaN equals a NaN, so a vector equals itself. == compares as QuantLib does, as double's
own == and Equals(double) differ.
public bool Equals(Vector? other)
Parameters
otherVectorThe other vector.
Returns
Equals(object?)
public override bool Equals(object? obj)
Parameters
objobject
Returns
Front()
The first element, which can also be set.
public ref double Front()
Returns
- double
A reference to the first element.
Exceptions
- InvalidOperationException
The vector is empty, where QuantLib reads past its end.
GetEnumerator()
Returns an enumerator over the elements, so that foreach works.
public Span<double>.Enumerator GetEnumerator()
Returns
- Span<double>.Enumerator
The enumerator, whose elements can also be set (
foreach (ref double x in v)).
GetHashCode()
A hash of the elements, which changes when they do.
public override int GetHashCode()
Returns
- int
The hash code.
Resize(int)
Changes the size, keeping the elements that fit; new elements are zero, where QuantLib leaves them uninitialized.
public void Resize(int n)
Parameters
nintThe new size.
Remarks
Shrinking moves the elements to new storage, where QuantLib keeps its storage, so a span from AsSpan() taken before a resize no longer reads the vector.
Exceptions
- ArgumentOutOfRangeException
The size is negative.
Swap(Vector)
Exchanges the contents of this vector with another's.
public void Swap(Vector from)
Parameters
fromVectorThe other vector.
ToString()
The vector as QuantLib writes it, e.g. "[ 1; 2.5; 3 ]", each element as a C++ stream writes it.
public override string ToString()
Returns
- string
The text.
Operators
operator +(Vector, Vector)
The element-by-element sum.
public static Vector operator +(Vector v1, Vector v2)
Parameters
Returns
- Vector
A new vector.
Exceptions
- ArgumentException
The sizes differ.
operator +(Vector, double)
The vector with a scalar added to every element.
public static Vector operator +(Vector v1, double a)
Parameters
Returns
- Vector
A new vector.
operator +(double, Vector)
A scalar added to every element of the vector.
public static Vector operator +(double a, Vector v2)
Parameters
Returns
- Vector
A new vector.
operator +=(Vector)
Adds another vector to this one, element by element, in place.
public void operator +=(Vector v)
Parameters
vVectorThe vector to add.
Exceptions
- ArgumentException
The sizes differ.
operator +=(double)
Adds a scalar to every element, in place.
public void operator +=(double x)
Parameters
xdoubleThe scalar.
operator /(Vector, Vector)
The element-by-element quotient.
public static Vector operator /(Vector v1, Vector v2)
Parameters
Returns
- Vector
A new vector.
Exceptions
- ArgumentException
The sizes differ.
operator /(Vector, double)
The vector with every element divided by a scalar.
public static Vector operator /(Vector v1, double a)
Parameters
Returns
- Vector
A new vector.
operator /(double, Vector)
A scalar divided by every element of the vector.
public static Vector operator /(double a, Vector v2)
Parameters
Returns
- Vector
A new vector.
operator /=(Vector)
Divides this vector by another, element by element, in place.
public void operator /=(Vector v)
Parameters
vVectorThe other vector.
Exceptions
- ArgumentException
The sizes differ.
operator /=(double)
Divides every element by a scalar, in place.
public void operator /=(double x)
Parameters
xdoubleThe scalar.
operator ==(Vector?, Vector?)
Whether two vectors have the same size and equal elements, compared with == as QuantLib does: a NaN
element makes them unequal.
public static bool operator ==(Vector? left, Vector? right)
Parameters
Returns
operator !=(Vector?, Vector?)
Whether two vectors differ: the negation of ==.
public static bool operator !=(Vector? left, Vector? right)
Parameters
Returns
operator *=(Vector)
Multiplies this vector by another, element by element, in place.
public void operator *=(Vector v)
Parameters
vVectorThe other vector.
Exceptions
- ArgumentException
The sizes differ.
operator *=(double)
Multiplies every element by a scalar, in place.
public void operator *=(double x)
Parameters
xdoubleThe scalar.
operator *(Vector, Vector)
The element-by-element product.
public static Vector operator *(Vector v1, Vector v2)
Parameters
Returns
- Vector
A new vector.
Exceptions
- ArgumentException
The sizes differ.
operator *(Vector, double)
The vector with every element multiplied by a scalar.
public static Vector operator *(Vector v1, double a)
Parameters
Returns
- Vector
A new vector.
operator *(double, Vector)
A scalar multiplied by every element of the vector.
public static Vector operator *(double a, Vector v2)
Parameters
Returns
- Vector
A new vector.
operator -(Vector, Vector)
The element-by-element difference.
public static Vector operator -(Vector v1, Vector v2)
Parameters
Returns
- Vector
A new vector.
Exceptions
- ArgumentException
The sizes differ.
operator -(Vector, double)
The vector with a scalar subtracted from every element.
public static Vector operator -(Vector v1, double a)
Parameters
Returns
- Vector
A new vector.
operator -(double, Vector)
Every element of the vector subtracted from a scalar.
public static Vector operator -(double a, Vector v2)
Parameters
Returns
- Vector
A new vector.
operator -=(Vector)
Subtracts another vector from this one, element by element, in place.
public void operator -=(Vector v)
Parameters
vVectorThe vector to subtract.
Exceptions
- ArgumentException
The sizes differ.
operator -=(double)
Subtracts a scalar from every element, in place.
public void operator -=(double x)
Parameters
xdoubleThe scalar.
operator -(Vector)
The vector negated, element by element.
public static Vector operator -(Vector v)
Parameters
vVectorThe vector.
Returns
- Vector
A new vector.
operator +(Vector)
The vector itself, a copy: QuantLib's unary +.
public static Vector operator +(Vector v)
Parameters
vVectorThe vector.
Returns
- Vector
A copy.