summaryrefslogtreecommitdiff
path: root/2.3-1/src/c/linearAlgebra/spec/dspec1a.c
blob: 28440be6c08d7a538cee5aba4dda07e5886019ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* Copyright (C) 2017 - IIT Bombay - FOSSEE

 This file must be used under the terms of the CeCILL.
 This source file is licensed as described in the file COPYING, which
 you should have received as part of this distribution.  The terms
 are also available at
 http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
 Author: Sandeep Gupta
 Organization: FOSSEE, IIT Bombay
 Email: toolbox@scilab.in
 */

/*This function finds the hessenberg form of a matrix A.*/

#include "spec.h"
#include <stdio.h>
#include "string.h"
#include "stdlib.h"
#include "lapack.h"
#include "matrixTranspose.h"
#include "matrixMultiplication.h"
#include "doubleComplex.h"

extern int dggev_(char *,char *,int *,double *,int *,double *,int *,double *,double *,double *,double *,int *,double *,int *,double *,int *,int *);

void assembleEigenvectorsInPlace(int N,double *ALPHAI,double *EVreal,double *EVimg){
	int j,i;
	j=0;
	while(j<N){
		if(ALPHAI[j] == 0){
			//printf(" * ");
			j+=1;	
		}	
		else{	
			int ij;
			int ij1;
			for(i=0;i<N;i++){
				ij = i+j*N;
				ij1 = i+(j+1)*N;	
				EVimg[ij] = EVreal[ij1];
				EVimg[ij1] = -EVreal[ij1];	
				EVreal[ij1] = EVreal[ij];
			}
			j+=2;						
		}
	}
}

void dspec1a(double *in1,double *in2,int size,int nout,doubleComplex *out1,double *out2,doubleComplex *out3,doubleComplex *out4){
	
	int i,j;
	char JOBVL;
	char JOBVR;
	int N=size;
	
	double *A;
	int LDA=N;
	A = (double *)malloc(N*N*sizeof(double));	
	memcpy(A,in1,N*N*sizeof(double));
	
	double *B;
	int LDB=N;
	B = (double *)malloc(N*N*sizeof(double));
	memcpy(B,in2,N*N*sizeof(double));	
	
	double *ALPHAR;
	ALPHAR = (double *)malloc(N*sizeof(double));
	
	double *ALPHAI;
	ALPHAI = (double *)malloc(N*sizeof(double));
		
	double *BETA;
	BETA = (double *)malloc(N*sizeof(double));
	
	double *VL;
	VL = (double *)malloc(N*N*sizeof(double));
	
	int LDVL=N;

	double *VR;
	VR = (double *)malloc(N*N*sizeof(double));
	int LDVR=N;
	
	int LWORK=8*N;

	double *WORK;
	WORK = (double *)malloc(LWORK*sizeof(double));
	
	int INFO;
	if(nout == 1){			/*out1 = spec(A,B)*/
		JOBVL = 'N';
		JOBVR = 'N';
		dggev_(&JOBVL,&JOBVR,&N,A,&LDA,B,&LDB,ALPHAR,ALPHAI,BETA,VL,&LDVL,VR,&LDVR,WORK,&LWORK,&INFO);
		for(i=0;i<N;i++){
			out1[i] = DoubleComplex(ALPHAR[i]/BETA[i],ALPHAI[i]/BETA[i]);
		}			
	}
	else if(nout == 2){		/*[out1,out2] = spec(A,B)*/
		JOBVL = 'N';
		JOBVR = 'N';
		dggev_(&JOBVL,&JOBVR,&N,A,&LDA,B,&LDB,ALPHAR,ALPHAI,BETA,VL,&LDVL,VR,&LDVR,WORK,&LWORK,&INFO);
		for(i=0;i<N;i++){
			out1[i] = DoubleComplex(ALPHAR[i],ALPHAI[i]);		
		}
		memcpy(out2,BETA,N*sizeof(double));
	}
	else if(nout == 3){	 /* [out1,out2,out3] = spec(A,B) */
		JOBVL = 'N';
		JOBVR = 'V';	
		dggev_(&JOBVL,&JOBVR,&N,A,&LDA,B,&LDB,ALPHAR,ALPHAI,BETA,VL,&LDVL,VR,&LDVR,WORK,&LWORK,&INFO);
		for(i=0;i<N;i++){
			out1[i] = DoubleComplex(ALPHAR[i],ALPHAI[i]);		
		}	
		memcpy(out2,BETA,N*sizeof(double));
		
		/*Because lapack routine doesn't give result in actual format, \
			so we have to change the VR little-bit and then return the function */
		
		/*See the Scilab code || see the lapack subroutine libary - DGGEV where \
			it is very explantory and explains all this.			
		*/
		double *EVimg;
		EVimg = (double *)malloc(N*N*sizeof(double));
		for(i=0;i<N;i++){
			for(j=0;j<N;j++){
				EVimg[i+j*N] = 0;					
			}		
		}		
		assembleEigenvectorsInPlace(N,ALPHAI,VR,EVimg);		
		for(i=0;i<N;i++){
			for(j=0;j<N;j++){
				out3[i+j*N] = DoubleComplex(VR[i+j*N],EVimg[i+j*N]);			
			}		
		}
	}
	else if(nout == 4){
		JOBVL = 'V';
		JOBVR = 'V';	

		dggev_(&JOBVL,&JOBVR,&N,A,&LDA,B,&LDB,ALPHAR,ALPHAI,BETA,VL,&LDVL,VR,&LDVR,WORK,&LWORK,&INFO);

		for(i=0;i<N;i++){
			out1[i] = DoubleComplex(ALPHAR[i],ALPHAI[i]);		
		}	

		memcpy(out2,BETA,N*sizeof(double));
		
		double *EVimg;
		EVimg = (double *)malloc(N*N*sizeof(double));
		for(i=0;i<N;i++){
			for(j=0;j<N;j++){
				EVimg[i+j*N] = 0;					
			}		
		}		
		assembleEigenvectorsInPlace(N,ALPHAI,VR,EVimg);		
		for(i=0;i<N;i++){
			for(j=0;j<N;j++){
				out4[i+j*N] = DoubleComplex(VR[i+j*N],EVimg[i+j*N]);			
			}		
		}		
		
		double *EVimg1;
		EVimg1 = (double *)malloc(N*N*sizeof(double));
		for(i=0;i<N;i++){
			for(j=0;j<N;j++){
				EVimg1[i+j*N] = 0;			
			}		
		}		
		assembleEigenvectorsInPlace(N,ALPHAI,VL,EVimg1);
		for(i=0;i<N;i++){
			for(j=0;j<N;j++){
				out3[i+j*N] = DoubleComplex(VL[i+j*N],EVimg1[i+j*N]);			
			}		
		}
	}
}