diff options
author | Sandeep Gupta | 2017-06-18 23:55:40 +0530 |
---|---|---|
committer | Sandeep Gupta | 2017-06-18 23:55:40 +0530 |
commit | b43eccd4cffed5bd1017c5821524fb6e49202f78 (patch) | |
tree | 4c53d798252cbeae9bcf7dc9604524b20bb10f27 /2.3-1/src/c/linearAlgebra | |
download | Scilab2C-b43eccd4cffed5bd1017c5821524fb6e49202f78.tar.gz Scilab2C-b43eccd4cffed5bd1017c5821524fb6e49202f78.tar.bz2 Scilab2C-b43eccd4cffed5bd1017c5821524fb6e49202f78.zip |
First commit
Diffstat (limited to '2.3-1/src/c/linearAlgebra')
23 files changed, 1436 insertions, 0 deletions
diff --git a/2.3-1/src/c/linearAlgebra/balanc/dbalanca.c b/2.3-1/src/c/linearAlgebra/balanc/dbalanca.c new file mode 100644 index 00000000..a86a1967 --- /dev/null +++ b/2.3-1/src/c/linearAlgebra/balanc/dbalanca.c @@ -0,0 +1,75 @@ +/* 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: Siddhesh Wani + Organization: FOSSEE, IIT Bombay + Email: toolbox@scilab.in + */ + +/*Funtion to balance given square matrix to improve its condition number*/ +#include "balanc.h" +#include "lapack.h" +#include "stdlib.h" +#include "string.h" +#include "eye.h" + +void dbalanca(double* in1, int rows, double* in2, double* out1, \ + double* out2, double* out3, double* out4) +{ + char JOB = 'B'; + char RSIDE = 'R'; + char LSIDE = 'L'; + + int ILO, IHI, INFO; + double *buf1, *buf2, *LSCALE, *RSCALE; + double *LWORK; + + + if(in2 == NULL) + { + /*Single input matrix*/ + buf1 = (double*) malloc((double) rows*rows*sizeof(double)); + LSCALE = (double*) malloc((double) rows*sizeof(double)); + + /*copy input to temp buf as lapack function modifies input*/ + memcpy(buf1,in1,rows*rows*sizeof(double)); + + dgebal_(&JOB,&rows,buf1,&rows,&ILO,&IHI,LSCALE,&INFO); + deyea(out2,rows,rows); + dgebak_(&JOB,&RSIDE,&rows,&ILO,&IHI,LSCALE,&rows,out2,&rows,&INFO); + + /*copy computed matrix to output*/ + memcpy(out1,buf1,rows*rows*sizeof(double)); + } + else + { + /*two matrices as inputs*/ + buf1 = (double*) malloc((double) rows*rows*sizeof(double)); + buf2 = (double*) malloc((double) rows*rows*sizeof(double)); + LSCALE = (double*) malloc((double) rows*sizeof(double)); + RSCALE = (double*) malloc((double) rows*sizeof(double)); + LWORK = (double*) malloc((double) 6*rows*sizeof(double)); + + /*copy input to temp buf as lapack function modifies input*/ + memcpy(buf1,in1,rows*rows*sizeof(double)); + memcpy(buf2,in2,rows*rows*sizeof(double)); + + dggbal_(&JOB,&rows,buf1,&rows,buf2,&rows,&ILO,&IHI,LSCALE,RSCALE, \ + LWORK,&INFO); + deyea(out3,rows,rows); + deyea(out4,rows,rows); + + dgebak_(&JOB,&LSIDE,&rows,&ILO,&IHI,LSCALE,&rows,out3,&rows,&INFO); + dgebak_(&JOB,&RSIDE,&rows,&ILO,&IHI,LSCALE,&rows,out4,&rows,&INFO); + + /*copy computed matrix to output*/ + memcpy(out1,buf1,rows*rows*sizeof(double)); + memcpy(out2,buf2,rows*rows*sizeof(double)); + + } + +}
\ No newline at end of file diff --git a/2.3-1/src/c/linearAlgebra/bdiag/dbdiaga.c b/2.3-1/src/c/linearAlgebra/bdiag/dbdiaga.c new file mode 100644 index 00000000..fd455ba7 --- /dev/null +++ b/2.3-1/src/c/linearAlgebra/bdiag/dbdiaga.c @@ -0,0 +1,23 @@ +/* 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 performs the block-diagonalization of matrix A.*/ + +/*--------------------------------------------------------------------------*/ +/* [Ab [,X [,bs]]]=bdiag(A [,rMax]) */ +/*--------------------------------------------------------------------------*/ + +#include <stdio.h> +#include "string.h" +#include "stdlib.h" +#include "lapack.h" + diff --git a/2.3-1/src/c/linearAlgebra/hess/dhessa.c b/2.3-1/src/c/linearAlgebra/hess/dhessa.c new file mode 100644 index 00000000..57f81b35 --- /dev/null +++ b/2.3-1/src/c/linearAlgebra/hess/dhessa.c @@ -0,0 +1,81 @@ +/* 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 "hess.h" +#include <stdio.h> +#include "string.h" +#include "stdlib.h" +#include "lapack.h" +#include "matrixTranspose.h" +#include "matrixMultiplication.h" + +extern int dgehrd_(int *, int *,int *,double *,int *,double *,double *,int *,int *); +extern int dorghr_(int *, int *,int *,double *,int *,double *,double *,int *,int *); + + +void dhessa(double *in1,int size,int nout,double *out1, double *out2){ + int i,j,k; + int N = size; + int ILO=1; + int IHI=N; + double *A; + int LDA=N; + double *TAU; + double *WORK; + int LWORK = N; + int INFO; + A = (double *)malloc((double)size*size*sizeof(double)); + memcpy(A,in1,size*size*sizeof(double)); + TAU = (double *)malloc((double)size*sizeof(double)); + WORK = (double *)malloc((double)LWORK*sizeof(double)); + dgehrd_(&N,&ILO,&IHI,A,&LDA,TAU,WORK,&N,&INFO); + + for(i=0;i<N;i++) + for(j=0;j<N;j++) + out2[i+j*N] = A[i+j*N]; + + for(j=1;j<=N-2;j++){ + for(i=j+2;i<=N;i++){ + out2[(i-1)+(j-1)*N] = 0; + } + } + + if(nout > 1){ + dorghr_(&N,&ILO,&IHI,A,&LDA,TAU,WORK,&LWORK,&INFO); + for(i=0;i<N;i++) + for(j=0;j<N;j++) + out1[i+j*N] = A[i+j*N]; + } + + /*for(i=0;i<N;i++){ + for(j=0;j<N;j++){ + if(i == j) out1[i+j*N]=1; + else out1[i+j*N]=0; + } + } + double result[size*size]; + for(i=IHI-2;i>=ILO-1;i--){ + tau = TAU[i]; + double V[size],v[size],v1[size*size]; + for(j=0;j<i;j++) V[j]=0; + V[j]=1*tau; + for(j=i+1;j<IHI;j++) V[j] = tau*A[j+i*N]; + dtransposea (V,N,1,v); + dmulma(V,N,1,v,1,N,v1); + for(j=0;j<N;j++) for(k=0;k<N;k++) if(j == k) v1[j+k*N]--; + for(j=0;j<N;j++) for(k=0;k<N;k++) result[j+k*N] = out1[j+k*N]; + dmulma(v1,N,N,result,N,N,out1); + }*/ + //out2 = NULL; +} diff --git a/2.3-1/src/c/linearAlgebra/includes/balanc.h b/2.3-1/src/c/linearAlgebra/includes/balanc.h new file mode 100644 index 00000000..dcc66b28 --- /dev/null +++ b/2.3-1/src/c/linearAlgebra/includes/balanc.h @@ -0,0 +1,27 @@ + /* 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: Siddhesh Wani + Organization: FOSSEE, IIT Bombay + Email: toolbox@scilab.in + */ +#ifndef __BALANC_H__ +#define __BALANC_H__ + + +#ifdef __cplusplus +extern "C" { +#endif + +void dbalanca(double* in1, int rows, double* in2, double* out1, \ + double* out2, double* out3, double* out4); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /*__BALANC_H__*/ diff --git a/2.3-1/src/c/linearAlgebra/includes/hess.h b/2.3-1/src/c/linearAlgebra/includes/hess.h new file mode 100644 index 00000000..b9c53ded --- /dev/null +++ b/2.3-1/src/c/linearAlgebra/includes/hess.h @@ -0,0 +1,26 @@ + /* 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 + */ +#ifndef __HESS_H__ +#define __HESS_H__ +#include "types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void dhessa(double *in1,int size,int nout,double *out1,double *out2); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /*__HESS_H__*/ diff --git a/2.3-1/src/c/linearAlgebra/includes/rcond.h b/2.3-1/src/c/linearAlgebra/includes/rcond.h new file mode 100644 index 00000000..4796f029 --- /dev/null +++ b/2.3-1/src/c/linearAlgebra/includes/rcond.h @@ -0,0 +1,26 @@ + /* 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: Siddhesh Wani + Organization: FOSSEE, IIT Bombay + Email: toolbox@scilab.in + */ +#ifndef __RCOND_H__ +#define __RCOND_H__ + + +#ifdef __cplusplus +extern "C" { +#endif + +double drconda(double* in1, int rows); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /*__RCOND_H__*/ diff --git a/2.3-1/src/c/linearAlgebra/includes/schur.h b/2.3-1/src/c/linearAlgebra/includes/schur.h new file mode 100644 index 00000000..27b5c2aa --- /dev/null +++ b/2.3-1/src/c/linearAlgebra/includes/schur.h @@ -0,0 +1,30 @@ + /* 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: Siddhesh Wani + Organization: FOSSEE, IIT Bombay + Email: toolbox@scilab.in + */ +#ifndef __SCHUR_H__ +#define __SCHUR_H__ + + +#ifdef __cplusplus +extern "C" { +#endif + +double dschura(double* in1, int size, int flag, int nout, double* out1, \ + double* out2); + +double dgschura(double* in1, int size, double* in2, int flag, int nout, \ + double* out1, double* out2, double* out3, double* out4); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /*__SCHUR_H__*/ diff --git a/2.3-1/src/c/linearAlgebra/includes/sva.h b/2.3-1/src/c/linearAlgebra/includes/sva.h new file mode 100644 index 00000000..ea628a32 --- /dev/null +++ b/2.3-1/src/c/linearAlgebra/includes/sva.h @@ -0,0 +1,29 @@ + /* 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 + */ + +#ifndef __SVA_H__ +#define __SVA_H__ +#include "types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void dsvaa(int ninp,double *in1,int row,int col,double in2,double *out1, \ + double *out2,double *out3); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /*__SVA_H__*/ + diff --git a/2.3-1/src/c/linearAlgebra/includes/svd.h b/2.3-1/src/c/linearAlgebra/includes/svd.h new file mode 100644 index 00000000..260b87fb --- /dev/null +++ b/2.3-1/src/c/linearAlgebra/includes/svd.h @@ -0,0 +1,30 @@ + /* 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 + */ +#ifndef __SVD_H__ +#define __SVD_H__ +#include "types.h" +#include "doubleComplex.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void dsvda(double *in1,int row,int col,double in2,double nout,double *out1, \ + double *out2,double *out3); +void zsvda(doubleComplex *in1,int row,int col,int in2,int nout, doubleComplex *out1,\ + doubleComplex *out2,doubleComplex *out3); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /*__SVD_H__*/ diff --git a/2.3-1/src/c/linearAlgebra/interfaces/int_balanc.h b/2.3-1/src/c/linearAlgebra/interfaces/int_balanc.h new file mode 100644 index 00000000..a16ba8c2 --- /dev/null +++ b/2.3-1/src/c/linearAlgebra/interfaces/int_balanc.h @@ -0,0 +1,29 @@ + /* 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: Siddhesh Wani + Organization: FOSSEE, IIT Bombay + Email: toolbox@scilab.in + */ +#ifndef __INT_BALANC_H__ +#define __INT_BALANC_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#define d2balancd2d2(in1,size1,out1,out2) dbalanca(in1,size1[0],NULL,out1, \ + out2,NULL,NULL) + +#define d2d2balancd2d2d2d2(in1,size1,in2,size2,out1,out2,out3,out4) \ + dbalanca(in1,size1[0],in2,out1,out2,out3,out4) + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /*__INT_BALANC_H__*/ diff --git a/2.3-1/src/c/linearAlgebra/interfaces/int_hess.h b/2.3-1/src/c/linearAlgebra/interfaces/int_hess.h new file mode 100644 index 00000000..fb2ca720 --- /dev/null +++ b/2.3-1/src/c/linearAlgebra/interfaces/int_hess.h @@ -0,0 +1,28 @@ + /* 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 + */ + +#ifndef __INT_HESS_H__ +#define __INT_HESS_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#define d2hessd2(in1,size,out2) dhessa(in1,size[0],1,NULL,out2); +#define d2hessd2d2(in1,size,out1,out2) dhessa(in1,size[0],2,out1,out2); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /*__INT_HESS_H__*/ + diff --git a/2.3-1/src/c/linearAlgebra/interfaces/int_rcond.h b/2.3-1/src/c/linearAlgebra/interfaces/int_rcond.h new file mode 100644 index 00000000..6e6a4454 --- /dev/null +++ b/2.3-1/src/c/linearAlgebra/interfaces/int_rcond.h @@ -0,0 +1,25 @@ + /* 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: Siddhesh Wani + Organization: FOSSEE, IIT Bombay + Email: toolbox@scilab.in + */ +#ifndef __INT_RCOND_H__ +#define __INT_RCOND_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#define d2rcondd0(in1,size1) drconda(in1,size1[0]) + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /*__INT_RCOND_H__*/ diff --git a/2.3-1/src/c/linearAlgebra/interfaces/int_schur.h b/2.3-1/src/c/linearAlgebra/interfaces/int_schur.h new file mode 100644 index 00000000..81324e66 --- /dev/null +++ b/2.3-1/src/c/linearAlgebra/interfaces/int_schur.h @@ -0,0 +1,60 @@ + /* 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: Siddhesh Wani + Organization: FOSSEE, IIT Bombay + Email: toolbox@scilab.in + */ +#ifndef __INT_SCHUR_H__ +#define __INT_SCHUR_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#define d2schurd2(in1,size1,out1) dschura(in1,size1[0],0,1,out1,NULL) + +#define d2schurd2d2(in1,size1,out1,out2) dschura(in1,size1[0],0,2,out1,out2) + +#define d2g2schurd2(in1,size1,in2,size2,out1) (in2[0]=='c')? \ + dschura(in1,size1[0],1,1,out1,NULL): \ + dschura(in1,size1[0],2,1,out1,NULL) + +#define d2g2schurd2d0(in1,size1,in2,size2,out1) (in2[0]=='c')? \ + dschura(in1,size1[0],1,2,out1,NULL): \ + dschura(in1,size1[0],2,2,out1,NULL) + +#define d2g2schurd2d0d2(in1,size1,in2,size2,out1,out2) (in2[0]=='c')? \ + dschura(in1,size1[0],1,3,out1,out2): \ + dschura(in1,size1[0],2,3,out1,out2) + +#define d2d2schurd2d2(in1,size1,in2,size2,out1,out2) dgschura(in1,size1[0], \ + in2,0,2,out1,out2,NULL,NULL) + +#define d2d2schurd2d2d2d2(in1,size1,in2,size2,out1,out2,out3,out4) \ + dgschura(in1,size1[0],in2,0,4,out1,out2,out3,out4) + +#define d2d2g2schurd0(in1,size1,in2,size2,in3,size3) dgschura(in1,size1[0], \ + in2,1,1,NULL,NULL,NULL,NULL) + +#define d2d2g2schurd2d0(in1,size1,in2,size2,in3,size3,out1) \ + dgschura(in1,size1[0],in2,1,2,out1,NULL,NULL,NULL) + +#define d2d2g2schurd2d2d0(in1,size1,in2,size2,in3,size3,out1,out2) \ + dgschura(in1,size1[0],in2,1,3,out1,out2,NULL,NULL) + +#define d2d2g2schurd2d2d2d0(in1,size1,in2,size2,in3,size3,out1,out2,out3) \ + dgschura(in1,size1[0],in2,1,4,out1,out2,out3,NULL) + +#define d2d2g2schurd2d2d2d2d0(in1,size1,in2,size2,in3,size3,out1,out2,out3, \ + out4) dgschura(in1,size1[0],in2,1,5,out1,out2,out3,out4) + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /*__INT_SCHUR_H__*/ diff --git a/2.3-1/src/c/linearAlgebra/interfaces/int_sva.h b/2.3-1/src/c/linearAlgebra/interfaces/int_sva.h new file mode 100644 index 00000000..4a2ec56b --- /dev/null +++ b/2.3-1/src/c/linearAlgebra/interfaces/int_sva.h @@ -0,0 +1,28 @@ + /* 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 + */ + +#ifndef __INT_SVA_H__ +#define __INT_SVA_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#define d2svad2d2d2(in1,size,out1,out2,out3) dsvaa(1,in1,size[0],size[1],0,out1,out2,out3); +#define d2d0svad2d2d2(in1,size1,in2,size2,out1,out2,out3) dsvaa(2,in1,size1[0],size1[1],in2,out1,out2,out3); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /*__INT_SVA_H__*/ + diff --git a/2.3-1/src/c/linearAlgebra/interfaces/int_svd.h b/2.3-1/src/c/linearAlgebra/interfaces/int_svd.h new file mode 100644 index 00000000..449ee741 --- /dev/null +++ b/2.3-1/src/c/linearAlgebra/interfaces/int_svd.h @@ -0,0 +1,31 @@ + /* 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 + */ + +#ifndef __INT_SVD_H__ +#define __INT_SVD_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#define d2svdd2(in1,size1,out1) dsvda(in1,size1[0],size1[1],0,1,out1,NULL,NULL) +#define d2g2svdd2d2d2(in1,size1,in2,size2,out1,out2,out3) dsvda(in1,size1[0],size1[1],1,3,out1,out2,out3); +#define d2svdd2d2d2(in1,size1,out1,out2,out3) dsvda(in1,size1[0],size1[1],0,3,out1,out2,out3); +#define z2svdz2(in1,size1,out2) zsvda(in1,size1[0],size1[1],0,1,NULL,out2,NULL); +#define z2g2svdz2z2z2(in1,size1,in2,size2,out1,out2,out3) zsvda(in1,size1[0],size1[1],1,3,out1,out2,out3); +#define z2svdz2z2z2(in1,size1,out1,out2,out3) zsvda(in1,size1[0],size1[1],0,3,out1,out2,out3); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /*__INT_SVD_H__*/ diff --git a/2.3-1/src/c/linearAlgebra/rcond/drconda.c b/2.3-1/src/c/linearAlgebra/rcond/drconda.c new file mode 100644 index 00000000..a203c1e3 --- /dev/null +++ b/2.3-1/src/c/linearAlgebra/rcond/drconda.c @@ -0,0 +1,46 @@ +/* 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: Siddhesh Wani + Organization: FOSSEE, IIT Bombay + Email: toolbox@scilab.in + */ + +/*Funtion to find inverse condition number of square matrix*/ + +#include "lapack.h" +#include "stdlib.h" +#include "string.h" + +double drconda(double* in1, int rows) +{ + double *buf, *LDWORK; + int INFO, *IPIV, *LIWORK; + char one = '1'; + double ANORM; + double RCOND = 1; + + buf = (double*) malloc((double) rows*rows*sizeof(double)); + IPIV = (int*) malloc((int) rows*sizeof(int)); + LIWORK = (int*) malloc((int) rows*sizeof(int)); + LDWORK = (double*) malloc((double) 4*rows*sizeof(double)); + + /*Copy input in temp buf, as lapack modifies input*/ + memcpy(buf,in1,rows*rows*sizeof(double)); + + ANORM = dlange_(&one,&rows,&rows,buf,&rows,LDWORK); + + dgetrf_(&rows,&rows,buf,&rows,IPIV,&INFO); + + if(INFO == 0) + { + dgecon_(&one,&rows,buf,&rows,&ANORM,&RCOND,LDWORK,LIWORK,&INFO); + } + + return RCOND; + +}
\ No newline at end of file diff --git a/2.3-1/src/c/linearAlgebra/schur/dgschura.c b/2.3-1/src/c/linearAlgebra/schur/dgschura.c new file mode 100644 index 00000000..f17da8c8 --- /dev/null +++ b/2.3-1/src/c/linearAlgebra/schur/dgschura.c @@ -0,0 +1,161 @@ +/* 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: Siddhesh Wani + Organization: FOSSEE, IIT Bombay + Email: toolbox@scilab.in + */ + +/*Fucntion to find generalised schur decomposition of given square matrices */ +#include "schur.h" +#include "lapack.h" +#include "stdlib.h" +#include "string.h" +#include <math.h> + +/*flag --> 0: nothing + --> 1: continuous + --> 2: discrete +*/ + +lapack_logical selctg21( double* in1, double* in2, double* in3); +lapack_logical selctg22( double* in1, double* in2, double* in3); + +double dgschura(double* in1, int size, double* in2, int flag, int nout, \ + double* out1, double* out2, double* out3, double* out4) +{ + char JOBVSL = 'N'; + char JOBVSR = 'N'; + char SORT = 'N'; + int SDIM = 0; + int LDVSL = size, LDVSR = size; + int LWORK = 8*size+16, INFO; + double *ALPHAR, *ALPHAI, *BETA, *VSL, *VSR, *WORK; + int *BWORK; + double ret = 0; + double *buf1, *buf2; /*input is copied to buf, since lapack function directly + modifies the input variable*/ + + /*Used incase of flag > 0*/ + LAPACK_D_SELECT3 selctg = &selctg21; + + if(nout >= 2){ + JOBVSL = 'V'; + JOBVSR = 'V'; + } + if(flag > 0) SORT = 'S'; + + buf1 = (double*) malloc((double) size*size*sizeof(double)); + buf2 = (double*) malloc((double) size*size*sizeof(double)); + ALPHAR = (double*) malloc((double) size*sizeof(double)); + ALPHAI = (double*) malloc((double) size*sizeof(double)); + BETA = (double*) malloc((double) size*sizeof(double)); + VSL = (double*) malloc((double) LDVSL*size*sizeof(double)); + VSR = (double*) malloc((double) LDVSR*size*sizeof(double)); + WORK = (double*) malloc((double) LWORK*sizeof(double)); + BWORK = (int*) malloc((double) size*sizeof(double)); + + + memcpy(buf1,in1,size*size*sizeof(double)); + memcpy(buf2,in2,size*size*sizeof(double)); + + dgges_(&JOBVSL,&JOBVSR,&SORT,selctg,&size,buf1,&size,buf2,&size,&SDIM, \ + ALPHAR,ALPHAI,BETA,VSL,&LDVSL,VSR,&LDVSR,WORK,&LWORK,BWORK,&INFO); + + /*if (INFO != 0) + { + out1 = NULL; + return 0; + }*/ + + if(nout == 1) + { + return(SDIM); + } + else if(nout == 2) + { + if(flag == 0) + { + /*copy in1 to out1 and in2 to out2*/ + memcpy(out1,buf1,size*size*sizeof(double)); + memcpy(out2,buf2,size*size*sizeof(double)); + } + else + { + /*copy VSR to out1 and return SDIM */ + memcpy(out1,VSR,size*size*sizeof(double)); + ret = SDIM; + } + } + else if(nout == 3) + { + /*copy VSL to out1, VSR to out2, return SDIM*/ + memcpy(out1,VSL,size*size*sizeof(double)); + memcpy(out2,VSR,size*size*sizeof(double)); + ret = SDIM; + } + else if(nout == 4) + { + if(flag == 0) + { + /*copy in1 to out1 and in2 to out2*/ + memcpy(out1,buf1,size*size*sizeof(double)); + memcpy(out2,buf2,size*size*sizeof(double)); + /*copy VSL to out3 and VSR to out4*/ + memcpy(out3,VSL,size*size*sizeof(double)); + memcpy(out4,VSR,size*size*sizeof(double)); + } + else + { + /*copy in1 to out1 and in2 to out2*/ + memcpy(out1,buf1,size*size*sizeof(double)); + memcpy(out2,buf2,size*size*sizeof(double)); + /*copy VSR to out3 and return SDIM*/ + memcpy(out3,VSR,size*size*sizeof(double)); + ret = SDIM; + } + } + else /*nout = 5*/ + { + /*copy in1 to out1 and in2 to out2*/ + memcpy(out1,buf1,size*size*sizeof(double)); + memcpy(out2,buf2,size*size*sizeof(double)); + /*copy VSL to out3 and VSR to out4*/ + memcpy(out3,VSL,size*size*sizeof(double)); + memcpy(out4,VSR,size*size*sizeof(double)); + /*return SDIM*/ + ret = SDIM; + } + + free(buf1); + free(buf2); + free(VSL); + free(VSR); + free(ALPHAR); + free(ALPHAI); + free(BETA); + free(WORK); + free(BWORK); + + return ret; +} + +lapack_logical selctg21(double* in1, double* in2, double* in3) +{ + if((sqrt(*in1**in1+*in2**in2)/ *in3) < 1) + return 1; + else + return 0; +} + +lapack_logical selctg22(double* in1, double* in2, double* in3) +{ + if((sqrt(*in1**in1+*in2**in2)/ *in3) < 1) + return 1; + else + return 0; +}
\ No newline at end of file diff --git a/2.3-1/src/c/linearAlgebra/schur/dschura.c b/2.3-1/src/c/linearAlgebra/schur/dschura.c new file mode 100644 index 00000000..802caa81 --- /dev/null +++ b/2.3-1/src/c/linearAlgebra/schur/dschura.c @@ -0,0 +1,126 @@ +/* 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: Siddhesh Wani + Organization: FOSSEE, IIT Bombay + Email: toolbox@scilab.in + */ + +/*Funtion to find schur decomposition of given square matrix */ +#include "schur.h" +#include "lapack.h" +#include "stdlib.h" +#include "string.h" +#include <math.h> + +/*flag --> 0: nothing + --> 1: continuous + --> 2: discrete +*/ + +lapack_logical selctg11( double* in1, double* in2); +lapack_logical selctg12( double* in1, double* in2); + +double dschura(double* in1, int size, int flag, int nout, double* out1, \ + double* out2) +{ + char JOBVS = 'N'; + char SORT = 'N'; + int SDIM = 0; + int LVDS = size; + int LWORK = 3*size, INFO; + double *WR, *WI, *VS, *WORK; + int *BWORK; + double ret = 0; + double *buf; /*input is copied to buf, since lapack function direclty + modifies the input variable*/ + + + /*Used incase of flag > 0*/ + LAPACK_D_SELECT2 selctg; + + if(flag == 1 || flag == 0) + selctg = &selctg11; + else if(flag == 2) + selctg = &selctg12; + + if(nout >= 2) JOBVS = 'V'; + if(flag > 0) SORT = 'S'; + + buf = (double*) malloc((double) size*size*sizeof(double)); + WR = (double*) malloc((double) size*sizeof(double)); + WI = (double*) malloc((double) size*sizeof(double)); + VS = (double*) malloc((double) LVDS*size*sizeof(double)); + WORK = (double*) malloc((double) LWORK*sizeof(double)); + BWORK = (int*) malloc((double) size*sizeof(double)); + + + memcpy(buf,in1,size*size*sizeof(double)); + + dgees_(&JOBVS,&SORT,selctg,&size,buf,&size,&SDIM,WR,WI,VS,&LVDS, \ + WORK,&LWORK,BWORK,&INFO); + + /*if (INFO != 0) + { + out1 = NULL; + return 0; + } */ + + if(nout == 1) + { + /*Copy result in in1 to out1*/ + memcpy(out1,buf,size*size*sizeof(double)); + } + else if(nout == 2) + { + if(flag == 0) + { + /*copy in1 to out2 and VS to out1*/ + memcpy(out2,buf,size*size*sizeof(double)); + memcpy(out1,VS,size*size*sizeof(double)); + } + else + { + /*copy VS to out1 and SDIM to out2*/ + memcpy(out1,VS,size*size*sizeof(double)); + ret = SDIM; + } + } + else + { + /*copy VS to out1, SDIM to out2, in1 to out3*/ + memcpy(out1,VS,size*size*sizeof(double)); + memcpy(out2,buf,size*size*sizeof(double)); + ret = SDIM; + } + + free(buf); + free(WI); + free(WR); + free(VS); + free(WORK); + free(BWORK); + + return ret; +} + +lapack_logical selctg11(double* in1, double* in2) +{ + if(*in1 <= 0) + return 1; + else + return 0; +} + +lapack_logical selctg12(double* in1, double* in2) +{ + + if(sqrt(*in1**in1+*in2**in2) < 1) + return 1; + else + return 0; +}
\ No newline at end of file diff --git a/2.3-1/src/c/linearAlgebra/schur/zgschura.c b/2.3-1/src/c/linearAlgebra/schur/zgschura.c new file mode 100644 index 00000000..a49cbe51 --- /dev/null +++ b/2.3-1/src/c/linearAlgebra/schur/zgschura.c @@ -0,0 +1,164 @@ +/* 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 + */ + +/*Fucntion to find generalised schur decomposition of given square complex matrices */ + +#include "schur.h" +#include "lapack.h" +#include "stdlib.h" +#include "string.h" +#include <math.h> +#include "doubleComplex.h" +#include "matrixTranspose.h" + +/*flag --> 0: nothing + --> 1: continuous + --> 2: discrete +*/ + +lapack_logical selctg21( doubleComplex* in1, doubleComplex* in2, doubleComplex* in3); +lapack_logical selctg22( doubleComplex* in1, doubleComplex* in2, doubleComplex* in3); + +doubleComplex dgschura(doubleComplex* in1, int size, doubleComplex* in2, int flag, int nout, \ + doubleComplex* out1, doubleComplex* out2, doubleComplex* out3, doubleComplex* out4) +{ + char JOBVSL = 'N'; + char JOBVSR = 'N'; + char SORT = 'N'; + int SDIM = 0; + int LDVSL = size, LDVSR = size; + int LWORK = 2*size, INFO; + doubleComplex *ALPHA, *BETA, *VSL, *VSR, *WORK; + double *RWORK; + int *BWORK; + doubleComplex ret = 0; + doubleComplex *buf1, *buf2; /*input is copied to buf, since lapack function directly + modifies the input variable*/ + + /*Used incase of flag > 0*/ + LAPACK_D_SELECT3 selctg = &selctg21; + + if(nout >= 2){ + JOBVSL = 'V'; + JOBVSR = 'V'; + } + if(flag > 0) SORT = 'S'; + + buf1 = (doubleComplex*) malloc((doubleComplex) size*size*sizeof(doubleComplex)); + buf2 = (doubleComplex*) malloc((doubleComplex) size*size*sizeof(doubleComplex)); + ALPHA = (doubleComplex*) malloc((doubleComplex) size*sizeof(doubleComplex)); + RWORK = (double *)malloc((double)8*size*sizeof(double)); + BETA = (doubleComplex*) malloc((doubleComplex) size*sizeof(doubleComplex)); + VSL = (doubleComplex*) malloc((doubleComplex) LDVSL*size*sizeof(doubleComplex)); + VSR = (doubleComplex*) malloc((doubleComplex) LDVSR*size*sizeof(doubleComplex)); + WORK = (doubleComplex*) malloc((doubleComplex) LWORK*sizeof(doubleComplex)); + BWORK = (int*) malloc((doubleComplex) size*sizeof(doubleComplex)); + + + memcpy(buf1,in1,size*size*sizeof(doubleComplex)); + memcpy(buf2,in2,size*size*sizeof(doubleComplex)); + + zgges_(&JOBVSL,&JOBVSR,&SORT,selctg,&size,buf1,&size,buf2,&size,&SDIM, \ + ALPHA,BETA,VSL,&LDVSL,VSR,&LDVSR,WORK,&LWORK,RWORK,BWORK,&INFO); + + /*if (INFO != 0) + { + out1 = NULL; + return 0; + }*/ + + if(nout == 1) + { + return(SDIM); + } + else if(nout == 2) + { + if(flag == 0) + { + /*copy in1 to out1 and in2 to out2*/ + memcpy(out1,buf1,size*size*sizeof(doubleComplex)); + memcpy(out2,buf2,size*size*sizeof(doubleComplex)); + } + else + { + /*copy VSR to out1 and return SDIM */ + memcpy(out1,VSR,size*size*sizeof(doubleComplex)); + ret = SDIM; + } + } + else if(nout == 3) + { + /*copy VSL to out1, VSR to out2, return SDIM*/ + memcpy(out1,VSL,size*size*sizeof(doubleComplex)); + memcpy(out2,VSR,size*size*sizeof(doubleComplex)); + ret = SDIM; + } + else if(nout == 4) + { + if(flag == 0) + { + /*copy in1 to out1 and in2 to out2*/ + memcpy(out1,buf1,size*size*sizeof(doubleComplex)); + memcpy(out2,buf2,size*size*sizeof(doubleComplex)); + /*copy VSL to out3 and VSR to out4*/ + memcpy(out3,VSL,size*size*sizeof(doubleComplex)); + memcpy(out4,VSR,size*size*sizeof(doubleComplex)); + } + else + { + /*copy in1 to out1 and in2 to out2*/ + memcpy(out1,buf1,size*size*sizeof(doubleComplex)); + memcpy(out2,buf2,size*size*sizeof(doubleComplex)); + /*copy VSR to out3 and return SDIM*/ + memcpy(out3,VSR,size*size*sizeof(doubleComplex)); + ret = SDIM; + } + } + else /*nout = 5*/ + { + /*copy in1 to out1 and in2 to out2*/ + memcpy(out1,buf1,size*size*sizeof(doubleComplex)); + memcpy(out2,buf2,size*size*sizeof(doubleComplex)); + /*copy VSL to out3 and VSR to out4*/ + memcpy(out3,VSL,size*size*sizeof(doubleComplex)); + memcpy(out4,VSR,size*size*sizeof(doubleComplex)); + /*return SDIM*/ + ret = SDIM; + } + + free(buf1); + free(buf2); + free(VSL); + free(VSR); + free(ALPHA); + free(BETA); + free(WORK); + free(BWORK); + + return ret; +} + +lapack_logical selctg21(doubleComplex* in1, doubleComplex* in2, doubleComplex* in3) +{ + if((sqrt(*in1**in1+*in2**in2)/ *in3) < 1) + return 1; + else + return 0; +} + +lapack_logical selctg22(doubleComplex* in1, doubleComplex* in2, doubleComplex* in3) +{ + if((sqrt(*in1**in1+*in2**in2)/ *in3) < 1) + return 1; + else + return 0; +} diff --git a/2.3-1/src/c/linearAlgebra/sva/dsvaa.c b/2.3-1/src/c/linearAlgebra/sva/dsvaa.c new file mode 100644 index 00000000..ee27eef7 --- /dev/null +++ b/2.3-1/src/c/linearAlgebra/sva/dsvaa.c @@ -0,0 +1,92 @@ +/* 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 + + */ +#include "sva.h" +#include "svd.h" +#include "lapack.h" +#include <stdio.h> +#include <stdlib.h> +#include "string.h" +#include "matrixTranspose.h" + +#define eps 2.22044604925e-16 + +void dsvaa(int ninp,double *in1,int row,int col,double in2,double *out1, \ + double *out2,double *out3){ + + double tol; + double rk=0; + int N=col,M=row; + if(row == 0 && col == 0) return; + int i,j; + int arow; /*Actual row of given matrix*/ + int acol; /*Actual col of given matrix*/ + + /* Calculation of svd of a given matrix */ + double *U,*S,*V; + U = (double *)malloc((double)row*Min(row,col)*sizeof(double)); + S = (double *)malloc((double)Min(row,col)*Min(row,col)*sizeof(double)); + V = (double *)malloc((double)col*Min(row,col)*sizeof(double)); + + dsvda(in1,M,N,1,3,U,S,V); + + if (ninp == 1){ /* [u,s,v] = sva(A) when input is only matrix */ + tol = Max(row,col)*S[0]*eps; + rk = 0; + for(i=0;i<col;i++){ + if(S[i+i*row] > tol){ + rk+=1; + } + } + } + else{ /*[u,s,v] = sva(A,tol) when two input's are there */ + tol = in2; + if(tol > 1){ + rk = tol; + if(rk > Min(row,col)){ + printf("ERROR: Wrong value for input argument !"); + out1 = NULL; + out2 = NULL; + out3 = NULL; + return; + } + } + else{ + rk = 0; + for(i=0;i<col;i++){ + if(S[i+i*row] > tol){ + rk+=1; + } + } + } + } + arow = M; + acol = Min(M,N); + for(i=0;i<arow;i++){ + for(j=0;j<rk;j++){ + out1[i+j*row]=U[i+j*arow]; + } + } + arow = Min(M,N); + for(i=0;i<rk;i++){ + for(j=0;j<rk;j++){ + out2[i+j*(int)rk] = S[i+j*arow]; + } + } + arow = N; + acol = Min(M,N); + for(i=0;i<arow;i++){ + for(j=0;j<rk;j++){ + out3[i+j*arow] = V[i+j*arow]; + } + } +} diff --git a/2.3-1/src/c/linearAlgebra/svd/.1.c.swp b/2.3-1/src/c/linearAlgebra/svd/.1.c.swp Binary files differnew file mode 100644 index 00000000..81d9e9cf --- /dev/null +++ b/2.3-1/src/c/linearAlgebra/svd/.1.c.swp diff --git a/2.3-1/src/c/linearAlgebra/svd/dsvda.c b/2.3-1/src/c/linearAlgebra/svd/dsvda.c new file mode 100644 index 00000000..e6af3008 --- /dev/null +++ b/2.3-1/src/c/linearAlgebra/svd/dsvda.c @@ -0,0 +1,126 @@ +/* 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 + + */ + +/*Funtion to find singular value decomposition of given matrix */ + +#include "lapack.h" +#include <stdio.h> +#include <stdlib.h> +#include "string.h" +#include <math.h> +#include "svd.h" +#include "matrixTranspose.h" + +int min(int a,int b); +int max(int a,int b); + +extern double dgesvd_(char*,char*,int*,int*,double*,int*,double*,double*,int*,\ + double*,int*,double *,int*,int*); + +/* DGESVD computes the singular value decomposition (SVD) of a real + M-by-N matrix A, optionally computing the left and/or right singular + vectors. The SVD is written + + A = U * SIGMA * transpose(V) */ + +void dsvda(double *in1,int row,int col,double in2,double nout,double *out1, \ + double *out2,double *out3){ + + char JOBU,JOBVT; + int j,k; + int LDU=1; /*Leading Dimension of U */ + int LDVT=1; /*Leading Dimension of VT */ + int M = row; + int N = col; + double *buf; + double *S,*U,*VT; + double *WORK; + + if((nout > 1 && in2 == 1) && (M != N)){ /* [U,S,VT] = svd(x,'e') */ + if(M > N){ + JOBU = 'S'; + JOBVT = 'A'; + LDVT = N; + } + else{ + JOBU = 'A'; + JOBVT = 'S'; + LDVT = min(M,N); + } + LDU = M; + U = (double*) malloc((double) (LDU)*min(M,N)*sizeof(double)); + VT = (double*) malloc((double) (LDVT)*N*sizeof(double)); + } + else if(nout > 1){ /* [U,S,VT = svd(x)] */ + JOBU = 'A'; /*If JOBU = 'A', U contains the M-by-M orthogonal matrix U */ + JOBVT = 'A'; /*JOBVT = 'A': all N rows of V**T are returned in the array VT;*/ + LDU = M; + LDVT = N; + U = (double*) malloc((double) M*M*sizeof(double)); + VT = (double*) malloc((double) N*N*sizeof(double)); + } + else{ /* ans = svd(x) */ + JOBU = 'N'; + JOBVT = 'N'; + } + int LDA = max(1,M); + + /* Making a copy of input matrix */ + buf = (double*) malloc((double)M*N*sizeof(double)); + memcpy(buf,in1,M*N*sizeof(double)); + + S = (double*)malloc((double)min(col,row)*sizeof(double)); + + int LWORK = 5*min(M,N); + WORK = (double*)malloc((double)LWORK*sizeof(double)); + int INFO = 0; /*For successful exit */ + + dgesvd_(&JOBU,&JOBVT,&M,&N,buf,&LDA,S,U,&LDU,VT,&LDVT,WORK,&LWORK,&INFO); + /*Subroutine DGESVD from Lapack lib. */ + + if (nout == 1){ /* ans = svd(x)*/ + memcpy(out1,S,min(row,col)*sizeof(double)); + //printf("%lf %lf %lf",*(S),*(S+1),*(S+2)); + } /* [U,S,VT] = svd(x) */ + else if(in2 == 0 && nout > 1){ + memcpy(out1,U,LDU*M*sizeof(double)); + //memcpy(out3,VT,LDVT*min(row,col)*sizeof(double)); + for(j=0;j<M;j++){ + for(k=0;k<N;k++){ + if(j == k) *((out2+j*(min(M,N)))+k) = *(S+j); + else *((out2+j*(min(M,N)))+k) = 0; + } + } + dtransposea(VT,LDVT,N,out3); + } + else{ + memcpy(out1,U,LDU*min(row,col)*sizeof(double)); + for(j=0;j<min(M,N);j++){ + for(k=0;k<min(M,N);k++){ + if(j == k) *((out2+j*(min(M,N)))+k) = *(S+j); + else *((out2+j*(min(M,N)))+k) = 0; + } + } + dtransposea(VT,LDVT,N,out3); + } +} + +int min(int a,int b){ + if(a > b) return b; + return a; +} + +int max(int a,int b){ + if(a > b) return a; + return b; +} diff --git a/2.3-1/src/c/linearAlgebra/svd/zsvda.c b/2.3-1/src/c/linearAlgebra/svd/zsvda.c new file mode 100644 index 00000000..0d360222 --- /dev/null +++ b/2.3-1/src/c/linearAlgebra/svd/zsvda.c @@ -0,0 +1,173 @@ +/* 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 + + */ +#include "svd.h" +#include "lapack.h" +#include <stdio.h> +#include <stdlib.h> +#include "string.h" +#include "doubleComplex.h" +#include "matrixTranspose.h" +#include "conj.h" + +extern doubleComplex zgesvd_( char* , char* , int* , int* ,doubleComplex *,\ + int* , double* ,doubleComplex* , int* ,doubleComplex* , int* ,\ + doubleComplex* , int* , double* , int* ); + + +int Min(int a,int b){ + if(a > b) + return b; + return a; +} + +int Max(int a,int b){ + if(a > b) + return a; + else + return b; +} + +void zsvda(doubleComplex *in1,int row,int col,int in2,int nout, doubleComplex *out1,\ + doubleComplex *out2,doubleComplex *out3){ + + /* Allocating memory and copying the input in buf*/ + doubleComplex *buf; + buf = (doubleComplex *)malloc(row*col*sizeof(doubleComplex)); + memcpy(buf,in1,row*col*sizeof(doubleComplex)); + + /* Type of variable used */ + int i,j,k; + char JOBU,JOBVT; + int M = row; + int N = col; + int LDA,LDU,LDVT,LWORK,INFO; + + /*double precision array to store Sigma*/ + double *S; + S = (double *)malloc(Min(M,N)*sizeof(double)); + + /* amount of memory needed for work */ + LWORK = Max(1,2*Min(M,N)+Max(M,N)); + doubleComplex *WORK = malloc(Max(1,2*LWORK)*sizeof(doubleComplex)); + + double *RWORK; + RWORK = (double *)malloc(5*Min(M,N)*sizeof(double)); + + INFO = 0; + + if(nout == 1){ + JOBU = 'N'; + JOBVT = 'N'; + LDA = M; + LDU = M; + LDVT = N; + //doubleComplex *U,*VT; + //U = malloc(sizeof(doubleComplex)); + //VT = malloc(sizeof(doubleComplex)); + zgesvd_(&JOBU,&JOBVT,&M,&N,buf,&LDA,S,NULL,&LDU,NULL,&LDVT,WORK,&LWORK,RWORK,&INFO); + + //memcpy(out2,S,Min(M,N)*sizeof(double)); + for(i=0;i<Min(M,N);i++){ + out2[i] = DoubleComplex(S[i],0); + //out2[i] = S[i]; + //out2[i] = 0; + } + out1 = NULL; + out3 = NULL; + //for(i=0;i<Min(M,N);i++) printf("%lf ",S[i]); + //free(S); + } + else if(nout == 3){ + if(in2 == 0 || M == N){ + JOBU = 'A'; + JOBVT = 'A'; + LDA = M; + LDU = M; + LDVT = N; + doubleComplex *U = malloc(LDU*M*sizeof(doubleComplex)); + doubleComplex *VT = malloc(LDVT*N*sizeof(doubleComplex)); + + /*doubleComplex wopt; + LWORK = -1; + zgesvd_(&JOBU,&JOBVT,&M,&N,buf,&LDA,S,U,&LDU,VT,&LDVT,&wopt,&LWORK,RWORK,&INFO);*/ + + //LWORK = (int)zreals(wopt); + + WORK = (doubleComplex *)malloc(LWORK*sizeof(doubleComplex)); + zgesvd_(&JOBU,&JOBVT,&M,&N,buf,&LDA,S,U,&LDU,VT,&LDVT,WORK,&LWORK,RWORK,&INFO); + + memcpy(out1,U,LDU*Min(M,N)*sizeof(doubleComplex)); + //memcpy(out3,VT,N*N*sizeof(doubleComplex)); + for(i=0;i<N;i++){ + for(j=i;j<N;j++){ + out3[i+j*N] = zconjs(VT[j+i*N]); + out3[j+i*N] = zconjs(VT[i+j*N]); + } + } + //ztransposea(VT,LDVT,Min(M,N),out3); + /*for(i=0;i<N;i++){ + for(j=0;j<N;j++){ + printf("[ %lf %lf]",zreals(VT[i*N+j]),zimags(VT[i*N+j])); + } + printf("\n"); + }*/ + //free(U); + //free(VT); + } + else{ + LDA = M; + LDU = M; + if(M > N){ + JOBU = 'S'; + JOBVT = 'A'; + LDVT = N; + } + else{ + JOBU = 'A'; + JOBVT = 'S'; + LDVT = Min(M,N); + } + doubleComplex *U; + U = malloc(LDU*Min(M,N)*sizeof(doubleComplex)); + doubleComplex *VT; + VT = malloc(LDVT*N*sizeof(doubleComplex)); + zgesvd_(&JOBU,&JOBVT,&M,&N,buf,&LDA,S,U,&LDU,VT,&LDVT,WORK,&LWORK,RWORK,&INFO); + memcpy(out1,U,M*Min(M,N)*sizeof(doubleComplex)); + //ztransposea(VT,LDVT,Min(row,col),out3); + + /* These lines are added to patch an error of ZGESVD */ + /* + ij = i+(j-1)*N + ji = j+(i-1)*N + zstk(lV+ij-1) = conjg(zstk(lVT+ji-1)) + zstk(lV+ji-1) = conjg(zstk(lVT+ij-1)) + */ + for(i=0;i<Min(M,N);i++){ + for(j=0;j<N;j++){ + out3[j+i*N] = zconjs(VT[i+j*Min(M,N)]); + } + } + //free(U); + //free(VT); + } + /* output from zgesvd is copied to out2 variables in required format*/ + for(j=0;j<Min(M,N);j++){ + for(k=0;k<Min(M,N);k++){ + if(j == k) + out2[j*(Min(M,N))+k] = DoubleComplex(S[j],0); + else + out2[j*(Min(M,N))+k] = DoubleComplex(0,0); + } + } + } +} |