最近開始需要研究一些框架, 所以找到了這個, 簡單記錄一下就好.

 

 

Step1. Install Boost

前往網站 > https://www.boost.org/users/history/version_1_79_0.html > 

windows boost_1_79_0.7z

下載

 

 

Step2. 打開VS2017 > 開啟專案 

#include <iostream>
#include <cstdlib>
#include <array>
#include <bitset>
#include <boost/multiprecision/cpp_int.hpp>


using namespace std;
using namespace boost::multiprecision;

#include <omp.h>
typedef array<array<cpp_int, 2>, 2>matrix; //STL 2D array

// operator overloading as function
matrix operator*(const matrix& A, const matrix& B) {
    matrix C;
    for (int i = 0; i < 2; i++){
        for (int j = 0; j < 2; j++) {
            C[i][j] = 0;
            for (int k = 0; k < 2; k++)
                C[i][j] += A[i][k] * B[k][j];

        }
    }
    return C;
}


matrix pow(const matrix& A, int n) {
    matrix C{ {{1,0}, {0,1}} }; //identity matrix
    for (int i = 1; i <= n; i++) {
        C = C * A;
    }
    return C;
}

matrix operator^(const matrix& A, int n) {
    matrix C{ {{1,0},{0,1}} };
    if (n == 0)return C;
    else{
        bitset<32> b(n);
        for (int i = 31; i >= 0; i--) {
            C = C * C;
            if (b[i]) C = C * A;
        }
    }
    return C;
}


void print(matrix C) {
    cout << "----------------------------------------\n";
    for (int i = 0; i < 2; i++){
        for (int j = 0; j < 2; j++){
            cout << C[i][j] << "\t";
        }
        cout << endl;
    }
}


void main()
{
    matrix A{ {{1,1}, {1,0}} };
    matrix B{ {{1,1}, {0,1}} };
    double s = omp_get_wtime();
    matrix C = pow(A, 1023);
    s = omp_get_wtime() - s;
    print(C);
    cout << s << " sec.\n";


    s = omp_get_wtime();
    C = A ^ 1023;
    s = omp_get_wtime() - s;

    print(C);
    cout << s << " sec\n";

    cout << "Fibonacci sequence f (1023) = " << C[0][0] << endl;
    system("pause");


}

 

 

Step3. 更改屬性> Property Page > C/C++ > General > D:\boost_1_79_0  ( 剛剛安裝的目錄 ) > ok > 搞定

 

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Eric 的頭像
    Eric

    一個小小工程師的心情抒發天地

    Eric 發表在 痞客邦 留言(0) 人氣()