SciPy - fiedler_companion() Function



The scipy.linalg.fiedler_companion() function builds a Fiedler companion matrix from an one-dimensional array or sequence. It creates a structured matrix which has off-diagonal members representing sums of vector entries and diagonal members representing the vector's negative sum. Such matrices are very useful in polynomial analysis and matrix factorization.

Errors can arise in cases where input is not a 1D array or sequence or is of length zero; the computation gets quite complicated. If the size of input vectors is big, potentially uses more memory and reduces speed.

Fiedler companion matrix is extensively applied with scipy.linalg.eig() for polynomial roots analysis and for analyzing stability in a system with scipy.linalg.lu() in order to speed up solutions for structured linear equations.

Syntax

The syntax for the SciPy fiedler_companion() method is as follows −

.fiedler_companion(a)

Parameters

This method accepts the following parameters −

  • a (array_like) − A 1D array or sequence that defines the Fiedler companion matrix.

Return Value

F (ndarray) − A square Fiedler companion matrix of shape (len(a)-1, len(a)-1), with structured diagonal and off-diagonal elements derived from the input vector.

Example 1

The Fiedler companion matrix yields a structured form where the non-zero entries are in a reduced form row by row.

This code demonstrates how one can create an Fiedler companion matrix using the input list a = [1, 2, 3] and passing it to the fiedler_companion() method. The matrix arranges its rows combining components in 'A', initiated with the input sequence and reducing the number of non-zero members in each successive row.

import scipy.linalg
from scipy.linalg import fiedler_companion

# Input array
A = [1, 2, 3]

# Generate the Fiedler companion matrix
matrix = scipy.linalg.fiedler_companion(A)
print("Fiedler Companion Matrix:")
print(matrix)

When we run above program, it produces following result

Fiedler Companion Matrix:
[[-2. -3.]
 [ 1.  0.]]

Example 2

This code demonstrates how to use the Fiedler companion matrix to calculate roots of a polynomial. The input a = [1, -6, 11, -6] consists of coefficients for the polynomial x^3 - 6x^2 + 11x - 6 = 0. Using the coefficients, the fiedler_companion() method builds a matrix, and, using scipy.linalg.eigvals(), the eigenvalues calculated are the roots of the polynomial.

The output provides the Fiedler companion matrix and its eigenvalues, which correspond to polynomial roots. In this case, the roots are [3,2,1] and may imply that the polynomial can be factored as (x3)(x2)(x1)=0. It is useful for numerical approaches and applications that require the analysis of the roots of the polynomial.

from scipy.linalg import fiedler_companion, eigvals

# Polynomial coefficients
a = [1, -6, 11, -6]
matrix = fiedler_companion(a)

# Compute eigenvalues to find roots
roots = eigvals(matrix)
print("Fiedler Companion Matrix:")
print(matrix)
print("\nRoots of the Polynomial:")
print(roots)

Following is an output of the above code

Fiedler Companion Matrix:
[[  6. -11.   1.]
 [  1.   0.   0.]
 [  0.   6.   0.]]

Roots of the Polynomial:
[3.+0.j 2.+0.j 1.+0.j]

Example 3

A larger input array, such as a = [1, 2, 3, 4, 5], produces a larger Fiedler companion matrix. The matrix's structure expands proportionally, with each row containing fewer non-zero elements than the previous row.

from scipy.linalg import fiedler_companion

# Large input array
a = [1, 2, 3, 4, 5]

# Generate the Fiedler companion matrix
matrix = fiedler_companion(a)
print("Fiedler Companion Matrix:")
print(matrix)

Output of the above code is as follows

Fiedler Companion Matrix:
[[-2. -3.  1.  0.]
 [ 1.  0.  0.  0.]
 [ 0. -4.  0. -5.]
 [ 0.  1.  0.  0.]]
 
scipy_special_matrices_functions.htm
Advertisements