Pregunta

I am learning how to use the multiple precision mpir library built in VC++ 2008. Below the code is for factorial of integer computation:

#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;
}

My questions are:

1) how can I convert the same factorial calculator into an Matlab mex function so that I don't have to use the relatively slower symbolic toolbox ?

2) especially there will be a problem how to pass the "multiple precision" result into a symbolic matlab variable;

¿Fue útil?

Solución

Problem solved by:

1) learn how to configure the VS in order to compile an mex in VS IDE, the answer is already available here:configure VS2010 for mex

2) learn how to handle multiple precision and argument passing:pass arguments

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top