Developer Reference

ID 766877
Date 12/20/2021
Public

## CHEEVD Example Program in C

/*******************************************************************************
*  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
*  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.
*
********************************************************************************
*/
/*
CHEEVD Example.
==============

Program computes all eigenvalues and eigenvectors of a complex Hermitian
matrix A using divide and conquer algorithm, where A is:

(  3.40,  0.00) ( -2.36, -1.93) ( -4.68,  9.55) (  5.37, -1.23)
( -2.36,  1.93) (  6.94,  0.00) (  8.13, -1.47) (  2.07, -5.78)
( -4.68, -9.55) (  8.13,  1.47) ( -2.14,  0.00) (  4.68,  7.44)
(  5.37,  1.23) (  2.07,  5.78) (  4.68, -7.44) ( -7.42,  0.00)

Description.
============

The routine computes all eigenvalues and, optionally, eigenvectors of an
n-by-n complex Hermitian matrix A. The eigenvector v(j) of A satisfies

A*v(j) = lambda(j)*v(j)

where lambda(j) is its eigenvalue. The computed eigenvectors are
orthonormal.
If the eigenvectors are requested, then this routine uses a divide and
conquer algorithm to compute eigenvalues and eigenvectors.

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

CHEEVD Example Program Results

Eigenvalues
-21.97  -0.05   6.46  16.34

Eigenvectors (stored columnwise)
(  0.41,  0.00) ( -0.34,  0.00) ( -0.69,  0.00) (  0.49,  0.00)
(  0.02, -0.30) (  0.32, -0.21) ( -0.57, -0.22) ( -0.59, -0.21)
(  0.18,  0.57) ( -0.42, -0.32) (  0.06,  0.16) ( -0.35, -0.47)
( -0.62, -0.09) ( -0.58,  0.35) ( -0.15, -0.31) ( -0.10, -0.12)
*/
#include <stdlib.h>
#include <stdio.h>

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

/* CHEEVD prototype */
extern void cheevd( char* jobz, char* uplo, int* n, fcomplex* a, int* lda,
float* w, fcomplex* work, int* lwork, float* rwork, int* lrwork,
int* iwork, int* liwork, int* info );
/* Auxiliary routines prototypes */
extern void print_matrix( char* desc, int m, int n, fcomplex* a, int lda );
extern void print_rmatrix( char* desc, int m, int n, float* a, int lda );

/* Parameters */
#define N 4
#define LDA N

/* Main program */
int main() {
/* Locals */
int n = N, lda = LDA, info, lwork, lrwork, liwork;
int iwkopt;
int* iwork;
float rwkopt;
float* rwork;
fcomplex wkopt;
fcomplex* work;
/* Local arrays */
float w[N];
fcomplex a[LDA*N] = {
{ 3.40f,  0.00f}, {-2.36f,  1.93f}, {-4.68f, -9.55f}, { 5.37f,  1.23f},
{ 0.00f,  0.00f}, { 6.94f,  0.00f}, { 8.13f,  1.47f}, { 2.07f,  5.78f},
{ 0.00f,  0.00f}, { 0.00f,  0.00f}, {-2.14f,  0.00f}, { 4.68f, -7.44f},
{ 0.00f,  0.00f}, { 0.00f,  0.00f}, { 0.00f,  0.00f}, {-7.42f,  0.00f}
};
/* Executable statements */
printf( " CHEEVD Example Program Results\n" );
/* Query and allocate the optimal workspace */
lwork = -1;
lrwork = -1;
liwork = -1;
cheevd( "Vectors", "Lower", &n, a, &lda, w, &wkopt, &lwork, &rwkopt,
&lrwork, &iwkopt, &liwork, &info );
lwork = (int)wkopt.re;
work = (fcomplex*)malloc( lwork*sizeof(fcomplex) );
lrwork = (int)rwkopt;
rwork = (float*)malloc( lrwork*sizeof(float) );
liwork = iwkopt;
iwork = (int*)malloc( liwork*sizeof(int) );
/* Solve eigenproblem */
cheevd( "Vectors", "Lower", &n, a, &lda, w, work, &lwork, rwork,
&lrwork, iwork, &liwork, &info );
/* Check for convergence */
if( info > 0 ) {
printf( "The algorithm failed to compute eigenvalues.\n" );
exit( 1 );
}
/* Print eigenvalues */
print_rmatrix( "Eigenvalues", 1, n, w, 1 );
/* Print eigenvectors */
print_matrix( "Eigenvectors (stored columnwise)", n, n, a, lda );
/* Free workspace */
free( (void*)iwork );
free( (void*)rwork );
free( (void*)work );
exit( 0 );
} /* End of CHEEVD 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 real matrix */
void print_rmatrix( char* desc, int m, int n, float* 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", a[i+j*lda] );
printf( "\n" );
}
}
{"ft_lang_label":"__label__en","ft_lang_prob":0.5771667,"math_prob":0.9490882,"size":5153,"snap":"2023-40-2023-50","text_gpt3_token_len":1775,"char_repetition_ratio":0.14896096,"word_repetition_ratio":0.13445379,"special_character_ratio":0.43023482,"punctuation_ratio":0.29192007,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96360046,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-24T18:25:28Z\",\"WARC-Record-ID\":\"<urn:uuid:69f4c93f-f1ab-4f97-b894-e50731110fd4>\",\"Content-Length\":\"765322\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:70f80f28-7392-448c-8527-35871cedecf3>\",\"WARC-Concurrent-To\":\"<urn:uuid:f208a7a4-3d77-40e5-818e-652bab06b216>\",\"WARC-IP-Address\":\"104.73.27.18\",\"WARC-Target-URI\":\"https://www.intel.com/content/www/us/en/docs/onemkl/code-samples-lapack/2022-1/cheevd-example-c.html\",\"WARC-Payload-Digest\":\"sha1:7F26YSCJHGRKCPLMRG3PJ2MAYYVCL5TC\",\"WARC-Block-Digest\":\"sha1:NF2V4ZJRIQ6SQ6WBORMOJWYHPPAX32FA\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233506658.2_warc_CC-MAIN-20230924155422-20230924185422-00258.warc.gz\"}"}