예제 #1
0
bool SimpleReductor::reduceStep (Polynomial& r, const Polynomial& p) {
   Polynomial t;
   r.copy(p);
   Monomial lm = r.lm();
   int i, i0 = -1;
   for (i = 0; i < g.size(); ++i) {
      Monomial m = g[i].lm();
      if (MP.divides(lm, m)) {
         t.mul(g[i], MP.div(lm, m));
         Cf f;
         Ring::neg(f, r.lc());
         r.add(t, t.lc(), f);
         return true;
      }
   }
   return false;
}
예제 #2
0
int main()
{
    double df0 = 1.86548, dfn = -0.046115;
    int n = 18;
    double x[19] = {0.52, 3.1, 8, 17.95, 28.65, 39.62, 50.65, 78, 104.6, 156.6, 208.6, 260.7, 312.5, 364.4, 416.3, 468, 494, 507, 520};
    double y[19] = {5.288, 9.4, 13.84, 20.2, 24.9, 28.44, 31.1, 35, 36.9, 36.6, 34.6, 31, 26.34, 20.9, 14.8, 7.8, 3.7, 1.5, 0.2};
    double h[19], u[19], lamda[19], d[20];

    for (int i = 0; i < n; i++)
        h[i] = x[i+1] - x[i];
    for (int i = 1; i < n; i++)
        u[i] = h[i-1]/(h[i-1]+h[i]);
    u[n] = 1;
    lamda[1] = 1;
    for (int i = 2; i <= n; i++)
        lamda[i] = h[i-1]/(h[i-2]+h[i-1]);
    d[1] = 6.0/h[0]*((y[1]-y[0])/h[0] - df0);
    d[n+1] = 6.0/h[n-1]*(dfn - (y[n] - y[n-1])/h[n-1]);
    for (int i = 2; i <= n; i++)
        d[i] = 6.0*((y[i] - y[i-1])/h[i-1] - (y[i-1]-y[i-2])/h[i-2])/(h[i-2]+h[i-1]);
    Matrix mat(n+1);
    mat.setM(u, lamda);
    mat.setB(d);
    double *M = mat.getSolution();
    for (int i = 0; i <= n; i++)
        M[i] = M[i+1];
    int _j[6] = {0,4,8,13,15,17};
    double _x[6] = {2,30,133,390,470,515};
    Polynomial *tmp1 = new Polynomial(), *tmp2 = new Polynomial();
    for (int i = 0; i < 6; i++)
    {
        int j = _j[i];
        Polynomial S;

        tmp1->set1();
        tmp2->set1ci(-x[j+1]);
        for (int _i = 0; _i < 3; _i++)
            tmp1->multiply(tmp2);
        tmp1->multiply_number(-M[j]/6/h[j]);
        S.add(tmp1);

        tmp2->multiply_number((M[j]*h[j]*h[j]/6-y[j])/h[j]);
        S.add(tmp2);

        tmp1->set1();
        tmp2->set1ci(-x[j]);
        for (int _i = 0; _i < 3; _i++)
            tmp1->multiply(tmp2);
        tmp1->multiply_number(M[j+1]/6/h[j]);
        S.add(tmp1);

        tmp2->multiply_number((-M[j+1]*h[j]*h[j]/6+y[j+1])/h[j]);
        S.add(tmp2);

        cout << "f(" << _x[i] <<") = " << S.value(_x[i]) << '\n';
        cout << "f'(" << _x[i] <<") = " << S.d1(_x[i]) << '\n';
        cout << "f''(" << _x[i] <<") = " << S.d2(_x[i]) << '\n';
    }
    delete tmp1;
    delete tmp2;
    delete[] M;
    return 0;
}
int main(int argc, char *argv[])
{   
    // ################### file IO ###################
    ifstream fin;  // for reading file 
    ofstream fout; // for writing file 
    string temp; 
    
    // argv[0] 為程式路徑,所以要讀第二個參數,也就是 argv[1]。
    if (argc == 2){  
        // 開檔 input1.txt
        fin.open(argv[1]);
    }
    // 如果沒有在 command line 輸入參數,則從程式裡面輸入。
    else{ 
        cout << "Please input file name: ";
        cin >> temp;
        // 開檔 input1.txt
        fin.open(temp.c_str()); // c_str() : string to char string
    }
    
    //­ 若開檔 input1.txt 失敗,可能是檔案不存在。
    if (fin.fail()){ 
        cout << "Fail to open file\n";
        exit(1); // 非正常結束程式
    } 

    // 建新檔 output1.txt,預設為覆蓋。
    string output_fileName = "output";
    output_fileName = output_fileName + argv[1][5] + ".txt";
    fout.open(output_fileName.c_str()); 

    //­ 若開檔 output1.txt 失敗
    if (fout.fail()){ 
        cout << "Fail to open file\n";
        exit(1); // 非正常結束程式
    }

    // ################### file parse ###################
    char m;
    char cur_operator, next_operator;
    int a, b;
    int polyNumber = 1;
    Polynomial polyResult;
    // string operator = "+";
    
    while (!fin.eof()){  //­ not End-of-file,尚未讀到結尾。
        Polynomial poly;

        // set each Polynomial block
        while(true){
            fin >> a >> b;  // '>>' 等同 get(),character by character
            poly.set(b, a); // Polynomial set()
            fin.get();      // 把換行符號吃掉。
            m = fin.get();  // 嘗試吃一個 character

            // 如果 m 是 operator 
            if( m == '+' || m == '*'){
                next_operator = m;
                break;
            }
            else if( m == EOF ){ // end-of-file
                break;
            }
            else{
                fin.putback(m);  // 呃,吃錯了,吐回去。
            }
        }

        //print every poly 寫進 output file
        fout << "Polynomial " << polyNumber << ":" << poly.getPoly() << endl;    
        fout << "Degree:"<< poly.getDegree() << endl;
        fout << "# of nonzero var: " << poly.getNonZero() << endl<< endl;

        if( polyNumber == 1 ){ 
            polyResult.add(poly);
        }
        else if( cur_operator == '+'){
            polyResult.add(poly);
        }
        else if( cur_operator == '*'){
            polyResult.multiplies(poly);
        }
        cur_operator = next_operator; 
        polyNumber++;
    } 
    //printPolyResult
    fout << "Result Polynomial:" << polyResult.getPoly() << endl;    
    fout << "Degree:"<< polyResult.getDegree() << endl;
    fout << "# of nonzero var: " << polyResult.getNonZero();

    // file close
    fin.close();
    fout.close(); 
}