Running a script file inside c++ code

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Running a script file inside c++ code

sumeet kumar
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.

Sumeet

--
         
Sumeet Kumar Sinha
Graduate Student
Phone: (+1)<a href="tel:%28%2B91%29%209910516219" value="+919910516219" style="font-size:12.8px;font-family:&quot;trebuchet ms&quot;,sans-serif;color:rgb(17,85,204)" target="_blank"> 5306018271
Reply | Threaded
Open this post in threaded view
|

Re: Running a script file inside c++ code

tmacchant
--- sumeet.kumar507 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.
>
> Sumeet
>
> --
>          
> Sumeet Kumar SinhaGraduate Student
> Phone: (+1) 5306018271
> Website : http://www.sumeetsinha.in/ 

https://www.gnu.org/software/octave/doc/interpreter/Standalone-Programs.html#Standalone-Programs

Is the second example useful for you?

Other method is to commucate with octave via pipe.

HTH

Tatsuro

Reply | Threaded
Open this post in threaded view
|

Re: Running a script file inside c++ code

Mike Miller-4
In reply to this post by sumeet kumar
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

Reply | Threaded
Open this post in threaded view
|

Re: Running a script file inside c++ code

sumeet kumar
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 
  • FUNCTION(string) -> directly giving me result
  • Also, in the string I should be able to use any function of octave or from any external file, which I am currently able to use through my approach as shown above.

Thanks

Sumeet


On Sat, Aug 13, 2016 at 3:29 PM, Mike Miller <[hidden email]> 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)<a href="tel:%28%2B91%29%209910516219" value="+919910516219" style="font-size:12.8000001907349px;font-family:&#39;trebuchet ms&#39;,sans-serif;color:rgb(17,85,204)" target="_blank"> 5306018271
Reply | Threaded
Open this post in threaded view
|

Re: Running a script file inside c++ code

sumeet kumar
In reply to this post by tmacchant
HI Tatsuro,

I don't think its useful for me. 

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. Because the Source_File() function requires a file_name as parameter.

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 
  • FUNCTION(string) -> directly giving me result
  • Also, in the string I should be able to use any function of octave or from any external file, which I am currently able to use through my approach as shown above.

Thanks



On Sat, Aug 13, 2016 at 2:59 PM, [hidden email] <[hidden email]> wrote:
--- sumeet.kumar507 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.
>
> Sumeet
>
> --
>          
> Sumeet Kumar SinhaGraduate Student
> Phone: <a href="tel:%28%2B1%29%C2%A05306018271" value="+15306018271">(+1) 5306018271
> Website : http://www.sumeetsinha.in/

https://www.gnu.org/software/octave/doc/interpreter/Standalone-Programs.html#Standalone-Programs

Is the second example useful for you?

Other method is to commucate with octave via pipe.

HTH

Tatsuro



--
         
Sumeet Kumar Sinha
Graduate Student
Phone: (+1)<a href="tel:%28%2B91%29%209910516219" value="+919910516219" style="font-size:12.8000001907349px;font-family:&#39;trebuchet ms&#39;,sans-serif;color:rgb(17,85,204)" target="_blank"> 5306018271
Reply | Threaded
Open this post in threaded view
|

Re: Running a script file inside c++ code

Mike Miller-4
On Mon, Aug 15, 2016 at 23:03:49 -0700, sumeet kumar wrote:

> This, all works well. The only problem is that I need to generate a
> "script" file each time I do any of these computations. Because the
> *Source_File*() function requires a file_name as parameter.
>
> 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
>
>
>    - FUNCTION(string) -> directly giving me result
>    - Also, in the string I should be able to use any function of octave or
>    from any external file, which I am currently able to use through my
>    approach as shown above.

Your code shows that you are using both source_file and eval_string.
Have you tried using eval_string alone? Your description looks exactly
like what eval_string is.

--
mike

Reply | Threaded
Open this post in threaded view
|

Re: Running a script file inside c++ code

sumeet kumar

eval_string() cannot evaluate if the string contains any outside function.
I use only eval string to get the value of "ans" variable only.


Sumeet

On Tue, Aug 16, 2016 at 8:00 AM, Mike Miller <[hidden email]> wrote:
On Mon, Aug 15, 2016 at 23:03:49 -0700, sumeet kumar wrote:
> This, all works well. The only problem is that I need to generate a
> "script" file each time I do any of these computations. Because the
> *Source_File*() function requires a file_name as parameter.
>
> 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
>
>
>    - FUNCTION(string) -> directly giving me result
>    - Also, in the string I should be able to use any function of octave or
>    from any external file, which I am currently able to use through my
>    approach as shown above.

Your code shows that you are using both source_file and eval_string.
Have you tried using eval_string alone? Your description looks exactly
like what eval_string is.

--
mike



--
         
Sumeet Kumar Sinha
Graduate Student
Phone: (+1)<a href="tel:%28%2B91%29%209910516219" value="+919910516219" style="font-size:12.8000001907349px;font-family:&#39;trebuchet ms&#39;,sans-serif;color:rgb(17,85,204)" target="_blank"> 5306018271
Reply | Threaded
Open this post in threaded view
|

Re: Running a script file inside c++ code

Mike Miller-4
Please reply below the quoted text on this list if you would.

On Tue, Aug 16, 2016 at 08:37:29 -0700, sumeet kumar wrote:
> eval_string() cannot evaluate if the string contains any outside function.
> I use only eval string to get the value of "ans" variable only.

I assume you mean the "invalid use of statement list" error when you
give it a string containing more than one expression?

That is only if you call eval_string with nargout=1 (which is the
default), meaning you expect the string to be a single expression with a
single result value.

--
mike