Пример #1
0
//
//测试行列式是否计算正确;
void Matrix::TestDet()
{
    int test_sum=0;
    //
    /*/输入;
    char * ssb = "  \
    4               \
    1 1             \
    5               \
    4 4             \
    1 1 1 1         \
    1 2 4 8         \
    1 3 9 27        \
    1 4 16 64       \
    3 3             \
    2 0 1           \
    1 -4 -1         \
    -1 8 3          \
    4 4             \
    4 1 2 4         \
    1 2 0 2         \
    10 5 2 0        \
    0 1 1 7         \
    ";
    strstream sst(ssb,strlen(ssb));
    cin = sst;//*/
    //答案;
    double ans[] = {5,12,-4,0};
    //
    cin >> test_sum;
    for(int count=0;count<test_sum;count++)
    {
        int n,m;
        cin >> n >> m;
        //
        Matrix mat;
        mat.NewMatrix(n,m);
        mat.ReadStd();
        int swp = mat.RowSimplify();
        //
        if(Equal(mat.Det(swp),ans[count]))
            cout << count << ".DetRight" << endl;
        else
            cout << count << ".DetWrong" << endl;
        //
        mat.DeleteMatrix();
    }
    cout << endl;
}
Пример #2
0
//
//测试秩是否计算正确;
void Matrix::TestRank()
{
    int test_sum=0;
    //
    /*/输入
    char * ssb = "\
    3           \
    4 5         \
    2 -1 -1 1 2 \
    1 1 -2 1 4  \
    4 -6 2 -2 4 \
    3 6 -9 7 9  \
    3 3         \
    1 2 3       \
    2 3 -5      \
    4 7 1       \
    4 5         \
    3 2 0 5 0   \
    3 -2 3 6 -1 \
    2 0 1 5 -3  \
    1 6 -4 -1 4 \
    ";
    strstream sst(ssb,strlen(ssb));
    cin = sst;//*/
    //答案;
    double ans[] = {3,2,3};
    //
    cin >> test_sum;
    for(int count=0;count<test_sum;count++)
    {
        int n,m;
        cin >> n >> m;
        //
        Matrix mat;
        mat.NewMatrix(n,m);
        mat.ReadStd();
        mat.RowSimplify();
        //
        //mat.Print();
        if(Equal(mat.Rank(),ans[count]))
            cout << count << ".RankRight" << endl;
        else
            cout << count << ".RankWrong" << endl;
        //
        mat.DeleteMatrix();
    }
    cout << endl;
}
Пример #3
0
void Fitting::calculate(double radius)
{
    /*
     * 扩展上三角和最后一列。 */
    for(int i=0;i<MATRIX_SIZE;i++)
    {
        for(int j=i+1;j<MATRIX_SIZE;j++)
        {
            m_matrix[i][j] = m_matrix[j][i];
        }
        m_matrix[i][MATRIX_SIZE] = 0.0;
    }
    /*
     * 解线性方程。 */
    Matrix A;
    A.NewMatrix(MATRIX_SIZE,MATRIX_SIZE+1);// 最后一列是0。
    A.Phase((double *)m_matrix);
    //
    A.RowSimplify();
    /*
     * 作弊,忽略最后一行,解齐次方程的手段,否则只有零解。 */
    A.SetElement(MATRIX_SIZE-1,MATRIX_SIZE-1,0.0);
    A.RowSimplifyMaxly();
    //
    double C[MATRIX_SIZE];
    for(int i=0;i<MATRIX_SIZE;i++)
        C[i] = 1;
    //
    double vp[MATRIX_SIZE+1];
    A.Solve(C,vp);
    A.DeleteMatrix();
    //
    double k = (4*radius*radius);
    k /= (vp[3]*vp[3]/vp[0] + vp[4]*vp[4]/vp[1] \
          + vp[5]*vp[5]/vp[2] - 4*vp[6]);
    m_result[0] = std::sqrt(k * vp[0]);
    m_result[1] = std::sqrt(k * vp[1]);
    m_result[2] = std::sqrt(k * vp[2]);
    m_result[3] = k*vp[3] / (2 * m_result[0]);
    m_result[4] = k*vp[4] / (2 * m_result[1]);
    m_result[5] = k*vp[5] / (2 * m_result[2]);
}