Joe Riel

9530 Reputation

23 Badges

20 years, 29 days

MaplePrimes Activity


These are answers submitted by Joe Riel

?DynamicSystems[Linearize] returns a sequence of four expressions.  The first expression is what you want.  In the first example, you can access the state-space matrix a by doing, say, linm[1]:-a;
 

It sounds to me that an rtable (Vector or one-dimension Array) is what you want, not an expression sequence.  The reason being, an rtable is mutuable.  That is, you can change an element of it. An expression sequence (and associated constructs lists and sets) is not mutuable.  That is, to record a change to an element of an expression sequence, a new data structure has to be created.

One approach is to use ?frontend.  For example,

(**) z := y^2*diff(y(x),x)^3;         
                                    2 /d      \3
                              z := y  |-- y(x)|
                                      \dx     /

(**) frontend(diff, [z,diff(y(x),x)]);
                                   2 /d      \2
                                3 y  |-- y(x)|
                                     \dx     /

Here is a post describing some of the subtleties of using frontend.

Maybe the easiest is

y := 3/8*2^(1/3)*k^(4/3): 
eval(y, k = sqrt(k^2));
                                  1/3   2 2/3
                                3 2    (k )
                                --------------
                                      8


See Acer's recent post, which is really about that procedure and its effect on the speed of some computations.

John's solution is interesting, but I wouldn't want a worksheet to mess with my initialization file. There is another way to do what you want, however, as with most things, it is easy to do in linux, and a bit more challenging in Windows. 

If you launch the Maple gui from the command line, you can use the -c option to assign a command that the Maple engine executes when it starts up, and reexecutes following a restart.  So if you wanted to load a package few packages you could do (in linux)

$ maple -x -c 'with(plots)' -c 'with(LinearAlgebra)'

The -x option says to launch the gui, each -c option defines a command that is executed. For a more complicated sequence of commands, you could put them into a text file, say restart.mpl, and then do

$ maple -x -c 'read("restart.mpl")'

The Maple commands in restart.mpl will be read and executed at startup and following each restart.

Getting this to work in Windows is doable, I believe, though I don't use Windows so cannot verify.

The time is not spent in the Maple kernel. The length of the returned message is 25 kB.  On my machine I monitored the network speed while executing X1, it was receiving at 25 kB/sec, so was taking approximately 1 second per call, which matched precisely with how long it took.

One possibility is that google only serves an individual request so fast. To partially test this, I created a small shell script that calls the Maple script, each executing X1 10 times.  I then ran 8 of those scripts in parallel.  The total time required is the same as it takes to run one script.  That indicates that the bottleneck isn't the network (not surprising, I can download considerably faster than 25 kB/s).

The bottleneck is not in Maple but rather in the access to the external site. Wrap your call in CodeTools:-Usage:

CodeTools:-Usage(X1()):
memory used=275.27KiB, alloc change=0 bytes, cpu time=0.00s, real time=0.59s

The cpu time (0.00) is the Maple kernel time.

While it won't help the time (it's hard to improve on 0), you can reduce the memory usage by using ?StringTools[StringBuffer] to catenate the lines:

X1 := proc ()
local str, sid, b, buf;
uses Sockets;
    buf := StringTools:-StringBuffer();
    sid := Open("google.com", 80);
    Write(sid, cat("GET /finance/historical?q=INDEXSP:.INX&histperiod=daily HTTP/1.0 \n\n"));
    do
        b := Read(sid);
        if b = false then break; end if;
        buf:-append(b);
    end do;
    Close(sid);
    buf:-value();
end proc:
CodeTools:-Usage(X1()):
memory used=38.13KiB, alloc change=0 bytes, cpu time=0.00s, real time=0.56s

I'm not familar with the details of the Maple plot command, however, I doubt that it is reentrant. Every evaluation of f, which is called by the main call to plot, attempts to create a new plot.

(**) f := z/(z-1):
(**) thaw(normal(subs(z=1/freeze(1/z), f)));
                                              1
                                         - --------
                                           -1 + 1/z



There are no differentials in that list of equations.  What are you attempting to solve for?

I don't know what you mean by "work on".  My immediate thought is to try what you suggested, that is, either assign q[O] as a piecewise expression,

q[O] := piecewise(x < 1, x, x^2):

or assign an equation to a variable:

eq := q[O] = piecewise(x < 1, x, x^2):

The latter may be more useful in that it allows you to use q[O] in other expressions.

I'll suggest saving the code as as a text file and using VerbatimInput from the fancyvrb LaTeX package.  This has the advantage in that any subsequent changes to the code do not require LaTeX edits.  Use the normal \section commands (after \appendix) in the LaTeX source. For example

\appendix
\section{Main Procedure}
\label{sec:maple-main}
\VerbatimInput{Main.mpl}
\section{Plot Routines}
\label{sec:maple-plot}
\VerbatimInput{Plot.mpl}

Consider using the separate text files as the source for the Maple package.  You can use the preprocessor $include directive in command-line Maple to handle stuff that is nested.  For example, your file layout may look something like

MyProg.mpl:

MyProg := module()
export ModuleLoad, Print, DoSomething;

$include <ModuleLoad.mpl>
$include <Print.mpl>
$include <DoSomething.mpl>

end module:
# save module to archive
LibraryTools:-Save(MyProg, "MyProg.mla"):

ModuleLoad.mpl:

ModuleLoad := proc()
...
end proc:

Running the following from the command-line creates the Maple archive MyProg.mla, which Maple automatically loads if it is located in the path defined by the ?libname variable.

$ maple MyProg.mpl

Did you look at your loop structure?  The inner loop is being executed more than 7777^5=28e18 times.

A few concepts have gone awry. The conditional statement, at the top of your post, checks the values of i and k before you have assigned them in the for-loop.  The loop itself keeps reassigning beta the same procedure.  The parameter i and k for beta have nothing to do with the loop variables (declared parameters to a procedure always have a local scope).  A simple fix is to eliminate the conditional (if) and the loops, and just assign alpha and beta the procedures you have.  In the call to Matrix, pass the values of i and k to alpha and beta.  That is

alpha := (i,k) -> ...
beta := (i) -> ...
Matrix(M, M, (i, k) →`if`(i = k, beta(i), alpha(i,k)));
First 62 63 64 65 66 67 68 Last Page 64 of 114