cc04e203创建于 2022年9月18日历史提交

Special Matrices

This package contains methods for generating special kinds of matrices.

Methods

=================================== ============ Method Description =================================== ============ vander_ (x, N[, increasing]) Vandermonde matrix blockDiag_ (a) Block-diagonal marix circulant_ (x) Circulant matrix companion_ (x) Companion matrix convolutionMatrix_ (a, n[, mode]) Convolution matrix dft_ (n) Discrete fourier transform matrix fiedler_ (x) Symmetric Fiedler matrix hadamard_ (n) Hadamard matrix hankel_ (x[, y]) Hankel matrix helmert_ (n) Helmert matrix hilbert_ (N) Hilbert matrix leslie_ (f, s) Leslie matrix pascal_ (n[, kind]) Pascal matrix toeplitz_ (c, r) Toeplitz matrix =================================== ============

Description

.. _vander:

.. function:: vander(x, N[, increasing])

:param T: type parameter, implements Float
:param x: Vector<T>
:param N: Int64
:param increasing: Bool, default to false.
:return: Matrix<T>

Generate the Vandermonde matrix for given vector of values and maximum exponent.
Each row of the result consists of powers of one entry in :math:`x`. The rows are
increasing or decreasing depending on the parameter ``increasing``.

Example:

1. Generate Vandermonde matrix with decreasing rows.

::

    let x = vector<Float64>([1.0, 2.0, 3.0, 5.0])
    vander(x, 3)

The result is:

.. math::

    \begin{bmatrix}
        1.0 & 1.0 & 1.0 \\ 
        4.0 & 2.0 & 1.0 \\
        9.0 & 3.0 & 1.0 \\
        25.0 & 5.0 & 1.0
    \end{bmatrix}

2. Generate Vandermonde matrix with increasing rows.

::

    let x = vector<Float64>([1.0, 2.0, 3.0, 5.0])
    vander(x, 3, increasing:true)

The result is:

.. math::

    \begin{bmatrix}
        1.0 & 1.0 & 1.0 \\ 
        1.0 & 2.0 & 4.0 \\
        1.0 & 3.0 & 9.0 \\
        1.0 & 5.0 & 25.0
    \end{bmatrix}

.. _blockDiag:

.. function:: blockDiag(a)

:param T: type parameter, implements Number
:param a: Array<Matrix<T>>
:return: Matrix<T>

Generate a block diagonal matrix, where the sequence of blocks is given by the list ``a``.

Example:

1. Generate block diagonal matrix with given list of matrices.

::

    var A = matrix<Int64>([[1, 0], [0, 1]])
    var B = matrix<Int64>([[3, 4, 5], [6, 7, 8]])
    var C = matrix<Int64>([[7]])
    var arr = Array<Matrix<Int64>>([A, B, C])
    blockDiag(arr)

The result is:

::

    matrix<Int64>(
        [[1, 0, 0, 0, 0, 0],
         [0, 1, 0, 0, 0, 0],
         [0, 0, 3, 4, 5, 0],
         [0, 0, 6, 7, 8, 0],
         [0, 0, 0, 0, 0, 7]
    ])

.. _circulant:

.. function:: circulant(x)

:param T: type parameter, implements Number
:param x: Vector<T>
:return: Matrix<T>

Generate a circulant matrix. The rows of the matrix are rotations of the input
vector.

Example:

1. Generate a circulant matrix of :math:`[1, 2, 3]`.

::

    let x = vector<Float64>([1.0, 2.0, 3.0])
    circulant(x)

The result is:

::

    matrix<Float64>([
        [1.0, 2.0, 3.0],
        [2.0, 3.0, 1.0],
        [3.0, 1.0, 2.0]
    ])

.. _companion:

.. function:: companion(x)

:param T: type parameter, implements Float
:param x: Vector<T>
:return: Matrix<T>

Returns the companion matrix associated with the polynomial whose coefficients
are given by :math:`x`.

Example:

1. Create the companion matrix of polynomial :math:`2x^3-10x^2+31x-30`.

