# Chapter 29. Using BLAS, LAPACK and ARPACK for igraph matrices and graphs

## 1. BLAS interface in igraph

BLAS is a highly optimized library for basic linear algebra operations such as vector-vector, matrix-vector and matrix-matrix product. Please see http://www.netlib.org/blas/ for details and a reference implementation in Fortran. igraph contains some wrapper functions that can be used to call BLAS routines in a somewhat more user-friendly way. Not all BLAS routines are included in igraph, and even those which are included might not have wrappers; the extension of the set of wrapped functions will probably be driven by igraph's internal requirements. The wrapper functions usually substitute double-precision floating point arrays used by BLAS with igraph_vector_t and igraph_matrix_t instances and also remove those parameters (such as the number of rows/columns) that can be inferred from the passed arguments directly.

### 1.1. `igraph_blas_ddot` — Dot product of two vectors.

    igraph_error_t igraph_blas_ddot(const igraph_vector_t *v1, const igraph_vector_t *v2,
                           igraph_real_t *res);

Arguments:

`v1`: | The first vector.
`v2`: | The second vector.
`res`: | Pointer to a real, the result will be stored here.

Time complexity: O(n) where n is the length of the vectors.

Example 29.1. File `examples/simple/blas.c`

    #include <igraph.h>

    int main(void) {
        igraph_matrix_t m;
        igraph_vector_t x, y, z;
        igraph_real_t xz, xx;

        igraph_vector_init_real(&x, 3, 1.0, 2.0, 3.0);
        igraph_vector_init_real(&y, 4, 4.0, 5.0, 6.0, 7.0);
        igraph_vector_init_real(&z, 3, -1.0, 0.0, 0.5);

        igraph_matrix_init(&m, 4, 3);
        MATRIX(m, 0, 0) = 1;
        MATRIX(m, 0, 1) = 2;
        MATRIX(m, 0, 2) = 3;
        MATRIX(m, 1, 0) = 2;
        MATRIX(m, 1, 1) = 3;
        MATRIX(m, 1, 2) = 4;
        MATRIX(m, 2, 0) = 3;
        MATRIX(m, 2, 1) = 4;
        MATRIX(m, 2, 2) = 5;
        MATRIX(m, 3, 0) = 4;
        MATRIX(m, 3, 1) = 5;
        MATRIX(m, 3, 2) = 6;

        /* Compute 2 m.x + 3 y and store it in y. */
        igraph_blas_dgemv(/* transpose= */ 0, /* alpha= */ 2, &m, &x, /* beta= */ 3, &y);
        igraph_vector_print(&y);

        /* Compute the squared norm of x, as well as the dor product of x and z. */
        igraph_blas_ddot(&x, &x, &xx);
        igraph_blas_ddot(&x, &z, &xz);
        printf("x.x = %g, x.z = %g\n", xx, xz);

        igraph_matrix_destroy(&m);
        igraph_vector_destroy(&z);
        igraph_vector_destroy(&y);
        igraph_vector_destroy(&x);

        return 0;
    }

### 1.2. `igraph_blas_dnrm2` — Euclidean norm of a vector.

    igraph_real_t igraph_blas_dnrm2(const igraph_vector_t *v);

Arguments:

`v`: | The vector.

Returns:

`` | Real value, the norm of `v`.

Time complexity: O(n) where n is the length of the vector.

### 1.3. `igraph_blas_dgemv` — Matrix-vector multiplication using BLAS, vector version.

    igraph_error_t igraph_blas_dgemv(igraph_bool_t transpose, igraph_real_t alpha,
                           const igraph_matrix_t *a, const igraph_vector_t *x,
                           igraph_real_t beta, igraph_vector_t *y);

This function is a somewhat more user-friendly interface to the `dgemv` function in BLAS. `dgemv` performs the operation `y = alpha*A*x + beta*y`, where `x` and `y` are vectors and `A` is an appropriately sized matrix (symmetric or non-symmetric).

Arguments:

`transpose`: | Whether to transpose the matrix `A`.
`alpha`: | The constant `alpha`.
`a`: | The matrix `A`.
`x`: | The vector `x`.
`beta`: | The constant `beta`.
`y`: | The vector `y` (which will be modified in-place). It must always have the correct length, but its elements need not be set when `beta=0`.

Time complexity: O(nk) if the matrix is of size n x k

Returns:

`` | `IGRAPH_EOVERFLOW` if the matrix is too large for BLAS, `IGRAPH_SUCCESS` otherwise.

See also:

`` | `igraph_blas_dgemv_array` if you have arrays instead of vectors.

Example 29.2. File `examples/simple/blas.c`

    #include <igraph.h>

    int main(void) {
        igraph_matrix_t m;
        igraph_vector_t x, y, z;
        igraph_real_t xz, xx;

        igraph_vector_init_real(&x, 3, 1.0, 2.0, 3.0);
        igraph_vector_init_real(&y, 4, 4.0, 5.0, 6.0, 7.0);
        igraph_vector_init_real(&z, 3, -1.0, 0.0, 0.5);

        igraph_matrix_init(&m, 4, 3);
        MATRIX(m, 0, 0) = 1;
        MATRIX(m, 0, 1) = 2;
        MATRIX(m, 0, 2) = 3;
        MATRIX(m, 1, 0) = 2;
        MATRIX(m, 1, 1) = 3;
        MATRIX(m, 1, 2) = 4;
        MATRIX(m, 2, 0) = 3;
        MATRIX(m, 2, 1) = 4;
        MATRIX(m, 2, 2) = 5;
        MATRIX(m, 3, 0) = 4;
        MATRIX(m, 3, 1) = 5;
        MATRIX(m, 3, 2) = 6;

        /* Compute 2 m.x + 3 y and store it in y. */
        igraph_blas_dgemv(/* transpose= */ 0, /* alpha= */ 2, &m, &x, /* beta= */ 3, &y);
        igraph_vector_print(&y);

        /* Compute the squared norm of x, as well as the dor product of x and z. */
        igraph_blas_ddot(&x, &x, &xx);
        igraph_blas_ddot(&x, &z, &xz);
        printf("x.x = %g, x.z = %g\n", xx, xz);

        igraph_matrix_destroy(&m);
        igraph_vector_destroy(&z);
        igraph_vector_destroy(&y);
        igraph_vector_destroy(&x);

        return 0;
    }

### 1.4. `igraph_blas_dgemm` — Matrix-matrix multiplication using BLAS.

    igraph_error_t igraph_blas_dgemm(igraph_bool_t transpose_a, igraph_bool_t transpose_b,
            igraph_real_t alpha, const igraph_matrix_t *a, const igraph_matrix_t *b,
            igraph_real_t beta, igraph_matrix_t *c);

This function is a somewhat more user-friendly interface to the `dgemm` function in BLAS. `dgemm` calculates alpha*a*b + beta*c, where a, b and c are matrices, of which a and b can be transposed.

