1 /*****************************************************************************
2   Copyright (c) 2014, Intel Corp.
3   All rights reserved.
4 
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions are met:
7 
8     * Redistributions of source code must retain the above copyright notice,
9       this list of conditions and the following disclaimer.
10     * Redistributions in binary form must reproduce the above copyright
11       notice, this list of conditions and the following disclaimer in the
12       documentation and/or other materials provided with the distribution.
13     * Neither the name of Intel Corporation nor the names of its contributors
14       may be used to endorse or promote products derived from this software
15       without specific prior written permission.
16 
17   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27   THE POSSIBILITY OF SUCH DAMAGE.
28 *****************************************************************************
29 * Contents: Native middle-level C interface to LAPACK function dtrevc
30 * Author: Intel Corporation
31 *****************************************************************************/
32 
33 #include "lapacke_utils.h"
34 
LAPACKE_dtrevc_work(int matrix_layout,char side,char howmny,lapack_logical * select,lapack_int n,const double * t,lapack_int ldt,double * vl,lapack_int ldvl,double * vr,lapack_int ldvr,lapack_int mm,lapack_int * m,double * work)35 lapack_int LAPACKE_dtrevc_work( int matrix_layout, char side, char howmny,
36                                 lapack_logical* select, lapack_int n,
37                                 const double* t, lapack_int ldt, double* vl,
38                                 lapack_int ldvl, double* vr, lapack_int ldvr,
39                                 lapack_int mm, lapack_int* m, double* work )
40 {
41     lapack_int info = 0;
42     if( matrix_layout == LAPACK_COL_MAJOR ) {
43         /* Call LAPACK function and adjust info */
44         LAPACK_dtrevc( &side, &howmny, select, &n, t, &ldt, vl, &ldvl, vr,
45                        &ldvr, &mm, m, work, &info );
46         if( info < 0 ) {
47             info = info - 1;
48         }
49     } else if( matrix_layout == LAPACK_ROW_MAJOR ) {
50         lapack_int ldt_t = MAX(1,n);
51         lapack_int ldvl_t = MAX(1,n);
52         lapack_int ldvr_t = MAX(1,n);
53         double* t_t = NULL;
54         double* vl_t = NULL;
55         double* vr_t = NULL;
56         /* Check leading dimension(s) */
57         if( ldt < n ) {
58             info = -7;
59             LAPACKE_xerbla( "LAPACKE_dtrevc_work", info );
60             return info;
61         }
62         if( ldvl < mm ) {
63             info = -9;
64             LAPACKE_xerbla( "LAPACKE_dtrevc_work", info );
65             return info;
66         }
67         if( ldvr < mm ) {
68             info = -11;
69             LAPACKE_xerbla( "LAPACKE_dtrevc_work", info );
70             return info;
71         }
72         /* Allocate memory for temporary array(s) */
73         t_t = (double*)LAPACKE_malloc( sizeof(double) * ldt_t * MAX(1,n) );
74         if( t_t == NULL ) {
75             info = LAPACK_TRANSPOSE_MEMORY_ERROR;
76             goto exit_level_0;
77         }
78         if( LAPACKE_lsame( side, 'b' ) || LAPACKE_lsame( side, 'l' ) ) {
79             vl_t = (double*)
80                 LAPACKE_malloc( sizeof(double) * ldvl_t * MAX(1,mm) );
81             if( vl_t == NULL ) {
82                 info = LAPACK_TRANSPOSE_MEMORY_ERROR;
83                 goto exit_level_1;
84             }
85         }
86         if( LAPACKE_lsame( side, 'b' ) || LAPACKE_lsame( side, 'r' ) ) {
87             vr_t = (double*)
88                 LAPACKE_malloc( sizeof(double) * ldvr_t * MAX(1,mm) );
89             if( vr_t == NULL ) {
90                 info = LAPACK_TRANSPOSE_MEMORY_ERROR;
91                 goto exit_level_2;
92             }
93         }
94         /* Transpose input matrices */
95         LAPACKE_dge_trans( matrix_layout, n, n, t, ldt, t_t, ldt_t );
96         if( ( LAPACKE_lsame( side, 'l' ) || LAPACKE_lsame( side, 'b' ) ) &&
97             LAPACKE_lsame( howmny, 'b' ) ) {
98             LAPACKE_dge_trans( matrix_layout, n, mm, vl, ldvl, vl_t, ldvl_t );
99         }
100         if( ( LAPACKE_lsame( side, 'r' ) || LAPACKE_lsame( side, 'b' ) ) &&
101             LAPACKE_lsame( howmny, 'b' ) ) {
102             LAPACKE_dge_trans( matrix_layout, n, mm, vr, ldvr, vr_t, ldvr_t );
103         }
104         /* Call LAPACK function and adjust info */
105         LAPACK_dtrevc( &side, &howmny, select, &n, t_t, &ldt_t, vl_t, &ldvl_t,
106                        vr_t, &ldvr_t, &mm, m, work, &info );
107         if( info < 0 ) {
108             info = info - 1;
109         }
110         /* Transpose output matrices */
111         if( LAPACKE_lsame( side, 'b' ) || LAPACKE_lsame( side, 'l' ) ) {
112             LAPACKE_dge_trans( LAPACK_COL_MAJOR, n, mm, vl_t, ldvl_t, vl,
113                                ldvl );
114         }
115         if( LAPACKE_lsame( side, 'b' ) || LAPACKE_lsame( side, 'r' ) ) {
116             LAPACKE_dge_trans( LAPACK_COL_MAJOR, n, mm, vr_t, ldvr_t, vr,
117                                ldvr );
118         }
119         /* Release memory and exit */
120         if( LAPACKE_lsame( side, 'b' ) || LAPACKE_lsame( side, 'r' ) ) {
121             LAPACKE_free( vr_t );
122         }
123 exit_level_2:
124         if( LAPACKE_lsame( side, 'b' ) || LAPACKE_lsame( side, 'l' ) ) {
125             LAPACKE_free( vl_t );
126         }
127 exit_level_1:
128         LAPACKE_free( t_t );
129 exit_level_0:
130         if( info == LAPACK_TRANSPOSE_MEMORY_ERROR ) {
131             LAPACKE_xerbla( "LAPACKE_dtrevc_work", info );
132         }
133     } else {
134         info = -1;
135         LAPACKE_xerbla( "LAPACKE_dtrevc_work", info );
136     }
137     return info;
138 }
139