Table of Contents

Class TridiagonalOperator

Namespace
NanoQL.Methods
Assembly
NanoQL.dll

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

from TridiagonalOperator

The 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

low Vector

The lower diagonal, one shorter than the main one.

mid Vector

The main diagonal.

high Vector

The 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

size int

The 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

Vector

lowerDiagonal

The lower diagonal, of size n − 1: element i is row i + 1's.

protected Vector lowerDiagonal

Field Value

Vector

n

The size: the length of the main diagonal.

protected int n

Field Value

int

timeSetter

The time-setting logic, or null for an operator that doesn't depend on time.

protected TridiagonalOperatorTimeSetter? timeSetter

Field Value

TridiagonalOperatorTimeSetter

upperDiagonal

The upper diagonal, of size n − 1: element i is row i's.

protected Vector upperDiagonal

Field Value

Vector

Properties

IsTimeDependent

Gets a value indicating whether the operator has a time setter, which SetTime(double) calls.

public bool IsTimeDependent { get; }

Property Value

bool

Size

Gets the size: the number of rows.

public int Size { get; }

Property Value

int

Methods

ApplyTo(Vector)

Applies the operator to a vector.

public Vector ApplyTo(Vector v)

Parameters

v Vector

The 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

Vector

A new vector, of size Size.

Identity(int)

The identity operator of the given size.

public static TridiagonalOperator Identity(int size)

Parameters

size int

The 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

Vector

A new vector, of size Size − 1.

SOR(Vector, double)

Solves the linear system for a right-hand side by successive over-relaxation.

public Vector SOR(Vector rhs, double tol)

Parameters

rhs Vector

The right-hand side, of the operator's size, which is also the first guess.

tol double

The 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

valB double

The main diagonal's element.

valC double

The upper diagonal's element.

SetLastRow(double, double)

Sets the last row's lower and main elements.

public void SetLastRow(double valA, double valB)

Parameters

valA double

The lower diagonal's element.

valB double

The main diagonal's element.

SetMidRow(int, double, double, double)

Sets an inner row's three elements.

public void SetMidRow(int i, double valA, double valB, double valC)

Parameters

i int

The row, from 1 to Size − 2.

valA double

The lower diagonal's element.

valB double

The main diagonal's element.

valC double

The 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

valA double

The lower diagonal's element.

valB double

The main diagonal's element.

valC double

The 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

t double

The time.

SolveFor(Vector)

Solves the linear system for a right-hand side.

public Vector SolveFor(Vector rhs)

Parameters

rhs Vector

The 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

rhs Vector

The right-hand side, of the operator's size.

result Vector

The 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

from TridiagonalOperator

The other operator.

UpperDiagonal()

A copy of the upper diagonal.

public Vector UpperDiagonal()

Returns

Vector

A new vector, of size Size − 1.

Operators

operator +(TridiagonalOperator, TridiagonalOperator)

The sum, diagonal by diagonal, with no time setter.

public static TridiagonalOperator operator +(TridiagonalOperator D1, TridiagonalOperator D2)

Parameters

D1 TridiagonalOperator

The first operator.

D2 TridiagonalOperator

The 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

D TridiagonalOperator

The operator.

a double

The 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

D TridiagonalOperator

The operator.

a double

The 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

a double

The scalar.

D TridiagonalOperator

The 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

D1 TridiagonalOperator

The first operator.

D2 TridiagonalOperator

The 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

D TridiagonalOperator

The 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

D TridiagonalOperator

The operator.

Returns

TridiagonalOperator

A copy.