Arguments:

`transpose_a`: | whether to transpose the matrix `a`
`transpose_b`: | whether to transpose the matrix `b`
`alpha`: | the constant `alpha`
`a`: | the matrix `a`
`b`: | the matrix `b`
`beta`: | the constant `beta`
`c`: | the matrix `c`. The result will also be stored here. If beta is zero, c will be resized to fit the result.

Time complexity: O(n m k) where matrix a is of size n × k, and matrix b is of size k × m.

Returns:

`` | `IGRAPH_EOVERFLOW` if the matrix is too large for BLAS, `IGRAPH_EINVAL` if the matrices have incompatible sizes, `IGRAPH_SUCCESS` otherwise.

Example 29.3. File `examples/simple/blas_dgemm.c`

    #include <igraph.h>

    int main(void) {
        igraph_matrix_t a, b, c;

        igraph_matrix_init(&a, 2, 2);
        MATRIX(a, 0, 0) = 1;
        MATRIX(a, 0, 1) = 2;
        MATRIX(a, 1, 0) = 3;
        MATRIX(a, 1, 1) = 4;

        igraph_matrix_init(&b, 2, 2);
        MATRIX(b, 0, 0) = 5;
        MATRIX(b, 0, 1) = 6;
        MATRIX(b, 1, 0) = 7;
        MATRIX(b, 1, 1) = 8;

        igraph_matrix_init(&c, 2, 2);

        igraph_blas_dgemm(1, 1, 0.5, &a, &b, 0, &c);
        igraph_matrix_printf(&c, "%g");

        igraph_matrix_destroy(&a);
        igraph_matrix_destroy(&b);
        igraph_matrix_destroy(&c);
        return 0;
    }

### 1.5. `igraph_blas_dgemv_array` — Matrix-vector multiplication using BLAS, array version.

    igraph_error_t igraph_blas_dgemv_array(igraph_bool_t transpose, igraph_real_t alpha,
                                 const igraph_matrix_t* a, const igraph_real_t* x,
                                 igraph_real_t beta, igraph_real_t* y);

This function is a somewhat more user-friendly interface to the `dgemv` function in BLAS. `dgemv` performs the operation y = alpha*A*x + beta*y, where x and y are vectors and A is an appropriately sized matrix (symmetric or non-symmetric).

Arguments:

`transpose`: | whether to transpose the matrix `A`
`alpha`: | the constant `alpha`
`a`: | the matrix `A`
`x`: | the vector `x` as a regular C array
`beta`: | the constant `beta`
`y`: | the vector `y` as a regular C array (which will be modified in-place)

Time complexity: O(nk) if the matrix is of size n x k

Returns:

`` | `IGRAPH_EOVERFLOW` if the matrix is too large for BLAS, `IGRAPH_SUCCESS` otherwise.

See also:

`` | `igraph_blas_dgemv` if you have vectors instead of arrays.

## 2. LAPACK interface in igraph

LAPACK is written in Fortran90 and provides routines for solving systems of simultaneous linear equations, least-squares solutions of linear systems of equations, eigenvalue problems, and singular value problems. The associated matrix factorizations (LU, Cholesky, QR, SVD, Schur, generalized Schur) are also provided, as are related computations such as reordering of the Schur factorizations and estimating condition numbers. Dense and banded matrices are handled, but not general sparse matrices. In all areas, similar functionality is provided for real and complex matrices, in both single and double precision.

igraph provides an interface to a very limited set of LAPACK functions, using the regular igraph data structures.

See more about LAPACK at http://www.netlib.org/lapack/

### 2.1. Matrix factorization, solving linear systems

#### 2.1.1. `igraph_lapack_dgetrf` — LU factorization of a general M-by-N matrix.

    igraph_error_t igraph_lapack_dgetrf(igraph_matrix_t *a, igraph_vector_int_t *ipiv,
                             int *info);

The factorization has the form A = P * L * U where P is a permutation matrix, L is lower triangular with unit diagonal elements (lower trapezoidal if m > n), and U is upper triangular (upper trapezoidal if m < n).

Arguments:

`a`: | The input/output matrix. On entry, the M-by-N matrix to be factored. On exit, the factors L and U from the factorization A = P * L * U; the unit diagonal elements of L are not stored.
`ipiv`: | An integer vector, the pivot indices are stored here, unless it is a null pointer. Row `i` of the matrix was interchanged with row `ipiv[i]`.
`info`: | LAPACK error code. Zero on successful exit. If its value is a positive number i, it indicates that U(i,i) is exactly zero. The factorization has been completed, but the factor U is exactly singular, and division by zero will occur if it is used to solve a system of equations. If LAPACK returns an error, i.e. a negative info value, then an igraph error is generated as well.

Returns:

`` | Error code.

Time complexity: TODO.

#### 2.1.2. `igraph_lapack_dgetrs` — Solve general system of linear equations using LU factorization.

    igraph_error_t igraph_lapack_dgetrs(igraph_bool_t transpose, const igraph_matrix_t *a,
                             const igraph_vector_int_t *ipiv, igraph_matrix_t *b);

This function calls LAPACK to solve a system of linear equations A * X = B or A' * X = B with a general N-by-N matrix A using the LU factorization computed by `igraph_lapack_dgetrf`.

Arguments:

`transpose`: | Logical scalar, whether to transpose the input matrix.
`a`: | A matrix containing the L and U factors from the factorization A = P*L*U. L is expected to be unitriangular, diagonal entries are those of U. If A is singular, no warning or error wil be given and random output will be returned.
`ipiv`: | An integer vector, the pivot indices from `igraph_lapack_dgetrf()` must be given here. Row `i` of A was interchanged with row `ipiv[i]`.
`b`: | The right hand side matrix must be given here. The solution will also be placed here.

Returns:

`` | Error code.

Time complexity: TODO.

#### 2.1.3. `igraph_lapack_dgesv` — Solve system of linear equations with LU factorization.

    igraph_error_t igraph_lapack_dgesv(igraph_matrix_t *a, igraph_vector_int_t *ipiv,
                                       igraph_matrix_t *b, int *info);

This function computes the solution to a real system of linear equations A * X = B, where A is an N-by-N matrix and X and B are N-by-NRHS matrices.

The LU decomposition with partial pivoting and row interchanges is used to factor A as A = P * L * U, where P is a permutation matrix, L is unit lower triangular, and U is upper triangular. The factored form of A is then used to solve the system of equations A * X = B.

Arguments:

`a`: | Matrix. On entry the N-by-N coefficient matrix, on exit, the factors L and U from the factorization A=P*L*U; the unit diagonal elements of L are not stored.
`ipiv`: | An integer vector or a null pointer. If not a null pointer, then the pivot indices that define the permutation matrix P, are stored here. Row i of the matrix was interchanged with row IPIV(i).
`b`: | Matrix, on entry the right hand side matrix should be stored here. On exit, if there was no error, and the info argument is zero, then it contains the solution matrix X.
`info`: | The LAPACK info code. If it is positive, then U(info,info) is exactly zero. In this case the factorization has been completed, but the factor U is exactly singular, so the solution could not be computed.

Returns:

`` | Error code.

Time complexity: TODO.

Example 29.4. File `examples/simple/igraph_lapack_dgesv.c`

    #include <igraph.h>
    #include <stdio.h>

    #define DIM 10

    void igraph_print_warning(const char *reason, const char *file,
                              int line) {
        IGRAPH_UNUSED(file);
        IGRAPH_UNUSED(line);
        printf("Warning: %s\n", reason);
    }

    int main(void) {

        igraph_matrix_t A, B, RHS;
        int info;
        int i, j;

        /* Identity matrix, you have to start somewhere */

        igraph_matrix_init(&A, DIM, DIM);
        igraph_matrix_init(&B, DIM, 1);
        for (i = 0; i < DIM; i++) {
            MATRIX(A, i, i) = 1.0;
            MATRIX(B, i, 0) = i + 1;
        }

        igraph_matrix_init_copy(&RHS, &B);
        igraph_lapack_dgesv(&A, /*ipiv=*/ 0, &RHS, &info);

        if (info != 0) {
            return 1;
        }
        if (!igraph_matrix_all_e(&B, &RHS)) {
            return 2;
        }

        igraph_matrix_destroy(&A);
        igraph_matrix_destroy(&B);
        igraph_matrix_destroy(&RHS);

        /* Diagonal matrix */

        igraph_matrix_init(&A, DIM, DIM);
        igraph_matrix_init(&RHS, DIM, 1);
        for (i = 0; i < DIM; i++) {
            MATRIX(A, i, i) = i + 1;
            MATRIX(RHS, i, 0) = i + 1;
        }

        igraph_lapack_dgesv(&A, /*ipiv=*/ 0, &RHS, &info);

        if (info != 0) {
            return 3;
        }
        for (i = 0; i < DIM; i++) {
            if (MATRIX(RHS, i, 0) != 1.0) {
                return 4;
            }
        }

        igraph_matrix_destroy(&A);
        igraph_matrix_destroy(&RHS);

        /* A general matrix */

        igraph_rng_seed(igraph_rng_default(), 42);

        igraph_matrix_init(&A, DIM, DIM);
        igraph_matrix_init(&B, DIM, 1);
        igraph_matrix_init(&RHS, DIM, 1);
        for (i = 0; i < DIM; i++) {
            int j;
            MATRIX(B, i, 0) = igraph_rng_get_integer(igraph_rng_default(), 1, 10);
            for (j = 0; j < DIM; j++) {
                MATRIX(A, i, j) = igraph_rng_get_integer(igraph_rng_default(), 1, 10);
            }
        }
        igraph_blas_dgemv_array(/*transpose=*/ 0, /*alpha=*/ 1.0, /*a=*/ &A,
                                               /*x-*/ &MATRIX(B, 0, 0), /*beta=*/ 0,
                                               /*y=*/ &MATRIX(RHS, 0, 0));

        igraph_lapack_dgesv(&A, /*ipiv=*/ 0, &RHS, &info);
        if (info != 0) {
            return 5;
        }
        for (i = 0; i < DIM; i++) {
            if (fabs(MATRIX(B, i, 0) - MATRIX(RHS, i, 0)) > 1e-11) {
                return 6;
            }
        }

        igraph_matrix_destroy(&A);
        igraph_matrix_destroy(&B);
        igraph_matrix_destroy(&RHS);

        /* A singular matrix */

        igraph_matrix_init(&A, DIM, DIM);
        igraph_matrix_init(&B, DIM, 1);
        igraph_matrix_init(&RHS, DIM, 1);
        for (i = 0; i < DIM; i++) {
            MATRIX(B, i, 0) = igraph_rng_get_integer(igraph_rng_default(), 1, 10);
            for (j = 0; j < DIM; j++) {
                MATRIX(A, i, j) = i == j ? 1 : 0;
            }
        }
        for (i = 0; i < DIM; i++) {
            MATRIX(A, DIM - 1, i) = MATRIX(A, 0, i);
        }

        igraph_blas_dgemv_array(/*transpose=*/ 0, /*alpha=*/ 1.0, /*a=*/ &A,
                                               /*x-*/ &MATRIX(B, 0, 0), /*beta=*/ 0,
                                               /*y=*/ &MATRIX(RHS, 0, 0));

        igraph_set_warning_handler(igraph_print_warning);
        igraph_lapack_dgesv(&A, /*ipiv=*/ 0, &RHS, &info);
        if (info != 10) {
            printf("LAPACK returned info = %d, should have been 10", info);
            return 7;
        }

        igraph_matrix_destroy(&A);
        igraph_matrix_destroy(&B);
        igraph_matrix_destroy(&RHS);

        return 0;
    }

### 2.2. Eigenvalues and eigenvectors of matrices

#### 2.2.1. `igraph_lapack_dsyevr` — Selected eigenvalues and optionally eigenvectors of a symmetric matrix.

    igraph_error_t igraph_lapack_dsyevr(const igraph_matrix_t *A,
                             igraph_lapack_dsyev_which_t which,
                             igraph_real_t vl, igraph_real_t vu, int vestimate,
                             int il, int iu, igraph_real_t abstol,
                             igraph_vector_t *values, igraph_matrix_t *vectors,
                             igraph_vector_int_t *support);

Calls the DSYEVR LAPACK function to compute selected eigenvalues and, optionally, eigenvectors of a real symmetric matrix A. Eigenvalues and eigenvectors can be selected by specifying either a range of values or a range of indices for the desired eigenvalues.

See more in the LAPACK documentation.

Arguments:

`A`: | Matrix, on entry it contains the symmetric input matrix. Only the leading N-by-N upper triangular part is used for the computation.
`which`: | Constant that gives which eigenvalues (and possibly the corresponding eigenvectors) to calculate. Possible values are `IGRAPH_LAPACK_DSYEV_ALL`, all eigenvalues; `IGRAPH_LAPACK_DSYEV_INTERVAL`, all eigenvalues in the half-open interval (vl,vu]; `IGRAPH_LAPACK_DSYEV_SELECT`, the il-th through iu-th eigenvalues.
`vl`: | If `which` is `IGRAPH_LAPACK_DSYEV_INTERVAL`, then this is the lower bound of the interval to be searched for eigenvalues. See also the `vestimate` argument.
`vu`: | If `which` is `IGRAPH_LAPACK_DSYEV_INTERVAL`, then this is the upper bound of the interval to be searched for eigenvalues. See also the `vestimate` argument.
`vestimate`: | An upper bound for the number of eigenvalues in the (vl,vu] interval, if `which` is `IGRAPH_LAPACK_DSYEV_INTERVAL`. Memory is allocated only for the given number of eigenvalues (and eigenvectors), so this upper bound must be correct.
`il`: | The index of the smallest eigenvalue to return, if `which` is `IGRAPH_LAPACK_DSYEV_SELECT`.
`iu`: | The index of the largets eigenvalue to return, if `which` is `IGRAPH_LAPACK_DSYEV_SELECT`.
`abstol`: | The absolute error tolerance for the eigevalues. An approximate eigenvalue is accepted as converged when it is determined to lie in an interval [a,b] of width less than or equal to abstol + EPS * max(|a|,|b|), where EPS is the machine precision.
`values`: | An initialized vector, the eigenvalues are stored here, unless it is a null pointer. It will be resized as needed.
`vectors`: | An initialized matrix, the eigenvectors are stored in its columns, unless it is a null pointer. It will be resized as needed.
`support`: | An integer vector. If not a null pointer, then it will be resized to (2*max(1,M)) (M is a the total number of eigenvalues found). Then the support of the eigenvectors in `vectors` is stored here, i.e., the indices indicating the nonzero elements in `vectors`. The i-th eigenvector is nonzero only in elements support(2*i-1) through support(2*i).

Returns:

`` | Error code.

Time complexity: TODO.

Example 29.5. File `examples/simple/igraph_lapack_dsyevr.c`

    #include <igraph.h>

    int main(void) {

        igraph_matrix_t A;
        igraph_matrix_t vectors;
        igraph_vector_t values;

        igraph_matrix_init(&A, 2, 2);
        igraph_matrix_init(&vectors, 0, 0);
        igraph_vector_init(&values, 0);

        MATRIX(A, 0, 0) = 2.0;
        MATRIX(A, 0, 1) = -1.0;
        MATRIX(A, 1, 0) = -1.0;
        MATRIX(A, 1, 1) = 3.0;

        printf("Take a subset:\n");

        igraph_lapack_dsyevr(&A, IGRAPH_LAPACK_DSYEV_SELECT, /*vl=*/ 0, /*vu=*/ 0,
                             /*vestimate=*/ 0, /*il=*/ 1, /*iu=*/ 1,
                             /*abstol=*/ 1e-10, &values, &vectors,
                             /*support=*/ 0);
        printf("eigenvalues:\n");
        igraph_vector_print(&values);
        printf("eigenvectors:\n");
        igraph_matrix_print(&vectors);

        printf("\nTake a subset based on an interval:\n");

        igraph_lapack_dsyevr(&A, IGRAPH_LAPACK_DSYEV_INTERVAL, /*vl*/ 3, /*vu*/ 4,
                             /*vestimate=*/ 1, /*il=*/ 0, /*iu=*/ 0,
                             /*abstol=*/ 1e-10, &values, &vectors,
                             /*support=*/ 0);

        printf("eigenvalues:\n");
        igraph_vector_print(&values);
        printf("eigenvectors:\n");
        igraph_matrix_print(&vectors);

        igraph_vector_destroy(&values);
        igraph_matrix_destroy(&vectors);
        igraph_matrix_destroy(&A);

        return 0;
    }

#### 2.2.2. `igraph_lapack_dgeev` — Eigenvalues and optionally eigenvectors of a non-symmetric matrix.

    igraph_error_t igraph_lapack_dgeev(const igraph_matrix_t *A,
                            igraph_vector_t *valuesreal,
                            igraph_vector_t *valuesimag,
                            igraph_matrix_t *vectorsleft,
                            igraph_matrix_t *vectorsright,
                            int *info);

This function calls LAPACK to compute, for an N-by-N real nonsymmetric matrix A, the eigenvalues and, optionally, the left and/or right eigenvectors.

The right eigenvector v(j) of A satisfies A * v(j) = lambda(j) * v(j) where lambda(j) is its eigenvalue. The left eigenvector u(j) of A satisfies u(j)^H * A = lambda(j) * u(j)^H where u(j)^H denotes the conjugate transpose of u(j).

The computed eigenvectors are normalized to have Euclidean norm equal to 1 and largest component real.

Arguments:

`A`: | matrix. On entry it contains the N-by-N input matrix.
`valuesreal`: | Pointer to an initialized vector, or a null pointer. If not a null pointer, then the real parts of the eigenvalues are stored here. The vector will be resized as needed.
`valuesimag`: | Pointer to an initialized vector, or a null pointer. If not a null pointer, then the imaginary parts of the eigenvalues are stored here. The vector will be resized as needed.
`vectorsleft`: | Pointer to an initialized matrix, or a null pointer. If not a null pointer, then the left eigenvectors are stored in the columns of the matrix. The matrix will be resized as needed.
`vectorsright`: | Pointer to an initialized matrix, or a null pointer. If not a null pointer, then the right eigenvectors are stored in the columns of the matrix. The matrix will be resized as needed.

`info`:

This argument is used for two purposes. As an input argument it gives whether an igraph error should be generated if the QR algorithm fails to compute all eigenvalues. If `info` is non-zero, then an error is generated, otherwise only a warning is given. On exit it contains the LAPACK error code. Zero means successful exit. A negative values means that some of the arguments had an illegal value, this always triggers an igraph error. An i positive value means that the QR algorithm failed to compute all the eigenvalues, and no eigenvectors have been computed; element i+1:N of `valuesreal` and `valuesimag` contain eigenvalues which have converged. This case only generates an igraph error, if `info` was non-zero on entry.

Returns:

`` | Error code.

Time complexity: TODO.

Example 29.6. File `examples/simple/igraph_lapack_dgeev.c`

    #include <igraph.h>
    #include <stdio.h>

    int main(void) {

        igraph_matrix_t A;
        igraph_matrix_t vectors_left, vectors_right;
        igraph_vector_t values_real, values_imag;
        igraph_vector_complex_t values;
        int info = 1;

        igraph_matrix_init(&A, 2, 2);
        igraph_matrix_init(&vectors_left, 0, 0);
        igraph_matrix_init(&vectors_right, 0, 0);
        igraph_vector_init(&values_real, 0);
        igraph_vector_init(&values_imag, 0);
        MATRIX(A, 0, 0) = 1.0;
        MATRIX(A, 0, 1) = 1.0;
        MATRIX(A, 1, 0) = -1.0;
        MATRIX(A, 1, 1) = 1.0;

        igraph_lapack_dgeev(&A, &values_real, &values_imag,
                            &vectors_left, &vectors_right, &info);
        igraph_vector_complex_create(&values, &values_real, &values_imag);
        printf("eigenvalues:\n");
        igraph_vector_complex_print(&values);
        printf("left eigenvectors:\n");
        igraph_matrix_print(&vectors_left);
        printf("right eigenvectors:\n");
        igraph_matrix_print(&vectors_right);

        igraph_vector_destroy(&values_imag);
        igraph_vector_destroy(&values_real);
        igraph_vector_complex_destroy(&values);
        igraph_matrix_destroy(&vectors_right);
        igraph_matrix_destroy(&vectors_left);
        igraph_matrix_destroy(&A);

        return 0;
    }

