Matrix calculation written by C ++ class

xiaoxiao2021-03-06  125

#pragma overce # include "dibimage.h" #include "exception.h" // matrix class - declaration

Class Matrix {private: // Matrix Width long width; // Matrix Height Long Height; Protected: // Matrix First Address Double * P; Public: Static Matrix One (long n); Void Info (); // Display Information Long getHeight (); long getWidth (); bool isvector (); // If it is false, that is a number double deter (); // seeking a row of determinant bool isptv (); // Is it matrix eigen (); // Sicular value Matrix Adjoint (); // accompanied by matrix matrix t (); // transfers Matrix INV (); // Multi-matrix Void Unity (); // Normalized Void Store (Const Char * filename); // Store the matrix to the file matrix bicubic (long n); // constructs the following similar matrix / * {2, 0.5, 0, 0, 0, 0}, {0.5, 2, 0.5, 0, 0, 0 }, {0, 0.5, 2, 0.5, 0, 0}, {0, 0, 0.5, 2, 0.5, 0}, {0,0,0,0.5,2,0.5,}, {0, 0, 0, 0, 0.5, 2,}}; * / Matrix Submatrix (long offset); // throw (Exception &); Matrix Submatrix (long x, long y, long widthoffset, long heightoffset); // Refer to a number of positions District // Matrix Submatrix (Double X, Double Y, Long Widthoffset, long Heightoffset); // Take a small number of position sub-area void test (); // Test function / ********** Overloaded section -OverLoaded Part **************** / Matrix Operator (Matrix &); Matrix Operator- (Matrix &); Matrix Operator * (Matrix &); Friend Matrix Oper Ator * (Double Alpha, Matrix &); // Real Number multiplied Matrix Operator * (double alpha); // matrix multiplying Matrix Operator / (Matrix &); // Real as a real number or matrix and Real numbers to remove Matrix Operator / (Double Sub); Matrix Operator = (Matrix &); Matrix Operator - = (Matrix &); Matrix Operator * = (Matrix &); // Matrix Multiply Matrix Operator * = (Double) Alpha); // matrix with real numbers Matrix & Operator = (Matrix &); // Assign Double * Operator [] (long Heightpos); // Used to implement [] [] Operation matrix element Friend Matrix SQRT (Matrix M ); // 开方 Friend Double Abs (Matrix &); // Take Absolute Value (Pan) Friend Double Sum (Matrix &); // Qi Friend Matrix Multiply (Matrix & M1, Matrix & M2); // Press Elements multiplied Friend Double Operator (Double DBL, Matrix &); Friend Double Operator (Matrix &, Double DBL);

Friend Double Operator- (Double DBL, Matrix &); Friend Double Operator- (Matrix &, Double DBL); Friend Ostream & Operator << (Ostream &, Matrix &); // Output Matrix (Void); // Default Construction Function Matrix (long N); // Unit Matrix Matrix (Dibimage & Dibimage); // Type Transition (converted image file into matrix for image calculation processing) Matrix (double * arraddress, long arrwidth; // Constructive Victor matrix (one line, arrwidth column) Matrix (double * arraddress, long arrheight, long arrival); // construct a two-dimensional matrix Matrix (Long Height, long width); // Constructs an empty matrix Matrix (Const Matrix &); // Copy constructor Virtual ~ matrix (void); // default analysis function / ********** Overloaded section -OverLoaded part *************** * /}; # incrude "stdafx.h" #include "./matrix.h"// matrix class - realize #define Sign ((b)> 0? FABS (a): -fabs (a )): Width (1), Height (1) {p = new double [1]; // width = 1; // Height = 1; // cout << "default constructor invoked. "<< Endl;} matrix :: matrix (long n) {height = width = n; p = new double [n * n]; for (long i = 0; i

Matrix :: Matrix (Dibimage & Dibimage) {width (); height = dibimage.getHEight (); p = new double [width * height]; if (p == null) Assert (0); // Memory allocation fail LONG LLINEBYTES = Widthbytes (width * 8); lpstr src = :: Findibbits ((lpstr) :: Globalock ((hglobal) Dibimage.getHDIB ())); for (long (long i = 0; i

} Matrix Matrix :: One (long n) {Matrix M (1, N); for (long i = 0; i = 0)) Throw Exception ("Error At Matrix Matrix :: Submatrix (Long Offset) ./ n Not Height == Width && Offset <= Width && Offset> = 0"); Double * T = New Double [Offset * offset]; for (long (long i = 0; i width || heightoffset * 2 1> HEI GHT) {cout <<> Subarea Is Bigger Than Matrix In Matrix :: Submatrix ()! / n "; log = false;} else if ((x-widthoffset) <0 || (x widthoffset)> Width) {COUT << "Width Error In Matrix :: Submatrix ()! / N"; log = false;} else if (Y-heightoffset) <0 || (y heightoffset)> Height) {cout << "Height Error In Matrix :: Submatrix ()! / n ";

LOG = false;} assert (log); Matrix M (Heightoffset * 2 1, Widthoffset * 2 1); for (long i = 0; i

Bool Matrix :: ISPTV () {assert (width == Height); // is the square array can calculate BOOL RESULT = true; Matrix M; for (long i = 1; i <= height; i ) {m = this -> Submatrix (i); if (m.deter () <= 0) {result = false; break;}} returnrate;}} return result

Matrix Matrix :: T () {DOUBLE * T = New Double [width * height]; for (long i = 0; i

Double ** A, * WR, * Wi; long NN, M, L, K, J, ITS, I, Mmin; Double Z, Y, X, W, V, U, T, S, R, Q, P , anorm; long n = matrix.getwidth (); a = new double * [n 1]; wr = new double [n 1]; wi = new double [n 1]; for (i = 0; i

For (i = 0; i

Anorm = FABS (a [1] [1]); for (i = 2; i <= n; i ) for (j = (i-1); j <= n; j ) anorm = Fabs (a [a [ I] [j]); nn = n; T = 0.0; while (nn> = 1) {ITS = 0; do {for (l = nn; l> = 2; l -) {s = fabs (a [L-1] [L-1]) FABS (a [l] [l]); if (s == 0.0) s = anorm; IF (Fabs (a [L] [L-1]) S == s) Break;} x = a [nn] [nn]; if (l == nn) {WR [NN] = x T; Wi [nn -] = 0.0;} else {y = a [ NN-1] [NN-1]; W = a [NN] [NN-1] * a [NN-1] [NN]; if (l == (NN-1)) {P = 0.5 * (YX ); Q = p * p w; z = SQRT (Fabs (q)); x = T; IF (q> = 0.0) {z = p sign (Z, p); WR [NN-1] = WR [NN] = x z; if (z) WR [NN] = xw / z; Wi [NN-1] = Wi [NN] = 0.0;} else {WR [NN-1] = WR [NN ] = x p; Wi [NN-1] = - (Wi [nn] = z);} nn - = 2;} else {if (ITS == 30) Printf ("Too Many Itemions In HQR"); IF (ITS == 10 || ITS == 20) {t = x; for (i = 1; i <= nn; i ) a [i] [i] - = x; s = FABS (A [NN ] [NN-1]) FABS (a [NN-1] [NN-2]); y = x = 0.75 * s; w = -0.4375 * s * s;} ITS; for (m = NN-2); m> = L; M -) {z = a [m] [m]; r = xz; S = yz; p = (r * SW) / a [m 1] [m] a [m] [m 1]; q = a [m 1] [m 1] -zrs; r = A [M 2] [M 1]; s = FABS (P) Fabs (q) Fabs (R); P / = S; Q / = S; R / = S; IF (M == L Break; U = FABS (a [M] [M-1]) * (FABS (q) Fabs (R)); V = Fabs (P) * (Fabs (A [M-1] [M-1 ]) FABS (Z) FABS (a [M 1] [M 1]));

IF (U V == V) Break;} for (i = m 2; i <= nn; i ) {a [i] [i-2] = 0.0; if (i! = (m 2) ) a [i] [i-3] = 0.0;} for (k = m; k <= nn-1; k ) {IF (k! = m) {P = a [k] [k-1]; q = a [k 1] [k-1]; r = 0.0; if (k! = (nn-1)) R = a [k 2] [k-1]; if (x = Fabs (P ) FABS (q) Fabs (R)) {p / = x; q / = x; r / = x;}}} f (s = SIGN (SQRT (P * P Q * q r * r) , p)) {if (k == m) {if (l! = m) a [k] [k-1] = -a [k] [k-1];} else a [k] [k- 1] = -s * x; p = S; x = p / s; y = q / s; z = r / s; q / = p; r / = p; for (j = k; j <= NN; j ) {p = a [k] [j] q * a [k 1] [j]; if (k! = (nn-1)) {p = r * a [k 2] [j]; a [k 2] [j] - = p * z;} a [k 1] [j] - = p * y; a [k] [j] - = p * x;} mmin = NN inv () * this-> deter ();} void matrix :: unit () {Double Total Total = SQRT (SUM (Multiply (* this, * this)))); for (long i = 0; i height; i ) {for (long j = 0; J <

This-> width; j ) {(* this) [i] [j] / = Total;}} return;} void matrix :: store (const char * filename) {OFStream Fout (filename); int w = 5; For (long i = 0; i 0); Matrix m = * this; double * pdbsrc = mp; long nlen = m.width; int * IS, * JS, I, J, K; Double D, P; is = new int rt [Nlen ]; Js = new int [NLEN]; for (k = 0; k d) {d = p; IS [k] = i; js [k] = j;}} f (d 1.0 == 1.0) {delete is; delete js; assert (0); // Return False;} if (IS [k]! = K) for (j = 0; j

IF (j! = K) {PDBSRC [i * Nlen J] - = PDBSRC [i * Nlen K] * PDBSRC [K * Nlen J];} for (i = 0; i = 0; k -) {IF ( JS [k]! = k) for (j = 0; j 1); delete [] p; width = n; height = n; p = new double [n * n]; // first Row * (P) = 0.5; * (p 1) = 2; for (long k = 2; k

Return * this;} Matrix Matrix :: Operator (Matrix & m1) {assert (m1.height == height && m1.width == width); long tmpheight = m1.height; long tempwidth = m1.width; double * t = New Double [TmpWidth * Tmpheight]; for (long (long i = 0; i isvector ) && m1.gector ()) {// Left number, right is matrix matrix m; m = p [0] * m1; return m;} else f (this-> isvector () &&! m1.Isvector ()) {// Left is matrix, right is Matrix m; m = * this * m1 [0] [0]; return m;} else if (! This-> isvector () && m1.Vector ()) {// 都 = Double * t = new double [1]; t [0] = p [0] * m1 [0] [0]; Matrix M (T, 1, 1); delete [] t; return M;} else if (this-> isvector () && m1.gector () && width == m1.height) {// Left is matrix, right is the matrix Double Sum; Double * T = New Double [Height * m1. Width]; for (Long i = 0; i

M1.Width * i j) = sum;}} Matrix M (t, height, m1.width); delete [] t; return m;} else {assert (0); // Unknown operation Return * this;} Matrix Operator * (Double Alpha, Matrix & M1) {Matrix M = m1; for (long i = 0; i = 0 && m.getHeight () == 1 && m.Getwidth () == 1); M [0] [0] = SQRT (M ​​[0] [0]); returnof;} Double ABS (MATRIX & M) {Double Sum = 0; for (Long i = 0; i

0); Matrix M = * this; for (long (long i = 0; i = 0 && Heightpos <= height); // Remove the error RETURN P Width * Heightpos;} Double Operator (Double DBL, Matrix & M) {Assert (M.GetHeight () == 1 && M.GetWidth () == 1); Return DBL M [0] [0];} Double Operator (Matrix & M, Double DBL) {Return DBL M;} Double Operator- (Double DBL, Matrix & M) {Assert (M.GetHeight () == 1 && M.GetWidth () == 1); Return DBL-M [0] [0];} Double Operator- (Matrix & M, Double DBL) {Return - (DBL-M);} Ostream & Operator << (Ostream & OS, Matrix & m) {OS << "Matrix: height =" << m.Height << Endl; OS << "Matrix: width =" 转载请注明原文地址:https://www.9cbs.com/read-101817.html


New Post(0)