acer

32313 Reputation

29 Badges

19 years, 314 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are replies submitted by acer

@zenterix What will be the next changes in the task, after this?

I'd prefer to save time and accomodate a few of the coming requirement changes in one shot.

Tom's toy example saves and reads back in reasonably quickly.

This is simply save, and read.

Below I call save on the result of display of all the frames together (with `insequence`). But you could just as easily save/read a mere list of the sequence of frames, in case say you want eventually to display together frames saved from different Grid processes.

It's easier than exporting just the numeric data (which will involve lots more work if it's supposed to also store receive additional information about color, style, etc, and even more if it has to support reassembling CURVES, GRID (and POINTS, and MESH?) structures, arrows, and so on...)

restart;

with(plots):

odesys:=[diff(x(t),t) = x(t) - x(t)*y(t),
         diff(y(t),t) = x(t)*y(t) - y(t)]:
tv:= Array([seq(j, j=0..10,0.1)]):

ics:=[x(0)=k1, y(0)=k2]:

sol:=[seq([dsolve([odesys[], eval(ics, [k1=j, k2=1])[]],
                  numeric, output=tv)[2,1][..,2..3],
           dsolve([odesys[], eval(ics, [k1=j, k2=2])[]],
                  numeric, output=tv)[2,1][..,2..3]][],
          j=1.5..2.5,0.1)]:

plt1:=plottools:-circle([1,1], 1, thickness=4):
Anim:=display([seq(display([plt1, listplot(sol[j], color=red),
                           seq(listplot(sol[k], color=blue),k=2..j+1,2)]),
                   j=1..numelems(sol),2)],insequence=true):

save Anim, cat(kernelopts(homedir),"/mapleprimes/testanim.m");

restart;

read cat(kernelopts(homedir),"/mapleprimes/testanim.m");

Anim;

Download toyAnim_dotm.mw

This is simply save, and read.

It seems to me that the main question is whether this gets undesirably slow, as the size and number of the animations increases. If it doesn't, then extracting/exporting the numeric data (and all that potential work cobbling each frame's elements back together) seems unnecessarily complicated and onerous.

@AHSAN You can suppress the label on a particular axis by using the empty string formed by a pair of double-quotes.

You can color between two (or more) colors of your choice, through the colorspace of your choice. The key "xgradient" makes it vary with the value in the axis[1] direction, "ygradient" makes it vary with the value in the axis[2] direction (unrelated to your variable name y), and "zgradient" makes it vary with the value in the axis[3] direction.

For example, coloring in hue by the values of Q,

plot3d(eval(u,[We=0.1,n=0.5,lambda=0.0095,x=0,k=0.1]),
       y = 0 .. 1, Q = 0. .. 0.7,
       labels = [y, "", "u"], orientation = [0, 90, 90],
       colorscheme = ["ygradient",[red,blue],colorspace="HSV"]);

@zenterix This is quite different from what you originally described in your Question.

Perhaps there are still more key details that matter.

Do you need maximal efficiency because, say, you're going to be reading the results back in later? How much delay can you bear, for export/import?

What happens if you assign the computed plot structures (whole), to either individual .m files or to key names within .mla files? Is the save&read too expensive and slow?

@Carl Love It should be pointed out that in order to use procedure new_ex (rather than its symbolic return value of your step 4) one must make a procedure call.

That is, if that step 4 is not made then such a procedure call is made for each and every subsequent use of new_ex.

The added cost of a user-defined procedure call in Maple is significant, if being compared to (say) purely numeric arithmetic operations.

That added cost is a bit improved if the introduced procedure will be run under evalhf, or compiled. But it's still relatively significant.

On the other hand, that step 4 must be made if the goal is further symbolic manipulation of an ensuing symbolic expression.

There is also a case in which new_ex can be used to form new symbolic expression (eg. by being passed new symbolic arguments). In that case the optimized subexpression generation that new_ex performs can be a significant cost savings (esp. for complicated arguments).

It gets complicated to assess "best", outside of precise context. We weren't told whether the OP's goal was code-translation for a microcontroller, mathematical typesetting, further symbolics (and of what kind), or other. But we were told that the OP wanted a short target expression, so it was reasonable to assume that construction was a key aspect.

I'm glad you brought this up, it's an interesting topic.

@ecterrab Yes, I used a combinatorially generated list of lists and did an iterated search. It's not cheap in general, although for this example an improved result could be obtained in reasonable time using only lists of small subselections of the names.