#### 2.2.3. `igraph_lapack_dgeevx` — Eigenvalues/vectors of nonsymmetric matrices, expert mode.

    igraph_error_t igraph_lapack_dgeevx(igraph_lapack_dgeevx_balance_t balance,
                             const igraph_matrix_t *A,
                             igraph_vector_t *valuesreal,
                             igraph_vector_t *valuesimag,
                             igraph_matrix_t *vectorsleft,
                             igraph_matrix_t *vectorsright,
                             int *ilo, int *ihi, igraph_vector_t *scale,
                             igraph_real_t *abnrm,
                             igraph_vector_t *rconde,
                             igraph_vector_t *rcondv,
                             int *info);

This function calculates the eigenvalues and optionally the left and/or right eigenvectors of a nonsymmetric N-by-N real matrix.

Optionally also, it computes a balancing transformation to improve the conditioning of the eigenvalues and eigenvectors (`ilo`, `ihi`, `scale`, and `abnrm`), reciprocal condition numbers for the eigenvalues (`rconde`), and reciprocal condition numbers for the right eigenvectors (`rcondv`).

The right eigenvector v(j) of A satisfies A * v(j) = lambda(j) * v(j) where lambda(j) is its eigenvalue. The left eigenvector u(j) of A satisfies u(j)^H * A = lambda(j) * u(j)^H where u(j)^H denotes the conjugate transpose of u(j).

The computed eigenvectors are normalized to have Euclidean norm equal to 1 and largest component real.

Balancing a matrix means permuting the rows and columns to make it more nearly upper triangular, and applying a diagonal similarity transformation D * A * D^(-1), where D is a diagonal matrix, to make its rows and columns closer in norm and the condition numbers of its eigenvalues and eigenvectors smaller. The computed reciprocal condition numbers correspond to the balanced matrix. Permuting rows and columns will not change the condition numbers (in exact arithmetic) but diagonal scaling will. For further explanation of balancing, see section 4.10.2 of the LAPACK Users' Guide. Note that the eigenvectors obtained for the balanced matrix are backtransformed to those of `A`.

Arguments:

`balance`:

Scalar that indicated, whether the input matrix should be balanced. Possible values:

`IGRAPH_LAPACK_DGEEVX_BALANCE_NONE` | no not diagonally scale or permute.
`IGRAPH_LAPACK_DGEEVX_BALANCE_PERM` | perform permutations to make the matrix more nearly upper triangular. Do not diagonally scale.
`IGRAPH_LAPACK_DGEEVX_BALANCE_SCALE` | diagonally scale the matrix, i.e. replace A by D*A*D^(-1), where D is a diagonal matrix, chosen to make the rows and columns of A more equal in norm. Do not permute.
`IGRAPH_LAPACK_DGEEVX_BALANCE_BOTH` | both diagonally scale and permute A.

`A`: | The input matrix, must be square.
`valuesreal`: | An initialized vector, or a NULL pointer. If not a NULL pointer, then the real parts of the eigenvalues are stored here. The vector will be resized, as needed.
`valuesimag`: | An initialized vector, or a NULL pointer. If not a NULL pointer, then the imaginary parts of the eigenvalues are stored here. The vector will be resized, as needed.

`vectorsleft`:

An initialized matrix or a NULL pointer. If not a null pointer, then the left eigenvectors are stored here. The order corresponds to the eigenvalues and the eigenvectors are stored in a compressed form. If the j-th eigenvalue is real then column j contains the corresponding eigenvector. If the j-th and (j+1)-th eigenvalues form a complex conjugate pair, then the j-th and (j+1)-th columns contain the real and imaginary parts of the corresponding eigenvectors.

`vectorsright`: | An initialized matrix or a NULL pointer. If not a null pointer, then the right eigenvectors are stored here. The format is the same, as for the `vectorsleft` argument.
`ilo`: |
`ihi`: | if not NULL, `ilo` and `ihi` point to integer values determined when A was balanced. The balanced A(i,j) = 0 if I>J and J=1,...,ilo-1 or I=ihi+1,...,N.

`scale`:

Pointer to an initialized vector or a NULL pointer. If not a NULL pointer, then details of the permutations and scaling factors applied when balancing `A`, are stored here. If P(j) is the index of the row and column interchanged with row and column j, and D(j) is the scaling factor applied to row and column j, then

`scale(J) = P(J), for J = 1,...,ilo-1` |
`scale(J) = D(J), for J = ilo,...,ihi` |
`scale(J) = P(J) for J = ihi+1,...,N.` |

The order in which the interchanges are made is N to `ihi`+1, then 1 to `ilo`-1.

`abnrm`: | Pointer to a real variable, the one-norm of the balanced matrix is stored here. (The one-norm is the maximum of the sum of absolute values of elements in any column.)
`rconde`: | An initialized vector or a NULL pointer. If not a null pointer, then the reciprocal condition numbers of the eigenvalues are stored here.
`rcondv`: | An initialized vector or a NULL pointer. If not a null pointer, then the reciprocal condition numbers of the right eigenvectors are stored here.

`info`:

This argument is used for two purposes. As an input argument it gives whether an igraph error should be generated if the QR algorithm fails to compute all eigenvalues. If `info` is non-zero, then an error is generated, otherwise only a warning is given. On exit it contains the LAPACK error code. Zero means successful exit. A negative values means that some of the arguments had an illegal value, this always triggers an igraph error. An i positive value means that the QR algorithm failed to compute all the eigenvalues, and no eigenvectors have been computed; element i+1:N of `valuesreal` and `valuesimag` contain eigenvalues which have converged. This case only generated an igraph error, if `info` was non-zero on entry.

Returns:

`` | Error code.

Time complexity: TODO

