MaplePrimes Posts

MaplePrimes Posts are for sharing your experiences, techniques and opinions about Maple, MapleSim and related products, as well as general interests in math and computing.

Latest Post
  • Latest Posts Feed
  • SOS! I need to find how to plot y=f(x) if x and y are columns of 2D matrix. Thanks, Evgeni
    Is it possible to solve integration with derivatives and unknown functions inside the integral... I am trying to solve the integral > int((diff(a(x),x,x))*(diff(u[1](x),x,x,x,x))*u[2](x)+((u[1](x))*(u[2](x)))*b(x),x=0..L); where u[1](x), u[2](x), a(x), and b(x) are unknown functions of x, and it keeps giving me back the integral... Is it possible...
    When entering 2D Math I occasionally like to make a character non-italic and Bold. The help files explain how to do this several different ways. The changes do appear on the screen before and after the worksheet is saved. When the worksheet is closed and opened again, the format changes are gone. It would appear that typesetting rules are taking over so what the help pages say under the topic '2D Math' is not what happens in practice. Bug? Feature? Workaround?
    FYI, Maplesoft just announced the Japanese language edition of Maple 10. Maple has had a Japanese version since Maple V Release 5 (circ. 1998 I believe).
    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.
    I just tried to download a copy of a worksheet from a MaplePrimes posting. The file has a .mw extension and does contain a Maple worksheet. However, when I clicked on "download the worksheet" (or words to that effect) I saw the raw worksheet file displayed in my browser - I would prefer to have the worksheet open in Maple (or allow me to save it to my local disk). The HTML code that provided the link to the worksheet was created automatically by the "convert worksheet" utility available within MaplePrimes (http://beta.mapleprimes.com/mwconvert/). My conjecture is that the MaplePrimes server is not sending out .mw files with a MIME type that allows my browser to recognize it as a Maple worksheet. I believe the correct MIME type is application/maple.
    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.
    It seems that the posting timestamp is an hour off: my last post appeared to have been posted an hour earlier than it was.
    Hi to all. My first post. I'm new to Mapleprimes, but have been involved with Maple for a few years. I would like to have a generalized genetic algorithm optimization tool that I could use to explore various engineering system optimization problems. I'm not aware of anything "out there" in this area. Is anyone aware of such a tool (open source or commercial)? Sherrell @ Oak Ridge National Lab
    Is there a trial version for Maple 10.0 if so from where I can download 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.    
    It would be good to have User Attachment Control Panel (as in phpBB) where I could look at my attached files, delete them if necessary, replace, and add files. Especially because the quota is not that big.
    First 292 293 294 295 296 297 298 Last Page 294 of 306