## How to clear variables after each iteration -- for...

Dear All,

The following code calculates the Rotation_number for given set of parameters. I would like to plot the value of Rotation_number against R for R from 500 to 1000. Since there are many variables for each iteration, I would like to know if there is a way to create a for loop for the following code while automatically clear variables except for Rotation_number to speed it up?

 > restart: with(plots): with(DEtools): with(plottools):with(LinearAlgebra): t_start:=2500: t_end:=5000: b:=-3: c:=3: v1:=1: f:=-4: v2:=2.0: omega:=1: epsilon:=evalf(1/R): R:=100: k:=0.25: kH:=(c+1)/(b-1): sys:=diff(u1(t),t)=v1*u1(t)-(omega+k*u2(t)^2)*u2(t)-(u1(t)^2+u2(t)^2-b*z(t)^2)*u1(t),      diff(u2(t),t)=(omega+k*u1(t)^2)*u1(t)+v1*u2(t)-(u1(t)^2+u2(t)^2-b*z(t)^2)*u2(t),      diff(z(t),t)=z(t)*(kH*v1+c*u1(t)^2+c*u2(t)^2+z(t)^2)+epsilon*z(t)*(v2+f*z(t)^4): solA:=dsolve({sys, u1(0)=0.6, u2(0)=0.6, z(0)=0.1},              {u1(t),u2(t),z(t)},               numeric, method=rkf45, maxfun=10^7,               events=[[[u1(t)=0, u2(t)>0], halt]]): evs:=Array(): evs(1,1..4):=Array([t,u1(t),u2(t),z(t)]): interface(warnlevel=0): for k from 2 do     w:=solA(t_end):     if rhs(w[1])=t_start then break; end if; end do: R:=M[i..,3]: Z:=M[i..,4]: N_alpha:=numelems(R): r_center:=add(R[i],i=1..N_alpha)/N_alpha: z_center:=add(Z[i],i=1..N_alpha)/N_alpha: Zshift:=Z-~r_center: Rshift:=R-~z_center: alpha:=(arctan~(Zshift/~Rshift)+(Pi/2)*sign~(Rshift))/~(2*Pi)+~0.5: alpha2:=alpha[2..N_alpha]: alpha1:=alpha[1..N_alpha-1]: del_alpha:=alpha2-alpha1: m:=numelems(del_alpha): for i from 1 to m do if del_alpha[i]<0 then del_alpha[i]:=del_alpha[i]+1; else del_alpha[i]:=del_alpha[i]; fi: od: Rotation:=add(del_alpha[i],i=1..m)/m;
 (1)

Rotation_number.mw

Thank you!

Very kind wishes,

Wang Zhe

## What's the intended function of LinearSolve with i...

I've read the spec online for LinearSolve but it's not clear on the function of inplace on nonsquare matrices. For example, consider the following:

A:=<0;1>;
B:=<0:2>;
LinearSolve(A,B,inplace=true)

This last line outputs <2;0>, which is also the value stored in B after the operation, whereas [2] would be the desired result. In the case of A being a row vector an error is encountered due to lack of storage. Does what happened above generalise for any matrix with more rows than columns: storing the result in B, but adding zeroes to the bottom unused parts of B, due to B being larger than the solution?

Also, does anyone have any advice on an efficient method for solving A.x=b, in the case where b is a vector, and A is a large but tall (varying size, but often 5x as many rows as columns, e.g. 10000x2000) integer matrix, where most of the entries in any particular column are zero (more than 90%)? I've found the option method='modular' helps quite a lot, but not enough, any ideas for quick fixes?

## Dihedral symmetry: Perl vs Maple...

Greetings to all. I am writing to present a Maple computation that is somewhat of a programming challenge. A recent Post at Math.Stackexchange.Com asked to compute the number of inequivalent colorings of the vertices and edges of a regular hexagon with two colors available for the vertices and three colors for the edges (vertex colors different from edge colors) under the simultaneous action of the dihedral group D6 on the vertices and edges. This is a standard application of Burnside's Lemma and can be computed very straightforwardly using a negligible amount of computing resources. Important: this is the accepted method, it is documented at the MSE link and it works quite well. The answer is that there are 4183 unique configurations.

Now how to profit from this experience. A question naturally appears at this point: can we optimize the Maple code so as to bring it into the range of the parameters of the Perl? Here we permit all sorts of optimizations that may occur to the Maple coder other than using Burnside where the motivation is that for many of us including myself there always remain additional Maple programming techniques to learn and acquire. I hope this makes for an enjoyable exercise and I am looking forward to seeing a Maple implementation that can compete with the Perl. This is not code golf but conciseness is a plus.

hex-maple.txt

hex-pl.txt

Happy computing!

Best regards, Marko Riedel

PS: I suspect working with hash tables rather than sets may be a start.

## Position of element of list...

Hy

I have a list

A:=[[2,1],[1,2],[3,5],[7,6]]

I want to be able to tell what position the element [3,5] is in the list

I'm using

member([3,5],A,'t')

is there anything faster

maybe instead of storing as a list, I can use rtables or something?

## Is there a better way to code up this vector...

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

## Animate lines between points while animating a con...

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

## complexplot, 1 million points...

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

## animation of 3D plot rotation

by: Maple 15

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:

## 10 Tips for writing fast Mathematica code

by: Maple

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

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?

## procedures are good for you

by: Maple

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

## fastest way of computing an integral numerically?...

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

## subvectors as arguments

by:

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