n2 , . , n n2 , . , n=25 300 0:
(n2-n)/2 = (252-25)/2=(625-25)/2 = 300
. , , , .
. .
= +
- Ci,j = Ai,j + Bi,j.
= -
- Ci,j = Ai,j + Bi,j.
= *
- Ci,j, i j :
Ci,j=(Ai,0*B0,j)+ (Ai,1*B1,j)+ (Ai,2*B2,j)++ (Ai,n-1*Bn-1,j)
, . .
n2, , . . . 3.1 , .
1
n | (A0,0A0,n-1) | |
n-1 | (A1,1A1,n-1) | |
n-2 | (A2,2A2,n-1) | |
n-2 | (An-2,n-2An-2,n-1) | |
n-1 | (An-1,n-1) |
, Ai,j. j < i Ai,j 0 . j ³ i i. i (rowTable) .
rowTable | ||
rowTable[0] = 0 | 0 0 | |
rowTable [1] = n | n 1 ( 0) | |
rowTable [2] = n + n-1 | n + n-1 2 | |
rowTable[3] = n + n-l+n-2 | 3 | |
n-1 | rowTable[n-1] = n + n-1 +... +2 |
4.
, , Ai,j :
|
|
i j,
rowTable
Ai,j :
j<i, Ai,j = 0 .
j³i, rowTable[i], , , i. i i . Ai,j M[rowTable[i]+(j-i)].
5.
[3][3] 3.4:
1.0,2 =M[rowTable[0]+(2-0)]=[0+2]=[2]=0
2.X1,0
3.1,2 =M[rowTable[l]+(2-1)]=[3+1]=[4]=1
TriMat
TriMat . . , , 25. 300=(252-25)/2 , 325 .
TriMat
#include <iostream.h>
#include <stdlib.h>
//
//
const int ELEMENTLIMIT = 325;
const int ROWLIMIT = 25;
class TriMat
{
private:
// -
int rowTable[ROWLIMIT]; //
int n; // /
double [ELEMENTLIMIT];
public:
// TriMat(int matsize);
//
void PutElement (double item, int i, int j);
double GetElement(int i, int j) const;
//
TriMat AddMat(const TriMat& A) const;
double DelMat(void) const;
// /
void ReadMat(void);
void WriteMat(void) const;
//
int GetDimension(void) const;
};
. PutEle-ment GetElement . GetElement 0 . AddMat . . / ReadMat WriteMat n x n. ReadMat - .
#include trimat.h // TriMat
TriMat A (10), (10), (10); // 10x10
A.ReadMat (); //
B.ReadMat ();
= A. AddMat (); // = +
C.WriteMat (); //
TriMat
n matsize. . rowTable, . matsize ROWLIMIT, .
// n rowTable
TriMat::TriMat (int matsize)
{
int storedElements = 0;
// , matsize ROWLIMIT
if (matsize > ROWLIMIT)
{
cerr << " " << ROWLIMIT << "x" << ROWLIMIT << endl;
exit (1);
}
n = matsize;
//
|
|
for (int i = 0; i < n; i++)
{
rowTable [i] = storedElements;
storedElements += n - i;
}
}
. . i j , PutElement GetElement .
GetDimension . , , :
// n
int TriMat::GetDimension(void) const
{
return n;
}
PutElement i j. j ³ i, , : i j 0.. (n-1), :
// [i,j]
void TriMat::PutElement (double item, int i, int j)
{
// ,
//
if ((i < 0 || i >= n) || (j < 0 |1 j >= n))
{
cerr << "PutElement: 0"<< n-1 << endl;
exit (1);
}
// if (j >= i)
M[rowTable[i] + j-i] = item;
}
GetElement i j. i j 0(n 1), . j<i, 0. GetElement 0. , j³i, :
// [i, j]
double TriMat::GetElement(int i, int j) const
{
// ,
if ((i < 0 || i >= ) || (j < 0 |I j >= n))
{
cerr << "GetElement: 0"<< n-1 << endl;
exit (1);
}
if (j >= i)
// ,
return M[rowTable [i] + j-i];
else
// 0,
return 0;
}
/ . , , , . TriMat . , .
// ,
// (n x n)
void TriMat::ReadMat (void)
double item;
int i, j;
for(i = 0; i<n; i++) //
for(j = 0; j<n; j++) //
{
cin >> item; // [i, j ]-
PutElement (item, i, j); //
//
void TriMat::WriteMat (void) const
{
int i,j;
//
cout. setf (ios::fixed);
cout.precision (3);
cout.setf (ios::showpoint);
for (i =0; i < n; i++)
{
for (j = 0; j < n; j++)
cout << setw(7) << GetElement (i,j);
cout << endl;
}
}
. TriMat . AddMat , . . , X Y AddMat X. , Z.
Z = + Y
Z = X.AddMat(Y);
TriMat Bi,j = CurrentObjectyi,j + Ai,j:
|
|
// .
//
TriMat TriMat::AddMat (const TriMat& A) const
int i, j;
double itemCurrent, itemA;
TriMat B(A.n); //
for (i = 0; i < n; i++) //
{
for (j = i; j < n; j++) //
{
itemCurrent=GetElement i, j);
itemA = A.GetElement (i, j);
B. PutElement (itemCurrent + itemA, i, j);
}
}
return B;
}
DetMat . - , . TriMat .
3.5. TriMat
TriMat /, . .
#include <iostream.h>
#include <iomanip.h>
#include "trimat.h" // TriMat
void main(void)
{
int n;
//
cout << " ? ";
cin >> n;
// (n x n)
TriMat A(n), B(n), C(n);
//
cout<<" "<<n<<""<<n<<
" " << endl;
A.ReadMat();
cout << endl;
cout << " " << n << " x " << n <<
" " << endl;
B.ReadMat();
cout << endl;
//
cout << " + " << endl;
= A.AddMat();
C.WriteMat();
Cout << endl;
cout << " += " << .DetMat () << endl;
/*
< 3.5>
? 4
4x4
4x4
+
2.000 6.000 10.000 12.000
0.000 4.000 10.000 2.000
0.000 0.000 6.000 8.000
0.000 0.000 0.000 7.000
+= 336.000 */