Example 29.7. File `examples/simple/igraph_lapack_dgeevx.c`

    #include <igraph.h>
    #include <stdio.h>

    void matrix_to_complex_vectors(igraph_vector_complex_t *c1, igraph_vector_complex_t *c2, igraph_matrix_t *m) {
        IGRAPH_REAL(VECTOR(*c1)[0]) = MATRIX(*m, 0, 0);
        IGRAPH_REAL(VECTOR(*c1)[1]) = MATRIX(*m, 1, 0);
        IGRAPH_IMAG(VECTOR(*c1)[0]) = MATRIX(*m, 0, 1);
        IGRAPH_IMAG(VECTOR(*c1)[1]) = MATRIX(*m, 1, 1);

        IGRAPH_REAL(VECTOR(*c2)[0]) = MATRIX(*m, 0, 0);
        IGRAPH_REAL(VECTOR(*c2)[1]) = MATRIX(*m, 1, 0);
        IGRAPH_IMAG(VECTOR(*c2)[0]) = -MATRIX(*m, 0, 1);
        IGRAPH_IMAG(VECTOR(*c2)[1]) = -MATRIX(*m, 1, 1);
    }

    int main(void) {

        igraph_matrix_t A;
        igraph_matrix_t vectors_left, vectors_right;
        igraph_vector_t values_real, values_imag;
        igraph_vector_complex_t values;
        igraph_vector_complex_t eigenvector1;
        igraph_vector_complex_t eigenvector2;
        int info = 1;
        igraph_real_t abnrm;

        igraph_matrix_init(&A, 2, 2);
        igraph_matrix_init(&vectors_left, 0, 0);
        igraph_matrix_init(&vectors_right, 0, 0);
        igraph_vector_init(&values_real, 0);
        igraph_vector_init(&values_imag, 0);
        igraph_vector_complex_init(&eigenvector1, 2);
        igraph_vector_complex_init(&eigenvector2, 2);
        MATRIX(A, 0, 0) = 1.0;
        MATRIX(A, 0, 1) = 1.0;
        MATRIX(A, 1, 0) = -1.0;
        MATRIX(A, 1, 1) = 1.0;

        igraph_lapack_dgeevx(IGRAPH_LAPACK_DGEEVX_BALANCE_BOTH,
                             &A, &values_real, &values_imag,
                             &vectors_left, &vectors_right, NULL, NULL,
                             /*scale=*/ NULL, &abnrm, /*rconde=*/ NULL,
                             /*rcondv=*/ NULL, &info);

        igraph_vector_complex_create(&values, &values_real, &values_imag);
        printf("eigenvalues:\n");
        igraph_vector_complex_print(&values);

        printf("\nleft eigenvectors:\n");
        /*matrix_to_complex_vectors only works because we have two complex
          conjugate eigenvalues */
        matrix_to_complex_vectors(&eigenvector1, &eigenvector2, &vectors_left);
        igraph_vector_complex_print(&eigenvector1);
        igraph_vector_complex_print(&eigenvector2);

        printf("\nright eigenvectors:\n");
        matrix_to_complex_vectors(&eigenvector1, &eigenvector2, &vectors_right);
        igraph_vector_complex_print(&eigenvector1);
        igraph_vector_complex_print(&eigenvector2);
        printf("\nOne-norm of the balanced matrix:\n%g\n", abnrm);

        igraph_vector_destroy(&values_imag);
        igraph_vector_destroy(&values_real);
        igraph_vector_complex_destroy(&values);
        igraph_vector_complex_destroy(&eigenvector1);
        igraph_vector_complex_destroy(&eigenvector2);
        igraph_matrix_destroy(&vectors_right);
        igraph_matrix_destroy(&vectors_left);
        igraph_matrix_destroy(&A);

        return 0;
    }

## 3. ARPACK interface in igraph

ARPACK is a library for solving large scale eigenvalue problems. The package is designed to compute a few eigenvalues and corresponding eigenvectors of a general `n` by `n` matrix `A`. It is most appropriate for large sparse or structured matrices `A` where structured means that a matrix-vector product `w <- Av` requires order `n` rather than the usual order `n^2` floating point operations. Please see http://www.caam.rice.edu/software/ARPACK/ for details.

The eigenvalue calculation in ARPACK (in the simplest case) involves the calculation of the `Av` product where `A` is the matrix we work with and `v` is an arbitrary vector. A user-defined function of type `igraph_arpack_function_t` is expected to perform this product. If the product can be done efficiently, e.g. if the matrix is sparse, then ARPACK is usually able to calculate the eigenvalues very quickly.

In igraph, eigenvalue/eigenvector calculations usually involve the following steps:

1. Initialization of an `igraph_arpack_options_t` data structure using `igraph_arpack_options_init`.

2. Setting some options in the initialized `igraph_arpack_options_t` object.

3. Defining a function of type `igraph_arpack_function_t`. The input of this function is a vector, and the output should be the output matrix multiplied by the input vector.

4. Calling `igraph_arpack_rssolve()` (is the matrix is symmetric), or `igraph_arpack_rnsolve()`.

The `igraph_arpack_options_t` object can be used multiple times.

If we have many eigenvalue problems to solve, then it might worth to create an `igraph_arpack_storage_t` object, and initialize it via `igraph_arpack_storage_init()`. This structure contains all memory needed for ARPACK (with the given upper limit regerding to the size of the eigenvalue problem). Then many problems can be solved using the same `igraph_arpack_storage_t` object, without always reallocating the required memory. The `igraph_arpack_storage_t` object needs to be destroyed by calling `igraph_arpack_storage_destroy()` on it, when it is not needed any more.

igraph does not contain all ARPACK routines, only the ones dealing with symmetric and non-symmetric eigenvalue problems using double precision real numbers.

### 3.1. Data structures

#### 3.1.1. `igraph_arpack_options_t` — Options for ARPACK.

    typedef struct igraph_arpack_options_t {
        /* INPUT */
        char bmat[1];          /* I-standard problem, G-generalized */
        int n;                 /* Dimension of the eigenproblem */
        char which[2];         /* LA, SA, LM, SM, BE */
        int nev;               /* Number of eigenvalues to be computed */
        igraph_real_t tol;     /* Stopping criterion */
        int ncv;               /* Number of columns in V */
        int ldv;               /* Leading dimension of V */
        int ishift;            /* 0-reverse comm., 1-exact with tridiagonal */
        int mxiter;            /* Maximum number of update iterations to take */
        int nb;                /* Block size on the recurrence, only 1 works */
        int mode;              /* The kind of problem to be solved (1-5)
                                   1: A*x=l*x, A symmetric
                                   2: A*x=l*M*x, A symm. M pos. def.
                                   3: K*x = l*M*x, K symm., M pos. semidef.
                                   4: K*x = l*KG*x, K s. pos. semidef. KG s. indef.
                                   5: A*x = l*M*x, A symm., M symm. pos. semidef. */
        int start;             /* 0: random, 1: use the supplied vector */
        int lworkl;            /* Size of temporary storage, default is fine */
        igraph_real_t sigma;   /* The shift for modes 3,4,5 */
        igraph_real_t sigmai;  /* The imaginary part of shift for rnsolve */
        /* OUTPUT */
        int info;              /* What happened, see docs */
        int ierr;              /* What happened  in the dseupd call */
        int noiter;            /* The number of iterations taken */
        int nconv;
        int numop;             /* Number of OP*x operations */
        int numopb;            /* Number of B*x operations if BMAT='G' */
        int numreo;            /* Number of steps of re-orthogonalizations */
        /* INTERNAL */
        int iparam[11];
        int ipntr[14];
    } igraph_arpack_options_t;

This data structure contains the options of the ARPACK eigenvalue solver routines. It must be initialized by calling `igraph_arpack_options_init()` on it. Then it can be used for multiple ARPACK calls, as the ARPACK solvers do not modify it. Input options:

Values:

`bmat`: | Character. Whether to solve a standard ('I') ot a generalized problem ('B').
`n`: | Dimension of the eigenproblem.

`which`:

Specifies which eigenvalues/vectors to compute. Possible values for symmetric matrices:

`LA` | Compute `nev` largest (algebraic) eigenvalues.
`SA` | Compute `nev` smallest (algebraic) eigenvalues.
`LM` | Compute `nev` largest (in magnitude) eigenvalues.
`SM` | Compute `nev` smallest (in magnitude) eigenvalues.
`BE` | Compute `nev` eigenvalues, half from each end of the spectrum. When `nev` is odd, compute one more from the high en than from the low end.

Possible values for non-symmetric matrices:

`LM` | Compute `nev` largest (in magnitude) eigenvalues.
`SM` | Compute `nev` smallest (in magnitude) eigenvalues.
`LR` | Compute `nev` eigenvalues of largest real part.
`SR` | Compute `nev` eigenvalues of smallest real part.
`LI` | Compute `nev` eigenvalues of largest imaginary part.
`SI` | Compute `nev` eigenvalues of smallest imaginary part.

`nev`: | The number of eigenvalues to be computed.
`tol`: | Stopping criterion: the relative accuracy of the Ritz value is considered acceptable if its error is less than `tol` times its estimated value. If this is set to zero then machine precision is used.
`ncv`: | Number of Lanczos vectors to be generated. Setting this to zero means that `igraph_arpack_rssolve` and `igraph_arpack_rnsolve` will determine a suitable value for `ncv` automatically.
`ldv`: | Numberic scalar. It should be set to zero in the current igraph implementation.
`ishift`: | Either zero or one. If zero then the shifts are provided by the user via reverse communication. If one then exact shifts with respect to the reduced tridiagonal matrix `T`. Please always set this to one.
`mxiter`: | Maximum number of Arnoldi update iterations allowed.
`nb`: | Blocksize to be used in the recurrence. Please always leave this on the default value, one.

`mode`:

The type of the eigenproblem to be solved. Possible values if the input matrix is symmetric:

1. A*x=lambda*x, A is symmetric.

2. A*x=lambda*M*x, A is symmetric, M is symmetric positive definite.

3. K*x=lambda*M*x, K is symmetric, M is symmetric positive semi-definite.

4. K*x=lambda*KG*x, K is symmetric positive semi-definite, KG is symmetric indefinite.

5. A*x=lambda*M*x, A is symmetric, M is symmetric positive semi-definite. (Cayley transformed mode.)

Please note that only `mode` ==1 was tested and other values might not work properly. Possible values if the input matrix is not symmetric:

1. A*x=lambda*x.

2. A*x=lambda*M*x, M is symmetric positive definite.

3. A*x=lambda*M*x, M is symmetric semi-definite.

4. A*x=lambda*M*x, M is symmetric semi-definite.

Please note that only `mode` == 1 was tested and other values might not work properly.

`start`: | Whether to use the supplied starting vector (1), or use a random starting vector (0). The starting vector must be supplied in the first column of the `vectors` argument of the `igraph_arpack_rssolve()` of `igraph_arpack_rnsolve()` call.

Output options:

Values:

`info`:

Error flag of ARPACK. Possible values:

`0` | Normal exit.
`1` | Maximum number of iterations taken.
`3` | No shifts could be applied during a cycle of the Implicitly restarted Arnoldi iteration. One possibility is to increase the size of `ncv` relative to `nev`.

ARPACK can return other error flags as well, but these are converted to igraph errors, see `igraph_error_type_t`.

`ierr`: | Error flag of the second ARPACK call (one eigenvalue computation usually involves two calls to ARPACK). This is always zero, as other error codes are converted to igraph errors.
`noiter`: | Number of Arnoldi iterations taken.
`nconv`: | Number of converged Ritz values. This represents the number of Ritz values that satisfy the convergence critetion.
`numop`: | Total number of matrix-vector multiplications.
`numopb`: | Not used currently.
`numreo`: | Total number of steps of re-orthogonalization.

Internal options:

Values:

`lworkl`: | Do not modify this option.
`sigma`: | The shift for the shift-invert mode.
`sigmai`: | The imaginary part of the shift, for the non-symmetric or complex shift-invert mode.
`iparam`: | Do not modify this option.
`ipntr`: | Do not modify this option.

#### 3.1.2. `igraph_arpack_storage_t` — Storage for ARPACK.

    typedef struct igraph_arpack_storage_t {
        int maxn, maxncv, maxldv;
        igraph_real_t *v;
        igraph_real_t *workl;
        igraph_real_t *workd;
        igraph_real_t *d;
        igraph_real_t *resid;
        igraph_real_t *ax;
        int *select;
        /* The following two are only used for non-symmetric problems: */
        igraph_real_t *di;
        igraph_real_t *workev;
    } igraph_arpack_storage_t;

Public members, do not modify them directly, these are considered to be read-only.

Values:

`maxn`: | Maximum rank of matrix.
`maxncv`: | Maximum NCV.
`maxldv`: | Maximum LDV.

These members are considered to be private:

Values:

`workl`: | Working memory.
`workd`: | Working memory.
`d`: | Memory for eigenvalues.
`resid`: | Memory for residuals.
`ax`: | Working memory.
`select`: | Working memory.
`di`: | Memory for eigenvalues, non-symmetric case only.
`workev`: | Working memory, non-symmetric case only.

#### 3.1.3. `igraph_arpack_function_t` — Type of the ARPACK callback function.

    typedef igraph_error_t igraph_arpack_function_t(igraph_real_t *to, const igraph_real_t *from,
                                         int n, void *extra);

Arguments:

`to`: | Pointer to an `igraph_real_t`, the result of the matrix-vector product is expected to be stored here.
`from`: | Pointer to an `igraph_real_t`, the input matrix should be multiplied by the vector stored here.
`n`: | The length of the vector (which is the same as the order of the input matrix).
`extra`: | Extra argument to the matrix-vector calculation function. This is coming from the `igraph_arpack_rssolve()` or `igraph_arpack_rnsolve()` function.

Returns:

`` | Error code. If not `IGRAPH_SUCCESS`, then the ARPACK solver considers this as an error, stops and calls the igraph error handler.

#### 3.1.4. `igraph_arpack_options_init` — Initialize ARPACK options.

    void igraph_arpack_options_init(igraph_arpack_options_t *o);

Initializes ARPACK options, set them to default values. You can always pass the initialized `igraph_arpack_options_t` object to built-in igraph functions without any modification. The built-in igraph functions modify the options to perform their calculation, e.g. `igraph_pagerank()` always searches for the eigenvalue with the largest magnitude, regardless of the supplied value.

If you want to implement your own function involving eigenvalue calculation using ARPACK, however, you will likely need to set up the fields for yourself.

Arguments:

`o`: | The `igraph_arpack_options_t` object to initialize.

Time complexity: O(1).

