octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Running a script file inside c++ code


From: sumeet kumar
Subject: Re: Running a script file inside c++ code
Date: Sat, 13 Aug 2016 16:09:42 -0700

Hi Miller, actually I don't know how to do it by the 1st way and thats why asked in the forum.

Since, I had no solution until someone said me that you should ask that question in forum/mailing list.

Anyway, what I currently have, I can show you.

I made a OctPaser class.

-- OctParser.h

#ifndef OCTPARSER_H
#define OCTPARSER_H

#include <string>

using namespace::std;

class OctParser{

    public:

        OctParser();
        ~OctParser();

        string eval(const string& command);
        void exit();

    private:

};

#endif //OCTPARSER_H


--OctParser.cpp

#include <octave/oct.h>
#include <octave/octave.h>
#include <octave/parse.h>
#include <octave/toplev.h>
#include <octave/ov.h>
#include "OctParser.h"

#include <iostream>
#include <fstream>
#include <string>

using namespace::std;

/******************************************************************************
****************************** Constructor ************************************
******************************************************************************/

OctParser::OctParser(){
    char *oct_argv[3] = {(char*)"embeded",(char*)"-q",(char*)"--interactive"};
    octave_main(3,oct_argv,true);
}

OctParser::~OctParser(){
}

/******************************************************************************
**************************** Public Functions *********************************
******************************************************************************/

string OctParser::eval(const string& function) {

    int status = 0;
    ofstream ScriptFile("script", ios::out);
    ScriptFile << function ;
    ScriptFile.close();
    source_file("script");
    //eval_string(function,false,status);
    octave_value f = eval_string("num2str(ans,10);",false,status);
    string val = f.string_value();
    return val;
}

void OctParser::exit(){

    clean_up_and_exit (1);
    // reset();
}

Now let say, I have to compute the following " x=1; y=5;z= x+y; sin(z)". Now what I do is write in a string and then write in a"script"  file using and then evaluate it [look at the function] . An example is shown below.

main.cpp

OctParser Evaluate = OctParser();
string function = " x=1; y=5; x+y"
string result =Evaluate.eval(function);

result

result = 0.07845

This, all works well. The only problem is that I need to generate a "script" file each time I do any of these computations.

Now, what I want is the following.

  1. I do not want to generate any file and then pass on the octave parser. This is very slow and takes lot of time.
  2. I want a function that do not need to generate any external file. i.e I would like a function such as 

Thanks

Sumeet


On Sat, Aug 13, 2016 at 3:29 PM, Mike Miller <address@hidden> wrote:
On Sat, Aug 13, 2016 at 00:18:57 -0700, sumeet kumar wrote:
> Hello all,
>
> I am using octave parser to evaluate _expression_ inside my c++ code. The
> expressions can be very simple can can also be complicated  depending upon
> the user.
>
> 1)So basically, I have a string which contains all the commands that needs
> to be executed by octave. *But I am unable to execute and get the results
> of the string containing commands. *
>
> 2)So, I found another way, in which I write the string in an file and then
> parse it using octave as
>
>     char *oct_argv[3] =
> {(char*)"embeded",(char*)"-q",(char*)"--interactive"};
>     octave_main(3,oct_argv,true);
>     ofstream ScriptFile("script", ios::out);
>     ScriptFile << function ;
>     ScriptFile.close();
>     source_file("script");
>
> where function is the string that needs to be evaluated.
>
> The second approach works but the problem is very slow because of
> generation of file each time I want a computation. I usually end up in
> thousands/millions of them and it gets extremely slow, like taking days..
> to complete.
>
>
> In order to be more efficient, i want to do it as 1st way. So anybody has
> any idea, how to do it more faster/effecient or how to do as in first way.

Please show your code. What have you tried and why didn't it work?

--
mike



--
         
Sumeet Kumar Sinha
Graduate Student
Phone: (+1) 5306018271

reply via email to

[Prev in Thread] Current Thread [Next in Thread]