Items tagged with efficiency efficiency Tagged Items Feed

Hi,

I have coded up a vector that is of my interest. The code runs witout any problem and gives me exactly what I want.

newtest.mw

All I want to know, is that if there are more efficient way to do so?

Any tricks, or better use of a particular function that I wasn't aware?

The only tiny bit of unsatisfactory is that, the (1-w) term is at the first term of the addition, is that possible to move it to the last term? Which is more conventional to read.

 

Thanks,

 

casper

Hi Maple Prime-ers!

I have a question about efficiency.  I have a set of algebraic equations with some polynomials, that I would like to solve at different points.  I've tried using a for-loop and a map-loop.  Here is a example:

 

n:=10000;  #Number of solving points
eq1:={b = ''a^2'', c = b^3/2, d = c^(1/2)*4 + b^2}; #Equation to solve

a := convert([seq(i,i=1..n)],Vector);  #timesteps

ans := Vector[column](n)

## Try solving in a for-next loop
t1 := time():
for q from 1 to n do
ans(q):=solve(subs({'a' = a(q)},eq1)):
od:
t2 := time() - t1;

## try solving in a map loop
t1s := time():
ans_s := map(q->solve(subs({'a' = a(q)},eq1)),a);
t2s := time() - t1s;

On my computer (2.2Ghz, 2 cores), these both take 115s to solve.  Using Map over For-Next did not speed up computational speed.  

The problem I wish to tackle has 12 equations, invovles 5th order polynomials, and n ~= 300000.  Solving this set of equations takes 2-3 hours.

Anyone know a more efficient method?  Thanks for reading :D

 

Hi,

     So I'm trying to animate a ball moving around in a moving circle. I can do a sequence of points, and a sequence of static circles which works okay but I'd like a continuous animation. Here is the points where the ball hits the circle (x,y) position and the times (t).  and the animation of the circle

restart;
with(plots);

x1 := [.9, -0.6953537244e-1, .5084436548, .5084436548, -.5253004431, -.4186356935,
     -.8180728424, -.8180728424,...

I'm having problems using the complexplot command in Maple 16 with over 1 million points. I really want to plot the points - I don't want an approximation. I noticed some strange things with the list/table carrying my complex points. If I ask for individual values, like T[20], it won't give the correct value, but if I ask for a range, like T[1..20], it will display the correct values.

I get an error when trying to use complexplot saying either invalid range, if I don't...

I have a vector containing data which is m*n long (ie from 1...(m*n)) and I want to convert it to a matrix of m columns and n rows.

What's the quickest and most efficient way in Maple to do this?

Suppose that you wish to animate the whole view of a plot. By whole view, I mean that it includes the axes and is not just a rotation of a plotted object such as a surface.

One simple way to do this is to call plots:-animate (or plots:-display on a list of plots supplied in a list, with its `insequence=true` option). The option `orientation` would contain the parameter that governs the animation (or generates the sequence).

But that entails recreating the same plot each time. The plot data might not even change. The key thing that changes is the ORIENTATION() descriptor within each 3d plot object in the reulting data structure. So this is inefficient in two key ways, in the worst case scenario.

1) It may even compute the plot's numeric results, as many times as there are frames in the resulting animation.

2) It stores as many instances of the grid of computed numeric data as there are frames.

We'd like to do better, if possible, reducing down to a single computation of the data, and a single instance of storage of a grid of data.

To keep this understandable, I'll consider the simple case of plotting a single 3d surface. More complicated cases can be handled with revisions to the techniques.

Avoiding problem 1) can be done in more than one way. Instead of plotting an expression, a procedure could be plotted, where that procedure has `option remember` so that it automatically stores computed results an immediately returns precomputed stored result when the arguments (x and y values) have been used already.

Another way to avoid problem 1) is to generate the unrotated plot once, and then to use plottools:-rotate to generate the other grids without necessitating recomputation of the surface. But this rotates only objects in the plot, and does alter the view of the axes.

But both 1) and 2) can be solved together by simply re-using the grid of computed data from an initial plot3d call, and then constructing each frame's plot data structure component "manually". The only thing that has to change, in each, is the ORIENTATION(...) subobject.

At 300 frames, the difference in the following example (Intel i7, Windows 7 Pro 64bit, Maple 15.01) is a 10-fold speedup and a seven-fold reduction is memory allocation, for the creation of the animation structure. I'm not inlining all the plots into this post, as they all look the same.

restart:
P:=1+x+1*x^2-1*y+1*y^2+1*x*y:

st,ba:=time(),kernelopts(bytesalloc):

plots:-animate(plot3d,[P,x=-5..5,y=-5..5,orientation=[A,45,45],
                       axes=normal,labels=[x,y,z]],
               A=0..360,frames=300);

