81 for (
integer r = nrowd; r-- > 0; ) {
87 for (
integer k = ncol1; k-- > 0; ) {
88 dd[0] += mm1[k*ndim1]*mm2[k];
126 for (
integer r = nrowd; r-- > 0; ) {
131 for (
integer k = ncol1; k-- > 0; ) {
132 dd[0] += mm1[k*ndim1]*mm2[k];
152 ndim2, nrow2, ncol2, m2,
153 ndim3, nrow3, ncol3, m3,
154 ndimd, nrowd, ncold, d);
156 return gpc_mul(ndim1, nrow1, ncol1, m1,
157 ndim2, nrow2, ncol2, m2,
158 ndimd, nrowd, ncold, d);
192 for (
integer r = nrow1; r-- > 0; ) {
198 for (
integer k = ncol1; k-- > 0; ) {
199 dd[0] += mm1[k*ndim1]*mm2[k];
218 for (
integer j = ncols; j-- > 0; ) {
222 for (
integer i = nrows; i-- > 0; ) {
241 for (
integer j = ncols; j-- > 0; ) {
245 for (
integer i = nrows; i-- > 0; ) {
260 for (
integer i = size; i-- > 0; ) {
273 for (
integer i = size; i-- > 0; ) {
305 integer size = nout*pa+nin*(pb+1);
307 if (C != NULL && pa > 0) {
315 gpc_mcopy_t(size, nout, nout*pa, source, ndimA, dest);
319 source = Theta+nout*pa+nin*(pb+1);
322 source, ndimB, dest);
330 gpc_mcopy_t(size, nout, nin*pb, source, ndimB, dest);
334 source = Theta+nout*pa;
335 dest = P+nout*(s-1)+ndimP*(nin*(s-1));
348 for (
integer i = s-1; i > 0; i--) {
352 for (
integer j = 1; j <= pa-1; j++) {
357 A+nout*(i-1)+ndimA*(nout*(j-1));
361 A+nout*(s-1)+ndimA*(nout*(j-1));
367 ndimA, nout, nout, m2,
368 ndimA, nout, nout, m3,
369 ndimA, nout, nout, dest);
376 doublereal* dest = A+nout*(i-1)+ndimA*(nout*(pa-1));
379 doublereal* m2 = A+nout*(s-1)+ndimA*(nout*(pa-1));
382 ndimA, nout, nout, m2,
383 ndimA, nout, nout, dest);
387 for (
integer j = 1; j <= pb-1; j++) {
392 B+nout*(i-1)+ndimB*(nin*(j-1));
396 B+nout*(s-1)+ndimB*(nin*(j-1));
402 ndimB, nout, nin, m2,
403 ndimB, nout, nin, m3,
404 ndimB, nout, nin, dest);
411 doublereal* dest = B+nout*(i-1)+ndimB*(nin*(pb-1));
414 doublereal* m2 = B+nout*(s-1)+ndimB*(nin*(pb-1));
417 ndimB, nout, nin, m2,
418 ndimB, nout, nin, dest);
421 if (C != NULL && pa > 0) {
422 for (
integer j = 1; j <= pa-1; j++) {
427 C+nout*(i-1)+ndimC*(nout*(j-1));
431 C+nout*(s-1)+ndimC*(nout*(j-1));
437 ndimC, nout, nout, m2,
438 ndimC, nout, nout, m3,
439 ndimC, nout, nout, dest);
446 doublereal* dest = C+nout*(i-1)+ndimC*(nout*(pa-1));
449 doublereal* m2 = C+nout*(s-1)+ndimC*(nout*(pa-1));
452 ndimC, nout, nout, m2,
453 ndimC, nout, nout, dest);
466 P+nout*(i-1)+ndimP*(nin*(i-1));
468 gpc_mcopy(ndimP, nout, nin*(s-i), source,
476 doublereal* dest = P+nout*(i-1)+ndimP*(nin*(s-1));
479 doublereal* m2 = P+nout*(s-1)+ndimP*(nin*(s-1));
485 ndimP, nout, nin, m2,
486 ndimB, nout, nin, m3,
487 ndimP, nout, nin, dest);
499 __FC_DECL__(
dgesvd)(
char* jobu,
char* jobvt,
531 static char jobu =
'S';
532 static char jobvt =
'S';
534 __FC_DECL__(
dgesvd)(&jobu, &jobvt, &m, &n,
a, &lda, s,
535 u, &ldu, vt, &ldvt, work, &lwork, &info);
538 silent_cerr(
"warning: LAPACK libraries are not available," << std::endl
539 <<
"so the pseudo-inversion will not be performed" << std::endl);
550 for (
integer i = x; i-- > 0; ) {
559 for (
integer k = m; k-- > 0; ) {
565 for (
integer i = n; i-- > 0; ) {
568 for (
integer j = m; j-- > 0; ) {
571 for (
integer k = x; k-- > 0; ) {
572 aa[j] += vvt[k]*uu[k*ldu];
614 iMin = std::min(m, n);
615 iMax = std::max(m, n);
632 #ifndef OPTIMIZE_WORK
647 #ifndef OPTIMIZE_WORK
654 #ifndef OPTIMIZE_WORK
704 if ((A = m_get(m, n)) == MNULL) {
705 silent_cerr(
"A = m_get(" << m <<
',' << n <<
") failed" << std::endl);
709 if ((diag = v_get(std::min(m, n))) == VNULL) {
710 silent_cerr(
"diag = v_get(" << std::min(m, n) <<
") failed" << std::endl);
714 if ((x = v_get(n)) == VNULL) {
715 silent_cerr(
"x = v_get(" << n <<
") failed" << std::endl);
719 if ((b = v_get(m)) == VNULL) {
720 silent_cerr(
"b = v_get(" << m <<
") failed" << std::endl);
724 if ((pivot = px_get(n)) == PNULL) {
725 silent_cerr(
"pivot = px_get(" << n <<
") failed" << std::endl);
730 GPC_Meschach_QRinv::~GPC_Meschach_QRinv(
void)
749 for (
int j = n; j-- > 0; ) {
752 for (
int i = m; i-- > 0; ) {
757 QRCPfactor(A, diag, pivot);
760 for (
int j = m; j-- > 0; ) {
763 QRCPsolve(A, diag, pivot, b, x);
766 for (
int i = n; i-- > 0; ) {
786 : iNumOutputs(iNumOut),
794 dPeriodicFactor(dPF),
850 :
GPCDesigner(iNumOut, iNumIn, iOrdA, iOrdB, iPredS, iContrS, iContrS, 0, dPF),
851 iDim(iNumOutputs*iPredStep),
852 iTmpRows(iNumOutputs*(iPredStep-iPredHor)),
853 iTmpCols(iNumInputs*(iContrStep-0)),
857 #if !defined(USE_MESCHACH) && !defined(USE_LAPACK)
858 #error "need LAPACK or Meschach for pseudo-inversion"
865 GPC_Meschach_QRinv(iTmpRows, iTmpCols));
893 for (
integer j = i; j-- > 0; ) {
912 DeadBeat::~DeadBeat(
void)
918 DeadBeat::DesignControl(
const doublereal* pdTheta,
937 iNumOutputs, iNumInputs,
938 iPredStep, iOrderA, iOrderB,
942 integer info = pInv->Inv(iDim, iTmpRows, iTmpCols, pdPTmp);
945 silent_cerr(
"DeadBeat::DesignControl(): illegal value in "
946 << -info <<
"-th argument of dgesvd()" << std::endl);
948 }
else if (info > 0) {
949 silent_cerr(
"DeadBeat::DesignControl(): error in dgesvd()" << std::endl);
956 doublereal* p = pdPTmp+iTmpRows*(iNumInputs*(iContrStep-1));
957 for (
integer i = iTmpRows*iNumInputs; i-- > 0; ) {
962 doublereal cc = dPeriodicFactor*dPeriodicFactor;
963 for (
integer i = iNumInputs; i-- > 0; ) {
968 for (
integer j = iNumOutputs*iOrderA; j-- > 0; ) {
972 for (
integer k = iTmpRows; k-- > 0; ) {
979 p = pdbc+i*iNumInputs*iOrderB;
980 for (
integer j = iNumInputs*iOrderB; j-- > 0; ) {
983 for (
integer k = iTmpRows; k-- > 0; ) {
991 p = pdcc+i*iNumOutputs*iOrderA;
992 for (
integer j = iNumOutputs*iOrderA; j-- > 0; ) {
995 for (
integer k = iTmpRows; k-- > 0; ) {
1020 :
GPCDesigner(iNumOut, iNumIn, iOrdA, iOrdB, iPredS, iContrS, iPredH, iContrH,
1022 iDim(iNumOutputs*iPredStep),
1023 iTmpRows(iNumOutputs*(iPredStep-iPredHor)),
1024 iTmpCols(iNumInputs*iContrStep),
1037 #if !defined(USE_MESCHACH) && !defined(USE_LAPACK)
1038 #error "need LAPACK or Meschach for pseudoinverse"
1045 GPC_Meschach_QRinv(iTmpCols, iTmpCols));
1058 silent_cerr(
"unable to create GPCInv" << std::endl);
1080 for (
integer j = i; j-- > 0; ) {
1088 pdInvP = pdM+iTmpCols*iTmpCols;
1089 pdac = pdInvP+iTmpCols*iTmpRows;
1112 for (
integer j = ncolp; j-- > 0; ) {
1115 for (
integer i = ncolp; i-- > 0; ) {
1120 for (
integer k = s; k-- > 0; ) {
1123 for (
integer l = nout; l-- > 0; ) {
1129 mm[0] += pik[l]*pjk[l]*W[k];
1137 m[j] += lambda*R[j/nin];
1154 for (
integer i = nrowl; i-- > 0; ) {
1158 for (
integer j = ncolr; j-- > 0; ) {
1166 for (
integer k = ncoll; k-- > 0; ) {
1171 dij[0] += Li[k]*Rj[ndimr*k];
1217 silent_cerr(
"GPC::DesignControl(): illegal value in "
1218 << -info <<
"-th argument of dgesvd()" << std::endl);
1220 }
else if (info > 0) {
1221 silent_cerr(
"GPC::DesignControl(): error in dgesvd()" << std::endl);
1248 pmik[l] = pik[l]*
pdW[k];
1266 p[j] -= pm[k]*pa[k];
1277 p[j] -= pm[k]*pb[k];
1292 p[j] -= pm[k]*pc[k];
1320 doublereal d[nout*nout*pa*s+nout*nin*pb*s+nout*s*nin*s+nout*nout*pa*s];
1321 doublereal theta[nout*nout*pa+nout*nin*(pb+1)+nout*nout*pa] = {
1322 2., 0.,-1., 0., 1., 0.,-1., 0., 1., 0., -2., 0., 1., 0.,
1323 0., 2., 0.,-1., 0., 1., 0.,-1., 0., 1., 0.,-2., 0., 1.
1333 nout*s, d+nout*nout*pa*s,
1334 nout*s, d+nout*nout*pa*s+nout*nin*pb*s,
1335 nout*s, d+nout*nout*pa*s+nout*nin*pb*s+nout*s*nin*s,
1341 silent_cout(
"A:" << std::endl);
1342 for (
integer i = 0; i < nout*s; i++) {
1343 for (
integer j = 0; j < nout*pa; j++) {
1344 silent_cout(std::setw(8) << pA[i+nout*s*j]);
1346 silent_cout(std::endl);
1349 silent_cout(
"B:" << std::endl);
1350 for (
integer i = 0; i < nout*s; i++) {
1351 for (
integer j = 0; j < nin*pb; j++) {
1352 silent_cout(std::setw(8) << pB[i+nout*s*j]);
1354 silent_cout(std::endl);
1356 doublereal* pP = d+nout*nout*pa*s+nout*s*nin*pb;
1357 silent_cout(
"P:" << std::endl);
1358 for (
integer i = 0; i < nout*s; i++) {
1359 for (
integer j = 0; j < nin*s; j++) {
1360 silent_cout(std::setw(8) << pP[i+nout*s*j]);
1362 silent_cout(std::endl);
1364 doublereal* pC = d+nout*nout*pa*s+nout*s*nin*pb+nout*s*nin*s;
1365 silent_cout(
"C:" << std::endl);
1366 for (
integer i = 0; i < nout*s; i++) {
1367 for (
integer j = 0; j < nout*pa; j++) {
1368 silent_cout(std::setw(8) << pC[i+nout*s*j]);
1370 silent_cout(std::endl);
1377 GPC_Meschach_QRinv inv(tmprow, tmpcol);
1379 #error "Need Meschach"
1382 inv.Inv(s*nout, tmprow, tmpcol, pT);
1384 silent_cout(
"Inv(P'):" << std::endl);
1385 for (
integer i = 0; i < tmpcol; i++) {
1386 for (
integer j = 0; j < tmprow; j++) {
1387 silent_cout(std::setw(12) << pT[tmprow*i+j]);
1389 silent_cout(std::endl);
1392 DeadBeat db(nout, nin,
1400 db.DesignControl(theta, &pac, &pbc, &pmd, &pcc);
1402 silent_cout(
"Ac:" << std::endl);
1403 for (
integer i = 0; i < nin; i++) {
1404 for (
integer j = 0; j < nout*pa; j++) {
1405 silent_cout(std::setw(12) << pac[i*nout*pa+j]);
1407 silent_cout(std::endl);
1409 silent_cout(
"Bc:" << std::endl);
1410 for (
integer i = 0; i < nin; i++) {
1411 for (
integer j = 0; j < nin*pb; j++) {
1412 silent_cout(std::setw(12) << pbc[i*nin*pb+j]);
1414 silent_cout(std::endl);
1416 silent_cout(
"Cc:" << std::endl);
1417 for (
integer i = 0; i < nin; i++) {
1418 for (
integer j = 0; j < nout*pa; j++) {
1419 silent_cout(std::setw(12) << pcc[i*nout*pa+j]);
1421 silent_cout(std::endl);
1423 silent_cout(
"Md:" << std::endl);
1424 for (
integer i = 0; i < nin; i++) {
1425 for (
integer j = 0; j < tmprow; j++) {
1426 silent_cout(std::setw(12) << pmd[i*tmprow+j]);
1428 silent_cout(std::endl);
1431 silent_cerr(
"cannot use GPC/Deadbeat" << std::endl);
virtual ~GPCDesigner(void)
#define MBDYN_EXCEPT_ARGS
static int gpc_zero(integer size, doublereal *v)
static int gpc_build_matrices(integer ndimA, doublereal *A, integer ndimB, doublereal *B, integer ndimP, doublereal *P, integer ndimC, doublereal *C, integer nout, integer nin, integer s, integer pa, integer pb, doublereal *Theta)
int main(int argc, char *argv[])
#define SAFEDELETEARR(pnt)
static int gpc_mcopy(integer ndims, integer nrows, integer ncols, const doublereal *s, integer ndimd, doublereal *d)
GPC(integer iNumOut, integer iNumIn, integer iOrdA, integer iOrdB, integer iPredS, integer iContrS, integer iPredH, integer iContrH, doublereal *pW, doublereal *pR, DriveCaller *pDC, doublereal dPF, flag f)
doublereal dPeriodicFactor
static int gpc_addmul(integer ndim1, integer nrow1, integer ncol1, const doublereal *m1, integer ndim2, integer nrow2, integer ncol2, const doublereal *m2, integer ndim3, integer nrow3, integer ncol3, const doublereal *m3, integer ndimd, integer nrowd, integer ncold, doublereal *d)
virtual integer Inv(integer ndima, integer nrowa, integer ncola, doublereal *a)=0
GradientExpression< UnaryExpr< FuncFabs, Expr > > fabs(const GradientExpression< Expr > &u)
static integer gpc_pinv(integer lda, integer m, integer n, doublereal *a, doublereal *s, integer ldu, doublereal *u, integer ldvt, doublereal *vt, integer lwork, doublereal *work)
int dgesvd(char *jobu, char *jobvt, integer *m, integer *n, doublereal *a, integer *lda, doublereal *s, doublereal *u, integer *ldu, doublereal *vt, integer *ldvt, doublereal *work, integer *lwork, integer *info)
Matrix< T, 2, 2 > Inv(const Matrix< T, 2, 2 > &A)
static int gpc_mul(integer ndim1, integer nrow1, integer ncol1, const doublereal *m1, integer ndim2, integer nrow2, integer ncol2, const doublereal *m2, integer ndimd, integer nrowd, integer ncold, doublereal *d)
static int gpc_set(integer size, doublereal *v, const doublereal d)
integer Inv(integer ndima, integer nrowa, integer ncola, doublereal *a)
#define ASSERT(expression)
void DesignControl(const doublereal *pdTheta, doublereal **ppda=NULL, doublereal **ppdb=NULL, doublereal **ppdm=NULL, doublereal **ppdc=NULL)
#define SAFENEWWITHCONSTRUCTOR(pnt, item, constructor)
static std::stack< cleanup * > c
GPCDesigner(integer iNumOut, integer iNumIn, integer iOrdA, integer iOrdB, integer iPredS, integer iContrS, integer iPredH, integer iContrH, doublereal dPF)
doublereal dGet(const doublereal &dVar) const
#define SAFENEWARR(pnt, item, sz)
static int gpc_add_mul(integer ndim1, integer nrow1, integer ncol1, const doublereal *m1, integer ndim2, integer nrow2, integer ncol2, const doublereal *m2, integer ndim3, integer nrow3, integer ncol3, const doublereal *m3, integer ndimd, integer nrowd, integer ncold, doublereal *d)
static const doublereal a
static int gpc_mcopy_t(integer ndims, integer nrows, integer ncols, const doublereal *s, integer ndimd, doublereal *d)
virtual void DesignControl(const doublereal *, doublereal **ppda=NULL, doublereal **ppdb=NULL, doublereal **ppdm=NULL, doublereal **ppdc=NULL)
GPC_LAPACK_pinv(integer n, integer m)