usage of push_back/front and pop_back/front in scilab API

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

usage of push_back/front and pop_back/front in scilab API

Hello,

I have a newbie question about the usage of lists in the new scilab API.
When e.g. you have to construct a list of vectors constructed from a vector

double* YData

which is updated and to be stored after each update in a list names
pDblYOutList. By writing

pDblYOutList.push_back(pdYData);

at each update, since the address of YData does not change, at the end
every term of the list will contain the same vector (ode bug :-(). I
have some ideas to fix that but how does one proceed *properly* in the
coding style of Scilab API ?

S.



--
Stéphane Mottelet
Ingénieur de recherche
EA 4297 Transformations Intégrées de la Matière Renouvelable
Département Génie des Procédés Industriels
Sorbonne Universités - Université de Technologie de Compiègne
CS 60319, 60203 Compiègne cedex
Tel : +33(0)344234688
http://www.utc.fr/~mottelet               

_______________________________________________
dev mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/dev
mottelet mottelet
Reply | Threaded
Open this post in threaded view
|

Re: usage of push_back/front and pop_back/front in scilab API

Le 09/04/2018 à 15:45, Stéphane Mottelet a écrit :

> Hello,
>
> I have a newbie question about the usage of lists in the new scilab
> API. When e.g. you have to construct a list of vectors constructed
> from a vector
>
> double* YData
>
> which is updated and to be stored after each update in a list names
> pDblYOutList. By writing
>
> pDblYOutList.push_back(pdYData);
>
> at each update, since the address of YData does not change, at the end
> every term of the list will contain the same vector (ode bug :-(). I
> have some ideas to fix that but how does one proceed *properly* in the
> coding style of Scilab API ?
>
> S.
>
>
>
Supposing that pDblY0 exists, with  the required size, and pDblYOut is a
types::Double*, does the following do the trick:

             pDblYOut = new types::Double(pDblY0->getRows(), 1);
             pDblYOut->set(pdYData);
             pDblYOutList.push_back(pDblYOut->get());

Thanks for hints, comments..

S.

--
Stéphane Mottelet
Ingénieur de recherche
EA 4297 Transformations Intégrées de la Matière Renouvelable
Département Génie des Procédés Industriels
Sorbonne Universités - Université de Technologie de Compiègne
CS 60319, 60203 Compiègne cedex
Tel : +33(0)344234688
http://www.utc.fr/~mottelet

_______________________________________________
dev mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/dev
Clément David-3 Clément David-3
Reply | Threaded
Open this post in threaded view
|

Re: usage of push_back/front and pop_back/front in scilab API

Le lundi 09 avril 2018 à 16:07 +0200, Stéphane Mottelet a écrit :

> Le 09/04/2018 à 15:45, Stéphane Mottelet a écrit :
> > Hello,
> >
> > I have a newbie question about the usage of lists in the new scilab
> > API. When e.g. you have to construct a list of vectors constructed
> > from a vector
> >
> > double* YData
> >
> > which is updated and to be stored after each update in a list names
> > pDblYOutList. By writing
> >
> > pDblYOutList.push_back(pdYData);
> >
> > at each update, since the address of YData does not change, at the end
> > every term of the list will contain the same vector (ode bug :-(). I
> > have some ideas to fix that but how does one proceed *properly* in the
> > coding style of Scilab API ?
> >
> > S.
> >
> >
> >
>
> Supposing that pDblY0 exists, with  the required size, and pDblYOut is a
> types::Double*, does the following do the trick:
>
>              pDblYOut = new types::Double(pDblY0->getRows(), 1);
>              pDblYOut->set(pdYData);
>              pDblYOutList.push_back(pDblYOut->get());
>
> Thanks for hints, comments..
>
> S.

Hello Stéphane,

As `double* pdYData` is updated you have to copy the data to preserve the history ; you could either
 use Scilab API to store types::Double or plain C++ to store double* :

 auto pDblYOut = new types::Double(pDblY0->getRows(), 1);
 pDblYOut->set(pdYData); // will copy pdYData
 pDblYOutList->getData()->push_back(pDblYOut); // to preserve a pointer to the Scilab datatype with
                                               // types::List* pDblYOutList

OR

 auto temp = new double[pDblY0->getRows()];
 memcpy(temp, pdYData, pDblY0->getRows()*sizeof(double));
 pDblYOutList.push_back(temp); // using std::vector<double*> pDblYOutList

However (especially in ode() case) not that allocating on each loop step can be really slow ; having
all the buffers pre-allocated at first will slightly improve loop caching.

Regards,

--
Clément
_______________________________________________
dev mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/dev