::

    let x = vector<Float64>([2.0, -10.0, 31.0, -30.0])
    companion(x)

The result is:

::

    matrix<Float64>([
        [5.0, -15.5, 15.0],
        [1.0, 0.0, 0.0],
        [0.0, 1.0, 0.0]
    ])

.. _convolutionMatrix:

.. function:: convolution(a, n[, mode])

:param T: type parameter, implements Number
:param a: Vector<T>
:param n: Int64
:param mode: String, one of "full" (default), "same" and "valid"
:return: Matrix<T>

Construct the matrix for computing a convolution. The result is
slightly different depending on parameter ``mode``.

Example:

1. In the "full" mode, the generated matrix is lower-triangular.

::

    let a = vector<Int64>([-1, 4, -2])
    convolution_matrix(a, 5)

The result is:

::

    matrix<Int64>([
        [-1, 0, 0, 0, 0],
        [ 4, -1, 0, 0, 0],
        [-2, 4, -1, 0, 0],
        [ 0, -2, 4, -1, 0],
        [ 0, 0, -2, 4, -1],
        [ 0, 0, 0, -2, 4],
        [ 0, 0, 0, 0, -2]
    ])

2. In the "valid" mode, the generated matrix is upper-triangular.

::

    let a = vector<Int64>([-1, 4, -2])
    convolution_matrix(a, 5, mode:"valid")

The result is:

::

    matrix<Int64>([
        [-2, 4, -1, 0, 0],
        [ 0, -2, 4, -1, 0],
        [ 0, 0, -2, 4, -1]
    ])

3. In the "same" mode, the generated matrix has roughly same number of bands
above and below the diagonal.

::

    let a = vector<Int64>([-1, 4, -2])
    convolution_matrix(a, 5, mode:"same")

The result is:

::

    matrix<Int64>([
        [4, -1, 0, 0, 0],
        [-2, 4, -1, 0, 0],
        [ 0, -2, 4, -1, 0],
        [0, 0, -2, 4, -1],
        [ 0, 0, 0, -2, 4]
    ])

.. _dft:

.. function:: dft(n)

:param n: Int64
:return: Matrix<Complex64>

Generate the matrix for discrete fourier transform with size :math:`n`.

.. _fiedler:

.. function:: fiedler(x)

:param T: type parameter, implements Real
:param x: Vector<T>
:return: Matrix<T>

Computes the symmetric Fiedler matrix. The entry :math:`F[i,j]` is given
by :math:`|x[i] - x[j]|`.

Example:

1. Compute the Fiedler matrix of :math:`[1, 4, 12, 45, 77]`.

::

    let x = vector<Int64>([1, 4, 12, 45, 77])
    fiedler(x)

The result is:

::

    matrix<Int64>([
        [0, 3, 11, 44, 76],
        [3, 0, 8, 41, 73],
        [11, 8, 0, 33, 65],
        [44, 41, 33, 0, 32],
        [76, 73, 65, 32, 0]
    ])

.. _hadamard:

.. function:: hadamard(n)

:param n: Int64
:return: Matrix<Int64>

Compute the Hadamard matrix of order :math:`n`. Here :math:`n` must be a power of 2.

Example:

1. Compute the Hadamard matrix of order 4.

::
    
    hadamard(4)

The result is:

::

    matrix<Int64>([
        [1, 1, 1, 1],
        [1, -1, 1, -1],
        [1, 1, -1, -1],
        [1, -1, -1, 1]
    ])

.. _hankel:

.. function:: hankel(x)

:param T: type parameter, implements Number
:param x: Vector<T>
:return: Matrix<T>

Compute the Hankel matrix of a single vector.

Example:

1. Compute the Hankel matrix of :math:`[1, 17, 99]`.

::

    let x = vector<Int64>([1, 17, 99])
    hankel(x)

The result is:

::

    matrix<Int64>([
        [1, 17, 99],
        [17, 99, 0],
        [99, 0, 0]
    ])

.. function:: hankel(x, y)

:param T: type parameter, implements Number
:param x: Vector<T>
:param y: Vector<T>
:return: Matrix<T>

Compute the Hankel matrix of two vectors.

Example:

1. Compute the Hankel matrix of two vectors.

::

    let x = vector<Int64>([1, 2, 3, 4])
    let y = vector<Int64>([4, 7, 7, 8, 9])
    hankel(x, y)

The result is:

::

    matrix<Int64>([
        [1, 2, 3, 4, 7],
        [2, 3, 4, 7, 7],
        [3, 4, 7, 7, 8],
        [4, 7, 7, 8, 9]
    ])

.. _helmert:

.. function:: helmert(n)

:param n: Int64
:return: Matrix<Float64>

Compute the Helmert matrix of order :math:`n`.

.. _hilbert:

.. function:: hilbert(N)

:param T: type parameter, implements Float
:param N: Int64
:return: Matrix<T>

Compute the Hilbert matrix of order :math:`N`. The entry
:math:`h[i,j]` is given by :math:`1/(i+j+1)` (counting from zero).

Example:

1. Compute the Hilbert matrix of order 3, returning values in Float64.

::
    
    hilbert<Float64>(3)

The result is:

::

    matrix<Float64>([
        [1.000000, 0.500000, 0.333333],
        [0.500000, 0.333333, 0.250000],
        [0.333333, 0.250000, 0.200000]
    ])

.. _leslie:

.. function:: leslie(f, s)

:param T: type parameter, implements Number
:param f: Vector<T>
:param s: Vector<T>
:return: Matrix<T>

Compute the Leslie matrix of :math:`f` (the "fecundity" coefficients)
and :math:`s` (the "survival" coefficients). The vector :math:`f` has one
more entry than :math:`s`.

Example:

1. Compute the Leslie matrix of two vectors.

::
    
    let f = vector<Float64>([0.1, 2.0, 1.0, 0.1])
    let s = vector<Float64>([0.2, 0.8, 0.7])
    leslie(f, s)

The result is:

::

    matrix<Float64>([
        [0.1, 2.0, 1.0, 0.1],
        [0.2, 0.0, 0.0, 0.0],
        [0.0, 0.8, 0.0, 0.0],
        [0.0, 0.0, 0.7, 0.0]
    ])

.. _pascal:

.. function:: pascal(n[, kind])

:param n: Int64
:param kind: String, one of "symmetric" (default), "lower" and "upper".
:return: Matrix<Int64>

Compute the Pascal matrix of order :math:`n`. Parameter ``kind``
determines the form of the matrix.

Example:

1. Compute the symmetric Pascal matrix of order 4.

::

    pascal(4)

The result is:

::

    matrix<Int64>([
        [1, 1, 1, 1],
        [1, 2, 3, 4],
        [1, 3, 6, 10],
        [1, 4, 10, 20]
    ])

2. Compute the lower Pascal matrix of order 4.

::

    pascal(4, kind:"lower")

The result is:

::

    matrix<Int64>([
        [1, 0, 0, 0],
        [1, 1, 0, 0],
        [1, 2, 1, 0],
        [1, 3, 3, 1]
    ])

3. Compute the upper Pascal matrix of order 4.

::

    pascal(4, kind:"upper")

The result is:

::

    matrix<Int64>([
        [1, 1, 1, 1],
        [0, 1, 2, 3],
        [0, 0, 1, 3],
        [0, 0, 0, 1]
    ])

.. _toeplitz:

.. function:: toeplitz(c, r)

:param T: type parameter, implements Number
:param c: Vector<T>
:param r: Vector<T>
:return: Matrix<T>

Constructs the Toeplitz matrix where the first column is :math:`c` and the
first row is :math:`r`. The first entry of :math:`r` is ignored.

Example:

1. Construct a Toeplitz matrix.

::

    let a = vector<Int64>([1, 2, 3])
    let b = vector<Int64>([1, 4, 5, 6])
    toeplitz(a, b)

The result is:

::

    matrix<Int64>([
        [1, 4, 5, 6],
        [2, 1, 4, 5],
        [3, 2, 1, 4]
    ])