Developer Reference

Intel® oneAPI Math Kernel Library LAPACK Examples

ID 766877
Date 3/31/2023
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

CSYSV Example Program in C

/*******************************************************************************
*  Copyright (C) 2009-2015 Intel Corporation. All Rights Reserved.
*  The information and material ("Material") provided below is owned by Intel
*  Corporation or its suppliers or licensors, and title to such Material remains
*  with Intel Corporation or its suppliers or licensors. The Material contains
*  proprietary information of Intel or its suppliers and licensors. The Material
*  is protected by worldwide copyright laws and treaty provisions. No part of
*  the Material may be copied, reproduced, published, uploaded, posted,
*  transmitted, or distributed in any way without Intel's prior express written
*  permission. No license under any patent, copyright or other intellectual
*  property rights in the Material is granted to or conferred upon you, either
*  expressly, by implication, inducement, estoppel or otherwise. Any license
*  under such intellectual property rights must be express and approved by Intel
*  in writing.
*
********************************************************************************
*/
/*
   CSYSV Example.
   ==============
 
   The program computes the solution to the system of linear equations
   with a complex symmetric matrix A and multiple right-hand sides B,
   where A is the coefficient matrix:

   (  9.99, -4.73) ( -5.68, -0.80) ( -8.94,  1.32) ( -9.42,  2.05)
   ( -5.68, -0.80) ( -8.01,  4.61) (  1.64, -6.29) (  6.79, -2.17)
   ( -8.94,  1.32) (  1.64, -6.29) (  9.04,  3.96) ( -4.51, -7.54)
   ( -9.42,  2.05) (  6.79, -2.17) ( -4.51, -7.54) (  0.40,  4.06)

   and B is the right-hand side matrix:
 
   (  5.71, -1.20) (  2.84, -0.18)
   ( -7.70,  6.47) ( -8.29, -1.72)
   (  3.77, -7.40) ( -4.28, -8.25)
   ( -3.78,  0.33) ( -2.70, -0.39)
 
   Description.
   ============
 
   The routine solves for X the complex system of linear equations A*X = B,
   where A is an n-by-n symmetric matrix, the columns of matrix B are
   individual right-hand sides, and the columns of X are the corresponding
   solutions.

   The diagonal pivoting method is used to factor A as A = U*D*UT or
   A = L*D*LT , where U (or L) is a product of permutation and unit upper
   (lower) triangular matrices, and D is symmetric and block diagonal with
   1-by-1 and 2-by-2 diagonal blocks.

   The factored form of A is then used to solve the system of equations A*X = B.

   Example Program Results.
   ========================
 
 CSYSV Example Program Results

 Solution
 (  0.13,  0.13) (  0.63,  0.34)
 (  0.32, -0.07) (  0.61,  0.21)
 ( -0.26, -0.44) ( -0.01, -0.10)
 ( -0.40,  0.51) (  0.21,  0.02)

 Details of factorization
 (-16.42,  1.69) ( -0.53,  0.35) (  0.36,  0.41) ( -0.78,  0.49)
 (  0.00,  0.00) (  3.69,  0.64) (-16.58, -1.61) ( -0.10, -0.65)
 (  0.00,  0.00) (  0.00,  0.00) (  1.02, -3.74) ( -0.73, -0.52)
 (  0.00,  0.00) (  0.00,  0.00) (  0.00,  0.00) (  9.04,  3.96)

 Pivot indices
      1     -1     -1      3
*/
#include <stdlib.h>
#include <stdio.h>

/* Complex datatype */
struct _fcomplex { float re, im; };
typedef struct _fcomplex fcomplex;

/* CSYSV prototype */
extern void csysv( char* uplo, int* n, int* nrhs, fcomplex* a, int* lda,
                int* ipiv, fcomplex* b, int* ldb, fcomplex* work, int* lwork, int* info );
/* Auxiliary routines prototypes */
extern void print_matrix( char* desc, int m, int n, fcomplex* a, int lda );
extern void print_int_vector( char* desc, int n, int* a );

/* Parameters */
#define N 4
#define NRHS 2
#define LDA N
#define LDB N

/* Main program */
int main() {
        /* Locals */
        int n = N, nrhs = NRHS, lda = LDA, ldb = LDB, info, lwork;
        fcomplex wkopt;
        fcomplex* work;
        /* Local arrays */
        int ipiv[N];
        fcomplex a[LDA*N] = {
           { 9.99f, -4.73f}, { 0.00f,  0.00f}, { 0.00f,  0.00f}, { 0.00f,  0.00f},
           {-5.68f, -0.80f}, {-8.01f,  4.61f}, { 0.00f,  0.00f}, { 0.00f,  0.00f},
           {-8.94f,  1.32f}, { 1.64f, -6.29f}, { 9.04f,  3.96f}, { 0.00f,  0.00f},
           {-9.42f,  2.05f}, { 6.79f, -2.17f}, {-4.51f, -7.54f}, { 0.40f,  4.06f}
        };
        fcomplex b[LDB*NRHS] = {
           { 5.71f, -1.20f}, {-7.70f,  6.47f}, { 3.77f, -7.40f}, {-3.78f,  0.33f},
           { 2.84f, -0.18f}, {-8.29f, -1.72f}, {-4.28f, -8.25f}, {-2.70f, -0.39f}
        };
        /* Executable statements */
        printf( " CSYSV Example Program Results\n" );
        /* Query and allocate the optimal workspace */
        lwork = -1;
        csysv( "Upper", &n, &nrhs, a, &lda, ipiv, b, &ldb, &wkopt, &lwork, &info );
        lwork = (int)wkopt.re;
        work = (fcomplex*)malloc( lwork*sizeof(fcomplex) );
        /* Solve the equations A*X = B */
        csysv( "Upper", &n, &nrhs, a, &lda, ipiv, b, &ldb, work, &lwork, &info );
        /* Check for the exact singularity */
        if( info > 0 ) {
                printf( "The element of the diagonal factor " );
                printf( "D(%i,%i) is zero, so that D is singular;\n", info, info );
                printf( "the solution could not be computed.\n" );
                exit( 1 );
        }
        /* Print solution */
        print_matrix( "Solution", n, nrhs, b, ldb );
        /* Print details of factorization */
        print_matrix( "Details of factorization", n, n, a, lda );
        /* Print pivot indices */
        print_int_vector( "Pivot indices", n, ipiv );
        /* Free workspace */
        free( (void*)work );
        exit( 0 );
} /* End of CSYSV Example */

/* Auxiliary routine: printing a matrix */
void print_matrix( char* desc, int m, int n, fcomplex* a, int lda ) {
        int i, j;
        printf( "\n %s\n", desc );
        for( i = 0; i < m; i++ ) {
                for( j = 0; j < n; j++ )
                        printf( " (%6.2f,%6.2f)", a[i+j*lda].re, a[i+j*lda].im );
                printf( "\n" );
        }
}

/* Auxiliary routine: printing a vector of integers */
void print_int_vector( char* desc, int n, int* a ) {
        int j;
        printf( "\n %s\n", desc );
        for( j = 0; j < n; j++ ) printf( " %6i", a[j] );
        printf( "\n" );
}