📄 matrix.cpp
字号:
// Matrix.cpp: Implementierung der Klasse CMatrix.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
//////////////////////////////////////////////////////////////////////
// Konstruktion/Destruktion
//////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include "Matrix.h"
/*
Author : Bernd Allmendinger
Date : 14.02.2000
e-mail : webmaster@neurocomputing.de
homepage : www.neurocomputing.de
Version : 1.0
*/
CMatrix::CMatrix()
{ wert = 0;
dimx = 0;
dimy = 0;
nrl = 0;
nrh = 0;
ncl = 0;
nch = 0;
}
CMatrix::~CMatrix()
{ int i;
for(i=nrh;i>=nrl;i--)
if ((wert[i]+ncl)) delete [] (wert[i]+ncl);
// free((char*) (m[i]+ncl));
// free((char*) (m+nrl));
if ((wert+nrl))
{ delete [] (wert+nrl);
wert = NULL;
}
/*
for(l=0; l < dimy; l++)
{ if (wert[l]) delete [] wert[l];
}
if (wert)
{ delete [] wert;
wert = 0;
}
*/
dimx = 0;
dimy = 0;
nrl = 0;
nrh = 0;
ncl = 0;
nch = 0;
}
void CMatrix::operator=(CMatrix & a)
{ int i,j;
// Wenn dim des vektors Null ist, dann erst speicher allocieren
if (dimx == 0 && dimy == 0)
{ setDim(a.nrl,a.nrh, a.ncl, a.nch);
dimy = a.nrh-a.nrl+1;
dimx = a.nch-a.ncl+1;
}
if (a.dimx == dimx && a.dimy == dimy)
{
nrl=a.nrl;
nrh=a.nrh;
ncl=a.ncl;
nch=a.nch;
for (i=nrl; i <= nrh; i++)
{ for (j=ncl; j <= nch; j++)
{ wert[i][j] = a.wert[i][j];
}
}
}
}
int CMatrix::setDim(int unrl,int unrh, int uncl, int unch)
{ int i, j;
nrl=unrl;
nrh=unrh;
ncl=uncl;
nch=unch;
if (dimx == 0 && dimy == 0)
{ dimy =nrh-nrl+1;
dimx = nch-ncl+1;
wert=new double* [nrh-nrl+1];
if (wert == NULL)
return (NOSPEICHER);
wert -= nrl;
for(i=nrl;i<=nrh;i++) {
wert[i]=new double[nch-ncl+1];
if (wert[i]==NULL)
return (NOSPEICHER);
wert[i] -= ncl;
}
for (i=nrl; i <= nrh; i++)
{ for (j=ncl; j <= nch; j++)
{ wert[i][j] = 0;
}
}
}
return(OK);
}
int CMatrix::getDimx()
{
return(dimx);
}
int CMatrix::getDimy()
{
return(dimy);
}
void CMatrix::fprintWerte(char* fname) /*
--------------------------------- */
{ FILE* fp;
int i, j;
int wrt;
if (!(fp = fopen(fname, "w")))
{ return;
}
fprintf(fp,"\nAusgabe von Matrix mit dimx: %d, dimy %d:\n", dimx, dimy);
for (i=nrl; i <= nrh; i++)
{ for (j=ncl; j <= nch; j++)
{ wrt = (int) wert[i][j];
if (fabs((double) wrt-wert[i][j])<0.0001)
fprintf(fp,"%d\t",wrt);
else
fprintf(fp,"%10.8f\t",wert[i][j]);
}
fprintf(fp,"\n");
}
fclose(fp);
}
void CMatrix::fprintWerteT(char* fname) /*
--------------------------------- */
{ //Ausgabe der Werte Transponiert
FILE* fp;
int i, j;
int wrt;
if (!(fp = fopen(fname, "w")))
{ return;
}
fprintf(fp,"\nAusgabe von Matrix mit dimx: %d, dimy %d:\n", dimx, dimy);
for (i=nrl; i <= nrh; i++)
{ for (j=ncl; j <= nch; j++)
{ wrt = (int) wert[i][j];
if (fabs((double) wrt-wert[i][j])<0.0001)
fprintf(fp,"\t%d",wrt);
else
fprintf(fp,"\t%4.3f",wert[i][j]);
}
fprintf(fp,"\n");
}
fclose(fp);
}
void CMatrix::multiplikation(CMatrix* a, CMatrix* b) /*
------------------------------------------------- */
{ /* Multipliziert die Matrizen a*b, wobei immer erste Mtraix mal zweite Matri
ACHTUNG c.multiplikation(a,b) ist nicht gleich c.multiplikation(b,a) :
| b11 b12
| b21 b22
| b31 b32
----------------|-----------------------------------------------
a11 a12 a13 |a11*b11+a12*b21+a13*b31 a11*b12+a12*b22+a12*b32
a21 a22 a23 |a21*b11+a22*b21+a23*b31 a21*b12+a22*b22+a23*b32
Speicher allocieren !!
c.setDim(a.nrl,a.nrh, b.ncl,b.nch);
*/
int i,j, k;
double d=0.0;
for (i=nrl; i <= nrh; i++)
{ for (j=ncl; j <= nch; j++)
{ d=0.0;
for (k=a->ncl; k <= a->nch; k++)
{ d +=a->wert[i][k]*b->wert[k][j];
}
wert[i][j] = d;
}
}
}
void CMatrix::T(CMatrix* a) /*
------------------------------------------------- */
{ /* Transponiert Matrix a
c.setDim(a.ncl,a.nch, a.nrl, a.nrh) wobei c die Transponierte
matrix ist
*/
int i,j;
for (i=nrl; i <= nrh; i++)
{ for (j=ncl; j <= nch; j++)
{
wert[i][j] = a->wert[j][i];
}
}
}
double CMatrix::diagonal_product()
{
int j;
double d=1.0;
for (j=nrl; j <= nrh; j++)
d*= wert[j][j];
return (d);
}
double CMatrix::trace()
{
int j;
double d=0.0;
for (j=nrl; j <= nrh; j++)
d+= wert[j][j];
return d;
}
void CMatrix::spiegeln() /*
--------------------------- */
{ /* Spiegelt den Inhalt der Matrix oberhalb der Diagonalen
nach unten (untehalb der Diagonalen)
*/
int i,j;
for (i=nrl; i <= nrh; i++)
for (j=i+1; j <= nrh; j++)
wert[j][i] = wert[i][j];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -