/*当方程组是多维的时候
,求解效率很低
。 Cramer法则详细如下:
设线性方程组为:
a11x1 + a12x2 + ... + a1nxn = b1
a21x1 + a22x2 + ... + a2nxn = b2
................
an1x1 + an2x2 + ... + annxn = bn
那么 x1 = D1/D
, x2 = D2/D,......, xn = Dn/D
其中 D是系数行列式,Di是用常数向量替换D的第j列所得的n阶行列式
*/
void cramer_calculate(int (*p)[3], int* B, int size)
{
double* X = new double[size];
int D = 0;
for(int j, i = 0; i < size; i++) //求取D的值
{
j = 0;
int temp = 1;
while(j < size)
{
if( i + j < size)
temp *= p[i + j][j];
else
temp *= p[i + j - size][j];
j++;
}
D += temp;
j = 0;
temp = 1;
while(j < size)
{
if(i - j >= 0)
temp *= p[i - j][j];
else
temp *= p[i - j + size][j];
j++;
}
D -= temp;
}
int k = 0, Di;
while(k < size) //求解Xi的值
{
Di = 0;
for(int j, i = 0; i < size; i++)
{
j = 0;
int temp = 1;
while(j < size)
{
if(i + j <size)
{
if(j == k)
temp *= B[i + j];
else
temp *= p[i + j][j];
}
else
{
if(j == k)
temp *= B[i + j - size];
else
temp *= p[i + j - size][j];
}
j++;
}
Di += temp;
j = 0;
temp = 1;
while(j < size)
{
if(i - j >= 0)
{
if(j == k)
temp *= B[i - j];
else
temp *= p[i - j][j];
}
else
{
if(j == k)
temp *= B[i - j + size];
else
temp *= p[i - j + size][j];
}
j++;
}
Di -= temp;
}
X[k] = Di * 1.0/D * 1.0;
k++;
}
for(int i = 0; i < size; i++)
std::cout << X[i] << '
';
delete[]X;
}