8 #ifdef USING_NEW_SKYLMAT 30 static LoggerPtr logger(Logger::getLogger(
"pz.matrix.tpzskylmatrix"));
49 InitializeElem(skyline,fStorage,fElem);
56 InitializeElem(skyline,fStorage,fElem);
63 int64_t size = this->fElem.NElements();
65 PZError <<
"\nFATAL ERROR at " << __PRETTY_FUNCTION__ <<
"\n";
69 for(int64_t i = 0; i < size; i++){
70 if((this->fElem[i]-this->fElem[0]) != (B.
fElem[i]-B.
fElem[0])){
71 PZError <<
"\nFATAL ERROR at " << __PRETTY_FUNCTION__ <<
"\n";
79 const int64_t n = this->fStorage.NElements();
80 for(int64_t i = 0; i < n; i++)
81 this->fStorage[i] += TVar(k) * B.
fStorage[i];
95 memcpy(&fStorage[0], &(skylmat->
fStorage[0]) , fStorage.NElements()*
sizeof(TVar));
106 return PutVal(c, r, value);
109 int64_t sz = Size(c);
115 cerr <<
"TPZSkylMatrix::PutVal Size" << sz;
122 int64_t index = r + sz - 1 - c;
123 fElem[c][index] = value;
135 unsigned dim = this->Dim();
137 if(r >= dim || c >= dim || r < 0 || c < 0) {
138 cerr <<
"TPZSkylMatrix::GetVal index out of range row = " << r
139 <<
" col = " << c << endl;
144 int64_t sz = Size(c);
146 int64_t index = r + sz - 1 - c;
147 return (fElem[c][index]);
151 if(this->gZero != TVar(0.)) {
152 cerr <<
"TPZSkylMatrix gZero = " << this->gZero << endl;
156 return(this->gZero );
171 int64_t sz = Size(c);
178 int64_t index = r + sz - 1 - c;
180 return fElem[c][index];
185 const TVar alpha,
const TVar beta ,
const int opt)
const 203 cerr <<
"x.Cols = " << x.
Cols() <<
" y.Cols()"<< y.
Cols() <<
" z.Cols() " << z.
Cols()
204 <<
" x.Rows() " << x.
Rows() <<
" y.Rows() "<< y.
Rows() <<
" z.Rows() "<< z.
Rows() << endl;
208 this->PrepareZ(y,z,beta,opt);
210 int64_t rows = this->
Rows();
211 int64_t xcols = x.
Cols();
213 for (ic = 0; ic < xcols; ic++) {
214 for( r = 0 ; r < rows ; r++ ) {
215 int64_t offset = Size(r);
217 const TVar *p = &x.
g((r-offset+1),ic);
218 TVar *diag = fElem[r];
219 TVar *diaglast = fElem[r+1]-1;
220 while( diag < diaglast ) {
224 if( diag == diaglast )
227 z(r,ic) += val*alpha;
229 TVar *zp = &z((r-offset+1),ic);
232 while( diag < diaglast ) {
233 *zp += alpha * *diag++ *
val;
253 if ( this->Dim() != A.
Dim() )
257 ComputeMaxSkyline(*
this,A,skylinesize);
265 for (int64_t col = 0; col < this->Dim(); col++) {
267 if ( Size(col) > A.
Size(col) ) {
269 elemMax = fElem[col];
270 sizeMin = A.
Size(col);
271 elemMin = A.
fElem[col];
274 sizeMax = A.
Size(col);
275 elemMax = A.
fElem[col];
277 elemMin = fElem[col];
280 TVar *dest =
res.fElem[col];
282 for ( ; i < sizeMin; i++ )
283 *dest++ = (*elemMax++) + (*elemMin++);
284 for ( ; i < sizeMax; i++ )
285 *dest++ = *elemMax++;
295 if ( this->Dim() != A.
Dim() )
299 ComputeMaxSkyline(*
this,A,skylinesize);
302 for ( int64_t col = 0; col < this->fRow; col++ ) {
303 int64_t sizeThis = Size(col);
304 TVar *elemThis = fElem[col];
305 int64_t sizeA = A.
Size(col);
306 TVar *elemA = A.
fElem[col];
308 TVar *dest =
res.fElem[col];
310 for ( i = 0; (i < sizeThis) && (i < sizeA); i++ )
311 *dest++ = (*elemThis++) - (*elemA++);
314 for ( ; i < sizeThis; i++ ) *dest++ = *elemThis++;
316 for ( ; i < sizeA; i++ ) *dest++ = -(*elemA++);
326 if ( this->Dim() != A.
Dim() )
338 if ( this->Dim() != A.
Dim() )
351 int64_t nelem =
res.fStorage.NElements();
352 TVar *elemRes =
res.fElem[0];
354 for (int64_t i=0; i<nelem; i++) {
370 int64_t nelem = fStorage.NElements();
371 TVar *elemRes = fElem[0];
373 for (int64_t i=0; i<nelem; i++) {
377 this->fDecomposed = 0;
386 if ( newDim == this->Dim() )
389 fElem.Resize(newDim+1);
392 int64_t min =
MIN( newDim, this->Dim() );
393 for (int64_t i = min+1; i <= newDim; i++ )
394 fElem[i] = fElem[i-1];
397 fStorage.Resize(fElem[newDim]-fElem[0]);
398 this->fRow = this->fCol = newDim;
399 this->fDecomposed = 0;
406 if ( newDim == this->Dim() ) {
412 fElem.Resize(newDim);
414 this->fRow = this->fCol = newDim;
415 this->fDecomposed = 0;
423 this->fDecomposed = 0;
424 this->fDefPositive = 0;
434 for(int64_t i=0; i<dim; i++) {
435 nelem += i-skyline[i]+1;
445 int64_t nel = NumElements(skyline);
450 point[0] = &storage[0];
451 point[dim] = &storage[0]+nel;
457 for(int64_t i=1; i<dim+1; i++) {
458 point[i] = point[i-1] + (i-1)-skyline[i-1]+1;
466 if (first.
Rows() != second.
Rows()) {
467 cout<<
"ComputeMaxSkyline : incompatible dimension";
470 int64_t dim = first.
Rows();
473 for(int64_t i=0; i<dim; i++) {
474 int64_t sz_first = first.
Size(i);
475 int64_t sz_secon = second.
Size(i);
476 if (sz_first > sz_secon)
477 res[i] = i-(sz_first-1);
479 res[i] = i-(sz_secon-1);
486 REAL &
tol,
const int FromCurrent,
const int direction)
489 cout <<
"TPZMatrix::SolveSOR called with residual and F equal, no solution\n";
492 REAL res = 2.*tol+1.;
493 if(residual) res =
Norm(*residual);
497 TVar over = overrelax;
498 int64_t r = this->Dim();
499 int64_t c = F.
Cols();
500 int64_t i,ifirst = 0, ilast = r, iinc = 1;
501 if(direction == -1) {
507 for(it=0; it<numiterations && res >
tol; it++) {
510 for(int64_t ic=0; ic<c; ic++) {
515 for(i=ifirst; i!=ilast; i+= iinc) {
517 int64_t offset = Size(i);
519 TVar *diaglast = (fElem[i+1]-1);
520 TVar *scratchp = &scratch(i-offset+1,ic);
523 int64_t lastid = diaglast-p;
525 for(
id=0;
id<=lastid;
id++)
526 *scratchp++ -= *p++ * val;
534 for(i=ifirst; i!=ilast; i+= iinc) {
536 int64_t offset = Size(i);
537 TVar val = scratch(i,ic);
538 TVar *p = &result(i-offset+1,ic);
539 TVar *diag = fElem[i];
540 TVar *diaglast = (fElem[i+1]-1);
541 while( diag < diaglast )
542 val -= *diag++ * *p++;
544 result(i,ic) += val*over/ (*diag);
552 for(i=ifirst; i!=ilast; i+= iinc) {
554 int64_t offset = Size(i);
555 TVar val = scratch(i,ic);
556 TVar *p = &result(i-offset+1,ic);
557 TVar *diag = fElem[i];
558 TVar *diaglast = (fElem[i+1]-1);
559 while( diag < diaglast )
560 val -= *diag++ * *p++;
567 for(i=ifirst; i!=ilast; i+= iinc) {
569 int64_t offset = Size(i);
571 TVar *diaglast = (fElem[i+1]-1);
572 TVar *scratchp = &scratch(i-offset+1,ic);
574 TVar val = scratch(i,ic);
575 val -= *diaglast * result(i,ic);
577 val = over * val / *diaglast;
580 TVar *diag = fElem[i];
581 while( diag < diaglast )
582 *scratchp++ -= *diag++ *
val;
589 this->Residual(result,F,*residual);
601 std::cout <<
"Passei por aqui\n";
603 int64_t icoef,jcoef,ieq,jeq,ieqs,jeqs;
604 for(icoef=0; icoef<nelem; icoef++) {
605 ieq = destination[icoef];
606 ieqs = source[icoef];
607 for(jcoef=icoef; jcoef<nelem; jcoef++) {
608 jeq = destination[jcoef];
609 jeqs = source[jcoef];
610 int64_t row(ieq), col(jeq);
613 this->Swap(&row, &col);
616 if(row >= this->Dim() || col >= this->Dim()) {
617 cout <<
"TPZSkylMatrix::GetVal index out of range row = " <<
618 row <<
" col = " << col << endl;
622 int64_t sz = Size(col);
624 int64_t index = row + sz - 1 - col;
626 fElem[col][index] += elmat(ieqs,jeqs);
639 int64_t icoef,jcoef,ieq,jeq,ieqs,jeqs;
640 for(icoef=0; icoef<nelem; icoef++) {
641 ieq = destination[icoef];
642 ieqs = source[icoef];
643 for(jcoef=icoef; jcoef<nelem; jcoef++) {
644 jeq = destination[jcoef];
645 jeqs = source[jcoef];
646 int64_t row(ieq), col(jeq);
649 this->Swap(&row, &col);
652 if(row >= this->Dim() || col >= this->Dim()) {
653 cout <<
"TPZSkylMatrix::GetVal index out of range row = " <<
654 row <<
" col = " << col << endl;
658 int64_t sz = Size(col);
660 int64_t index = row + sz - 1 - col;
662 fElem[col][index] += elmat(ieqs,jeqs);
674 int64_t icoef,jcoef,ieq,jeq,ieqs,jeqs;
675 for(icoef=0; icoef<nelem; icoef++) {
676 ieq = destination[icoef];
677 ieqs = source[icoef];
678 for(jcoef=icoef; jcoef<nelem; jcoef++) {
679 jeq = destination[jcoef];
680 jeqs = source[jcoef];
681 int64_t row(ieq), col(jeq);
684 this->Swap(&row, &col);
687 if(row >= this->Dim() || col >= this->Dim()) {
688 cout <<
"TPZSkylMatrix::GetVal index out of range row = " <<
689 row <<
" col = " << col << endl;
694 int64_t sz = Size(col);
696 int64_t index = row + sz - 1 - col;
698 fElem[col][index] += elmat(ieqs,jeqs);
738 if (this->fDecomposed )
741 #ifdef DUMP_BEFORE_DECOMPOSE 742 dump_matrix(
this,
"TPZSkylMatrix::Decompose_Cholesky()");
750 for ( int64_t k = 0; k <
dimension; k++ ) {
757 TVar *elem_k = fElem[k];
758 TVar *end_k = fElem[k+1]-1;
760 #pragma clang loop vectorize_width(2) 761 for ( ; elem_k < end_k; elem_k++ )
762 sum += (*elem_k) * (*elem_k);
764 elem_k = fElem[k+1]-1;
765 TVar* first_k = fElem[k];
766 TVar* last_k = elem_k;
767 int64_t k_sz = last_k - first_k;
770 pivot = *elem_k - sum;
773 if (pivot < TVar(1.e-10)) {
774 singular.push_back(k);
778 pivot =
sqrt( pivot );
784 for ( int64_t j = 2; i<
dimension; j++,i++ ) {
786 TVar* upd_elem = fElem[i+1] - j;
787 TVar* first_i = fElem[i];
788 TVar* last_i = upd_elem;
790 if (first_i < last_i) {
795 int64_t min_sz = last_i - first_i;
799 TVar* ip = last_i - min_sz;
800 TVar* kp = last_k - min_sz;
802 #pragma clang loop vectorize_width(2) 803 for(
unsigned l=0; l<min_sz; l++)
804 sum += (*ip++) * (*kp++);
807 *upd_elem = (*upd_elem -sum) / pivot;
809 else if (first_i == last_i) {
811 *upd_elem = *upd_elem / pivot;
817 if(this->
Rows() && (GetVal(this->
Rows()-1,this->
Rows()-1)) < TVar(1.e-15)) {
818 singular.push_back(this->
Rows()-1);
819 PutVal(this->
Rows()-1,this->
Rows()-1,1.);
823 this->fDefPositive = 1;
855 if (this->fDecomposed )
858 #ifdef DUMP_BEFORE_DECOMPOSE 859 dump_matrix(
this,
"TPZSkylMatrix::Decompose_Cholesky()");
868 TVar minpivot = 10000.;
869 int64_t dimension = this->Dim();
871 for ( int64_t k = 0; k <
dimension; k++ ) {
878 TVar *elem_k = fElem[k];
879 TVar *end_k = fElem[k+1]-1;
880 #pragma clang loop vectorize_width(2) 881 for ( ; elem_k < end_k; elem_k++ )
882 sum += (*elem_k) * (*elem_k);
884 elem_k = fElem[k+1]-1;
885 TVar* first_k = fElem[k];
886 TVar* last_k = elem_k;
887 int64_t k_sz = last_k - first_k;
890 pivot = *elem_k - sum;
891 minpivot = minpivot < pivot ? minpivot : pivot;
892 if ( pivot < TVar(0.) ||
IsZero(pivot) ) {
893 cout <<
"TPZSkylMatrix::DecomposeCholesky! Matrix is not positive definite" << pivot << endl;
897 pivot =
sqrt( pivot );
903 for ( int64_t j = 2; i<
dimension; j++,i++ ) {
905 TVar* upd_elem = fElem[i+1] - j;
906 TVar* first_i = fElem[i];
907 TVar* last_i = upd_elem;
909 if (first_i < last_i) {
914 int64_t min_sz = last_i - first_i;
918 TVar* ip = last_i - min_sz;
919 TVar* kp = last_k - min_sz;
921 #pragma clang loop vectorize_width(2) 922 for(
unsigned l=0; l<min_sz; l++)
923 sum += (*ip++) * (*kp++);
926 *upd_elem = (*upd_elem -sum) / pivot;
928 else if (first_i == last_i) {
930 *upd_elem = *upd_elem / pivot;
937 this->fDefPositive = 1;
939 std::cout << __PRETTY_FUNCTION__ <<
" minpivot " << minpivot << std::endl;
976 if( this->fDecomposed ==
ELDLt)
979 if( this->fDecomposed ) {
980 TPZMatrix<TVar>::Error(__PRETTY_FUNCTION__,
" Decompose_LDLt <Matrix already Decomposed with different decomposition>" );
983 #ifdef DUMP_BEFORE_DECOMPOSE 984 dump_matrix(
this,
"TPZSkylMatrix::Decompose_LDLt(singular)");
991 int64_t j,l,minj,minl,minrow,dimension = this->Dim();
994 while(j < dimension) {
1001 minrow = (minj<minl)? minl:minj;
1005 elj = fElem[j+1] - (j+1) + minrow ;
1006 ell = fElem[l+1] - (l+1) + minrow ;
1010 sum += *elj++ * *ell++ * *(fElem[k+1]-1);
1020 singular.push_back(l);
1029 singular.push_back(this->
Rows()-1);
1030 PutVal(this->
Rows()-1,this->
Rows()-1,1.);
1033 this->fDecomposed =
ELDLt;
1034 this->fDefPositive = 0;
1039 template<
class TVar>
1042 if( this->fDecomposed ==
ELDLt)
1044 if ( this->fDecomposed )
1045 TPZMatrix<TVar>::Error(__PRETTY_FUNCTION__,
" Decompose_LDLt <Matrix already Decomposed with different decomposition>" );
1047 #ifdef DUMP_BEFORE_DECOMPOSE 1048 dump_matrix(
this,
"TPZSkylMatrix::Decompose_LDLt()");
1053 int64_t j,l,minj,minl,minrow,dimension = this->Dim();
1056 diag[j] = *(fElem[j+1]-1);
1062 while(j < dimension) {
1072 minrow = (minj<minl)? minl:minj;
1075 elj = fElem[j]+minrow-minj;
1076 ell = fElem[l]+minrow-minl;
1077 TVar *diagptr = &diag[k];
1080 sum += *elj++ * *ell++ * *diagptr++;
1084 if(ell != elj) *elj /= *ell;
1087 std::stringstream sout;
1088 sout <<
"col = " << j <<
" diagonal " << *elj;
1093 cout <<
"TPZSkylMatrix pivot = " << *elj << endl;
1094 cout <<
"TPZSkylMatrix::DecomposeLDLt zero pivot\n";
1095 cout <<
"j = " << j <<
" l = " << l << endl;
1104 this->fDecomposed =
ELDLt;
1105 this->fDefPositive = 0;
1123 if ( (B->
Rows() != this->Dim()) || this->fDecomposed !=
ECholesky)
1126 int n = this->Dim();
1129 for (
int i=0; i<n+1; i++)
1130 pntr[i] = fElem[i] - &fStorage[0] + 1;
1132 char desc[4] = {
'T',
'L',
'N',
'F' };
1136 for(
int j=0; j<B->
Cols(); j++)
1137 mkl_dskysv(&trans, &n, &alfa, desc, &fStorage[0], &pntr[0], &(*B)(0,j), &(*B)(0,j));
1148 if ( (B->
Rows() != this->Dim()) || this->fDecomposed !=
ECholesky)
1151 int n = this->Dim();
1154 for (
int i=0; i<n+1; i++)
1155 pntr[i] = fElem[i] - &fStorage[0] + 1;
1157 char desc[4] = {
'T',
'L',
'N',
'F' };
1161 for(
int j=0; j<B->
Cols(); j++)
1162 mkl_dskysv(&trans, &n, &alfa, desc, &fStorage[0], &pntr[0], &(*B)(0,j), &(*B)(0,j));
1170 if ( (B->
Rows() != this->Dim()) || this->fDecomposed !=
ECholesky)
1173 int n = this->Dim();
1176 for (
int i=0; i<n+1; i++)
1177 pntr[i] = fElem[i] - &fStorage[0] + 1;
1179 char desc[4] = {
'T',
'L',
'N',
'F' };
1183 for(
int j=0; j<B->
Cols(); j++)
1184 mkl_sskysv(&trans, &n, &alfa, desc, &fStorage[0], &pntr[0], &(*B)(0,j), &(*B)(0,j));
1195 if ( (B->
Rows() != this->Dim()) || this->fDecomposed !=
ECholesky)
1198 int n = this->Dim();
1201 for (
int i=0; i<n+1; i++)
1202 pntr[i] = fElem[i] - &fStorage[0] + 1;
1204 char desc[4] = {
'T',
'L',
'N',
'F' };
1208 for(
int j=0; j<B->
Cols(); j++)
1209 mkl_sskysv(&trans, &n, &alfa, desc, &fStorage[0], &pntr[0], &(*B)(0,j), &(*B)(0,j));
1217 template<
class TVar>
1220 if ( (B->
Rows() != this->Dim()) || this->fDecomposed !=
ECholesky)
1221 TPZMatrix<TVar>::Error(__PRETTY_FUNCTION__,
" TPZSkylMatrix::Subst_Forward not decomposed with cholesky");
1223 #ifdef DUMP_BEFORE_SUBST 1224 dump_matrices(
this, B,
"TPZSkylMatrix::Subst_Forward(B)");
1228 int64_t dimension=this->Dim();
1229 for ( int64_t j = 0; j < B->
Cols(); j++ ) {
1233 while (k<dimension && (*B)(k,j) == TVar(0)) k++;
1240 TVar *elem_ki = fElem[k];
1241 TVar *end_ki = fElem[k+1]-1;
1242 int64_t array_sz = end_ki - elem_ki;
1243 TVar* BPtr = &(*B)(k,j) - array_sz;
1245 for (
unsigned l=0; l<array_sz; l++)
1246 sum += (*elem_ki++) * (*BPtr++);
1250 *BPtr = (*BPtr - sum) / fElem[k+1][-1];
1259 template<
class TVar>
1262 if ( (B->
Rows() != this->Dim()) || this->fDecomposed !=
ECholesky)
1263 TPZMatrix<TVar>::Error(__PRETTY_FUNCTION__,
"TPZSkylMatrix::Subst_Backward not decomposed with cholesky");
1265 #ifdef DUMP_BEFORE_SUBST 1266 dump_matrices(
this, B,
"TPZSkylMatrix::Subst_Backward(B)");
1269 int64_t Dimension = this->Dim();
1273 for (int64_t j = 0; j < B->
Cols(); j++ ) {
1275 int64_t k = Dimension-1;
1278 while (k>0 && (*B)(k,j) == TVar(0.)) k--;
1281 for ( ; k > 0; k--) {
1284 TVar *elem_ki = fElem[k];
1285 TVar *end_ki = fElem[k+1]-1;
1286 int64_t array_sz = end_ki - elem_ki;
1288 TVar *BPtr = &(*B)(k,j);
1289 *BPtr = *BPtr / *end_ki;
1292 BPtr = BPtr - array_sz;
1294 for (
unsigned l=0; l<array_sz; l++) {
1295 *BPtr++ -= ((*elem_ki++) * val);
1300 for(int64_t j = 0; j < B->
Cols(); j++)
1301 (*B)(0,j) /= fElem[0][0];
1308 template<
class TVar>
1312 if ( (B->
Rows() != this->Dim()) || (this->fDecomposed !=
ELDLt && this->fDecomposed !=
ELU) )
1315 int64_t dimension =this->Dim();
1316 for ( int64_t k = 0; k <
dimension; k++ ) {
1317 for ( int64_t j = 0; j < B->
Cols(); j++ ) {
1321 TVar *elem_ki = fElem[k];
1322 TVar *end_ki = fElem[k+1]-1;
1323 int64_t array_sz = end_ki - elem_ki;
1324 TVar* BPtr = &(*B)(k,j) - array_sz;
1325 for (
unsigned l=0; l<array_sz; l++)
1326 sum += (*elem_ki++) * (*BPtr++);
1338 template<
class TVar>
1341 if ( (B->
Rows() != this->Dim()) || this->fDecomposed !=
ELDLt)
1344 int64_t dimension = this->Dim();
1346 for (int64_t j = 0; j < B->
Cols(); j++ ) {
1347 TVar *BPtr = &(*B)(0,j);
1350 while(k < dimension) {
1352 *BPtr++ /= *(fElem[k+1]-1);
1360 template<
class TVar>
1363 if ( (B->
Rows() != this->Dim()) || !this->fDecomposed || this->fDecomposed ==
ECholesky) {
1367 int64_t Dimension = this->Dim();
1369 for ( int64_t k = Dimension-1; k > 0; k-- ) {
1370 for ( int64_t j = 0; j < B->
Cols(); j++ ) {
1372 TVar *elem_ki = fElem[k];
1373 TVar *end_ki = fElem[k+1]-1;
1374 int64_t array_sz = end_ki - elem_ki;
1375 TVar* BPtr = &(*B)(k,j) - array_sz;
1376 TVar val = (*B)(k,j);
1378 for (
unsigned l=0; l<array_sz; l++) {
1379 *BPtr++ -= (*elem_ki++) * val;
1388 template<
class TVar>
1392 this->fStorage.Resize(0);
1393 this->fElem.Resize(0);
1394 this->fRow = this->fCol = 0;
1395 this->fDecomposed = 0;
1399 template<
class TVar>
1402 int64_t dimension = A.
Dim();
1407 if(fStorage.NElements())
1408 firstp = &fStorage[0];
1417 template <
class TVar>
1421 buf.
Read( fStorage);
1428 fElem.Resize(this->
Rows()+1);
1429 for (int64_t i=0; i<this->
Rows()+1; i++) {
1430 fElem[i] = skyl[i] + ptr;
1434 template <
class TVar>
1438 buf.
Write( fStorage);
1444 for (int64_t i=0; i<this->
Rows()+1; i++) {
1445 skyl[i] = fElem[i] - ptr;
1450 template <
class TVar>
1454 buf.
Write( fStorage);
1460 for (int64_t i=0; i<this->
Rows()+1; i++) {
1461 skyl[i] = fElem[i] - ptr;
1466 template<
class TVar>
1471 int64_t skprev, skcol;
1474 skprev = SkyHeight(prevcol);
1475 skcol = SkyHeight(col);
1477 ptrprev = Diag(prevcol);
1480 if((prevcol-skprev) > (col-skcol)){
1481 minline = prevcol - skprev;
1484 minline = col - skcol;
1486 if(minline > prevcol) {
1487 cout <<
"error condition\n";
1491 TVar *run1 = ptrprev - (prevcol-minline);
1492 TVar *run2 = ptrcol - (col-minline);
1495 while(run1 != ptrprev)
1496 sum += (*run1++)*(*run2++);
1524 template<
class TVar>
1529 int64_t skprev, skcol;
1532 skprev = SkyHeight(prevcol);
1533 skcol = SkyHeight(col);
1535 ptrprev = Diag(prevcol);
1538 if((prevcol-skprev) > (col-skcol)){
1539 minline = prevcol - skprev;
1542 minline = col - skcol;
1544 if(minline > prevcol) {
1545 cout <<
"error condition\n";
1549 TVar *run1 = ptrprev - (prevcol-minline);
1550 TVar *run2 = ptrcol - (col-minline);
1553 while(run1 != ptrprev)
1554 sum += (*run1++)*(*run2++);
1561 if ( pivot < TVar(1.e-10) ) {
1563 std::stringstream sout;
1564 sout <<
"equation " << col <<
" is singular pivot " << pivot;
1567 singular.push_back(col);
1592 template<
class TVar>
1598 int64_t skprev, skcol;
1601 skprev = SkyHeight(prevcol);
1602 skcol = SkyHeight(col);
1604 ptrprev = Diag(prevcol);
1607 if((prevcol-skprev) > (col-skcol)){
1608 minline = prevcol - skprev;
1611 minline = col - skcol;
1613 if(minline > prevcol) {
1614 cout <<
"error condition\n";
1619 TVar *run1 = ptrprev - 1;
1620 TVar *run2 = ptrcol - ((col-prevcol)+1);
1621 TVar *lastptr = ptrprev - (prevcol-minline+1);
1623 TVar *modify = ptrcol-(col-prevcol);
1625 while(run1 > lastptr)
1626 sum += (*run1--)*(*run2--);
1628 int64_t n=lastptr-run1;
1629 sum = cblas_ddot(n,run1-(n-1),1,run2-(n-1),1);
1633 *modify /= *ptrprev;
1636 if ( *modify < TVar(1.e-25) ) {
1637 cout <<
"TPZSkylMatrix::DecomposeCholesky a matrix nao e positiva definida" << *modify << endl;
1640 *modify=
sqrt(*modify);
1644 template <
class TVar>
1646 if (nrow != ncol || !symmetric)
1652 fElem.resize(nrow+1);
1654 for (int64_t i=0; i<nrow; i++) {
1655 skyline[i]=(i*(rand()+RAND_MAX/2))/RAND_MAX;
1657 InitializeElem(skyline,fStorage,fElem);
1659 for (int64_t i=0; i<nrow; i++) {
1661 for (int64_t j=skyline[i]; j<i; j++) {
1662 TVar val = ((TVar)rand())/RAND_MAX;
1691 #else // USING_NEW_SKYLMAT 1714 static LoggerPtr logger(Logger::getLogger(
"pz.matrix.tpzskylmatrix"));
1717 using namespace std;
1728 template<
class TVar>
1736 template<
class TVar>
1746 template<
class TVar>
1752 PZError <<
"\nFATAL ERROR at " << __PRETTY_FUNCTION__ <<
"\n";
1756 for(int64_t i = 0; i < size; i++){
1758 PZError <<
"\nFATAL ERROR at " << __PRETTY_FUNCTION__ <<
"\n";
1767 for(int64_t i = 0L; i < n; i++) this->
fStorage[i] += TVar(k)*B.
fStorage[i];
1774 template<
class TVar>
1791 template<
class TVar>
1795 for (int64_t i = 0 ; i < this->
Rows() ; i++){
1796 if (skyline[i] < 0 || skyline[i] > i)
DebugStop();
1802 template<
class TVar>
1807 for(i=0; i<dim; i++) {
1808 nelem += i-skyline[i]+1;
1813 template<
class TVar>
1825 point[0] = &storage[0];
1826 point[dim] = &storage[0]+nel;
1830 for(i=1; i<dim+1; i++)
1831 point[i] = point[i-1]+(i-1)-skyline[i-1]+1;
1837 template<
class TVar>
1840 if (first.
Rows() != second.
Rows()) {
1841 cout<<
"ComputeMaxSkyline : incompatible dimension";
1844 int64_t i, dim = first.
Rows();
1847 for(i=1; i<dim+1; i++) {
1849 int64_t aux = ( first.
Size(i) > second.
Size(i) ) ? first.
Size(i) : second.
Size(i);
1854 template<
class TVar>
1857 int64_t row(r),col(c);
1858 if ( row > col ) this->
Swap( &row, &col );
1861 int64_t index = col - row;
1862 if ( index >=
Size(col) ) {
1867 return fElem[col][index];
1870 template<
class TVar>
1875 template<
class TVar>
1882 #define DECOMPOSE_CHOLESKY_OPT2 1886 #ifdef SKYLMATRIX_PUTVAL_OPT1 1887 #pragma message ( "warning: Using experimental version of TPZSkylMatrix<TVAr>::PutVal(...)" ) 1890 template<
class TVar>
1895 if (r > c)
return PutVal(c, r, value);
1898 int64_t index = c - r;
1901 if (index >=
Size(c)) {
1903 cout <<
"TPZSkylMatrix::PutVal Size" <<
Size(c);
1911 fElem[c][index] = value;
1918 template<
class TVar>
1923 int64_t row(r),col(c);
1925 this->
Swap( &row, &col );
1928 int64_t index = col - row;
1932 cout <<
"TPZSkylMatrix::PutVal Size" <<
Size(col);
1935 }
else if(index >=
Size(col))
return 1;
1936 fElem[col][index] = value;
1943 template<
class TVar>
1945 const TVar alpha,
const TVar beta ,
const int opt)
const {
1956 cout <<
"x.Cols = " << x.
Cols() <<
" y.Cols()"<< y.
Cols() <<
" z.Cols() " << z.
Cols() <<
" x.Rows() " << x.
Rows() <<
" y.Rows() "<< y.
Rows() <<
" z.Rows() "<< z.
Rows() << endl;
1960 int64_t rows = this->
Rows();
1961 int64_t xcols = x.
Cols();
1963 for (ic = 0; ic < xcols; ic++) {
1964 for( r = 0 ; r < rows ; r++ ) {
1965 int64_t offset =
Size(r);
1967 const TVar *p = &x.
g((r-offset+1),ic);
1968 TVar *diag =
fElem[r] + offset-1;
1969 TVar *diaglast =
fElem[r];
1970 while( diag > diaglast ) {
1971 val += *diag-- * *p;
1974 if( diag == diaglast ) val += *diag * *p;
1975 z(r,ic) += val*alpha;
1976 TVar *zp = &z((r-offset+1),ic);
1978 diag =
fElem[r] + offset-1;
1979 while( diag > diaglast ) {
1980 *zp += alpha * *diag-- *
val;
1987 template<
class TVar>
1990 REAL &tol,
const int FromCurrent,
const int direction) {
1992 if(residual == &F) {
1993 cout <<
"TPZMatrix::SolveSOR called with residual and F equal, no solution\n";
1996 REAL res = 2.*tol+1.;
1997 if(residual) res =
Norm(*residual);
2001 TVar over = overrelax;
2002 int64_t r = this->
Dim();
2003 int64_t c = F.
Cols();
2004 int64_t i,ifirst = 0, ilast = r, iinc = 1;
2005 if(direction == -1) {
2011 for(it=0; it<numiterations && res >
tol; it++) {
2014 for(int64_t ic=0; ic<c; ic++) {
2015 if(direction == 1) {
2019 for(i=ifirst; i!=ilast; i+= iinc) {
2021 int64_t offset =
Size(i);
2024 TVar *diaglast =
fElem[i];
2025 TVar *scratchp = &scratch(i-offset+1,ic);
2027 diag =
fElem[i] + offset-1;
2028 int64_t lastid = diag-diaglast;
2030 for(
id=0;
id<=lastid;
id++) *(scratchp+
id) -= *(diag-id) * val;
2038 for(i=ifirst; i!=ilast; i+= iinc) {
2040 int64_t offset =
Size(i);
2041 TVar val = scratch(i,ic);
2042 TVar *p = &result(i-offset+1,ic);
2043 TVar *diag =
fElem[i] + offset-1;
2044 TVar *diaglast =
fElem[i];
2045 while( diag > diaglast ) val -= *diag-- * *p++;
2046 res +=
abs(val*val);
2047 result(i,ic) += val*over/ (*diag);
2055 for(i=ifirst; i!=ilast; i+= iinc) {
2057 int64_t offset =
Size(i);
2058 TVar val = scratch(i,ic);
2059 TVar *p = &result(i-offset+1,ic);
2060 TVar *diag =
fElem[i] + offset-1;
2061 TVar *diaglast =
fElem[i];
2062 while( diag > diaglast ) val -= *diag-- * *p++;
2064 scratch(i,ic) =
val;
2069 for(i=ifirst; i!=ilast; i+= iinc) {
2071 int64_t offset =
Size(i);
2074 TVar *diaglast =
fElem[i];
2075 TVar *scratchp = &scratch(i-offset+1,ic);
2077 TVar val = scratch(i,ic);
2078 val -= *diaglast * result(i,ic);
2079 res +=
abs(val*val);
2080 val = over * val / *diaglast;
2081 result(i,ic) +=
val;
2083 diag =
fElem[i] + offset-1;
2084 while( diag > diaglast ) *scratchp++ -= *diag-- *
val;
2091 this->
Residual(result,F,*residual);
2097 #ifdef SKYLMATRIX_GETVAL_OPT1 2098 #pragma message ( "warning: Using experimental version of TPZSkylMatrix<TVAr>::GetVal(...)" ) 2099 template<
class TVar>
2103 if (r > c)
return GetVal(c,r);
2104 unsigned dim = this->
Dim();
2106 if(r >= dim || c >= dim || r < 0 || c < 0) {
2107 cout <<
"TPZSkylMatrix::GetVal index out of range row = " << r
2108 <<
" col = " << c << endl;
2113 int64_t index = c - r;
2114 if ( index <
Size(c) ) {
2115 return (
fElem[c][index]);
2118 if(this->
gZero != TVar(0.)) {
2119 cout <<
"TPZSkylMatrix gZero = " << this->
gZero << endl;
2122 return(this->
gZero );
2128 template<
class TVar>
2133 int64_t row(r),col(c);
2135 this->
Swap( &row, &col );
2137 if(row >= this->
Dim() || col >= this->
Dim() || row < 0 || col<0) {
2138 cout <<
"TPZSkylMatrix::GetVal index out of range row = " << row
2139 <<
" col = " << col << endl;
2143 int64_t index = col - row;
2146 if ( index <
Size(col) )
2147 return(
fElem[col][index] );
2149 if(this->
gZero != TVar(0.)) {
2150 cout <<
"TPZSkylMatrix gZero = " << this->
gZero << endl;
2153 return(this->
gZero );
2162 template<
class TVar>
2174 template<
class TVar>
2178 if ( this->
Dim() != A.
Dim() )
2190 for ( int64_t col = 0; col < this->
Dim(); col++ )
2196 sizeMax =
Size(col);
2197 elemMax =
fElem[col];
2198 sizeMin = A.
Size(col);
2199 elemMin = A.
fElem[col];
2203 sizeMax = A.
Size(col);
2204 elemMax = A.
fElem[col];
2205 sizeMin =
Size(col);
2206 elemMin =
fElem[col];
2212 TVar *dest = res.
fElem[col];
2214 for ( i = 0; i < sizeMin; i++ )
2215 *dest++ = (*elemMax++) + (*elemMin++);
2216 for ( ; i < sizeMax; i++ )
2217 *dest++ = *elemMax++;
2226 template<
class TVar>
2230 if ( this->
Dim() != A.
Dim() )
2237 for ( int64_t col = 0; col < this->
fRow; col++ )
2240 int64_t sizeThis =
Size(col);
2241 TVar *elemThis =
fElem[col];
2242 int64_t sizeA = A.
Size(col);
2243 TVar *elemA = A.
fElem[col];
2248 TVar *dest = res.
fElem[col];
2250 for ( i = 0; (i < sizeThis) && (i < sizeA); i++ ) *dest++ = (*elemThis++) - (*elemA++);
2251 if ( i == sizeA )
for ( ; i < sizeThis; i++ ) *dest++ = *elemThis++;
2252 else for ( ; i < sizeA; i++ ) *dest++ = -(*elemA++);
2258 template<
class TVar>
2264 int64_t icoef,jcoef,ieq,jeq,ieqs,jeqs;
2265 for(icoef=0; icoef<nelem; icoef++) {
2266 ieq = destination[icoef];
2267 ieqs = source[icoef];
2268 for(jcoef=icoef; jcoef<nelem; jcoef++) {
2269 jeq = destination[jcoef];
2270 jeqs = source[jcoef];
2271 int64_t row(ieq), col(jeq);
2274 this->
Swap(&row, &col);
2277 if(row >= this->
Dim() || col >= this->
Dim()) {
2278 cout <<
"TPZSkylMatrix::GetVal index out of range row = " <<
2279 row <<
" col = " << col << endl;
2284 int64_t index = col - row;
2287 if (index >=
Size(col)) {
2288 cerr <<
"Try TPZSkylMatrix gZero." << endl;
2293 fElem[col][index] += elmat(ieqs,jeqs);
2305 int64_t icoef,jcoef,ieq,jeq,ieqs,jeqs;
2306 for(icoef=0; icoef<nelem; icoef++) {
2307 ieq = destination[icoef];
2308 ieqs = source[icoef];
2309 for(jcoef=icoef; jcoef<nelem; jcoef++) {
2310 jeq = destination[jcoef];
2311 jeqs = source[jcoef];
2312 int64_t row(ieq), col(jeq);
2315 this->
Swap(&row, &col);
2318 if(row >= this->
Dim() || col >= this->
Dim()) {
2319 cout <<
"TPZSkylMatrix::GetVal index out of range row = " <<
2320 row <<
" col = " << col << endl;
2325 int64_t index = col - row;
2328 if (index >=
Size(col)) {
2329 std::cout <<
"Skyline wrongly configured " <<
" row " << row <<
" col " << col <<
" Size(col) " <<
Size(col) << std::endl;
2330 cerr <<
"Try TPZSkylMatrix gZero." << endl;
2331 std::cout << destination << std::endl;
2337 fElem[col][index] += elmat(ieqs,jeqs);
2350 int64_t icoef,jcoef,ieq,jeq,ieqs,jeqs;
2351 for(icoef=0; icoef<nelem; icoef++) {
2352 ieq = destination[icoef];
2353 ieqs = source[icoef];
2354 for(jcoef=icoef; jcoef<nelem; jcoef++) {
2355 jeq = destination[jcoef];
2356 jeqs = source[jcoef];
2357 int64_t row(ieq), col(jeq);
2360 this->
Swap(&row, &col);
2363 if(row >= this->
Dim() || col >= this->
Dim()) {
2364 cout <<
"TPZSkylMatrix::GetVal index out of range row = " <<
2365 row <<
" col = " << col << endl;
2370 int64_t index = col - row;
2373 if (index >=
Size(col)) {
2374 cerr <<
"Try TPZSkylMatrix gZero." << endl;
2380 fElem[col][index] += elmat(ieqs,jeqs);
2389 template<
class TVar>
2393 if ( this->
Dim() != A.
Dim() )
2404 template<
class TVar>
2408 if ( this->
Dim() != A.
Dim() )
2429 template<
class TVar>
2435 for ( int64_t col = 0; col < this->
Dim(); col++ )
2438 int64_t colsize =
Size(col);
2440 TVar *elemRes = res.
fElem[col];
2441 for ( int64_t i = 0; i < colsize; i++ )
2442 *elemRes++ *= value;
2455 template<
class TVar>
2465 int64_t col, colmax = this->
Dim();
2466 for (col=0; col<colmax; col++ )
2469 TVar *elem =
fElem[col];
2470 TVar *end =
fElem[col+1];
2471 while ( elem < end ) *elem++ *= value;
2486 template<
class TVar>
2488 if ( newDim == this->
Dim() )
2495 int64_t min =
MIN( newDim, this->
Dim() );
2497 for ( i = min+1; i <= newDim; i++ )
2502 this->fRow = this->
fCol = newDim;
2514 template<
class TVar>
2518 if ( newDim == this->
Dim() )
2527 this->fRow = this->
fCol = newDim;
2555 template<
class TVar>
2562 #ifdef DUMP_BEFORE_DECOMPOSE 2563 dump_matrix(
this,
"TPZSkylMatrix::Decompose_Cholesky(singular)");
2570 int64_t dimension = this->
Dim();
2575 for ( int64_t k = 0; k <
dimension; k++ )
2582 if (
Size(k) == 0 )
return( 0 );
2587 TVar *elem_k =
fElem[k]+1;
2589 #pragma clang loop vectorize_width(2) 2590 for ( ; elem_k < end_k; elem_k++ ) sum += (*elem_k) * (*elem_k);
2594 pivot =
fElem[k][0] - sum;
2597 singular.push_back(k);
2598 std::cout << __FUNCTION__ <<
" Singular equation pivot " << pivot <<
" k " << k << std::endl;
2608 for ( int64_t j = 2; i<
dimension; j++,i++ ) {
2611 if (
Size(i) > j ) {
2614 TVar *elem_i = &
fElem[i][j];
2615 TVar *end_i =
fElem[i+1];
2616 elem_k = &(
fElem[k][1]);
2618 unsigned max_l = end_i - elem_i;
2619 unsigned tmp = end_k - elem_k;
2620 if (tmp < max_l) max_l =
tmp;
2621 #pragma clang loop vectorize_width(2) 2622 for(
unsigned l=0; l<max_l; l++)
2623 sum += (*elem_i++) * (*elem_k++);
2625 fElem[i][j-1] = (
fElem[i][j-1] -sum) / pivot;
2626 }
else if (
Size(i) == j )
fElem[i][j-1] /= pivot;
2636 singular.push_back(this->
Rows()-1);
2666 template<
class TVar>
2673 #ifdef DUMP_BEFORE_DECOMPOSE 2674 dump_matrix(
this,
"TPZSkylMatrix::Decompose_Cholesky()");
2678 TVar minpivot = 10000.;
2679 int64_t dimension = this->
Dim();
2691 #ifdef DECOMPOSE_CHOLESKY_OPT2 2693 PZError <<
"warning: Using experimental (last_col check) version of TPZSkylMatrix<TVar>::Decompose_Cholesky()" << std::endl;
2696 int64_t y = dimension-1;
2697 for (int64_t k=(dimension-1); k>=0; k--) {
2698 int64_t min_row = k-
Size(k)+1;
2706 for ( int64_t k = 0; k <
dimension; k++ )
2713 if (
Size(k) == 0 )
return( 0 );
2719 TVar *elem_k =
fElem[k]+1;
2721 #pragma clang loop vectorize_width(2) 2722 for ( ; elem_k < end_k; elem_k++ ) sum += (*elem_k) * (*elem_k);
2726 pivot =
fElem[k][0] - sum;
2727 minpivot = minpivot < pivot ? minpivot : pivot;
2728 if ( pivot < ((TVar)0.) ||
IsZero(pivot) ) {
2729 cout <<
"TPZSkylMatrix::DecomposeCholesky a matrix nao e positiva definida" << pivot << endl;
2739 #ifdef DECOMPOSE_CHOLESKY_OPT2 2748 int64_t max_i = last_col[k];
2749 for ( int64_t j = 2; i <= max_i; j++,i++ ) {
2751 for ( int64_t j = 2; i<
dimension; j++,i++ ) {
2755 if (
Size(i) > j ) {
2758 TVar *elem_i = &
fElem[i][j];
2759 TVar *end_i =
fElem[i+1];
2760 elem_k = &(
fElem[k][1]);
2762 unsigned max_l = end_i - elem_i;
2763 unsigned tmp = end_k - elem_k;
2764 if (tmp < max_l) max_l =
tmp;
2765 #pragma clang loop vectorize_width(2) 2766 for(
unsigned l=0; l<max_l; l++)
2767 sum += (*elem_i++) * (*elem_k++);
2769 fElem[i][j-1] = (
fElem[i][j-1] -sum) / pivot;
2770 }
else if (
Size(i) == j )
fElem[i][j-1] /= pivot;
2784 std::cout << __PRETTY_FUNCTION__ <<
" minpivot " << minpivot << std::endl;
2809 template<
class TVar>
2818 TVar pivot = (TVar)0.;
2819 TVar minpivot = 10000.;
2820 int64_t dimension = this->
Dim();
2822 for (int64_t blk_i = 0; blk_i <
dimension; blk_i += blk_sz) {
2823 int64_t first_row = blk_i;
2824 int64_t first_col = blk_i;
2825 int64_t last_row = blk_i +
MIN(dimension,blk_i+blk_sz);
2828 for ( int64_t j = first_col; j <
dimension; j++) {
2833 int64_t first_i =
MAX(first_row, (j+1)-
Size(j));
2834 int64_t last_i =
MIN(last_row, j);
2835 for ( int64_t i = first_i; i < last_i; i++) {
2841 TVar* u_ij = &
fElem[j][I];
2844 TVar *elem_kj = u_ij+1;
2845 TVar *end_kj =
fElem[j+1];
2846 TVar *elem_ki = &
fElem[i][1];
2847 TVar *end_ki =
fElem[i+1];
2852 unsigned max_l = end_kj - elem_kj;
2853 unsigned tmp = end_ki - elem_ki;
2854 if (tmp < max_l) max_l =
tmp;
2855 #pragma clang loop vectorize_width(2) 2856 for(
unsigned l=0; l<max_l; l++)
2857 sum += (*elem_kj++) * (*elem_ki++);
2859 *u_ij = (*u_ij - sum) / pivot;
2866 TVar* u_jj = &
fElem[j][0];
2867 TVar *elem_k =
fElem[j]+1;
2868 TVar *end_k =
fElem[j+1];
2869 #pragma clang loop vectorize_width(2) 2870 for ( ; elem_k < end_k; elem_k++ ) sum += (*elem_k) * (*elem_k);
2871 pivot = *u_jj - sum;
2873 minpivot = minpivot < pivot ? minpivot : pivot;
2875 if ( pivot < ((TVar)0.) ||
IsZero(pivot) ) {
2876 cout <<
"TPZSkylMatrix::DecomposeCholesky a matrix nao e positiva definida" << pivot << endl;
2880 *u_jj =
sqrt( pivot );
2891 template<
class TVar>
2898 TPZMatrix<TVar>::Error(__PRETTY_FUNCTION__,
"Decompose_LDLt <Matrix already Decomposed with different decomposition>" );
2901 #ifdef DUMP_BEFORE_DECOMPOSE 2902 dump_matrix(
this,
"TPZSkylMatrix::Decompose_LDLt(singular)");
2909 int64_t j,l,minj,minl,minrow,dimension = this->
Dim();
2912 while(j < dimension) {
2917 minrow = (minj<minl)? minl:minj;
2920 elj =
fElem[j]+j-minrow;
2921 ell =
fElem[l]+l-minrow;
2926 sum += *elj-- * *ell-- * *(
fElem[k++]);
2929 if(ell != elj) *elj /= *ell;
2931 singular.push_back(l);
2941 singular.push_back(this->
Rows()-1);
2950 template<
class TVar>
2957 TPZMatrix<TVar>::Error(__PRETTY_FUNCTION__,
"Decompose_LDLt <Matrix already Decomposed with different decomposition>" );
2959 #ifdef DUMP_BEFORE_DECOMPOSE 2960 dump_matrix(
this,
"TPZSkylMatrix::Decompose_LDLt()");
2965 int64_t j,l,minj,minl,minrow,dimension = this->
Dim();
2969 diag[j] = *
fElem[j];
2976 while(j < dimension) {
2986 minrow = (minj<minl)? minl:minj;
2989 elj =
fElem[j]+j-minrow;
2990 ell =
fElem[l]+l-minrow;
2991 TVar *diagptr = &diag[k];
2996 sum += *elj-- * *ell-- * *diagptr++;
3000 if(ell != elj) *elj /= *ell;
3003 std::stringstream sout;
3004 sout <<
"col = " << j <<
" diagonal " << *elj;
3009 cout <<
"TPZSkylMatrix pivot = " << *elj << endl;
3010 cout <<
"TPZSkylMatrix::DecomposeLDLt zero pivot\n";
3011 cout <<
"j = " << j <<
" l = " << l << endl;
3032 template<
class TVar>
3039 #ifdef DUMP_BEFORE_SUBST 3040 dump_matrices(
this, B,
"TPZSkylMatrix::Subst_Forward(B)");
3044 int64_t dimension=this->
Dim();
3045 for ( int64_t j = 0; j < B->
Cols(); j++ )
3048 while (k<dimension && (*B)(k,j) == TVar(0)) {
3057 TVar *elem_ki =
fElem[k]+1;
3058 TVar *end_ki =
fElem[k+1];
3059 TVar* BPtr = &(*B)(k,j);
3066 while(elem_ki < end_ki) sum += (*elem_ki++) * (*--BPtr);
3072 *BPtr /=
fElem[k][0];
3084 template<
class TVar>
3089 TPZMatrix<TVar>::Error(__PRETTY_FUNCTION__,
"TPZSkylMatrix::Subst_Backward not decomposed with cholesky");
3091 #ifdef DUMP_BEFORE_SUBST 3092 dump_matrices(
this, B,
"TPZSkylMatrix::Subst_Backward(B)");
3094 int64_t Dimension = this->
Dim();
3095 if(!Dimension)
return 1;
3097 for ( j = 0; j < B->
Cols(); j++ )
3099 int64_t k = Dimension-1;
3100 while (k>0 && (*B)(k,j) == TVar(0.)) {
3110 TVar *elem_ki =
fElem[k];
3111 TVar *end_ki =
fElem[k+1];
3112 TVar *BPtr = &(*B)(k,j);
3113 *BPtr /= *elem_ki++;
3120 while(elem_ki < end_ki) *--BPtr -= (*elem_ki++) * val;
3123 for( j = 0; j< B->
Cols(); j++) (*B)(0,j) /=
fElem[0][0];
3133 template<
class TVar>
3139 int64_t dimension =this->
Dim();
3140 for ( int64_t k = 0; k <
dimension; k++ ) {
3141 for ( int64_t j = 0; j < B->
Cols(); j++ ) {
3145 TVar *elem_ki =
fElem[k]+1;
3146 TVar *end_ki =
fElem[k+1];
3147 TVar *BPtr = &(*B)(k,j);
3148 while(elem_ki < end_ki) sum += (*elem_ki++) * (*--BPtr);
3165 template<
class TVar>
3169 int64_t dimension = this->
Dim();
3173 for ( int64_t j = 0; j < B->
Cols(); j++ ) {
3174 TVar *BPtr = &(*B)(0,j);
3176 while(k < dimension) *BPtr++ /= *(
fElem[k++]);
3186 template<
class TVar>
3193 int64_t Dimension = this->
Dim();
3194 for ( int64_t k = Dimension-1; k > 0; k-- ) {
3195 for ( int64_t j = 0; j < B->
Cols(); j++ ) {
3199 TVar *elem_ki =
fElem[k]+1;
3200 TVar *end_ki =
fElem[k+1];
3201 TVar *BPtr = &(*B)(k,j);
3205 while(elem_ki < end_ki) *--BPtr -= (*elem_ki++) * val;
3218 template<
class TVar>
3232 template<
class TVar>
3239 this->fRow = this->
fCol = 0;
3247 template<
class TVar>
3251 int64_t dimension = A.
Dim();
3265 template<
class TVar>
3269 template <
class TVar>
3281 for (int64_t i=0; i<this->
Rows()+1; i++) {
3282 fElem[i] = skyl[i] + ptr;
3286 template <
class TVar>
3296 for (int64_t i=0; i<this->
Rows()+1; i++) {
3297 skyl[i] =
fElem[i] - ptr;
3303 template<
class TVar>
3307 int64_t skprev, skcol;
3313 ptrprev =
Diag(prevcol);
3316 if((prevcol-skprev) > (col-skcol)){
3317 minline = prevcol - skprev;
3320 minline = col - skcol;
3322 if(minline > prevcol) {
3323 cout <<
"error condition\n";
3327 TVar *run1 = ptrprev + (prevcol-minline);
3328 TVar *run2 = ptrcol + (col-minline);
3331 while(run1 != ptrprev) sum += (*run1--)*(*run2--);
3356 template<
class TVar>
3360 int64_t skprev, skcol;
3366 ptrprev =
Diag(prevcol);
3369 if((prevcol-skprev) > (col-skcol)){
3370 minline = prevcol - skprev;
3373 minline = col - skcol;
3375 if(minline > prevcol) {
3376 cout <<
"error condition\n";
3380 TVar *run1 = ptrprev + (prevcol-minline);
3381 TVar *run2 = ptrcol + (col-minline);
3384 while(run1 != ptrprev) sum += (*run1--)*(*run2--);
3390 if (
fabs(pivot) <
fabs((TVar)1.e-10) ) {
3392 std::stringstream sout;
3393 sout <<
"equation " << col <<
" is singular pivot " << pivot;
3396 singular.push_back(col);
3423 template<
class TVar>
3429 int64_t skprev, skcol;
3435 ptrprev =
Diag(prevcol);
3438 if((prevcol-skprev) > (col-skcol)){
3439 minline = prevcol - skprev;
3442 minline = col - skcol;
3444 if(minline > prevcol) {
3445 cout <<
"error condition\n";
3449 TVar *run1 = ptrprev + 1;
3450 TVar *run2 = ptrcol + (col-prevcol)+1;
3451 TVar *lastptr = ptrprev + prevcol-minline+1;
3453 TVar *modify = ptrcol+(col-prevcol);
3455 while(run1 != lastptr) sum += (*run1++)*(*run2++);
3458 int64_t n=lastptr-run1;
3459 sum = cblas_ddot(n,run1,1,run2,1);
3463 *modify /= *ptrprev;
3465 if (
fabs(*modify) <
fabs((TVar)1.e-25) ) {
3466 cout <<
"TPZSkylMatrix::DecomposeCholesky a matrix nao e positiva definida" << *modify << endl;
3467 *modify = ((TVar)1.e-10);
3469 *modify=
sqrt(*modify);
3473 template <
class TVar>
3475 if (nrow != ncol || !symmetric)
3483 for (int64_t i=0; i<nrow; i++) {
3484 skyline[i]=(i*(rand()))/RAND_MAX;
3489 for (int64_t i=0; i<nrow; i++) {
3491 for (int64_t j=skyline[i]; j<i; j++) {
3492 TVar val = ((TVar)rand())/((TVar)RAND_MAX);
3496 PutVal(i,i,sum+(TVar)1.);
3498 for (int64_t i=0; i<nrow; i++) {
3499 TVar sum = (TVar)0.;
3500 for (int64_t j=0; j<nrow; j++) {
3505 PutVal(i,i,sum+(TVar)1.);
3531 #endif // USING_NEW_SKYLMAT 3533 #if (defined DUMP_BEFORE_DECOMPOSE) || (defined DUMP_BEFORE_SUBST) 3535 pthread_mutex_t dump_matrix_mutex = PTHREAD_MUTEX_INITIALIZER;
3536 unsigned matrix_unique_id = 0;
3538 "Filename prefix for matrices dumped before decompose/subst",
3541 template<
class TVar>
3544 if (!dm_prefix.
was_set())
return;
3546 std::stringstream fname;
3547 fname << dm_prefix.
get_value() << fn_annotation <<
"_" << matrix_unique_id++ <<
".bin";
3548 std::cout <<
"Dump matrix before... (file: " << fname <<
")" << std::endl;
3552 std::cout <<
"Dump matrix before... [Done]" << std::endl;
3556 template<
class TVar>
3559 if (!dm_prefix.
was_set())
return;
3561 std::stringstream fname;
3562 fname << dm_prefix.
get_value() << fn_annotation <<
"_" << matrix_unique_id++ <<
".bin";
3563 std::cout <<
"Dump matrix before... (file: " << fname <<
")" << std::endl;
3568 std::cout <<
"Dump matrix before... [Done]" << std::endl;
int Subst_LForward(TPZFMatrix< TVar > *b) const override
Computes B = Y, where A*Y = B, A is lower triangular with A(i,i)=1.
#define PZ_PTHREAD_MUTEX_UNLOCK(mutex, fn)
expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ fabs
int Resize(const int64_t newDim, const int64_t) override
Redimensions a matriz keeping the previous values.
void AddKel(TPZFMatrix< TVar > &elmat, TPZVec< int64_t > &sourceindex, TPZVec< int64_t > &destinationindex) override
Add a contribution of a stiffness matrix.
virtual void Residual(const TPZFMatrix< TVar > &x, const TPZFMatrix< TVar > &rhs, TPZFMatrix< TVar > &res)
Computes res = rhs - this * x.
bool IsZero(long double a)
Returns if the value a is close Zero as the allowable tolerance.
Contains definitions to LOGPZ_DEBUG, LOGPZ_INFO, LOGPZ_WARN, LOGPZ_ERROR and LOGPZ_FATAL, and the implementation of the inline InitializePZLOG(string) function using log4cxx library or not. It must to be called out of "#ifdef LOG4CXX" scope.
void Write(TPZStream &buf, int withclassid) const override
Packs the object structure in a stream of bytes.
const TVar & GetVal(const int64_t row, const int64_t col) const override
Get values without bounds checking This method is faster than "Get" if DEBUG is defined.
static int Error(const char *msg, const char *msg2=0)
Returns error messages.
virtual void resize(const int64_t newsize)
clarg::argBool clk_rea("-skl_chk_rea", "Reallocate Skyline data before Cholesky Decomposition")
TVar & operator()(const int64_t row, const int64_t col)
clarg::argInt dimension("-d", "Matrices dimension M x M", 1000)
TPZVec< TVar > fStorage
fStorage is a unique vector which contains all the data of the skyline matrix
static void ComputeMaxSkyline(const TPZSkylMatrix< TVar > &first, const TPZSkylMatrix< TVar > &second, TPZVec< int64_t > &res)
Computes the highest skyline of both objects.
int64_t fRow
Number of rows in matrix.
void Write(TPZStream &buf, int withclassid) const override
Packs the object structure in a stream of bytes.
int Decompose_Cholesky() override
Decomposes the current matrix using Cholesky method. The current matrix has to be symmetric...
void PrepareZ(const TPZFMatrix< TVar > &y, TPZFMatrix< TVar > &z, const TVar beta, const int opt) const
Is an auxiliar method used by MultiplyAdd.
char fDecomposed
Decomposition type used to decompose the current matrix.
TPZSkylMatrix & operator-=(const TPZSkylMatrix< TVar > &A)
int64_t SkyHeight(int64_t col)
return the height of the skyline for a given column
TinyFad< 8, T > abs(const TinyFad< 8, T > &in)
REAL val(STATE &number)
Returns value of the variable.
#define LOGPZ_WARN(A, B)
Define log for warnings.
#define MAX(a, b)
Gets maxime value between a and b.
AutoPointerMutexArrayInit tmp
void Copy(const TPZSkylMatrix< TVar > &)
int Subst_Diag(TPZFMatrix< TVar > *b) const override
Computes B = Y, where A*Y = B, A is diagonal matrix.
TPZSkylMatrix & operator*=(TVar value)
char fDefPositive
Definite Posistiveness of current matrix.
static TVar gZero
Initializing value to static variable.
Implements a skyline storage format. A Skyline matrix is symmetric so square. Matrix.
TPZSkylMatrix< REAL > matrix
TVar * Diag(int64_t col)
This method returns a pointer to the diagonal element of the matrix of the col column.
TPZSkylMatrix operator*(const TVar v) const
virtual void UpdateFrom(TPZAutoPointer< TPZMatrix< TVar > > mat) override
Updates the values of the matrix based on the values of the matrix.
virtual TVar & s(const int64_t row, const int64_t col) override
The operators check on the bounds if the DEBUG variable is defined.
int Zero() override
Makes Zero all the elements.
virtual void Resize(const int64_t newsize, const T &object)
Resizes the vector object reallocating the necessary storage, copying the existing objects to the new...
int Subst_Forward(TPZFMatrix< TVar > *b) const override
Computes B = Y, where A*Y = B, A is lower triangular.
void SetSkyline(const TPZVec< int64_t > &skyline)
modify the skyline of the matrix, throwing away its values skyline indicates the minimum row number w...
#define PZ_PTHREAD_MUTEX_LOCK(mutex, fn)
virtual void Write(const bool val)
TPZSkylMatrix & operator+=(const TPZSkylMatrix< TVar > &A)
TVar Norm(const TPZFMatrix< TVar > &A)
Returns the norm of the matrix A.
Contains TPZMatrixclass which implements full matrix (using column major representation).
#define DebugStop()
Returns a message to user put a breakpoint in.
#define LOGPZ_DEBUG(A, B)
Define log for debug info.
TPZSkylMatrix & operator=(const TPZSkylMatrix< TVar > &A)
int64_t Rows() const
Returns number of rows.
int Zero() override
Zeroes the matrix.
int Decompose_LDLt() override
Decomposes the current matrix using LDLt.
expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ sqrt
static int64_t NumElements(const TPZVec< int64_t > &skyline)
Contains TPZSkyline class which implements a skyline storage format.
int64_t Size(const int64_t column) const
void Read(TPZStream &buf, void *context) override
Unpacks the object structure from a stream of bytes.
#define MIN(a, b)
Gets minime value between a and b.
TPZVec< TVar * > fElem
Storage to keep the first elements to each equation.
virtual int Redim(const int64_t newRows, const int64_t newCols)
Redimensions the matrix reinitializing it with zero.
Full matrix class. Matrix.
int Subst_Backward(TPZFMatrix< TVar > *b) const override
Computes B = Y, where A*Y = B, A is upper triangular.
int Redim(const int64_t newRows, const int64_t newCols) override
Redimension a matrix and ZERO your elements.
virtual int64_t Dim() const
Returns the dimension of the matrix if the matrix is square.
virtual void MultAdd(const TPZFMatrix< TVar > &x, const TPZFMatrix< TVar > &y, TPZFMatrix< TVar > &z, const TVar alpha, const TVar beta, const int opt=0) const override
It computes z = beta * y + alpha * opt(this)*x but z and x can not overlap in memory.
int Clear() override
It clears data structure.
int Redim(const int64_t newDim, const int64_t) override
Redimensions the matrix reinitializing it with zero.
TPZSkylMatrix operator-() const
int64_t fCol
Number of cols in matrix.
void Read(TPZStream &buf, void *context) override
Unpacks the object structure from a stream of bytes.
virtual void SolveSOR(int64_t &numiterations, const TPZFMatrix< TVar > &F, TPZFMatrix< TVar > &result, TPZFMatrix< TVar > *residual, TPZFMatrix< TVar > &scratch, const REAL overrelax, REAL &tol, const int FromCurrent=0, const int direction=1) override
Solves the linear system using Successive Over Relaxation method (Gauss Seidel). ...
TPZSkylMatrix operator+(const TPZSkylMatrix< TVar > &A) const
void Copy(TinyVector< T, Num > &y, const TinyVector< T, Num > &x)
int PutVal(const int64_t row, const int64_t col, const TVar &element) override
Put values without bounds checking This method is faster than "Put" if DEBUG is defined.
static void InitializeElem(const TPZVec< int64_t > &skyline, TPZVec< TVar > &storage, TPZVec< TVar *> &elem)
const T & get_value() const
static void Swap(int64_t *a, int64_t *b)
Swaps contents of a in b and b in a.
void AddSameStruct(TPZSkylMatrix< TVar > &B, double k=1.)
Add a skyline matrix B with same structure of this It makes this += k * B.
void Fill(const T ©, const int64_t from=0, const int64_t numelem=-1)
Will fill the elements of the vector with a copy object.
void DecomposeColumn(int64_t col, int64_t prevcol)
int Decompose_Cholesky_blk(int64_t blk_sz)
Contains TPZVerySparseMatrix class which implements a matrix whose nonzero elements are stored in bin...
int64_t Cols() const
Returns number of cols.
int ClassId() const override
Define the class id associated with the class.
void OpenWrite(const std::string &fileName)
Defines the interface for saving and reading data. Persistency.
int64_t NElements() const
Returns the number of elements of the vector.
clarg::argString m("-m", "input matrix file name (text format)", "matrix.txt")
clarg::argBool clk_mig("-skl_chk_pm", "Migrate Skyline pages before Cholesky Decomposition")
REAL ZeroTolerance()
Returns the tolerance to Zero value. Actually: .
TVar & g(const int64_t row, const int64_t col) const
Implements an interface to register a class id and a restore function. Persistence.
#define PZError
Defines the output device to error messages and the DebugStop() function.
void DecomposeColumn2(int64_t col, int64_t prevcol)
virtual void Read(bool &val)
int Subst_LBackward(TPZFMatrix< TVar > *b) const override
Computes B = Y, where A*Y = B, A is upper triangular with A(i,i)=1.
virtual void AutoFill(int64_t nrow, int64_t ncol, int symmetric)
Root matrix class (abstract). Matrix.
This class implements a reference counter mechanism to administer a dynamically allocated object...