acer

32313 Reputation

29 Badges

19 years, 316 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are replies submitted by acer

@Detlef Hoyer What I stated is true. I wrote that a number applied to an argument evaluates to that number.

In 2D Input the parser interpets your last example as multiplication. In 1D plaintext input it gets interpreted as function application.

My statement (to which you have objected) was not about what is typed or entered. My statement was about the parsed meaning. I made a statement about a number applied to an argument, not about a number positioned next to an opening bracket. You have mistakenly conflated the two, in your claim that what I wrote was not true.

What I wrote is also the central part of what went awry in your original Question's example, where adjacent bracketing was parsed as function application.

@GunnerMunk Your last comment's expectation is mistaken. You have the order of assignment back-to-front.

If you assign A:=3*a only after some assignment to `a`` then reassigning to `a` will not (by itself) update A.

If you assign A:=3*a prior to any assingment to `a` the you can get A updated automatically upon each new, subsequent assignment to `a`.

You've confused assigning three times "the current value of `a`" with assigning three times "a reference to `a`".

This behaviour is not directly related to this Question's original topic of re-execution flow of embedded context-menu actions on 2D Input.

@tomleslie That is a neat use of the nested seq for the indexing on the LHS of the prArr assignment statement, instead of a (uncompiled) nested loop. I suppose the inner seq can't be under a conditional because the entries aren't yet updated.

note: the only part of procedure Eratosthenes2 that was due to me was the single line,
   seq(`if`(L[n]=true,n,NULL), n=2..N);
as a replacement for the iterated list augmentation, because that was one inefficiency. The rest of that algorithm and choice of coding was due to the OP. I did revise the algorithm and do later modifications as procedures with other names, though.

@Ziaahmed812 

Last for me, fun as it was. This skips, dealing with the odd numbers only in the first stage. I didn't bother to reduce the Array size.

On my machine this does the N=2^20 case in 0.167 seconds, compared to the original which took 35.8 seconds.

restart;
Eratosthenes4 := module() local cstage1, stage1, ModuleApply;
  ModuleApply := proc(N::posint)
    local L, n, k;
    L := Array(1 .. N, 1, datatype=integer[4]);
    cstage1(N, L);
    2,seq(`if`(L[n]=1,n,NULL), n=3..N, 2);
  end proc;
  stage1 := proc(N, L::Array(datatype=integer[4]));
    local n, k;
    for n from 3 to trunc(sqrt(N)) by 2 do 
      if L[n] = 1 then 
        for k from n^2 to N by 2*n do 
          L[k] := 0;
        end do;
      end if; 
    end do;
    NULL; 
  end proc:
  cstage1 := Compiler:-Compile(stage1);
end module:

CodeTools:-Usage(Eratosthenes4(2^20)):
memory used=21.25MiB, alloc change=8.00MiB,
cpu time=167.00ms, real time=167.00ms, gc time=0ns

nops([%]);

                 82025

If you'd be willing to accept the results in an Array instead of a sequence (or list) then the second stage might be sped up too.

For fun,

restart;
Eratosthenes3 := module() local cstage1, stage1, ModuleApply;
  ModuleApply := proc(N::posint)
    local L, n, k;
    L := Array(1 .. N, 1, datatype=integer[4]);
    cstage1(N, L);
    seq(`if`(L[n]=1,n,NULL), n=2..N);
  end proc;
  stage1 := proc(N, L::Array(datatype=integer[4]));
    local n, k;
    for n from 2 to trunc(sqrt(N)) do 
      if L[n] = 1 then 
        for k from n^2 to N by n do 
          L[k] := 0;
        end do;
      end if; 
    end do;
    NULL; 
  end proc:
  cstage1 := Compiler:-Compile(stage1);
end module:

CodeTools:-Usage(Eratosthenes3(2^20)):
memory used=36.63MiB, alloc change=12.00MiB,
cpu time=319.00ms, real time=320.00ms, gc time=0ns

nops([%]);

                 82025

I have not implemented the refinement to work with only the odd numbers (and adjusting the increment accordinly).

The above is over 100 times faster than the (corrected) original posting, on my machine, for N=2^20. And it's slightly over twice as fast as the sieve routine that Tom's supplied in his Answer, on my machine, for N=2^20. But it is limited by the hardware integer size of N=2^32=4294967296.

@Ziaahmed812 For what it's worth, the following squeezes it a bit more (33% off the last timing above, for that size.):

restart;
Eratosthenes2a := proc(N::posint) 
local L, n, k; 
description "Calculate all primes less than or equal to N"; 
L := Array(2 .. N, i->true); 
for n from 2 to trunc(sqrt(N)) do 
  if L[n] = true then 
    for k from n^2 to N by n do 
      L[k] := false;
    end do;
  end if; 
end do; 
seq(`if`(L[n]=true,n,NULL), n=2..N);
end proc:

CodeTools:-Usage(Eratosthenes2a(2^20)):
memory used=132.76MiB, alloc change=20.00MiB,
cpu time=2.25s, real time=2.26s, gc time=988.78ms

nops([%]);

          82025

But I haven't looked at other accelerations.

@mmcdara I believe you would find that delaying evaluation (with uneval quotes, as you modified it) will not work in Malle 2021.1 for this example involving units. I had discussed this in my own Answer.

@Anthrazit I prefer to have a procedure do multiple-returns (ie, return an expression sequence), along with multiple-assignment. Eg,

  (A,B,C) := F(...);

with F here returning an expression sequence of three things.

That's for assignment/side-effects. For merely accessing globals I prefer using the colon-minus syntax for make references to a global variable, eg :-A to get at global A.

@GunnerMunk If I place the mouse-pointer on the output of the context-menu result (here, the result 9*Unit(m^2) from the inserted, inlined simplify call) and then make the keystrokes Ctrl= (`Ctrl` and `=` together) then that result of simplifying a*b gets updated with the new values for a and b.

In my Maple 2020.2 the consequence of pressing only the Enter key when the focus has progressed to the a*b 2D Input line is that I get a new, additional 2D Output (with updated values) , but the previous inlined context-menu result remains and is bumped down, as some kind of undesirable output artifact. That doesn't seem desirable, to me.

I don't use Document mode because I find the execution flow awkward, especially wrt to automatic advancing of the cursor when trying to use Enter or Tab, or lack thereof. I'm not telling you it's all ok. I'm simply informing you of ways that I was able to update the prior context-menu inlined output.

Your problems seem quite unspecific to units or the simplify command itself.

Your issues appears actually to be related to the execution flow that attains when on uses context-panel actions on 2D Input in a Document.

I do not know the "best" way to re-execute an inserted context-panel action (made on 2D Input, in a Document). But if I hit the menubar's !!! (triple exclamation button) then I can get all lines to re-execute sucessfully.

You did not attach your Document, which is not so helpful. I have guessed that it is like this:
  CM_Document.mw

note: I personally find Document mode's execution flow model to be unreliable and confusing, and only use Worksheet and 1D plaintext mode for input.

@Ronan  I see. The form you just gave didn't appear in your Question, but one way that it can be obtained is by using just a small part of the earlier code. Eg,

P := 14*c^4 + 84*c^3*d + 180*c^2*d^2 + 165*c*d^3 + 55*d^4 + 5*c^3
     + 21*c^2*d + 28*c*d^2 + 12*d^3 + 2*c^2 + 5*c*d + 3*d^2 + c + d + 1:

select(t->degree(t,[c,d])=3, P);

          3       2           2       3
       5 c  + 21 c  d + 28 c d  + 12 d 

If you need to guard against the case that polynomial P might consists of just a single term, then here is a reusable procedure that allows you to easily specify the degree and the variables,

F := (p,vars,deg)->`if`(p::`+`,select(t->degree(t,vars)=deg,p),
                        `if`(degree(P,vars)=deg,p,0)):

F(P, [c,d], 3);

           3       2           2       3
        5 c  + 21 c  d + 28 c d  + 12 d 

F(P, [c,d], 2);

             2              2
          2 c  + 5 c d + 3 d 

ps. The older link you gave is to a response from Carl Love, not me.

[edit] I made a correction to the code above.

@mmcdara I qualified my Answer with, "If I understand..."

I made no claim to have understood what the OP meant by his use of the word "extract".

The code I attached is due to an interpretation in which the terms are extracted -- eg. extracted separately into a list, rather than keeping the result as a sum of select terms with coefficients replaced/removed. (I believe that is consistent with how I interpreteted the word in an older Question -- if you're making a veiled allusion.)

I don't claim that I understand what is actually wanted here. The phrasing seems unclear.

It might even be that the OP wants to obtain programmatically the result,
    c^3+c^2d+c d^2+d^3
for the given example. And only a minor modification would be needed in the reusable/extensible code I attached, to handle that. As a shorter one-off,

P := 14*c^4 + 84*c^3*d + 180*c^2*d^2 + 165*c*d^3 + 55*d^4 + 5*c^3
     + 21*c^2*d + 28*c*d^2 + 12*d^3 + 2*c^2 + 5*c*d + 3*d^2 + c + d + 1:

`+`(map(t->t/coeffs(t,[c,d]),
        select(t->degree(t,[c,d])=3,`if`(P::`+`,[op(P)],[P])),[c,d])[]);

            3    2        2    3
           c  + c  d + c d  + d 

The use of operator form call `if`(P::`+`,...) above is to guard against the more general situation where polynomial P could be a single term instead of a sum of terms. The given example in the Question could also be handled with the following (if indeed this is what's wanted, and I make no claim that it is...),

map(t->t/coeffs(t,[c,d]),
    select(t->degree(t,[c,d])=3,P),[c,d]);

            3    2        2    3
           c  + c  d + c d  + d 

@JAMET If you haven't loaded any of the plots package then that ought to be,

  plots:-display(doPlot(a), doPlot(b), doPlot(rhs~(convert(c,list))));

and not merely (as you showed it),

  display(doPlot(a), doPlot(b), doPlot(rhs~(convert(c,list))));

which would return an unevaluated call to the (unassigned) name display, which would be an over-large output for printing.

 

@aksupriatna The CodeTools:-Usage command is used in the above code merely as a way of measuring how much time and memory the computation takes.

If your old Maple 13 version lacks that command then you can simply remove it from the above code. (The calls to the CodeTools:-Usage command merely wrap around the main computations.)

@srikantha087 If you want contours then why are you trying to use surfdata?

The surfdata command with its dimension=2 option does not allow for style=contour or style=surfacecontour. I have previously submitted requests/SCRs for that.

You didn't tag your Question as Maple 2019 when you asked it. (It's not so helpful to us, to leave out such key information. I've tagged it now, myself.)

In older Maple 2019 the contourplot command doesn't support a legend for the contours. It also doesn't support colorscheme.

[edit]
The listcontplot command also doesn't support legends or colorscheme, but after using Interpolate:-SplineInterpolation on the Matrix of data then we can obtain a bivariate expression in lieu of discrete data.

In this old answer I implemented a way to use colorscheme with a 2D contour-plot. And in this old post I implemented a way to contruct a colored legend for a 2D contout-plot. I've never merged together those two pieces of added functionality.

So,... if you want a legend of colored items (akin to a finite number of contours) along with a 2D density-plot then see my Answer below.

But if you really want a 2D contour-plot (with discrete shading) that allows for your multi-color colorscheme as well as legend items then please indicate that clearly.

First 125 126 127 128 129 130 131 Last Page 127 of 591