2013-10-29 18 views
0

我正在學習如何使用內置在VC多精度MPIR庫++ 2008 下面的代碼是整數計算階乘:如何將基於mpir庫的VC9.0代碼轉換爲Matlab mex函數?

#include "stdafx.h" 
#include <afxwin.h>//for clipboard operation 
#include <iostream> 
#include <iomanip> 
#include <stdio.h> 

#include <windows.h> 

#include <ctype.h> 

using namespace std; 

#pragma warning(disable: 4800) 
#include <mpir.h> 
#include <mpirxx.h> 
#pragma warning(default: 4800) 


HANDLE hCon; 


enum Color { DARKBLUE = 1, DARKGREEN, DARKTEAL, DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY, BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE }; 

void SetColor(Color c){ 
    if(hCon == NULL) 
     hCon = GetStdHandle(STD_OUTPUT_HANDLE); 
    SetConsoleTextAttribute(hCon, c); 
} 


int _tmain(int argc, _TCHAR* argv[]) 
{ 
    mpf_class a(1,19449),b(1,19449),c(1,19449);// 

    SetColor(GREEN); 
    cout<< "Factorial Calculator v0.0, Shuai Xiao Dai, 2013" <<endl<<endl; 

    SetColor(RED); 
    cout<<"Input an integer to calculate its factorial."<<endl; 
    SetColor(PINK); 


    while(cin>>a){ 
     c=a; 

     if (a<=0){ 

      if (a<0) 
      {SetColor(PINK); 
      cout<<"Sorry, we don't handle negative numbers ... "<<endl;} 
      a = 1; 

     } 
     else{ 
      if(a>=1){ 
       for(;a>=1;a--) 
       { 
        b *=a; 
       } 

      } 
      else{ 
       SetColor(PINK); 
       cout<<"Sorry, we don't what happens ... "<<endl;  
       a = 1;} 


      SetColor(YELLOW); 
      cout<<setprecision(50)<<c<<"! = "; 

      SetColor(WHITE); 
      cout << setprecision (10500) <<b << endl<<endl; 


      SetColor(RED); 
      cout<<"Input a new integer to calculate its factorial:" <<endl; 
      SetColor(PINK); 


      b=1; 

     } 


    } 
    system("pause"); 
    return 0; 
} 

我的問題是:

1)我怎麼能轉換將相同的因子計算器轉換爲Matlab mex函數,以便我不必使用相對較慢的符號工具箱? 2)特別是如何將「多精度」結果傳遞到符號的matlab變量中會出現問題;

+1

爲什麼使用'mpf'而不是'mpz'?另外還有'mpz_fac_ui'。 –

+0

謝謝你的回覆;你能否指出mpz的更多細節? – LCFactorization

+1

http://gmplib.org/manual/ –

回答