Face questions
The meaning of problems
Given \ (\ n-) on dimensional sphere \ (n + 1 \) point \ (A_0 \ SIM A_n \) , find the sphere center.
answer
Order spherical center section \ (I \) dimensional coordinates \ (x_i \) , the first \ (I \) of points \ (J \) dimensional coordinates \ ((A_i) _J \) , the sphere radius \ (R & lt \) , the equation can be obtained:
\[\sum_{j=1}^n ((A_i)_j-x_j)^2=r^2\]
Such equations total \ (n + 1 \) a, the \ (r ^ 2 = \ sum_ {j = 1} ^ n ((A_0) _j-x_j) ^ 2 \) substituting \ (A_1 \ sim A_n \) corresponding to the right side of the equation:
\[ \begin{aligned} \sum_{j=1}^n ((A_i)_j-x_j)^2&=\sum_{j=1}^n ((A_0)_j-x_j)^2\\ \sum_{j=1}^n (A_i)_j^2-\sum_{j=1}^n2(A_i)_j x_j+\sum_{j=1}^n x_j^2&=\sum_{j=1}^n (A_0)_j^2-\sum_{j=1}^n2(A_0)_j x_j+\sum_{j=1}^n x_j^2\\ \sum_{j=1}^n (2(A_0)_j-2(A_i)_j) x_j&=\sum_{j=1}^n (A_0)_j^2-\sum_{j=1}^n (A_i)_j^2\\ \end{aligned} \]
Can be obtained by the above method \ (n-\) a \ (n-\) linear equation, simultaneous equations, determined by the Gaussian elimination \ (x_1 \ sim x_n \) values.
Code
#include<iostream>
#include<cstdio>
using namespace std;
typedef long double ld;
const int maxn=15;
ld a[maxn][maxn],x[maxn];
void solve(int n,int m){
int i,j,k,p;
double t;
for (i=0;i<n;i++){
p=i;
for (j=i+1;j<n;j++) if (a[j][i]>a[p][i])
p=j;
for (j=0;j<m;j++) swap(a[i][j],a[p][j]);
for (j=0;j<n;j++) if (j!=i){
t=a[j][i]/a[i][i];
for (k=i;k<m;k++) a[j][k]-=t*a[i][k];
}
}
}
int main(){
int i,j,k,n;
ld t;
scanf("%d",&n);
for (i=0;i<n;i++) scanf("%Lf",&x[i]);
for (i=0;i<n;i++) for (j=0;j<n;j++){
scanf("%Lf",&t);
a[i][j]=2*t-2*x[j];
a[i][n]+=t*t-x[j]*x[j];
}
solve(n,n+1);
for (i=0;i<n;i++) printf("%.3Lf%c",a[i][n]/a[i][i],i<n-1?' ':'\n');
return 0;
}