time()-st,kernelopts(bytesalloc)-ba;

                                1.217, 25685408
restart:
P:=1+x+1*x^2-1*y+1*y^2+1*x*y:

st,ba:=time(),kernelopts(bytesalloc):

g:=plot3d(P,x=-5..5,y=-5..5,orientation=[-47,666,-47],
          axes=normal,labels=[x,y,z]):

plots:-display([seq(PLOT3D(GRID(op([1,1..2],g),op([1,3],g)),
                           remove(type,[op(g)],
                                  specfunc(anything,{GRID,ORIENTATION}))[],
                           ORIENTATION(A,45,45)),
                    A=0..360,360.0/300)],
               insequence=true);

time()-st,kernelopts(bytesalloc)-ba;

                                0.125, 3538296

By creating the entire animation data structure manually, we can get a further factor of 3 improvement in speed and a further factor of 3 reduction in memory allocation.

restart:
P:=1+x+1*x^2-1*y+1*y^2+1*x*y:

st,ba:=time(),kernelopts(bytesalloc):

g:=plot3d(P,x=-5..5,y=-5..5,orientation=[-47,666,-47],
          axes=normal,labels=[x,y,z]):

PLOT3D(ANIMATE(seq([GRID(op([1,1..2],g),op([1,3],g)),
                           remove(type,[op(g)],
                                  specfunc(anything,{GRID,ORIENTATION}))[],
                           ORIENTATION(A,45,45)],
                    A=0..360,360.0/300)));

time()-st,kernelopts(bytesalloc)-ba;

                                0.046, 1179432                            

Unfortunately, control over the orientation is missing from Plot Components, otherwise such an "animation" could be programmed into a Button. That might be a nice functionality improvement, although it wouldn't be very nice unless accompanied by a way to export all a Plot Component's views to GIF (or mpeg!).

The above example produces animations each of 300 frames. Here's a 60-frame version:

Recently posted onto Wolfram's Blog is a set of 10 tips for how to write fast Mathematica code.  It is a very amusing read -- go read it now, because below I am going to make some comments on it, assuming that you have read it.

 

  1. Use floating-point numbers if you can, and use them early.
    Basically: if you're using Mathematica as a...

I have a program that makes very simple operations (such as concatenations or comparisons of binary strings) but it is intended to produce many data.

Maple seems to be in trouble when the number of data grows: whith my (quite old) computer, it begins to be very unefficient when trepassing a thershold  about 5*104 items.

Are there some tricks I do not know?

Inside a procedure, local variables are evaluated only one level. Of what good is this, one might ask?

Well, for one thing it allows you to do checks or manipulations of an unevaluated function call without having that function call be evaluated over again. I mean, for function calls to routines which don't happen to remember earlier results.

This is a revision of an Answer

I'd like to numerically compute integrals of the forms:

int(exp(-t)/((-3+2*exp(-(1/10)*t))*(-3+2*exp(-(9/10)*t))), t = 0 .. infinity); evalf(%)

where there are much more terms in the denominator, e.g.,

Int(e^(-t)/(product(-3+2*exp(-.1*(1-.1)^i*t), i = 0 .. A)), t = 0 .. infinity); evalf(%)

for some positive integer A. Even when I have three terms it takes so long... So how do I compute such integrals numerically with maple; what's the efficient...

(This is a reply to PatrickT, who asked about a certain ArrayTools:-Copy example.)

Suppose that you need to do a computation which requires, in part, a certain subportion of a Vector V. Let's call the routine which does the work as `f`. Let's suppose that `f` is a system command and not something that we write ourselves. One natural way to call `f` and supply the subvector is to call `f` like so:

   f( V[a..b] );

Here the inner range a..b denotes...

How do I evaluate moments of Q4 in a reasonable time with a reasonable accuracy, i.e. to a few decimal points:
   
with(Statistics): RV := 'RandomVariable(Geometric(1/3))+1'; R := `$`('RV', 5);

Q4 := 1/(alpha*R[1]+alpha^2*R[2]+alpha^3*R[3]+(1-alpha-alpha^2-alpha^3)*R[4]);

Moment(Q4, 1);

E.g., can I tell maple that replacing infinite series underlying this calculation with sufficiently long partial sums is okay with me? Or maybe there...

Hi I've remarked something strange in a program I wrote. It's a mix of analytical and numerical computation, so that I use a value of Digits higher than the default, to improve and stabilize the results against floating numbers errors. What happens is that for Digits from 10 to 15 included, the time the PC needs to run the program increases reasonably, so that for Digits=15 it needs 10 minutes or so. But if I set Digits=16 the time rises exponentially, and becomes more than two days!!
1 2 3 Page 1 of 3