Class TridiagonalOperator
The base implementation of a tridiagonal operator: its lower, main and upper diagonals, applied to a vector or solved for a right-hand side.
[WorkingType]
public class TridiagonalOperator
- Inheritance
-
TridiagonalOperator
- Inherited Members
Remarks
A working type: mutable, owned by one computation and not thread-safe. The row setters and SetTime(double) change it; the operators return new operators. Clone() is the deep copy, where QuantLib copies an operator by value.
An extension point, as in QuantLib: the finite-difference operators derive from it. A derived operator reads and sets the diagonals through the protected fields, and makes itself time-dependent by setting timeSetter. To use real time-dependent algebra, a time-dependent derived operator overloads the operators, which here build operators without a time setter.
Constructors
TridiagonalOperator(TridiagonalOperator)
Initializes a new instance of the TridiagonalOperator class as a copy of another: its diagonals copied, and its time setter, which is shared, kept.
protected TridiagonalOperator(TridiagonalOperator from)
Parameters
fromTridiagonalOperatorThe operator to copy.
TridiagonalOperator(Vector, Vector, Vector)
Initializes a new instance of the TridiagonalOperator class from its diagonals, which it copies.
public TridiagonalOperator(Vector low, Vector mid, Vector high)
Parameters
lowVectorThe lower diagonal, one shorter than the main one.
midVectorThe main diagonal.
highVectorThe upper diagonal, one shorter than the main one.
Exceptions
- ArgumentException
A diagonal has the wrong size.
TridiagonalOperator(int)
Initializes a new instance of the TridiagonalOperator class of the given size.
public TridiagonalOperator(int size = 0)
Parameters
sizeintThe size: 0, or at least 2.
Remarks
The diagonals' elements are zero, where QuantLib leaves them uninitialized.
Exceptions
- ArgumentOutOfRangeException
The size is negative.
- NanoQLException
The size is 1.
Fields
diagonal
The main diagonal, of size n.
protected Vector diagonal
Field Value
lowerDiagonal
The lower diagonal, of size n − 1: element i is row i + 1's.
protected Vector lowerDiagonal
Field Value
n
The size: the length of the main diagonal.
protected int n
Field Value
timeSetter
The time-setting logic, or null for an operator that doesn't depend on time.
protected TridiagonalOperatorTimeSetter? timeSetter
Field Value
upperDiagonal
The upper diagonal, of size n − 1: element i is row i's.
protected Vector upperDiagonal
Field Value
Properties
IsTimeDependent
Gets a value indicating whether the operator has a time setter, which SetTime(double) calls.
public bool IsTimeDependent { get; }
Property Value
Size
Gets the size: the number of rows.
public int Size { get; }
Property Value
Methods
ApplyTo(Vector)
Applies the operator to a vector.
public Vector ApplyTo(Vector v)
Parameters
vVectorThe vector, of the operator's size.
Returns
- Vector
The product, a new vector.
Exceptions
- NanoQLException
The operator has size 0, or size 1, where QuantLib reads past its arrays.
- ArgumentException
The vector has the wrong size.
Clone()
A deep copy of the diagonals, sharing the time setter: QuantLib's copy constructor and assignment.
public TridiagonalOperator Clone()
Returns
- TridiagonalOperator
A new operator.
Diagonal()
A copy of the main diagonal.
public Vector Diagonal()
Returns
Identity(int)
The identity operator of the given size.
public static TridiagonalOperator Identity(int size)
Parameters
sizeintThe size.
Returns
- TridiagonalOperator
An operator whose main diagonal is 1 and whose other diagonals are 0.
Exceptions
- ArgumentOutOfRangeException
The size is less than 1.
LowerDiagonal()
A copy of the lower diagonal.
public Vector LowerDiagonal()
Returns
SOR(Vector, double)
Solves the linear system for a right-hand side by successive over-relaxation.
public Vector SOR(Vector rhs, double tol)
Parameters
rhsVectorThe right-hand side, of the operator's size, which is also the first guess.
toldoubleThe tolerance on the sum of the squared corrections of a sweep.
Returns
- Vector
The solution, a new vector.
Exceptions
- NanoQLException
The operator has size 0, or size 1, where QuantLib reads past its arrays, or 100,000 sweeps don't reach the tolerance.
- ArgumentException
The right-hand side has the wrong size.
SetFirstRow(double, double)
Sets the first row's main and upper elements.
public void SetFirstRow(double valB, double valC)
Parameters
SetLastRow(double, double)
Sets the last row's lower and main elements.
public void SetLastRow(double valA, double valB)
Parameters
SetMidRow(int, double, double, double)
Sets an inner row's three elements.
public void SetMidRow(int i, double valA, double valB, double valC)
Parameters
iintThe row, from 1 to Size − 2.
valAdoubleThe lower diagonal's element.
valBdoubleThe main diagonal's element.
valCdoubleThe upper diagonal's element.
Exceptions
- ArgumentOutOfRangeException
The row is the first, the last or out of range.
SetMidRows(double, double, double)
Sets every inner row's three elements to the same values.
public void SetMidRows(double valA, double valB, double valC)
Parameters
valAdoubleThe lower diagonal's element.
valBdoubleThe main diagonal's element.
valCdoubleThe upper diagonal's element.
SetTime(double)
Sets the time, through the time setter; does nothing for an operator without one.
public void SetTime(double t)
Parameters
tdoubleThe time.
SolveFor(Vector)
Solves the linear system for a right-hand side.
public Vector SolveFor(Vector rhs)
Parameters
rhsVectorThe right-hand side, of the operator's size.
Returns
- Vector
The solution, a new vector.
Exceptions
- NanoQLException
The operator has size 0, or a pivot is close to zero.
- ArgumentException
The right-hand side has the wrong size.
SolveFor(Vector, Vector)
Solves the linear system for a right-hand side into a given vector, which may be the right-hand side itself, which then changes.
public void SolveFor(Vector rhs, Vector result)
Parameters
rhsVectorThe right-hand side, of the operator's size.
resultVectorThe vector the solution is written into, at least of the operator's size.
Exceptions
- NanoQLException
The operator has size 0, or size 1, where QuantLib reads past its arrays, or a pivot is close to zero.
- ArgumentException
The right-hand side has the wrong size.
Swap(TridiagonalOperator)
Exchanges the contents of this operator with another's, the time setters included: QuantLib's member
swap and its free swap(TridiagonalOperator&, TridiagonalOperator&).
public void Swap(TridiagonalOperator from)
Parameters
fromTridiagonalOperatorThe other operator.
UpperDiagonal()
A copy of the upper diagonal.
public Vector UpperDiagonal()
Returns
Operators
operator +(TridiagonalOperator, TridiagonalOperator)
The sum, diagonal by diagonal, with no time setter.
public static TridiagonalOperator operator +(TridiagonalOperator D1, TridiagonalOperator D2)
Parameters
D1TridiagonalOperatorThe first operator.
D2TridiagonalOperatorThe second operator, of the same size.
Returns
- TridiagonalOperator
A new operator.
Exceptions
- ArgumentException
The sizes differ.
operator /(TridiagonalOperator, double)
The operator divided by a scalar, with no time setter.
public static TridiagonalOperator operator /(TridiagonalOperator D, double a)
Parameters
DTridiagonalOperatorThe operator.
adoubleThe scalar.
Returns
- TridiagonalOperator
A new operator, each diagonal element divided by
a.
operator *(TridiagonalOperator, double)
The operator times a scalar, with no time setter.
public static TridiagonalOperator operator *(TridiagonalOperator D, double a)
Parameters
DTridiagonalOperatorThe operator.
adoubleThe scalar.
Returns
- TridiagonalOperator
A new operator, each diagonal element multiplied by
a.
operator *(double, TridiagonalOperator)
A scalar times the operator, with no time setter.
public static TridiagonalOperator operator *(double a, TridiagonalOperator D)
Parameters
adoubleThe scalar.
DTridiagonalOperatorThe operator.
Returns
- TridiagonalOperator
A new operator, each diagonal element multiplied by
a.
operator -(TridiagonalOperator, TridiagonalOperator)
The difference, diagonal by diagonal, with no time setter.
public static TridiagonalOperator operator -(TridiagonalOperator D1, TridiagonalOperator D2)
Parameters
D1TridiagonalOperatorThe first operator.
D2TridiagonalOperatorThe second operator, of the same size.
Returns
- TridiagonalOperator
A new operator.
Exceptions
- ArgumentException
The sizes differ.
operator -(TridiagonalOperator)
The operator negated, with no time setter.
public static TridiagonalOperator operator -(TridiagonalOperator D)
Parameters
DTridiagonalOperatorThe operator.
Returns
- TridiagonalOperator
A new operator.
operator +(TridiagonalOperator)
The operator itself, a copy that keeps its time setter: QuantLib's unary +.
public static TridiagonalOperator operator +(TridiagonalOperator D)
Parameters
DTridiagonalOperatorThe operator.
Returns
- TridiagonalOperator
A copy.