[814] | 1 | #include <iostream.h>
|
---|
[775] | 2 | #include "intflapack.h"
|
---|
[814] | 3 | #include <typeinfo>
|
---|
[775] | 4 |
|
---|
| 5 | extern "C" {
|
---|
| 6 | void sgesv_(int_4* n, int_4* nrhs, r_4* a, int_4* lda,
|
---|
| 7 | int_4* ipiv, r_4* b, int_4* ldb, int_4* info);
|
---|
| 8 | void dgesv_(int_4* n, int_4* nrhs, r_8* a, int_4* lda,
|
---|
| 9 | int_4* ipiv, r_8* b, int_4* ldb, int_4* info);
|
---|
| 10 | void cgesv_(int_4* n, int_4* nrhs, complex<r_4>* a, int_4* lda,
|
---|
| 11 | int_4* ipiv, complex<r_4>* b, int_4* ldb, int_4* info);
|
---|
| 12 | void zgesv_(int_4* n, int_4* nrhs, complex<r_8>* a, int_4* lda,
|
---|
| 13 | int_4* ipiv, complex<r_8>* b, int_4* ldb, int_4* info);
|
---|
| 14 | }
|
---|
| 15 |
|
---|
[814] | 16 | template <class T>
|
---|
| 17 | TArray<T>& LapackServer<T>::LinSolve(TArray<T>& a, TArray<T> & b)
|
---|
| 18 | {
|
---|
| 19 | if ( ( a.NbDimensions() != 2 ) || ( b.NbDimensions() != 2 ) )
|
---|
| 20 | throw(SzMismatchError("LapackServer::LinSolve(a,b) a Or b NbDimensions() != 2"));
|
---|
| 21 |
|
---|
| 22 | uint_4 rowa = a.RowsKA();
|
---|
| 23 | uint_4 cola = a.ColsKA();
|
---|
| 24 | uint_4 rowb = b.RowsKA();
|
---|
| 25 | uint_4 colb = b.ColsKA();
|
---|
| 26 | if ( a.Size(rowa) != a.Size(cola))
|
---|
| 27 | throw(SzMismatchError("LapackServer::LinSolve(a,b) a Not a square Array"));
|
---|
| 28 | if ( a.Size(rowa) != b.Size(rowa))
|
---|
| 29 | throw(SzMismatchError("LapackServer::LinSolve(a,b) RowSize(a <> b) "));
|
---|
| 30 |
|
---|
| 31 | if (!a.IsPacked(rowa) || !b.IsPacked(rowb))
|
---|
| 32 | throw(SzMismatchError("LapackServer::LinSolve(a,b) a Or b Not Packed Columns "));
|
---|
| 33 |
|
---|
| 34 | int_4 n = a.Size(rowa);
|
---|
| 35 | int_4 nrhs = b.Size(colb);
|
---|
| 36 | int_4 lda = a.Step(cola);
|
---|
| 37 | int_4 ldb = b.Step(colb);
|
---|
| 38 | int_4 info;
|
---|
| 39 | int_4* ipiv = new int_4[n];
|
---|
| 40 |
|
---|
| 41 | if (typeid(T) == typeid(r_4) )
|
---|
| 42 | sgesv_(&n, &nrhs, (r_4 *)a.Data(), &lda, ipiv, (r_4 *)b.Data(), &ldb, &info);
|
---|
| 43 | else if (typeid(T) == typeid(r_8) )
|
---|
| 44 | dgesv_(&n, &nrhs, (r_8 *)a.Data(), &lda, ipiv, (r_8 *)b.Data(), &ldb, &info);
|
---|
| 45 | else if (typeid(T) == typeid(complex<r_4>) )
|
---|
| 46 | cgesv_(&n, &nrhs, (complex<r_4> *)a.Data(), &lda, ipiv,
|
---|
| 47 | (complex<r_4> *)b.Data(), &ldb, &info);
|
---|
| 48 | else if (typeid(T) == typeid(complex<r_8>) )
|
---|
| 49 | zgesv_(&n, &nrhs, (complex<r_8> *)a.Data(), &lda, ipiv,
|
---|
| 50 | (complex<r_8> *)b.Data(), &ldb, &info);
|
---|
| 51 | else {
|
---|
| 52 | delete[] ipiv;
|
---|
| 53 | string tn = typeid(T).name();
|
---|
| 54 | cerr << " LapackServer::LinSolve(a,b) - Unsupported DataType T = " << tn << endl;
|
---|
| 55 | throw TypeMismatchExc("LapackServer::LinSolve(a,b) - Unsupported DataType (T)");
|
---|
| 56 | }
|
---|
| 57 | delete[] ipiv;
|
---|
| 58 | return(b);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[775] | 61 | void rztest_lapack(TArray<r_4>& aa, TArray<r_4>& bb)
|
---|
| 62 | {
|
---|
| 63 | if ( aa.NbDimensions() != 2 ) throw(SzMismatchError("rztest_lapack(TMatrix<r_4> A Not a Matrix"));
|
---|
| 64 | if ( aa.SizeX() != aa.SizeY()) throw(SzMismatchError("rztest_lapack(TMatrix<r_4> A Not a square Matrix"));
|
---|
| 65 | if ( bb.NbDimensions() != 2 ) throw(SzMismatchError("rztest_lapack(TMatrix<r_4> A Not a Matrix"));
|
---|
[788] | 66 | if ( bb.SizeX() != aa.SizeX() ) throw(SzMismatchError("rztest_lapack(TMatrix<r_4> A <> B "));
|
---|
[775] | 67 | if ( !bb.IsPacked() || !bb.IsPacked() )
|
---|
| 68 | throw(SzMismatchError("rztest_lapack(TMatrix<r_4> Not packed A or B "));
|
---|
| 69 |
|
---|
[788] | 70 | int_4 n = aa.SizeX();
|
---|
| 71 | int_4 nrhs = bb.SizeY();
|
---|
[775] | 72 | int_4 lda = n;
|
---|
[788] | 73 | int_4 ldb = bb.SizeX();
|
---|
[775] | 74 | int_4 info;
|
---|
| 75 | int_4* ipiv = new int_4[n];
|
---|
| 76 | sgesv_(&n, &nrhs, aa.Data(), &lda, ipiv, bb.Data(), &ldb, &info);
|
---|
[814] | 77 | delete[] ipiv;
|
---|
[775] | 78 | cout << "rztest_lapack/Info= " << info << endl;
|
---|
| 79 | cout << aa << "\n" << bb << endl;
|
---|
| 80 | return;
|
---|
| 81 | }
|
---|
[814] | 82 |
|
---|
| 83 | ///////////////////////////////////////////////////////////////
|
---|
| 84 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
| 85 | #pragma define_template LapackServer<r_4>
|
---|
| 86 | #pragma define_template LapackServer<r_8>
|
---|
| 87 | #pragma define_template LapackServer< complex<r_4> >
|
---|
| 88 | #pragma define_template LapackServer< complex<r_8> >
|
---|
| 89 | #endif
|
---|
| 90 |
|
---|
| 91 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
| 92 | template class LapackServer<r_4>;
|
---|
| 93 | template class LapackServer<r_8>;
|
---|
| 94 | template class LapackServer< complex<r_4> >;
|
---|
| 95 | template class LapackServer< complex<r_8> >;
|
---|
| 96 | #endif
|
---|
| 97 |
|
---|
| 98 | #if defined(OS_LINUX)
|
---|
| 99 | // Pour le link avec f2c sous Linux
|
---|
| 100 | extern "C" {
|
---|
| 101 | void MAIN__();
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | void MAIN__()
|
---|
| 105 | {
|
---|
| 106 | cerr << "MAIN__() function for linking with libf2c.a " << endl;
|
---|
| 107 | cerr << " This function should never be called !!! " << endl;
|
---|
| 108 | throw PError("MAIN__() should not be called - see intflapack.cc");
|
---|
| 109 | }
|
---|
| 110 | #endif
|
---|