Product Tips & Techniques

Tips and Tricks on how to get the most about Maple and MapleSim

See here.

Consider the task of sorting a list of complex floating-point numbers by magnitude. First Attempt The usual method to do this in Maple is with the sort procedure. By passing a boolean-valued function that computes then compares the magnitudes of two complex numbers, we can sort the list. The following procedure shows how this is accomplished.
sort1 := proc(L)
    return sort(L, proc(z1,z2) abs(z1) <= abs(z2) end proc);
end proc:
A disadvantage of this approach is that the absolute-value procedure is called twice every time a pair of numbers is compared. For a long list, the time spent in the absolute value routine dominates the computation time.
It would be good if I could tell looking at the list of messages in the Inbox or other folder, to which of them I replied. Also, it would be a nice feature if I could see in the Sent folder whether my message to somebody had been read, or not.
Does anyone have advice for content in a maple tutorial for calculus students and any math students for that matter? I am doing a tutorial to help students avoid the fear of Maple and try to help them to avoid common mistakes. Any suggestions would be great!
The map function is a particularly useful Maple function. I use it frequently and thought I understood how it worked. However, I recently discovered an interesting feature of mapping over vectors, matrices, Vectors, and Matrices. That is, the order of application of the mapped function depends on the data type and may be indeterminate (for table based structures).
Hi, Colleagues, I need to use two colors for positive and negative values of a plotted function and I am trying to use a piecewise switch. My atempt was something like plot(sin(x), x=0..13, color=(x->piecewise(sin(x)
This blog entry evolved from my reply to Moira Chas's post. I want to thank her for initiating such an interesting topic. Usually Mandelbrot set is drawn in Maple using plot3d command. That also can be done using densityplot. In the example below I use mandelbrot from John Oprea's worksheet,
mandelbrot := proc(x, y) 
local c, z, m; 
c := evalf(x+y*I); z := c; 
for m to 50 while abs(z) < 2 do z := z^2+c od; 
m end;

plots[densityplot](mandelbrot,-2 .. 0.7, -1.35 .. 1.35,
s_tyle=patchnogrid,colorstyle=HUE,numpoints=62500,axes=none);
Hi, Colleagues, I need strongly reduce Maple generated graphs for a journal publication. How could I make the axes and tickmarks thicker? Thanks, Evgeni
SOS! I need to find how to plot y=f(x) if x and y are columns of 2D matrix. Thanks, Evgeni
The help page ?UndocumentedNames lists a few of the undocumented procedures in Maple. For example: inner - computes the inner product (dot product) of two lists MorrBrilCull - subroutine used by ifactor evalgf1 - used by `mod/Gcd` etc.
Similarly to Binary Arithmetic, Octal Arithmetic can be done using following module,
Silvexil announced in his blog a Binary Arithmetic package. Here is my version of it.
Here is the simplest program for obtaining the set of prime numbers less than or equal to n,
f:=n->select(isprime,{$1..n}):
It is not Eratosthenes Sieve though. The program ES below implements the Eratosthenes Sieve,

In this post, daniel asks about how to compute the list of primes less or equal to some integer n. This is often called the Sieve of Eratosthenes problem, but the term "Sieve of Eratosthenes" actually refers to a specific algorithm for solving this problem, not to the problem itself.

This problem interested me, so I tried a few different ways of performing the task in Maple. As with my previous blog post, this exercise should be seen as an attempt to explore some of the issues faced when trying to make Maple code run faster, not as an attempt to find the fastest all-time Maple implementation of an algorithm to solve this problem.

Here are seven different implementations:

Implementation 1

sp1 := n->select(isprime,{$1..n}):

This first was suggested by alec in response to daniel's post. It's by far the simplest code, and somewhat surprisingly turns out to be (almost) the fastest of the seven.

I had thought it wouldn't be, because the subexpression {$1..n}builds the expression sequence of all integers from 1 to n, and the code spends time checking the primality of all kinds of numbers which are obviously composite.

> time( sp1( 2^16 ) );
                             0.774

Implementation 2

sp2:=n->{seq(`if`(isprime(i),i,NULL),i=1..n)}:

This second attempt avoids the principal problem of sp1, which is the construction of that expression sequence mentioned previously. However, the evaluation of all those `if` conditionals is also expensive, so this approach is essentially the same as sp1in runtime.

> time( sp2( 2^16 ) );
                             0.770

Implementations 3 and 4

sp3 := proc(N)
    local S, n;
    S := {};
    n := 2;
    while n < N do
        S := S union {n};
        n := nextprime(n);
    end do;
    S
end proc:
sp4 := proc(N)
    local S, n;
    S := {};
    n := prevprime(N);
    while n>2 do
        S := S union {n};
        n := prevprime(n);
    end do;
    S union {2}
end proc:

These two approaches are essentially the same: sp3 uses nextprime and ascends, while sp4 uses prevprimeand descends.

The advantage to this approach is in avoiding all the unnecessary primality tests done by sp1 and sp2. Unfortunately, this advantage is offset by the fact that they rely on augmenting the set Sincrementally, which causes them to be slower than either of the previous two.

> time( sp3( 2^16 ) );
                             1.494

> time( sp4( 2^16 ) );
                             1.430

Implementation 5

There's really only one basic Maple command dealing with primes that we haven't yet used: ithprime. However, to use this effectively we need to know how far to go: i.e. what is the maximal m such that pm ≤ n?

Well, that's equivalent to asking how many prime numbers there are between 1 and n, or equivalently, what's the value of π(n), where π(n) is the prime counting function. This is implemented in Maple as numtheory[pi], so we'll use that in our code.

sp5 := N->{seq(ithprime(i), i=1..numtheory:-pi(N))}:

Happily, this turns out to be much faster than anything we've seen yet:

> time( sp5( 2^16 ) );
                             0.037

Implementation 6

Just for fun, I wondered whether speed might be improved by approximating π(n) with the Prime Number Theorem using Li, the logarithmic integral. (There are lots of other, better, approximations that we could use, but I'm not going to bother with those here.)

The approximation isn't perfect: as the help page for Li says, π(1000)=168, but Li(1000) ≅ 178. So we'll use ithprimeto find the first Li(N) primes (rounded down), remove all primes > N, and add in any primes ≤ N that might be missing.

sp6 := proc(N)
    local M, S, n;
    # Approximate number of primes with Prime Number Theorem
    M := trunc(evalf(Li(N)));
    S := {seq(ithprime(i),i=1..M)};
    # Do some cleanup: since the theorem provides only an
    # approximation, we might have gone too far or not far enough.
    # Remove primes that are too big; add in any that are missing
    S := select( type, S, integer[2..N] );
    n := ithprime(M);
    while n < N do
        S := S union {n};
        n := nextprime(n);
    end do;
    S
end proc:

This is faster than the first four attempts, but is not faster than sp5. It's unlikely to be, since numtheory[pi] is fairly fast. However, its speed could be improved with a closer approximation to π(n).

> time( sp6( 2^16 ) );
                             0.290

Implementation 7

This last attempt, to see what we get, is to cast away all of Maple's built-in tools, and actually implement a real Sieve of Eratosthenes. We dynamically build up a set of primes, then check each successive number for divisibility by each member of this set, making sure to use short-circuit evaluation so we don't waste time doing divisions once we know something's composite.

sp7 := proc(N)
    local S, n;
    S := {};
    for n from 2 to N do
        # if a prime p in S divides n, it's not prime
        if not ormap( p->irem(n, p)=0, S ) then
            S := S union {n};
        end if;
    end do;
    S
end proc:

This approach is, as we might expect, a lot slower than anything we've tried thus far, because it redoes a lot of things that Maple already does quite quickly. For comparison, I've done it for inputs of 2^12 and 2^16so you can see the blow-up.

> time( sp7( 2^12 ) );
                             0.527
> time( sp7( 2^16 ) );
                             77.665

So, the conclusion is that sp5 is the fastest of the bunch. I'm not sure how far its runtime generalizes; it may be as fast as it is largely because it uses a precomputed list of primes. However, even for larger inputs I suspect it is probably still faster than any of the other approaches above, simply because it avoids the creation of large intermediate data structures or needless checks that most of the others perform.

Yesterday I posted few screenshots of Maple 10. Here I am adding few different cmaple looks in Windows.    
First 59 60 61 62 63 Page 61 of 63