Items tagged with efficiency efficiency Tagged Items Feed

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

Handling big dataset

September 02 2011 by Carlo Carminati 30 Maple

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!!

The Maple 13.01 update is now available.  It includes:

  • 3D visualization: Increased speed and memory efficiency
  • Language packs: Improved French translations and expanded support for Greek
  • JRE Update: Updated JRE corrects many Java-related issues, and results in improvements in printing on Linux, printing international characters, redrawing components, and plotting
  • Other enhancements: Areas of improvement include the performance of plots with a large number of data points, curve colors used in drag-and-drop plotting, the display of mathematics in embedded components, and PDF export.

 As usual, single-user customers will automatically be prompted to update to Maple 13.01 though Maple's automatic "check for updates" mechanism, while network users will be notified by email and directed to the download site. An update CD will be available soon for customers who cannot get the update electronically. More information can be found on the Maple 13.01 download page.

Geetings!



I try to fit curve through set of points that I read  from a graph. Then use equation of this curve to define a function function. Only bspline approximation gave acceptable result. But how can I use it in defining a function?

 

XvalsCf := [0, 5, 10, 25, 50, 100, 140, 200, 250, 300, 400]

The following type of difference in behaviour, due to deterministic ordering of sets as introduced in Maple 12, may affect implementations of some algorithms.

    |\^/|     Maple 11 (X86 64 LINUX)
._|\|   |/|_. Copyright (c) Maplesoft, a division of Waterloo Maple Inc. 2007
 \  MAPLE  /  All rights reserved. Maple is a trademark of
 <____ ____>  Waterloo Maple Inc.
      |       Type ? for help.

> seq(x, x in {a,b,c,d,e,f,g}) assuming d>0;
   ...

This is a follow-up to an earlier post about CovarianceMatrix.

There are several ways in which Statistics:-CorrelationMatrix can be improved.

CorrelationMatrix shares some inefficiencies with CovarianceMatrix, by computing correlations between the n columns, pairwise. But in doing so it also computes...

Some years ago, before the advent of the Statistics package, a colleague asked for a fast way to generate thousands of normally distributed random numbers in Maple. The suggestion that worked quickest and most easily (using existing, simple Maple Library routines) was to generate random deviates using the usual formula associated with the distribution. But the key was to replace the scalar values (representing the uniformly distributed input) with a whole Matrix of input values....

John Fredsted suggested using the following procedure (slightly modified) to determine whether an expression was deeply algebraic.

isDeeplyAlgebraic := proc(x)
	if not type(x,'algebraic') then false
	elif type(x,'atomic') then true
	else andmap(procname,x)
	end if
end proc:
Rather than using a procedure as a predicate, this seems more usefully expressed as a type.  A nice way to  do that is with Maple's TypeTools[AddType] procedure.  For example
TypeTools:-AddType('DeeplyAlgebraic1'
                   , proc(x)
                         if not x :: 'algebraic' then false;
                         elif x :: 'atomic' then true;
                         else andmap(type,x,'DeeplyAlgebraic1');
                         end if;
                     end proc);
1 2 Page 1 of 2