#### 3.1.5. `igraph_arpack_storage_init` — Initialize ARPACK storage.

    igraph_error_t igraph_arpack_storage_init(igraph_arpack_storage_t *s, igraph_integer_t maxn,
                                   igraph_integer_t maxncv, igraph_integer_t maxldv,
                                   igraph_bool_t symm);

You only need this function if you want to run multiple eigenvalue calculations using ARPACK, and want to spare the memory allocation/deallocation between each two runs. Otherwise it is safe to supply a null pointer as the `storage` argument of both `igraph_arpack_rssolve()` and `igraph_arpack_rnsolve()` to make memory allocated and deallocated automatically.

Don't forget to call the `igraph_arpack_storage_destroy()` function on the storage object if you don't need it any more.

Arguments:

`s`: | The `igraph_arpack_storage_t` object to initialize.
`maxn`: | The maximum order of the matrices.
`maxncv`: | The maximum NCV parameter intended to use.
`maxldv`: | The maximum LDV parameter intended to use.
`symm`: | Whether symmetric or non-symmetric problems will be solved using this `igraph_arpack_storage_t`. (You cannot use the same storage both with symmetric and non-symmetric solvers.)

Returns:

`` | Error code.

Time complexity: O(maxncv*(maxldv+maxn)).

#### 3.1.6. `igraph_arpack_storage_destroy` — Deallocate ARPACK storage.

    void igraph_arpack_storage_destroy(igraph_arpack_storage_t *s);

Arguments:

`s`: | The `igraph_arpack_storage_t` object for which the memory will be deallocated.

Time complexity: operating system dependent.

### 3.2. ARPACK solvers

#### 3.2.1. `igraph_arpack_rssolve` — ARPACK solver for symmetric matrices.

    igraph_error_t igraph_arpack_rssolve(igraph_arpack_function_t *fun, void *extra,
                              igraph_arpack_options_t *options,
                              igraph_arpack_storage_t *storage,
                              igraph_vector_t *values, igraph_matrix_t *vectors);

This is the ARPACK solver for symmetric matrices. Please use `igraph_arpack_rnsolve()` for non-symmetric matrices.

Arguments:

`fun`: | Pointer to an `igraph_arpack_function_t` object, the function that performs the matrix-vector multiplication.
`extra`: | An extra argument to be passed to `fun`.
`options`: | An `igraph_arpack_options_t` object.
`storage`: | An `igraph_arpack_storage_t` object, or a null pointer. In the latter case memory allocation and deallocation is performed automatically. Either this or the `vectors` argument must be non-null if the ARPACK iteration is started from a given starting vector. If both are given `vectors` take precedence.
`values`: | If not a null pointer, then it should be a pointer to an initialized vector. The eigenvalues will be stored here. The vector will be resized as needed.
`vectors`: | If not a null pointer, then it must be a pointer to an initialized matrix. The eigenvectors will be stored in the columns of the matrix. The matrix will be resized as needed. Either this or the `storage` argument must be non-null if the ARPACK iteration is started from a given starting vector. If both are given `vectors` take precedence.

Returns:

`` | Error code.

Time complexity: depends on the matrix-vector multiplication. Usually a small number of iterations is enough, so if the matrix is sparse and the matrix-vector multiplication can be done in O(n) time (the number of vertices), then the eigenvalues are found in O(n) time as well.

#### 3.2.2. `igraph_arpack_rnsolve` — ARPACK solver for non-symmetric matrices.

    igraph_error_t igraph_arpack_rnsolve(igraph_arpack_function_t *fun, void *extra,
                              igraph_arpack_options_t *options,
                              igraph_arpack_storage_t *storage,
                              igraph_matrix_t *values, igraph_matrix_t *vectors);

Please always consider calling `igraph_arpack_rssolve()` if your matrix is symmetric, it is much faster. `igraph_arpack_rnsolve()` for non-symmetric matrices.

Note that ARPACK is not called for 2x2 matrices as an exact algebraic solution exists in these cases.

Arguments:

`fun`: | Pointer to an `igraph_arpack_function_t` object, the function that performs the matrix-vector multiplication.
`extra`: | An extra argument to be passed to `fun`.
`options`: | An `igraph_arpack_options_t` object.
`storage`: | An `igraph_arpack_storage_t` object, or a null pointer. In the latter case memory allocation and deallocation is performed automatically.
`values`: | If not a null pointer, then it should be a pointer to an initialized matrix. The (possibly complex) eigenvalues will be stored here. The matrix will have two columns, the first column contains the real, the second the imaginary parts of the eigenvalues. The matrix will be resized as needed.

`vectors`:

If not a null pointer, then it must be a pointer to an initialized matrix. The eigenvectors will be stored in the columns of the matrix. The matrix will be resized as needed. Note that real eigenvalues will have real eigenvectors in a single column in this matrix; however, complex eigenvalues come in conjugate pairs and the result matrix will store the eigenvector corresponding to the eigenvalue with positive imaginary part only. Since in this case the eigenvector is also complex, it will occupy two columns in the eigenvector matrix (the real and the imaginary parts, in this order). Caveat: if the eigenvalue vector returns only the eigenvalue with the negative imaginary part for a complex conjugate eigenvalue pair, the result vector will still store the eigenvector corresponding to the eigenvalue with the positive imaginary part (since this is how ARPACK works).

Returns:

`` | Error code.

Time complexity: depends on the matrix-vector multiplication. Usually a small number of iterations is enough, so if the matrix is sparse and the matrix-vector multiplication can be done in O(n) time (the number of vertices), then the eigenvalues are found in O(n) time as well.

#### 3.2.3. `igraph_arpack_unpack_complex` — Makes the result of the non-symmetric ARPACK solver more readable.

    igraph_error_t igraph_arpack_unpack_complex(igraph_matrix_t *vectors, igraph_matrix_t *values,
                                     igraph_integer_t nev);

This function works on the output of `igraph_arpack_rnsolve` and brushes it up a bit: it only keeps `nev` eigenvalues/vectors and every eigenvector is stored in two columns of the `vectors` matrix.

The output of the non-symmetric ARPACK solver is somewhat hard to parse, as real eigenvectors occupy only one column in the matrix, and the complex conjugate eigenvectors are not stored at all (usually). The other problem is that the solver might return more eigenvalues than requested. The common use of this function is to call it directly after `igraph_arpack_rnsolve` with its `vectors` and `values` argument and `options`->nev as `nev`. This will add the vectors for eigenvalues with a negative imaginary part and return all vectors as 2 columns, a real and imaginary part.

Arguments:

`vectors`: | The eigenvector matrix, as returned by `igraph_arpack_rnsolve`. It will be resized, typically it will be larger.
`values`: | The eigenvalue matrix, as returned by `igraph_arpack_rnsolve`. It will be resized, typically extra, unneeded rows (=eigenvalues) will be removed.
`nev`: | The number of eigenvalues/vectors to keep. Can be less or equal than the number originally requested from ARPACK.

Returns:

`` | Error code.

Time complexity: linear in the number of elements in the `vectors` matrix.