# [Scilab-users] The numderivative function

7 messages
Open this post in threaded view
|

## [Scilab-users] The numderivative function

 Hi; The numderivative function consumes a lot of time in the calculations. In the following script I show two options for my "odes" function In one of them I use the numderivative function; and the calculation is extended by 9 minutes. And in the other I use the Jacobian determinant of the function, for each variable, obtained in wxMaxima. And the calculation is a few, few, seconds. I am interested in using the function with numderivative, and very similar to how I have developed it, since it allows me to use it for systems of more equations. //Draghilev's Method clc; clear; function Sys=Q(x) Sys(1)=(x(1)-2)^2+(x(2)-2)^2+x(3)^2-9; Sys(2)=x(1)^6+x(2)^6+x(3)^6-12; Sys(3)=x(3); endfunction [j,v,info]=fsolve([2;-0.1;3],Q) ics=j; // N=100; smin=0.0; smax=0.046; h=0.001; *function [dydt]=odes(t,x) //Scilab gfd=numderivative(Q,x); [r,c]=size(gfd) ic=1:c for i=1:c dydt(i)=(-1)^i*det(gfd(1:\$-1,find(ic~=i))); end endfunction * //from wxMaxima function dydt=odes(t,x)     //dydt=zeros(x);     dydt(1)=-12*(x(2)-2)*x(3)^5+12*x(2)^5*x(3)     dydt(2)=12*(x(1)-2)*x(3)^5-12*x(1)^5*x(3)     dydt(3)=-12*(x(1)-2)*x(2)^5+12*x(1)^5*(x(2)-2) endfunction     step=smax/N;     t=[smin:step:smax*N];     t0=0     atol=h/100000;     LL= ode( ics,t0, t,atol,odes)     clf;     drawlater()     param3d(LL(3,:)',LL(2,:)',LL(1,:)'); set(gce(),"mark_mode","on","mark_style",2,"mark_size_unit","point","mark_size",4,"line_mode","off")     drawnow() Is it possible to decrease the calculation time if I manipulate the options of% ODEOPTIONS = [*, ..., *]? What parameters would I manipulate? Gracias -- Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: The numderivative function

 Le 18/11/2017 à 22:01, Hermes a écrit : ```Hi; The numderivative function consumes a lot of time in the calculations. In the following script I show two options for my "odes" function In one of them I use the numderivative function; and the calculation is extended by 9 minutes. And in the other I use the Jacobian determinant of the function, for each variable, obtained in wxMaxima. And the calculation is a few, few, seconds. I am interested in using the function with numderivative, and very similar to how I have developed it, since it allows me to use it for systems of more equations.``` Your script makes Scilab 6.0.0 crashing every time it is run => trials done only on 5.5.2 I get 145 s by default when numderivative() is called as usual from its library. Without numderivative(), i get 1.97 s. After exec(SCI+"/modules/optimization/macros/numderivative.sci",-1), this time decreases down to 45 s only. This is unlikely proper to numderivative(). 5.5.2 looks to miss an optimization. For the time being, no way to test this on 6.0.0. Your script calls numderivative() ~57000 times. Profiling  numderivative() and its dependencies shows that numderivative_evalf() spends a lot of time. Replacing execstr(.., "errcatch") in numderivative_evalf() with try/catch, we get 38 s Then, mainly, vectorizing the function to derivate in order to accept x with N columns instead of only 1, and changing numderivative() in order to be able to work with this new vectorized profile, we get 29 s. Improving numderivative() in order to be fully able to work with vectorized functions should be possible, but then at first sight it will be less easy to keep it able to run with functions that are NOT vectorized (=> back-compatibility issue) The jacobian would become an hypermatrix (OK) The hessian would get up to 4 dimensions You may post a report on bugzilla in order to point numderivative slowness() (if it is not already reported)(Bugzilla is currently off, taking its week-end). Regards Samuel Gougeon _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: The numderivative function

 Le 18/11/2017 à 22:38, Samuel Gougeon a écrit : Le 18/11/2017 à 22:01, Hermes a écrit : ```Hi; The numderivative function consumes a lot of time in the calculations. In the following script I show two options for my "odes" function In one of them I use the numderivative function; and the calculation is extended by 9 minutes. And in the other I use the Jacobian determinant of the function, for each variable, obtained in wxMaxima. And the calculation is a few, few, seconds. I am interested in using the function with numderivative, and very similar to how I have developed it, since it allows me to use it for systems of more equations.``` Your script makes Scilab 6.0.0 crashing every time it is run => trials done only on 5.5.2 I get 145 s by default when numderivative() is called as usual from its library. Without numderivative(), i get 1.97 s. After exec(SCI+"/modules/optimization/macros/numderivative.sci",-1), this time decreases down to 45 s only. This is unlikely proper to numderivative(). 5.5.2 looks to miss an optimization. For the time being, no way to test this on 6.0.0. Your script calls numderivative() ~57000 times. Profiling  numderivative() and its dependencies shows that numderivative_evalf() spends a lot of time. Replacing execstr(.., "errcatch") in numderivative_evalf() with try/catch, we get 38 s Then, mainly, vectorizing the function to derivate in order to accept x with N columns instead of only 1, and changing numderivative() in order to be able to work with this new vectorized profile, we get 29 s. Improving numderivative() in order to be fully able to work with vectorized functions should be possible, but then at first sight it will be less easy to keep it able to run with functions that are NOT vectorized (=> back-compatibility issue) The jacobian would become an hypermatrix (OK) The hessian would get up to 4 dimensions This is assuming that the architecture of the current numderivative() implementation is kept. A complete refactoring would be possible. But used for ode(), IMO this would not save much of the time spent, since anyway ode() calls the function for a single point. Beside the lack of vectorization, the order is not free. For instance, order=3 needs to call numderivative() twice, for instance with order=2 then with order=1. This limitation is a bit strange. SG _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: The numderivative function

 Hi samuel, Such good news! to be able to see completely:"Scilab users - Mailing Lists Archives" My questions to your answer. Thanks for them: How can I reproduce this: "Then, mainly, vectorizing the function to derive in order to accept x with N columns instead of only 1, and changing numderivative () in order to be able to work with this new vectorized profile, we get 29 s." You can guide me with your answer, if possible with an example: "Improving numderivative () in order to be fully able to work with vectorized functions should be possible, but then: ...."   regards I would very much like to be able to master numderivative. You could guide me to a website where I can read and study the vectorization in Scilab. I still do not feel comfortable with it. Hermes -- Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: The numderivative function

 Hello Hermes, As stated in my first answer, the only actually valuable thing to do to shorten the execution time is exec(SCI+"/modules/optimization/macros/numderivative.sci",-1) before the first call to numderivative() (through ode(), in your case). This will decrease the execution time by a factor of 2,33 (105s/45s, not 145/45. I made a mistake in my first answer). But this is not compatible with putting some "clear" instructions everywhere, as matlabers do. Any "clear" clears the redefinition done with exec() and so cancels the gain in speed. So if you are a "clear" addict, you should put it before the exec(). Otherwise, each call to numderivative() will use it from it library and take 105s (in all) instead of 45s. I never ever used "clear" () in my Scilab work for 20 years. I don't need it in Scilab, if it does not apply to some explicitly named variables. Except the execstr() => try/catch, other modifications = partial vectorization are somewhat some hacks, and the gain is poor, and back-compatibility is of concern. This is why i did not attach them to my mail. It is possible to improve a lot the speed of numderivative() in the case of calling it for a whole set of points instead of only a single point as currently done. But this  would need to completely overhaul and likely rewrite the code. But this will not be a big gain when calling it through ode(), because ode() calls the function with a scalar argument. With a complete fine profiling, there is likely some room for internal optimization of the existing code, but for a marginal gain (IMO less than a factor ~1.4 in speed). With 5.5.2, you can use add_profiling() and showprofile() to analyze the code efficiency, as i did. This will show you the parts of the code that take most of the execution time, so where to put your efforts to optimize it. Regards Samuel Le 19/11/2017 à 16:44, Hermes a écrit : ```Hi samuel, Such good news! to be able to see completely:"Scilab users - Mailing Lists Archives" My questions to your answer. Thanks for them: How can I reproduce this: "Then, mainly, vectorizing the function to derive in order to accept x with N columns instead of only 1, and changing numderivative () in order to be able to work with this new vectorized profile, we get 29 s." You can guide me with your answer, if possible with an example: "Improving numderivative () in order to be fully able to work with vectorized functions should be possible, but then: ...." regards I would very much like to be able to master numderivative. You could guide me to a website where I can read and study the vectorization in Scilab. I still do not feel comfortable with it. Hermes -- Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users ``` _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users