I used some old code that I had for doing this kind of thing. It has some speculative searching for collect (on such subsets), or combine/trig on subsets of unfrozen trig calls, and other tricks. Some of it is potentially very expensive. It spits out (in a running manner) the commands that produce a more compact result. It allows me to specify a metric.

@zenterix Yes, I mentioned that yesterday in responses (with documentation reference URL links) to another of your Question thread, i.e here and here.

The programmatic approach to which I alluded uses the PackageTools facilities. This can create a .maple file which can install/unpack your package's files when either used directly in the GUI or used from the Maple Cloud (and then accessible publicy or privately from within other's Maple GUIs). See the links I'd provided.

ps. A much older approach is to use the InstallerBuilder package, which can construct a special kind of .mla file that automatically opens a popup dialogue and runs its own installing/unpacking script when opened in the GUI using the march Maple command. This old and cloud-unaware.

ps. I have built dozens of Maple "packages" of various complexity, and I've used all the approaches described (including some others I haven't mentioned, but devised by myself or Maplesoft). There is no One True Approach; you can figure out what works best for you -- a custom approach, or use of stock facilities in the product. It can make a difference whether you want others to be able to use your package, and/or whether you want them to be able to rebuild it from source.

I have deleted a duplicate of this.

It looks like you may be assigning to mplPath an arithmetic expression (division) that is wrapped in single right-ticks (a.k.a. uneval quotes, a.k.a. single right-quotes, a.k.a. apostrophes).

That would make no sense at all. An unevaluated arithmetic expression is neither a name nor a string.

You Question contains a claim that you used this code:

mplPath := '/somePath/MyPackage.mpl';
mlaPath := 'somePath/MyPackage.mla';

But the first of those emits a syntax error and the second returns a multiplicative product of terms.

If that's your situation, try wrapping the directory location in quotation marks instead (a.k.a. double-quotes).

Even if you were actually using different kinds of quotes (ie, your Question here showed something different from what you actually did), it'd still be unclear how your given assignment to mlaPath would work. Is it supposed to refer to a subdirectory of the current working directory?

Details are missing here. The description of what you've tried is verbose but somewhat vague. Better would be an actual worksheet, to check actually used syntax. Knowing currentdir(), and what subdirectories exist, and what the fully qualified name of the target location you want to use, would also help. 

@zenterix

See the Help page for topic kernelopts to see a description of the includepath setting. It is wholly unrelated to libname or .mla files. It is related to how the kernel finds files when processing any $include directive. It can be set in a session with Maple's kernelopts command, or it can be passed in via the -I option when Maple is launched.

I use a convention in which both .mpl, and .mm are filename extensions for plaintext Maple code. Here, .mm is an acronym for module member. The distinction is a mnemonic convention, but can also be useful in shell-scripting in which I can programatically search for only .mpl files for reading/loading.

The lib directory directly under the Maple installation directory is in libname by default. Any lib directories that appear in the following pattern are also put in libname, if found when Maple launches:
  $HOME/maple/toolbox/*/lib
The pattern can also be restricted to a particular major version number, eg.
  $HOME/maple/toolbox/2022/*/lib
etc. Here I'm using "*" to stand for your particular choice of name. On MS-Windows the home directory is like C:/Users/acer and on Linux may be like /home/acer. You can find out using the Maple command,

    kernelopts(homedir)

Apart from these locations, the directory name lib is not (in and of itself) special to Maple.

The filename extension .maple is used for Maple workbooks, as opposed to .mw for worksheets. A workbook is a kind of container that the Maple GUI understands. It can contain multiple .mw files as well as data files, images, etc. This is documented. But, less well known, it's also a vehicle Maple can use to store (see PackageTools) and deploy/unpack a package (eg. as .mla and .help files unpacked to one of those special toolbox locations I just described).

note 1: Originally (Maple 6, onwards) the term "package" technically meant a module created with option package. But in recent years the term has also been used to refer to the mentioned deployment via Maple Cloud or .maple workbooks, via PackageTools. This kind of ambiguous meaning is unfortunate.

note 2: The exports of a module created with option package are protected, as mentioned here. I mention that because -- judging from another of your recent Questions -- that fact might be of interest to you (though perhaps not the only potential cause of difficulty for you).

@Hullzie16 I apologize if my message's tone came off as harsh. I'm feeling quite under the weather. You should never have to worry about being new to Maple programming. We all have things to learn, and we all started off at some time.

By the way, there are several other ways to construct the second Array, B.

For example, if you have already constructed Array A, then either of these can work:

Array([seq(F,t=A)]);

Ffun := unapply(F,t);
Ffun~(A);

Which unhelpful person keeps vandalizing this Question and stripping off the tags?

