File: LapackOperations.cpp

package info (click to toggle)
sofa-framework 1.0~beta4-11
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 88,820 kB
  • ctags: 27,300
  • sloc: cpp: 151,126; ansic: 2,387; xml: 581; sh: 417; makefile: 68
file content (200 lines) | stat: -rwxr-xr-x 6,445 bytes parent folder | download | duplicates (5)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/******************************************************************************
*       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4      *
*                (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS                    *
*                                                                             *
* This library is free software; you can redistribute it and/or modify it     *
* under the terms of the GNU Lesser General Public License as published by    *
* the Free Software Foundation; either version 2.1 of the License, or (at     *
* your option) any later version.                                             *
*                                                                             *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details.                                                           *
*                                                                             *
* You should have received a copy of the GNU Lesser General Public License    *
* along with this library; if not, write to the Free Software Foundation,     *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
*******************************************************************************
*                               SOFA :: Modules                               *
*                                                                             *
* Authors: The SOFA Team and external contributors (see Authors.txt)          *
*                                                                             *
* Contact information: contact@sofa-framework.org                             *
******************************************************************************/

#include <sofa/component/linearsolver/LapackOperations.h>

//Lapack
#include <blas3pp.h>
#include <blas2pp.h>
#include <laslv.h>
#include <lapackpp.h>

namespace sofa
{

  namespace component
  {

    namespace linearsolver
    {


//cblas_dgemm : double precision, general matrices, multiplication matrix-matrix
//              alpha*op(A)*op(B) + beta*C : with op() meaning wether or not we transpose the matrix

void applyLapackDGEMM(	double* A, bool isTransposeA,  double* B, bool isTransposeB, double* C,
					    double alpha, double beta,
						unsigned int rowsA, unsigned int columnsA, unsigned int rowsB, unsigned int columnsB)
{
	LaGenMatDouble MA(A,rowsA, columnsA,true);
	LaGenMatDouble MB(B,rowsB, columnsB,true);
	LaGenMatDouble MC(C,rowsA, columnsB,true);
	Blas_Mat_Mat_Mult(MA, MB, MC, isTransposeA, isTransposeB, alpha, beta);
}


void applyLapackDGEMM(  FullMatrix<double> &A, bool isTransposeA,  FullMatrix<double> &B, bool isTransposeB, FullMatrix<double> &C, double alpha, double beta)
{
	unsigned int rowA,colA,rowB,colB;
	if (isTransposeA) {rowA=A.colSize(); colA=A.rowSize();}
	else              {rowA=A.rowSize(); colA=A.colSize();}

	if (isTransposeB) {rowB=B.colSize(); colB=B.rowSize();}
	else              {rowB=B.rowSize(); colB=B.colSize();}


	LaGenMatDouble MA(A[0],rowA, colA,true);
	LaGenMatDouble MB(B[0],rowB, colB,true);
	LaGenMatDouble MC(C[0],rowA, colB,true);
	Blas_Mat_Mat_Mult(MA, MB, MC, isTransposeA, isTransposeB, alpha, beta);
}


//cblas_dgemv : double precision, general matrices, multiplication matrix-vector
//              alpha*op(A)*x + beta*y : with op() meaning wether or not we transpose the matrix
void applyLapackDGEMV(  double* A, bool isTransposeA,   double* x, double* y,
						double alpha, double beta,
						unsigned int rowsA, unsigned int columnsA)
{
	LaGenMatDouble MA(A,rowsA, columnsA,true);
	if (isTransposeA){
		LaVectorDouble Vx(x,rowsA);
		LaVectorDouble Vy(y,columnsA);
		Blas_Mat_Trans_Vec_Mult(MA, Vx, Vy, alpha, beta);
	}else{
		LaVectorDouble Vx(x,columnsA);
		LaVectorDouble Vy(y,rowsA);
		Blas_Mat_Vec_Mult(MA, Vx, Vy, alpha, beta);
	}
}


void applyLapackDGEMV(  FullMatrix<double> &A, bool isTransposeA,   FullVector<double> &x, FullVector<double> &y,
						double alpha, double beta)
{
	LaGenMatDouble MA(A[0],A.rowSize(),A.colSize(),true);
	LaVectorDouble Vx(x.ptr(),x.size());
	LaVectorDouble Vy(y.ptr(),y.size());
	if (isTransposeA)
		Blas_Mat_Trans_Vec_Mult(MA, Vx, Vy, alpha, beta);
	else
		Blas_Mat_Vec_Mult(MA, Vx, Vy, alpha, beta);
}


double applyLapackDDOT( const unsigned int N,  double *X,   double *Y)
{
	LaVectorDouble Vx(X,N);
	LaVectorDouble Vy(Y,N);
	return Blas_Dot_Prod(Vx, Vy);
}


double applyLapackDDOT(   FullVector<double> &X,   FullVector<double> &Y)
{
	return applyLapackDDOT(X.size(), X.ptr(), Y.ptr());
}


//Solve A.X=B
void applyLapackDGESV( double* A, double *X, double* B, int dimA,  int columnsB)
{
	LaGenMatDouble MA(A,dimA, dimA,true);
	LaGenMatDouble MB(B,dimA, columnsB,true);
	LaGenMatDouble MX(X,dimA, columnsB,true);
	LaLULinearSolve(MA, MX, MB);
}


void applyLapackDGESV( FullMatrix<double>& A, FullMatrix<double>& X, FullMatrix<double>& B)
{
	LaGenMatDouble MA(A[0],A.rowSize(), A.colSize(),true);
	LaGenMatDouble MB(B[0],A.rowSize(), B.colSize(),true);
	LaGenMatDouble MX(X[0],A.rowSize(), B.colSize(),true);
	LaLULinearSolve(MA, MX, MB);
}


void printMatrix( FullMatrix<double> &M, unsigned  int row,unsigned int col) 
{
	std::streamsize precisionSize = std::cout.precision();
	std::cout.precision(6);
	std::ios_base::fmtflags flagPrecision = std::cout.setf(std::ios::fixed);


	for (unsigned int r=0;r<row;r++)
	{
		if (r!=0) std::cout << "|\n|";
		else      std::cout << "|";

		for (unsigned int c=0;c<col;++c)
		{
			std::cout << M.element(r,c) << "\t";
		}
	}

	std::cout << "|\n";
	std::cout.precision(precisionSize);
	std::cout.setf(flagPrecision);
}


void printMatrix( FullMatrix<double> &M) 
{
	printMatrix(M,M.rowSize(),M.colSize());
}


void printVector( FullVector<double> &V, unsigned  int row) 
{
	std::streamsize precisionSize = std::cout.precision();
	std::cout.precision(6);
	std::ios_base::fmtflags flagPrecision = std::cout.setf(std::ios::fixed);


	for (unsigned int r=0;r<row;r++)
	{
		if (r!=0) std::cout << "|\n|";
		else      std::cout << "|";

		std::cout << V.element(r) << "\t";
	}

	std::cout << "|\n";
	std::cout.precision(precisionSize);
	std::cout.setf(flagPrecision);
}


void printVector( FullVector<double> &V) 
{
	printVector(V, V.size());
}



    }
  }
}