acer

32385 Reputation

29 Badges

19 years, 336 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by acer

From the Help page (Maple 18) for the Compiler,
   "Arrays can only appear as parameters of a procedure that is to be compiled."

In other words, you can construct your Array outside of the procedure, and pass it in as an argument. You shouldn't be able to assign an Array to a local variable within the compiled procedure. (The same should go for Vector and Matrix.)

You may have already noticed that you can drag the borders of the inlined plotting window to re-size (or set that size in modern Maple). You can right-click on the plot and change the Manipulator so as to zoom in/out or pan. This is all manual work, done using the mouse-pointer -- and some may have to be re-done if you re-execute.

So you might also wonder whether it can be done programmatically, so that you don't need to repeat manual mouse-work. See below.

You can use the commands Layout:-InlinePlot and Components:-Plot from the DocumentTools package to embed a 3D plot where you programmatically specify any offset (pan) or scaling factor (zoom).

The is just rendering, in effect. The result from the plot3d call is generated just the same (and doesn't itself contain the zoom and pan information), and gets used as one of the arguments to those commands.

The embedding is done using the InsertContent command. You can encapsulate it within a borderless GUI Table, so that the end result looks very much the same as a regularly displayed plot.

There are examples on the Help pages.

The Components:-Plot command's last example involves an animation where the the axes are oblong (long and narrow), and the idea there is that -- if you don't want to rotate it -- extra white space can be avoided. (That example may not show the effect in the actual Help page, but you can run it to see what I mean.)

Unfortunately the pan and zoom levels are not Properties of a PlotComponent, and so cannot be set using SetProperty as far as I know. But the commands I mentioned earlier can get you a way to accomplish it all programmatically.

You need to enter your procedure in just one single Document Block (or Execution Group).

From your image it appears that you've accidentally split it across two blocks, instead. You may have inadvertantly type the Enter/Return key too soon.

You can move to a new line (while typing code) using Ctl-Enter (possibly Cmd-Enter on Mac OSX), instead of Enter.

You can upload and attach a worksheet to this site using the Green up-arrow in the Mapleprimes editor. You don't have to paste it in.

The solve command has a relevant (and documented) option that can help for this example.

restart;

solve({x^2-x-2=0},x) assuming x>0;

{x = 2}, {x = -1}

solve({x^2-x-2=0},x,useassumptions) assuming x>0;

{x = 2}

 

Download solve_assumptions.mw

Here is the answer to your question about how MESH behaves (when the PLOT3D structure is printed):

The Array within a MESH substructure has to have datatype=float[8].

Invert_Torus_ac.mw

Try entering it as,

    'layout'=':-interactive'

The apostrophes (single right-quotes) protect againgst evaluation (in case you'd assigned values to the names).

The key thing is the colon-minus, where :-interactive references the original global name and not that of the export of the plots package. When you load the plots package you rebind the name.

After you have executed a Document Block, select all of it, both input and output. (You can use the mouse pointer to left-click & drag, or Ctl-Alt-Shift-D, or right-click in the border if your have the Markers visible.)

Then use the menubar item,
   Edit -> Document Blocks -> Toggle Input/Output display

That allows you to hide the input of a particular Document Block (as opposed to, say, all input in the sheet).

It's a little awkward to undo the effect, though you should be able to use the "Show Command" item from the same submenu.

That works for me in Maple 2019 without having to encapsulate in a Table (which has its own mechanism for this kind of thing). 

[edit: presumably you have some other examples to cover, with some general action, or else you could just square both sides, trivially...]

How do you feel about using the isolate command here?

restart;

eq:=sqrt(y)=tanh(x):

isolate(eq,y);

                           2
                y = tanh(x) 

Or perhaps you might be able to get by with temporarily substituting or freezing the trigh terms (but be more careful if you need to preserve the explicit dependence on name x, for subsequent or intermediate differentiation, say).

thaw(solve(subsindets(eq,trigh,u->freeze(u)),y));

There is quite often a balance between terse syntax and higher performance. By this I mean the contrast of short syntax against rewriting such operations in just one or two (evalhf'able or Compile'able) procedures that are verbose but very efficient in memory and computation time.

But here are some ideas. (I used Maple 2015, on Linux.)

I found it interesting that the original elementwise action of pi0 and pi1 on float[8] Vector Q would not produce a float[8] result. So I forced that -- saving some time and memory -- by using map[evalhf] instead, for which I had to reverse the order of their parameters. (Sometimes such a result does attain, with a builtin action, say. But not this pi0 & pi1.)

I have stopped well short of rewriting everything (except the Sample calls) in one  big evalhf'able/Compile'able procedure. There can be diminishing returns in such effort.

restart:

pi0 := (p, n) -> (1-p)^n:

pi1 := (p, n) -> n*(1-p)^(n-1) * p:

f4 := proc(REP, a, b, N)

  local Q, P0, P1, P01, U, Got01, Got0, N01, N0, N1:
 uses ST=Statistics, LA=LinearAlgebra:

  Q     := ST:-Sample(Uniform(a, b), REP):

  P0    := map[evalhf](pi0, Q, N):

  P1    := map[evalhf,inplace](pi1, Q, N):

  P01   := LA:-VectorAdd(P1,P0,inplace):

  U     := ST:-Sample(Uniform(0, 1), REP):

  N01   := add(`<=`~(U-P01,0)):

  N0    := add(`<=`~(U-P0,0)):

  N1    := N01-N0:

  N0, N1;

end proc:

CodeTools:-Usage(f4(10^5, 0, 5e-4, 25000), iterations=5);

 

memory used=24.07MiB, alloc change=44.49MiB, cpu time=132.80ms, real time=133.40ms, gc time=9.87ms

HFloat(7917.0), HFloat(8013.0)

pi0 := (p, n) -> (1-p)^n:

pi1 := (p, n) -> n*(1-p)^(n-1) * p:

f4a := proc(REP, a, b, N)

  local Q, P0, P1, P01, U, Got01, Got0, N01, N0, N1:
 uses ST=Statistics, LA=LinearAlgebra:

  Q     := ST:-Sample(Uniform(a, b), REP):

  P0    := map[evalhf](pi0, Q, N):

  P1    := map[evalhf](pi1, Q, N):

  LA:-VectorAdd(P1,P0,inplace):

  ST:-Sample(Uniform(0, 1), Q):

  N01   := add(`<=`~(Q-P1,0)):

  N0    := add(`<=`~(Q-P0,0)):

  N1    := N01-N0:

  N0, N1;

end proc:

CodeTools:-Usage(f4a(10^5, 0, 5e-4, 25000), iterations=5);

 

memory used=23.68MiB, alloc change=0 bytes, cpu time=123.00ms, real time=123.20ms, gc time=8.35ms

HFloat(8110.0), HFloat(7962.0)

shazam := proc(V1::Vector(datatype=float[8]),
               V2::Vector(datatype=float[8]),
               N::integer) local c::integer,i::integer;
  c := 0;
  for i from 1 to N do
    if V1[i] <= V2[i] then c := c+1; end if;
  end do;
  return c;
end proc:
try
  cshazam := Compiler:-Compile(shazam);
catch:
error;
  cshazam := (V1,V2,NN)->evalhf(shazam(V1,V2,NN));
end try:

f5 := proc(REP, a, b, N)

  local Q, P0, P1, P01, U, Got01, Got0, N01, N0, N1:
  uses ST=Statistics, LA=LinearAlgebra:

  Q     := ST:-Sample(Uniform(a, b), REP):

  P0    := map[evalhf](pi0, Q, N):

  P1    := map[evalhf,inplace](pi1, Q, N):

  P1    := LA:-VectorAdd(P1,P0,inplace):

  U     := ST:-Sample(Uniform(0, 1), REP):
  N01   := cshazam(U, P1, REP);
  N0    := cshazam(U, P0, REP);

  N1    := N01-N0:

  N0, N1;

end proc:

 

CodeTools:-Usage(f5(10^5, 0, 5e-4, 25000), iterations=5);

 

 

memory used=14.53MiB, alloc change=0 bytes, cpu time=78.80ms, real time=79.00ms, gc time=8.34ms

8054, 8179

 

Download MakeItFaster_ac.mw

If you have to repeat the whole computation several times, then the "memory used" number can have more effect (because it alludes to how much memory management is going on). Lower is better, even if the wall-clock timing is otherwise the same for a run. I used some in-place operations, to reduce production of collectible garbage within the procedure. There are several choices in how to go about that. But I didn't do a variant where Q is reused as the third argument to Sample, etc. Refactoring the code to use the fewest number of separate Vectors is another diminishing return, and can forsake readability.

 

There are several mistakes.

The syntax mistakes mostly relate to plot, assignment, etc, rather than to the Explore command.

You forgot the colon in the assignment syntax, for P, and so didn't actually assign to anything.

The syntax for the plotting command is strangely wrong. (Is this a cut&paste of something else?!)

It's difficult to guess what you mean by the data values for plotting. Which are the independent values? What is ()...() supposed to mean?

You forgot to assign any numeric values to M_0, M_1, k_0, and k_1.

You appear to have shown us something like an invalid plaintext version of some 2D Input. That's no good. Upload your actual worksheet, instead of doing that.

I can guess what sort of thing you intended for the data. I made a short guess. The following works, and you should be able to fill in the missing data. (If not, then upload your worksheet.)

explore_plot.mw

With a call to Table (from DocumentTools:-Layout) you can pass calls to Column (same package) and specify the `weight` option.

It takes as many Column calls as there are columns (counting span). The weights are relative to each other.

Let us know if that's not your context.

You can get the comparison using Units:-Simple instead of Units:-Standard, although zero meters is still just zero.

restart

with(Units:-Simple)

a := 15*Unit('m')

15*Units:-Unit(m)

b := 13000*Unit('mm')

13000*Units:-Unit(mm)

min(a, b)

13*Units:-Unit(m)

c := 0*Unit('m')

0

min(a, c)

0

NULL

Download UnitsStandardCompare_ac.mw

[edited] I've deleted my answer using the CenterOfMass command, as I overlooked the coordinate system and made an unfortunate choice for the integration bounds (related to the square).

Tom's done it in his own Answer, now.

Let me know if this is close to what you are after (or if it needs adjustment).

restart;

F:=proc(p::polynom(anything,x), n) local d;
  d := degree(p,x);
  if n<=d then
    error "n=%1 is not greater than degree=%2 of polynomial in x",n,d;
  end if;
  Matrix(n-d,n,'scan'=':-band'[0,d],
         [seq([coeff(p,x,i)$(n-d)],i=0..d)]);
end proc:

F(x^4+3*x^3+x^2+2*x+1, 10);

_rtable[18446883698950820438]

F(x^5+11*x^2-3*x, 10);

_rtable[18446883698955243518]

F(x^5+11*x^2-3*x, 5);

Error, (in F) n=5 is not greater than degree=5 of polynomial in x

F(x^5+11*x^2-3*x, 6);

_rtable[18446883698955249894]

F(x^5+11*x^2-3*x, 8);

_rtable[18446883698955250974]

 

Download mat_polycoeff.mw

Perhaps this will help. (I'm not sure whether it is good for your purposes...)

restart

with(XMLTools)

xmltree := XMLElement("database", ["version" = "test"])

_XML_Element(_XML_ElementType("database"), [_XML_Attribute(_XML_AttrName("version"), _XML_AttrValue("test"))], [])

xmlmaterial := XMLElement("materials")

_XML_Element(_XML_ElementType("materials"), [], [])

xmltree := AddChild(xmltree, xmlmaterial, 0)

_XML_Element(_XML_ElementType("database"), [_XML_Attribute(_XML_AttrName("version"), _XML_AttrValue("test"))], [_XML_Element(_XML_ElementType("materials"), [], [])])

 

HasChild(xmltree, XMLElement("materials"))

true

xmldoc := XMLDocument(xmltree); xmldocA := subsindets(xmldoc, function, proc (s) options operator, arrow; setattribute(s, ':-inert') end proc)

_XML_Document(_XML_Element(_XML_ElementType("database"), [_XML_Attribute(_XML_AttrName("version"), _XML_AttrValue("test"))], [_XML_Element(_XML_ElementType("materials"), [], [])]))

 

readxmltree := ParseString(ToString(xmltree)); readxmltreeA := subsindets(readxmltree, function, proc (s) options operator, arrow; setattribute(s, ':-inert') end proc)

_XML_Document(_XML_Element(_XML_ElementType("database"), [_XML_Attribute(_XML_AttrName("version"), _XML_AttrValue("test"))], [_XML_Element(_XML_ElementType("materials"), [], [])]))

NULL

HasChild(xmldocA, XMLElement("materials"))

true

``

FirstChild(xmldocA)

_XML_Element(_XML_ElementType("materials"), [], [])

HasChild(readxmltreeA, XMLElement("materials"))

true

FirstChild(readxmltreeA)

_XML_Element(_XML_ElementType("materials"), [], [])

``

Download HasChild_ac.mw

First 104 105 106 107 108 109 110 Last Page 106 of 336