@mmcdara Using the preformed Sample Vectors made it about ten times faster (at L=50000, and more as L increased I suspect), if I recall.

Having the loop code operate under fast evalhf mode made that about 10 times faster again, if I recall.

I used subs on a template for the helper procedure so that evalhf would not baulk (lexical scoping not supported) at evaluation of the beta_pdf procedure. Thus I shoe-horned the piecewise result from a call to beta_pdf (with appropriate symbolic name as argument) into a "template" for the helper procedure.

@mmcdara With some edits your mcmc procedure can be made faster, in the hardware floats case.

The following is done with L = 50000 which is ten times as many as you had above. On my machine it takes 0.15 sec using Maple 2015.2.

For L = 10^6 it took my machine 1.97 sec.

Below the Sample(proposal,...) is computed up front for 5*Length values. Hopefully that is enough, since each "early" acceptance keeps more guesses for the later tries. This total supply can be adjusted.

And similarly Sample(Uniform(0,1),...) is computed up front for Length values. The looping code can then be run under evalhf.

I also increased the Histogram's bin count, for fun. And randomize() can be added to the top of the worksheet if you want a different first run for each complete re-execution.

restart:

randomize():

mcmc := proc(N, K, a, b, Length, shrink)
  uses Statistics;
  local ARtest               := Vector(Length, datatype=float);
  local B                    := RandomVariable('Beta'(a, b));
  local beta_pdf             := unapply(PDF(B, x), x);
  local beta_effective_range := Probability(B > 0.99, numeric) - Probability(B < 0.01, numeric);
  local proposal             := RandomVariable(Normal(0, beta_effective_range/shrink));
  local chain                := Matrix(Length, 2, datatype=float);
  local S1                   := Vector(8*Length, datatype=float);
  local helper := subs(__d1=beta_pdf(g),
    proc(Le,c,s1,ARtest,N,K)
    local n, count, is_out, guess, L, g;
    count := 0;
    for n from 2 to Le do
      is_out := true;
      while is_out do
        count := count + 1;
        if count>8*Le then error("too many attempts"); end if;
        guess  := c[n-1, 1] + s1[count];
        is_out := guess < 0 or guess > 1;
      end do;
      g := guess;
      L := binomial(N, K)*g^K*(1 - g)^(N - K) * __d1;
      if L / c[n-1, 2] > ARtest[n] then
        c[n, 1] := guess;
        c[n, 2] := L;
      else
        c[n, 1] := c[n-1, 1]: c[n, 2] := c[n-1, 2]:
      end if;
    end do;
    return NULL;
    end proc);
  Sample(Uniform(0,1), ARtest), Sample(proposal, S1);
  chain[1, 1] := Sample(B, 1)[1];
  chain[1, 2] := binomial(N, K)*chain[1, 1]^K*(1-chain[1, 1])^(N-K)
                 * beta_pdf(chain[1, 1]);
  evalhf( helper(Length,chain,S1,ARtest,N,K) );
  return chain[.., 1];
end proc:

L    := 50000: Burn := 10000:
MC   := CodeTools:-Usage( mcmc(4, 2, 21, 1, L, 5) ):

memory used=6.98MiB, alloc change=3.05MiB, cpu time=186.00ms, real time=153.00ms, gc time=60.07ms

plots:-display( Statistics:-Histogram(MC, bincount=min(ceil(L/2),50)),
                plot(Statistics:-PDF('Beta'(21+2, 1+4-2), z), z=(min..max)(MC[Burn..L]),
                     color=red, thickness=2));

sort(MC[Burn..L])[round~([(L-Burn)*0.025, (L-Burn)*0.975])];

Vector[column]([[.735055285342138], [.971732369105087]])

Statistics:-ScatterPlot(`<,>`($1..L), MC, symbol=point);

L    := 1000000: Burn := 10000:
MC   := CodeTools:-Usage( mcmc(4, 2, 21, 1, L, 5) ):

memory used=93.72MiB, alloc change=76.46MiB, cpu time=2.00s, real time=1.97s, gc time=75.60ms

plots:-display( Statistics:-Histogram(MC, bincount=min(ceil(L/2),200), style=polygon),
                plot(Statistics:-PDF('Beta'(21+2, 1+4-2), z), z=(min..max)(MC[Burn..L]),
                     color=red, thickness=2));

 

Download MCMC_ac2.mw

There are several possible causes, eg. as shown here.

If you upload and attach your worksheet (green up-arrow in the Mapleprimes editor) then someone might be able to tell you where the mistake is, and how to correct it.

First 90 91 92 93 94 95 96 Last Page 92 of 591