ee5a5cd99f8c065e0e4bc6f33f075da5977dea94
[alexxy/gromacs.git] / src / gmxlib / gmx_lapack / sorgbr.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012, by the GROMACS development team, led by
5  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6  * others, as listed in the AUTHORS file in the top-level source
7  * directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 #include "gmx_lapack.h"
36 #include "lapack_limits.h"
37
38 void
39 F77_FUNC(sorgbr,SORGBR)(const char *vect,
40         int *m,
41         int *n,
42         int *k,
43         float *a,
44         int *lda,
45         float *tau,
46         float *work,
47         int *lwork,
48         int *info)
49 {
50   int wantq,iinfo,j,i,i1,wrksz;
51   int mn = (*m < *n) ? *m : *n;
52
53   wantq = (*vect=='Q' || *vect=='q');
54
55   *info = 0;
56   wrksz = mn*DORGBR_BLOCKSIZE;
57   if(*lwork==-1) {
58     work[0] = wrksz;
59     return;
60   }
61   
62   if(*m==0 || *n==0)
63     return;
64
65   if(wantq) {
66     if(*m>=*k)
67       F77_FUNC(sorgqr,SORGQR)(m,n,k,a,lda,tau,work,lwork,&iinfo);
68     else {
69       for(j=*m;j>=2;j--) {
70         a[(j-1)*(*lda)+0] = 0.0;
71         for(i=j+1;i<=*m;i++)
72           a[(j-1)*(*lda)+(i-1)] = a[(j-2)*(*lda)+(i-1)]; 
73       }
74       a[0] = 1.0;
75       for(i=2;i<=*m;i++)
76         a[i-1] = 0.0;
77       if(*m>1) {
78         i1 = *m-1;
79         F77_FUNC(sorgqr,SORGQR)(&i1,&i1,&i1,&(a[*lda+1]),lda,tau,work,lwork,&iinfo);
80       }
81     }
82   } else {
83     if(*k<*n)
84       F77_FUNC(sorglq,SORGLQ)(m,n,k,a,lda,tau,work,lwork,&iinfo);
85     else {
86       a[0] = 1.0;
87       for(i=2;i<=*m;i++)
88         a[i-1] = 0.0;
89       for(j=2;j<=*n;j++) {
90         for(i=j-1;i>=2;i--)
91           a[(j-1)*(*lda)+(i-1)] = a[(j-1)*(*lda)+(i-2)]; 
92         a[(j-1)*(*lda)+0] = 0.0;
93       }
94       if(*n>1) {
95         i1 = *n-1;
96         F77_FUNC(sorglq,SORGLQ)(&i1,&i1,&i1,&(a[*lda+1]),lda,tau,work,lwork,&iinfo);
97       }
98     }
99   }
100   work[0] = wrksz;
101   return;
102 }
103