📄 newmat4.cpp
字号:
{
if (+gm->Type() & MatrixType::Diagonal)
{ REPORT return MatrixBandWidth(0,0); }
else { REPORT return -1; }
}
MatrixBandWidth RowedMatrix::BandWidth() const { REPORT return -1; }
MatrixBandWidth ColedMatrix::BandWidth() const { REPORT return -1; }
MatrixBandWidth DiagedMatrix::BandWidth() const { REPORT return 0; }
MatrixBandWidth MatedMatrix::BandWidth() const { REPORT return -1; }
MatrixBandWidth ReturnMatrixX::BandWidth() const
{ REPORT return gm->BandWidth(); }
MatrixBandWidth GetSubMatrix::BandWidth() const
{
if (row_skip==col_skip && row_number==col_number)
{ REPORT return gm->BandWidth(); }
else { REPORT return MatrixBandWidth(-1); }
}
// ********************** the memory managment tools **********************/
// Rules regarding tDelete, reuse, GetStore
// All matrices processed during expression evaluation must be subject
// to exactly one of reuse(), tDelete(), GetStore() or BorrowStore().
// If reuse returns true the matrix must be reused.
// GetMatrix(gm) always calls gm->GetStore()
// gm->Evaluate obeys rules
// bm->Evaluate obeys rules for matrices in bm structure
void GeneralMatrix::tDelete()
{
if (tag<0)
{
if (tag<-1) { REPORT store=0; delete this; return; } // borrowed
else { REPORT return; }
}
if (tag==1)
{
if (store)
{
REPORT MONITOR_REAL_DELETE("Free (tDelete)",storage,store)
delete [] store;
}
store=0; tag=-1; return;
}
if (tag==0) { REPORT delete this; return; }
REPORT tag--; return;
}
static void BlockCopy(int n, Real* from, Real* to)
{
REPORT
int i = (n >> 3);
while (i--)
{
*to++ = *from++; *to++ = *from++; *to++ = *from++; *to++ = *from++;
*to++ = *from++; *to++ = *from++; *to++ = *from++; *to++ = *from++;
}
i = n & 7; while (i--) *to++ = *from++;
}
bool GeneralMatrix::reuse()
{
if (tag<-1)
{
if (storage>0)
{
REPORT
Real* s = new Real [storage]; MatrixErrorNoSpace(s);
MONITOR_REAL_NEW("Make (reuse)",storage,s)
BlockCopy(storage, store, s); store=s;
}
else store = 0;
tag=0; return true;
}
if (tag<0) { REPORT return false; }
if (tag<=1) { REPORT return true; }
REPORT tag--; return false;
}
Real* GeneralMatrix::GetStore()
{
if (tag<0 || tag>1)
{
Real* s;
if (storage)
{
s = new Real [storage]; MatrixErrorNoSpace(s);
MONITOR_REAL_NEW("Make (GetStore)",storage,s)
BlockCopy(storage, store, s);
}
else s = 0;
if (tag>1) { REPORT tag--; }
else if (tag < -1) { REPORT store=0; delete this; } // borrowed store
else { REPORT }
return s;
}
Real* s=store; store=0;
if (tag==0) { REPORT delete this; }
else { REPORT tag=-1; }
return s;
}
void GeneralMatrix::GetMatrix(const GeneralMatrix* gmx)
{
REPORT tag=-1; nrows=gmx->Nrows(); ncols=gmx->Ncols();
storage=gmx->storage; SetParameters(gmx);
store=((GeneralMatrix*)gmx)->GetStore();
}
GeneralMatrix* GeneralMatrix::BorrowStore(GeneralMatrix* gmx, MatrixType mt)
// Copy storage of *this to storage of *gmx. Then convert to type mt.
// If mt == 0 just let *gmx point to storage of *this if tag==-1.
{
if (!mt)
{
if (tag == -1) { REPORT gmx->tag = -2; gmx->store = store; }
else { REPORT gmx->tag = 0; gmx->store = GetStore(); }
}
else if (Compare(gmx->Type(),mt))
{ REPORT gmx->tag = 0; gmx->store = GetStore(); }
else
{
REPORT gmx->tag = -2; gmx->store = store;
gmx = gmx->Evaluate(mt); gmx->tag = 0; tDelete();
}
return gmx;
}
void GeneralMatrix::Eq(const BaseMatrix& X, MatrixType mt)
// Count number of references to this in X.
// If zero delete storage in this;
// otherwise tag this to show when storage can be deleted
// evaluate X and copy to this
{
#ifdef DO_SEARCH
int counter=X.search(this);
if (counter==0)
{
REPORT
if (store)
{
MONITOR_REAL_DELETE("Free (operator=)",storage,store)
REPORT delete [] store; storage=0; store = 0;
}
}
else { REPORT Release(counter); }
GeneralMatrix* gmx = ((BaseMatrix&)X).Evaluate(mt);
if (gmx!=this) { REPORT GetMatrix(gmx); }
else { REPORT }
Protect();
#else
GeneralMatrix* gmx = ((BaseMatrix&)X).Evaluate(mt);
if (gmx!=this)
{
REPORT
if (store)
{
MONITOR_REAL_DELETE("Free (operator=)",storage,store)
REPORT delete [] store; storage=0; store = 0;
}
GetMatrix(gmx);
}
else { REPORT }
Protect();
#endif
}
// version to work with operator<<
void GeneralMatrix::Eq(const BaseMatrix& X, MatrixType mt, bool ldok)
{
REPORT
if (ldok) mt.SetDataLossOK();
Eq(X, mt);
}
void GeneralMatrix::Eq2(const BaseMatrix& X, MatrixType mt)
// a cut down version of Eq for use with += etc.
// we know BaseMatrix points to two GeneralMatrix objects,
// the first being this (may be the same).
// we know tag has been set correctly in each.
{
GeneralMatrix* gmx = ((BaseMatrix&)X).Evaluate(mt);
if (gmx!=this) { REPORT GetMatrix(gmx); } // simplify GetMatrix ?
else { REPORT }
Protect();
}
void GeneralMatrix::Inject(const GeneralMatrix& X)
// copy stored values of X; otherwise leave els of *this unchanged
{
REPORT
Tracer tr("Inject");
if (nrows != X.nrows || ncols != X.ncols)
Throw(IncompatibleDimensionsException());
MatrixRow mr((GeneralMatrix*)&X, LoadOnEntry);
MatrixRow mrx(this, LoadOnEntry+StoreOnExit+DirectPart);
int i=nrows;
while (i--) { mrx.Inject(mr); mrx.Next(); mr.Next(); }
}
// ************* checking for data loss during conversion *******************/
bool Compare(const MatrixType& source, MatrixType& destination)
{
if (!destination) { destination=source; return true; }
if (destination==source) return true;
if (!destination.DataLossOK && !(destination>=source))
Throw(ProgramException("Illegal Conversion", source, destination));
return false;
}
// ************* Make a copy of a matrix on the heap *********************/
GeneralMatrix* Matrix::Image() const
{
REPORT
GeneralMatrix* gm = new Matrix(*this); MatrixErrorNoSpace(gm);
return gm;
}
GeneralMatrix* SymmetricMatrix::Image() const
{
REPORT
GeneralMatrix* gm = new SymmetricMatrix(*this); MatrixErrorNoSpace(gm);
return gm;
}
GeneralMatrix* UpperTriangularMatrix::Image() const
{
REPORT
GeneralMatrix* gm = new UpperTriangularMatrix(*this);
MatrixErrorNoSpace(gm); return gm;
}
GeneralMatrix* LowerTriangularMatrix::Image() const
{
REPORT
GeneralMatrix* gm = new LowerTriangularMatrix(*this);
MatrixErrorNoSpace(gm); return gm;
}
GeneralMatrix* DiagonalMatrix::Image() const
{
REPORT
GeneralMatrix* gm = new DiagonalMatrix(*this); MatrixErrorNoSpace(gm);
return gm;
}
GeneralMatrix* RowVector::Image() const
{
REPORT
GeneralMatrix* gm = new RowVector(*this); MatrixErrorNoSpace(gm);
return gm;
}
GeneralMatrix* ColumnVector::Image() const
{
REPORT
GeneralMatrix* gm = new ColumnVector(*this); MatrixErrorNoSpace(gm);
return gm;
}
GeneralMatrix* BandMatrix::Image() const
{
REPORT
GeneralMatrix* gm = new BandMatrix(*this); MatrixErrorNoSpace(gm);
return gm;
}
GeneralMatrix* UpperBandMatrix::Image() const
{
REPORT
GeneralMatrix* gm = new UpperBandMatrix(*this); MatrixErrorNoSpace(gm);
return gm;
}
GeneralMatrix* LowerBandMatrix::Image() const
{
REPORT
GeneralMatrix* gm = new LowerBandMatrix(*this); MatrixErrorNoSpace(gm);
return gm;
}
GeneralMatrix* SymmetricBandMatrix::Image() const
{
REPORT
GeneralMatrix* gm = new SymmetricBandMatrix(*this); MatrixErrorNoSpace(gm);
return gm;
}
GeneralMatrix* nricMatrix::Image() const
{
REPORT
GeneralMatrix* gm = new nricMatrix(*this); MatrixErrorNoSpace(gm);
return gm;
}
GeneralMatrix* IdentityMatrix::Image() const
{
REPORT
GeneralMatrix* gm = new IdentityMatrix(*this); MatrixErrorNoSpace(gm);
return gm;
}
GeneralMatrix* GeneralMatrix::Image() const
{
bool dummy = true;
if (dummy) // get rid of warning message
Throw(InternalException("Cannot apply Image to this matrix type"));
return 0;
}
// *********************** nricMatrix routines *****************************/
void nricMatrix::MakeRowPointer()
{
row_pointer = new Real* [nrows]; MatrixErrorNoSpace(row_pointer);
Real* s = Store() - 1; int i = nrows; Real** rp = row_pointer;
// while (i--) { *rp++ = s; s+=ncols; }
if (i) for (;;) { *rp++ = s; if (!(--i)) break; s+=ncols; }
}
void nricMatrix::DeleteRowPointer()
{ if (nrows) delete [] row_pointer; }
void GeneralMatrix::CheckStore() const
{
if (!store)
Throw(ProgramException("NRIC accessing matrix with unset dimensions"));
}
// *************************** CleanUp routines *****************************/
void GeneralMatrix::CleanUp()
{
// set matrix dimensions to zero, delete storage
REPORT
if (store && storage)
{
MONITOR_REAL_DELETE("Free (CleanUp) ",storage,store)
REPORT delete [] store;
}
store=0; storage=0; nrows=0; ncols=0;
}
void nricMatrix::CleanUp()
{ DeleteRowPointer(); GeneralMatrix::CleanUp(); }
void RowVector::CleanUp()
{ GeneralMatrix::CleanUp(); nrows=1; }
void ColumnVector::CleanUp()
{ GeneralMatrix::CleanUp(); ncols=1; }
void CroutMatrix::CleanUp()
{
if (nrows) delete [] indx;
GeneralMatrix::CleanUp();
}
void BandLUMatrix::CleanUp()
{
if (nrows) delete [] indx;
if (storage2) delete [] store2;
GeneralMatrix::CleanUp();
}
// ************************ simple integer array class ***********************
// construct a new array of length xn. Check that xn is non-negative and
// that space is available
SimpleIntArray::SimpleIntArray(int xn) : n(xn)
{
if (n < 0) Throw(Logic_error("invalid array length"));
else if (n == 0) { REPORT a = 0; }
else { REPORT a = new int [n]; if (!a) Throw(Bad_alloc()); }
}
// destroy an array - return its space to memory
SimpleIntArray::~SimpleIntArray() { REPORT if (a) delete [] a; }
// access an element of an array; return a "reference" so elements
// can be modified.
// check index is within range
// in this array class the index runs from 0 to n-1
int& SimpleIntArray::operator[](int i)
{
REPORT
if (i < 0 || i >= n) Throw(Logic_error("array index out of range"));
return a[i];
}
// same thing again but for arrays declared constant so we can't
// modify its elements
int SimpleIntArray::operator[](int i) const
{
REPORT
if (i < 0 || i >= n) Throw(Logic_error("array index out of range"));
return a[i];
}
// set all the elements equal to a given value
void SimpleIntArray::operator=(int ai)
{ REPORT for (int i = 0; i < n; i++) a[i] = ai; }
// set the elements equal to those of another array.
// check the arrays are of the same length
void SimpleIntArray::operator=(const SimpleIntArray& b)
{
REPORT
if (b.n != n) Throw(Logic_error("array lengths differ in copy"));
for (int i = 0; i < n; i++) a[i] = b.a[i];
}
// construct a new array equal to an existing array
// check that space is available
SimpleIntArray::SimpleIntArray(const SimpleIntArray& b) : n(b.n)
{
if (n == 0) { REPORT a = 0; }
else
{
REPORT
a = new int [n]; if (!a) Throw(Bad_alloc());
for (int i = 0; i < n; i++) a[i] = b.a[i];
}
}
// change the size of an array; optionally copy data from old array to
// new array
void SimpleIntArray::ReSize(int n1, bool keep)
{
if (n1 == n) { REPORT return; }
else if (n1 == 0) { REPORT n = 0; delete [] a; a = 0; }
else if (n == 0)
{ REPORT a = new int [n1]; if (!a) Throw(Bad_alloc()); n = n1; }
else
{
int* a1 = a;
if (keep)
{
REPORT
a = new int [n1]; if (!a) Throw(Bad_alloc());
if (n > n1) n = n1;
for (int i = 0; i < n; i++) a[i] = a1[i];
n = n1; delete [] a1;
}
else
{
REPORT n = n1; delete [] a1;
a = new int [n]; if (!a) Throw(Bad_alloc());
}
}
}
#ifdef use_namespace
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -