📄 realmat.h
字号:
/* ----------------------------------------------------------------------------
realmat: realmat.h
realmat is a C++ matrix class. The header file realmat.h describes its use.
Copyright (C) 1990.
A. Ronald Gallant
P.O. Box 5513
Raleigh NC 27650-5513
USA
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted, provided
that the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
This software is provided "as is" without any expressed or implied warranty.
------------------------------------------------------------------------------
This header defines a matrix class. A matrix X with r rows, and c columns is
constructed using realmat X(r,c). X is stored columnwise with no wasted space
which is to say that vec(X) is what is stored. Indexing of the elements starts
with 1, not zero. The protected members of realmat are rows, cols, len, and x.
As an illustration, suppose X is constructed using realmat X(r,c). Then
X.rows=r, X.cols=c, X.len=r*c, the pointer X.x points to the first element of
vec(X), X[i] returns the i-th element of vec(X), i=1,...,X.len, X.elem(i,j)
returns the (i,j)-th element of X, i=1,...,X.rows, j=1,...,.X.cols. An element
of X is of type REAL and X.rows, X.cols, and X.len are of type INTEGER. The
typedef of REAL and INTEGER is in usual.h. To allocate store at run time, use
realmat X; X.resize(r,c).
The operators +, -, *, =, and << are defined. T(X) transposes X. X.check1(i)
and X.check2(i,j) access the elements of vex(X) and X respectively with range
checking. invpsd(X) or invpsd(X,eps) returns a g-inverse of a positive semi-
definite, symmetric matrix X with minimal error checking; eps is the tolerance
used in rank determination and defaults to 1.e-13. rows(X) returns X.rows and
cols(X) returns X.cols.
-----------------------------------------------------------------------------*/
#ifndef __FILE_REALMAT_H_SEEN__
#pragma once
#define __FILE_REALMAT_H_SEEN__ 1
#include "usual.h"
#include "tools.h"
class realmat
{
protected:
INTEGER rows;
INTEGER cols;
INTEGER len;
REAL *x;
realmat(INTEGER r, INTEGER c, REAL* a);
void realmat_constructor(INTEGER r, INTEGER c, REAL fill_value);
void resize_constructor(INTEGER r, INTEGER c, REAL fill_value);
public:
realmat();
realmat(INTEGER r, INTEGER c);
realmat(INTEGER r, INTEGER c, REAL fill_value);
realmat(INTEGER r, INTEGER c, int fill_value);
realmat(int r, int c);
realmat(int r, int c, REAL fill_value);
realmat(int r, int c, int fill_value);
realmat(INTEGER r, int c);
realmat(INTEGER r, int c, REAL fill_value);
realmat(INTEGER r, int c, int fill_value);
realmat(int r, INTEGER c);
realmat(int r, INTEGER c, REAL fill_value);
realmat(int r, INTEGER c, int fill_value);
realmat(realmat&);
~realmat();
void resize(INTEGER r, INTEGER c);
void resize(INTEGER r, INTEGER c, REAL fill_value);
void resize(INTEGER r, INTEGER c, int fill_value);
void resize(int r, int c);
void resize(int r, int c, REAL fill_value);
void resize(int r, int c, int fill_value);
void resize(INTEGER r, int c);
void resize(INTEGER r, int c, REAL fill_value);
void resize(INTEGER r, int c, int fill_value);
void resize(int r, INTEGER c);
void resize(int r, INTEGER c, REAL fill_value);
void resize(int r, INTEGER c, int fill_value);
realmat& operator=(realmat& a);
REAL& operator[](INTEGER i);
REAL& elem(INTEGER i, INTEGER j);
REAL& check1(INTEGER i);
REAL& check2(INTEGER i, INTEGER j);
void error(const char* msg);
friend INTEGER rows(realmat& a);
friend INTEGER cols(realmat& a);
friend ostream& operator<<(ostream& stream, realmat& a);
friend realmat operator+(realmat& a, realmat& b);
friend realmat operator+(realmat& a);
friend realmat operator-(realmat& a, realmat& b);
friend realmat operator-(realmat& a);
friend realmat operator*(realmat& a, realmat& b);
friend realmat operator*(REAL& a, realmat& b);
friend realmat operator*(INTEGER& a, realmat& b);
friend realmat T(realmat& a);
friend realmat invpsd(realmat& a, REAL eps = 1.0e-13); //a is psd, symmetric
};
inline REAL& realmat::operator[](INTEGER i)
{
return x[i-1];
}
inline REAL& realmat::elem(INTEGER i, INTEGER j)
{
return x[i + rows*j - rows - 1]; // return x[rows*(j-1)+i-1]
}
inline INTEGER rows(realmat& a)
{
return a.rows;
}
inline INTEGER cols(realmat& a)
{
return a.cols;
}
inline realmat::realmat()
{ rows=0; cols=0; len=0; x=0;}
inline realmat::realmat(INTEGER r, INTEGER c)
{realmat_constructor(r, c, (REAL) 0);}
inline realmat::realmat(INTEGER r, INTEGER c, REAL fill_value)
{realmat_constructor(r, c, fill_value);}
inline realmat::realmat(INTEGER r, INTEGER c, int fill_value)
{realmat_constructor(r, c, (REAL) fill_value);}
inline realmat::realmat(int r, int c)
{realmat_constructor((INTEGER) r, (INTEGER) c, (REAL) 0);}
inline realmat::realmat(int r, int c, REAL fill_value)
{realmat_constructor((INTEGER) r, (INTEGER) c, fill_value);}
inline realmat::realmat(int r, int c, int fill_value)
{realmat_constructor((INTEGER) r, (INTEGER) c, (REAL) fill_value);}
inline realmat::realmat(INTEGER r, int c)
{realmat_constructor(r, (INTEGER) c, (REAL) 0);}
inline realmat::realmat(INTEGER r, int c, REAL fill_value)
{realmat_constructor(r, (INTEGER) c, fill_value);}
inline realmat::realmat(INTEGER r, int c, int fill_value)
{realmat_constructor(r, (INTEGER) c, (REAL) fill_value);}
inline realmat::realmat(int r, INTEGER c)
{realmat_constructor((INTEGER) r, c, (REAL) 0);}
inline realmat::realmat(int r, INTEGER c, REAL fill_value)
{realmat_constructor((INTEGER) r, c, fill_value);}
inline realmat::realmat(int r, INTEGER c, int fill_value)
{realmat_constructor((INTEGER) r, c, (REAL) fill_value);}
inline realmat::~realmat()
{delete x;}
inline void realmat::resize(INTEGER r, INTEGER c)
{resize_constructor(r, c, (REAL) 0);}
inline void realmat::resize(INTEGER r, INTEGER c, REAL fill_value)
{resize_constructor(r, c, fill_value);}
inline void realmat::resize(INTEGER r, INTEGER c, int fill_value)
{resize_constructor(r, c, (REAL) fill_value);}
inline void realmat::resize(int r, int c)
{resize_constructor((INTEGER) r, (INTEGER) c, (REAL) 0);}
inline void realmat::resize(int r, int c, REAL fill_value)
{resize_constructor((INTEGER) r, (INTEGER) c, fill_value);}
inline void realmat::resize(int r, int c, int fill_value)
{resize_constructor((INTEGER) r, (INTEGER) c, (REAL) fill_value);}
inline void realmat::resize(INTEGER r, int c)
{resize_constructor(r, (INTEGER) c, (REAL) 0);}
inline void realmat::resize(INTEGER r, int c, REAL fill_value)
{resize_constructor(r, (INTEGER) c, fill_value);}
inline void realmat::resize(INTEGER r, int c, int fill_value)
{resize_constructor(r, (INTEGER) c, (REAL) fill_value);}
inline void realmat::resize(int r, INTEGER c)
{resize_constructor((INTEGER) r, c, (REAL) 0);}
inline void realmat::resize(int r, INTEGER c, REAL fill_value)
{resize_constructor((INTEGER) r, c, fill_value);}
inline void realmat::resize(int r, INTEGER c, int fill_value)
{resize_constructor((INTEGER) r, c, (REAL) fill_value);}
extern void default_realmat_error_handler(const char* msg);
extern ONE_ARG_ERROR_HANDLER_T realmat_error_handler;
extern ONE_ARG_ERROR_HANDLER_T
set_realmat_error_handler(ONE_ARG_ERROR_HANDLER_T f);
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -