acer

32303 Reputation

29 Badges

19 years, 309 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by acer

@panke In order to extend the library path, you might begin by reading the Help page on `libname`, or the relevent section of the Programming Manual.

The main structural difference between a Worksheet and a Document is that the latter has at least one "Document Block".

A so-called Document can have Document Blocks (structurally a container of Execution Groups, which affects their appearance and behaviour). But a Document can also have traditional/plain Execution Groups on their own, each with the red prompt and red 1D plaintext code or black 2D Input.

If you use the menubar's Edit -> Document Blocks -> Create Document Block you can inject that into a Worksheet, and in such a way turn it into a Document -- which happens to have both Document Block(s) as well as Execution Groups proper.

IIRC there is no GUI easy one-shot facility for turning plain Execution Groups (red prompt) into Document Blocks.

If you use that Edit->... concoction from a plain Execution Group then you can subsequently cut&paste its input in as the newly created Document Block's input. (But I don't think that your can cut&paste both input&output together of such an Execution Group without its simply pasting back again in as a plain Execution Group.)

note: Hopefully you are not confusing Worksheet/Document with 1D/2D input mode. I've guessed that you are not.

note: Since a Document can have both Document Blocks as well as plain (solo) Execution groups, the conversion of Worksheet->Document is not strictly unique. Of course, you may want all plain execution Groups turned into Document Blocks. Such a conversion mechanism could be encoded in a reusable Maple procedure -- but not by me in the next few weeks.

note: I'd agree that the terminology is muddled. I find it better to always (and only) capitalize these words when using them in a Maple technical sense.

Note that the following happens if that target result of yours gets evaluated (without faking it via inert operators or similar):

(10/(3*t + 1250))^(1/3);

                             (1/3)
           (1/3) /    1     \     
         10      |----------|     
                 \3 t + 1250/     

Here are a few alternative approaches. (I could have used assumptions, but since you don't appear to be very concerned about whether the "simplification" is always correct I used the symbolic option.)

Download de_ac.mw

There are several t-values at which abs(u2) tends to infinity with x=0. For example, EllipticK(1/2)/2.

You can fsolve for the six t-values in t=-5..5, for x=0.

The attachment shows how Optimization:-Maximize can be used get some approximate values out, hence to illustrate/hint at that.

It also shows fsolve (and solve, for one exact root) acting on the denominator.

plot_ac.mw

Why would you have thought that? Please explain your thinking.

Take x=Pi/4, and take its sin value.  Now take x=2*Pi+Pi/4, and take its sin value. You should get sqrt(2)/2 or approximately .707 for both.

Now consider how to compute arcsin(sin(2*Pi+Pi/4)).

Do you think that the result ought to be 2*Pi+Pi/4? The arcsin command is just going to recieve sqrt(2)/2 from being passed sin(2*Pi+Pi/4).

Here's the kicker: how do you expect arcsin to know whether the sqrt(2)/2 it receives came from sin(2*Pi+Pi/4) or sin(Pi/4)?

You might review some of the basic concepts of trig functions and their inverses -- paying attention to the restricted ranges of the inverse trig functions.

simplify(arcsin(sin(x))) assuming -Pi/2<=x, x<=Pi/2;

         x

sin(arcsin(x));

         x

There are several ways to correct your use of NLPSolve.

Download 4.6_ac.mw

[edit] Your code numerically computes a minimum of an abs call, at a discrete number of N points. You could also plot the smooth curve of the continuous zero-crossing. (Again, there are several ways; I show three ways in the following attachment, one of them using implicitplot.)  4.6_acc.mw
Here is the overlay of that with your 10-point plot:

Even if I "optimize" the size in stored memory of the plotted 2D polygons, the 3D animation can still be quite a burden on GUI resources.

Download IdeaVolume_ac.mw

By "optimizing" the 2D polygons I mean ensuring that each non-empty-area POLYGONS substructure contains only data for the 4 corner points (and not more, ugh), and that the edges of the polygons arise from its style and not from unnecessary line segments. All the subsindets stuff in the attached worksheet is there to try and keep each frame smaller in memory. And the resolution of the extrusion can (reasonably) be kept smaller for smaller rotation, by having it be a function of the angle.

Hard on the GUI. Perform manual rotation at your own risk; I suggest bumping down the partition and frames numbers, then rotating to taste, then reverting and relying on plot-persistence to retain the orientation.  Programmatically specified orientation in animations is a mess.

@zenterix It should not be "obvious" (your word) to you that the two might produce the same plot, since the inputs are not similar at all.

In the example, plot(i, i=1..5), the first argument is a continuous function of i.

In your second example, plot(l[i], i=1..5), the first argument is a list reference which only produces values at a discrete set of integer values.

There are various ways to infom the plot command to sample the second example's first argument at only a fixed number of positive integer values of the independent variable i. But your second example makes use of no such option. The failure is not the fault of the plot command.

You could also re-use your existing code, with a caveat:
It's not clear what you want to happen at the transition. I'm not sure whether you want either, none, or both of the animations to show at t=0. You could change the total number of frames to the odd number 141, say, to get either at t=0 (depending on a strict vs nonstrict inequality). You could also modify the `if` (nesting it, say) to display both at t=0, though I'd guess you might not want that.

(I'm supposing that you would not want to merge two separate animations which both had a t=0 frame.)

restart

DBG> quit

r1 := .35; m := 626; g := -9.807; u0 := -20; v0 := -20; x0 := 0; z0 := 15
omega0 := -24
J := (2/5)*m*r1^2

"vx(t):=t*2+15:"

"vz(t):=t*2+15:"

x1 := int(vx(t), t)

z1 := int(vz(t), t)

eqx1 := m*(diff(u(t), t)) = 0.; eqz1 := m*(diff(v(t), t)) = m*g

eqx0 := diff(x(t), t) = u(t); eqz0 := diff(z(t), t) = v(t)

eqr1 := J*(diff(omega(t), t)) = 5000*sin(t)

with(plots)

ini := x(0) = x0, u(0) = u0, z(0) = z0, v(0) = v0, omega(0) = omega0

sol1 := dsolve({eqr1, eqx0, eqx1, eqz0, eqz1, ini}, {omega(t), u(t), v(t), x(t), z(t)}, type = numeric, output = listprocedure)

p1 := odeplot(sol1, [[x(t), z(t)]], 0 .. 1, color = black, linestyle = dot)

t from 0 to 1

 

F1 := proc (tau) local pos, rot, j, cols; cols := [red, blue, red, blue]; if type(tau, numeric) then pos := [(eval(x(t), sol1))(tau), (eval(z(t), sol1))(tau)]; rot := (eval(omega(t), sol1))(tau); return plottools:-rotate(plots:-display([seq(plottools:-sector(pos, 1, (1/2)*(j-1)*Pi .. (1/2)*j*Pi, color = cols[j]), j = 1 .. 4)]), rot, pos) else return 'thisproc(tau)' end if end proc

x1 := animate(display, [F1(t), scaling = constrained], t = -1 .. 1, frames = 70)

t from -1 to 0

 

F2 := proc (tau) local pos, rot, j, cols; cols := [green, blue, green, blue]; if type(tau, numeric) then pos := [(eval(x(t)+1, sol1))(tau), (eval(z(t)+1, sol1))(tau)]; rot := (eval(omega(t), sol1))(tau); return plottools:-rotate(plots:-display([seq(plottools:-sector(pos, 1, (1/2)*(j-1)*Pi .. (1/2)*j*Pi, color = cols[j]), j = 1 .. 4)]), rot, pos) else return 'thisproc(tau)' end if end proc

x2 := animate(display, [F2(t), scaling = constrained], t = -1 .. 1, frames = 70)

animate(display, [`if`(t < 0, F2(t), F1(t)), scaling = constrained], t = -1 .. 1, frames = 140)

NULL

Download how_transition_ac.mw

The reason that it works in the procedure is that inside the procedure the (local) name x has been assigned using the set X, ie. lhs~(X). Once you get back to the top-level, outside your procedure, the global name x lacks that value.

Your subsequent top-level replacement could also be accomplished as (simpler, IMO),

    subsindets([dd], suffixed(x), ee -> ee(t))

I think that using globals is awkward, when not necessary. I prefer to use multiple return values, and have the substituted expressions be an additional return value. This would be my preferred approach.  elementwise_equal_ac.mw

Try this, for purely numeric integration:

evalf(Int(abs(1/(x - 2) + 0.5333 + 0.3333*x + 0.1333*x^2), x = -1 .. 1));

                         0.06041415825

By simplifying the integrand (which here sees that x>-1, x<1) before doing the symbolic integration the issue can be avoided.

value(simplify(Int(abs(1/(x - 2) + 0.5333 + 0.3333*x + 0.1333*x^2), x = -1 .. 1)));

                     0.06041419887

That symbolic integration of the simplified inert definite integral happens to produce a correct result and avoid the underlying symbolic problem, but I suggest you not rely on that technique, especially if you are willing to accept a solution with floats. Instead, I'd suggest you try to rely on the first approach I gave of performing purely numeric quadrature.

The following manipulation obtains the same erroneous negative result, and illustrates how it can go awry by, say, failing to split piecewise at every sign change in the integrand. (I used Maple 2022.2).

restart;
int(abs(1/(x - 2) + 0.5333 + 0.3333*x + 0.1333*x^2), x = -1 .. 1);

-0.5685437800e-1

restart;
Int(subsindets(convert(convert(abs(1/(x - 2) + 0.5333 + 0.3333*x + 0.1333*x^2),
                               rational,exact),Heaviside,x),specfunc(Heaviside),
               u->convert(u,piecewise)), x=-1..1);
H:=simplify(%);
value(%);
evalf(%);

Int(-1/(x-2)-5333/10000-(3333/10000)*x-(1333/10000)*x^2+2*piecewise(x <= 2, 0, 2 < x, 1)/(x-2)+(5333/5000)*piecewise(x <= 2, 0, 2 < x, 1)+(3333/5000)*piecewise(x <= 2, 0, 2 < x, 1)*x+(1333/5000)*piecewise(x <= 2, 0, 2 < x, 1)*x^2, x = -1 .. 1)

-(1/10000)*(Int((1333*x^3+667*x^2-1333*x-666)/(x-2), x = -1 .. 1))

-4333/3750+ln(3)

-0.56854378e-1

Download int_neg_ex0.mw

If you plot the integrand in H you can see that it has the wrong sign, for some values.

I will submit a bug report. [edit] It may already be fixed in the version under deveopment.

One way is to use an anonymous procedure.

If the operation involves a command that passes extra arguments at the end then your particular implementation of `&/` allows a terser solution.

restart;

 

`&/`:=()->args[-1](args[1],args[2..-2]):

 

0.76 &/ (u->convert(u,fraction))

19/25

0.76 &/ (fraction,convert)

19/25

(a*x+x*b) &/ simplify &/ (u->subs(a=2,b=3,u))

5*x

(a*x+x*b) &/ simplify &/ (u->eval(u,[a=2,b=3]))

5*x

(a*x+x*b) &/ simplify &/ ([a=2,b=3],eval)

5*x

Download infix_fun.mw

1) You could change the initial creation of A to,

   A := Matrix(11,11);

and then do your loop.

2) Or you could get rid of the loop altogether and create it as,

   A := Matrix(11,11,(i,j)->i+j-2);

3) Or you could just change the entry assigning line to,

   A(i,j) := i+j;

It happens because A2 is a Matrix, a mutable data structure.

The assignment inside your procedure makes A1 a reference to A. And Matrix A2 is not copied when it is passed in as an argument to the procedure call, so A is a reference to A2.

So your updates to entries of A1 are updates to entries of A, and thus are actually updates to entries of A2. This is part of something that is sometimes called in-place semantics.

If you don't want that behaviour then you can use copy semantics instead, by replacing the line that assigns to A1 as,

   A1 := copy(A);

Changing just that aspect of your example,

restart

diagonalize := proc (A) local A1; A1 := copy(A); A1(1, 1) := 0; A1(2, 2) := 0; A1(3, 3) := 0; return A1 end proc

A2 := Matrix([[-3, -.1, -.2], [.1, 7, -.3], [.3, -.2, 10]])

diagonalize(A2)

Matrix(%id = 36893628348005758484)

A2

Matrix(%id = 36893628348005757404)

Download d_ac.mw

In-place semantics allow for reduced memory use, by not creating a copy unless necessary. This can be a useful and convenient default for mutable data structures; it can gain efficiency for large examples that do not require the copy (because, say, they might not need the original data preserved).

@zenterix How about using ExportMatrix to export purely numeric data to files? You could compare the timing by using (or not) binary (Matlab) file formats supported by that command.

If there are headers (containing nonnumeric data) for the rows or columns then you could save those separately. For large amounts of float data I suspect that it'd be more efficienct to export/re-import without headers (datatype=float[8], say), as Matrix or Array (not DataFrame or anything fancy).

You could generate the filenames in the parent process, and pass one into each Grid process.

First 56 57 58 59 60 61 62 Last Page 58 of 336