## 28100 Reputation

17 years, 244 days

## three ways to map it...

Here are three straightforward ways to map that operation across your ptlist,

(u->TransMatrix.u)~(ptlist);

map(u->TransMatrix.u, ptlist);

map(`.`, TransMatrix, ptlist);

## like this?...

Are you trying to accomplish something like this, using the command Physics:-diff to differentiate wrt the function call u(t)?

note: what you showed in plaintext is not valid 1D Maple Notation code, so I've guessed at what you might have intended. Adjust as needed.

```restart;
H:=(y,u,t)->y(t)^2+u(t)^2+1/(2)(x(t))^(4) +(`&PartialD;/&PartialD; x`)(V(t))*u(t):

Physics:-diff(H(y,u,t), u(t));

2*u(t)+(`&PartialD;/&PartialD; x`)(V(t))

H:=(y,u,t)->y(t)^2+u(t)^2+1/(2)(x(t))^(4) + Diff(V(t),x)*u(t):

Physics:-diff(H(y,u,t), u(t));

2*u(t)+Diff(V(t),x)
```

## examples...

You haven't shown us your actual expression. But you might be able to temporarily substitute for x-x0, then collect, then resubstitute.

Btw, your attempt using abs(x-x0) works as a specialized case of collecting wrt just abs.

 > restart;
 > expr := c*abs(x-x0) + d*abs(x-x0) + a*(x-x0) + b*x -b*x0 + abs(u) + e*abs(u); > collect(expr, abs(x-x0)); > collect(expr, abs); > collect(exp, x-x0);

Error, (in collect) cannot collect x-x0

 > rev := v=x-x0; temp := algsubs(x-x0=v, expr);  > eval( collect(temp, v), rev ); > # now also target abs(x-x0) eval( collect(temp, [v,abs(v)]), rev ); > # now also target all abs calls eval( collect(temp, [v,abs]), rev ); ## tetrahedron...

The results from implicitplot3d often look clunky, and sometimes edges inaccurately appear rounded, unless the sampling grid option is set so high that the GUI becomes sluggish when they are manually rotated, etc. It is an inefficient mechanism. It is a poor choice for handling this example.

I prefer to use plot3d when possible. Or, in this case, the tetrahedron plotting command can be used and produce an even more lightweight result.

Both of those can produce a much better rendition here than does implicitplot3d

 > restart;
 > with(plots): with(plottools):
 > display(tetrahedron([[0,0,0],[0,1,1],[1,1,1],[0,0,1]]), labels=[x,y,z]); > G := plot3d(y-1, x=0..1, y=x..1, filled, style=surface): R := transform((x,y,z)->[x,y,z+1])(G): display(R, labels=[x,y,z], glossiness=0); Unfortunately shadebetween cannot handle a variable range end-point. Hence my alternative was to fill a plot3d of y-1 and then raise by 1.

## two ways, with seq as requested...

Using seq is not the most efficient and convenient way to do this.

In later versions you could do it in 1D Input as,

seq(seq( if a[i,j]<0.2 then a[i,j]:=0 end if,i=1..20),j=1..20):
a;

Otherwise you could do it as,

seq(seq(`if`(b[i,j]<0.2,assign('b[i,j]',0),NULL),i=1..20),j=1..20);
a;

There are other variants.

## two ways...

Here are two straightforward ways, using either diff or the D operator.

(You might also augment the original pde system with a new equation for the desired derivative, but I'll leave that.)

 > restart
 > CA0 := 0:CAi := 0.0336:L := 3:`Γ_1` := 0.05:mu := 0.000894:rho := 998:Dab := 0.196e-8: Re_1 := 4*`Γ_1`*rho/mu:g := 9.8:delta := evalf(3*mu*`Γ_1`/(rho^2*g))^(1/3):
 > Eq1 := mu*diff(uy(z), z, z) = -rho*g; > Bcs := uy(delta) = 0., D(uy)(0) = 0.; > sol:=dsolve({Bcs, Eq1}):UY:=rhs(sol); > uy_bar := rho*g*delta^2/(3*mu):
 > Eq2 := UY*diff(CA(y, z), y) = Dab*diff(CA(y, z), z, z); > IBC := {CA(0, z) = 0, CA(y, 0) = 0.0336, D(CA)(y, delta) = 0}; > sol2:= pdsolve(Eq2, IBC, numeric) > sol2:-value(output=listprocedure); > U:= subs(sol2:-value(output=listprocedure), CA(y, z)); > U(1e-5,1e-6) > D(U)(0.3, 1e-4); >  > foo := diff( U(y,z), z ); > eval(foo, [y=0.3, z=1e-4]); >  ## opinion...

It's worded like that because only some tasks can be better done much more efficiently with the functional forms. It's not always the case.

Do-loops are not inherently inefficient. In fact they can sometimes be outright useful in writing very efficient code, eg. if one is trying to write procedures that can be run under evalhf or Compile.

I don't know your definition of "Generally speaking". Sometimes it seems more common for people to write inefficient code using loops -- though of course doing so is also possible with functional programming equivalents.

There are some common tasks that are often done poorly with loops. One class of such involves unnecessarily higher order computational complexity (often memory-space complexity, but sometimes time), eg. O(n^2) verus O(n), etc.

Would it help if you were shown examples of some common efficiency anti-patterns?

Here's one example. The procedure p3 below is inefficient in memory use and (partly due to ensuing memory management) time cost, and scales up very badly wrt size, but it's a code form that is often seen. I've even seen its poor technique in textbooks.

 > restart;
 > n := 10^5:

 > p1 := proc(N) local i;   [seq(i^2, i=1..N)]; end proc: p2 := proc(N) local i,T;   T := table():   for i from 1 to N do     T[i] := i^2;   end do:   convert(T,list); end proc: p3 := proc(N) local i,R;   R := []:   for i from 1 to N do     R := [op(R), i^2];   end do:   R; end proc:

And now,

 > CodeTools:-Usage( p1(n) ):

memory used=5.27MiB, alloc change=0 bytes, cpu time=20.00ms, real time=21.00ms, gc time=0ns

 > CodeTools:-Usage( p2(n) ):

memory used=33.79MiB, alloc change=46.78MiB, cpu time=302.00ms, real time=305.00ms, gc time=81.00ms

 > CodeTools:-Usage( p3(n) ):

memory used=37.26GiB, alloc change=24.71MiB, cpu time=31.15s, real time=26.31s, gc time=11.58s

While alternative p1 is by far the most efficient of the three above, its functional technique is not always very convenient without some structural code changes. Sometimes it's convenient to perform a given operation in a loop (say, because the operation is in stages and a handy procedure isn't yet written to do each one individually). The alternative p2 generates the same result too, and uses the same kind of loop structure as p3, but avoids the higher space complexity and ensuing memory management time cost. And while p2 doesn't scale up as well as p1, but it does do much better than p3.

The main problem with p3 is not the loop per se; the problem is the repeated augmentation of lists (an immutable data structure).

So why do people so often code such tasks in the manner of p3? Perhaps it's because it's approach is simple to understand and remember. Loops are more straightforward to debug. And using a table introduces another flavor of data structure -- ie. more details to remember.

Do loops tend -- more "generally" than do functional equivalents -- to be used for code with an innappropriate computational complexity? Maybe.

But it's also possible that a reason for seeing more inefficient loop code is that loop code is more common than functional equivalents; I doubt anyone's studied the proportions, for Maple.

And it's possible to write suboptimal code with a functional programming approach. Unnecessary collectible garbage might get be produced by performing an unnecessary number of separate mappings, eg. with L indexable,
f~( (L -~ 3.0)/~11.0 )/~ 5.0 + sin~( f~( (L -~ 3.0)/~11.0 )/~ 5.0 )
or what have you.

## collecting, then substituting...

You could call collect on an expression containing placeholders for names nu,nu,u,u,u, and then subsitute for those placeholders.

collect_complete_ac.mw

## one way...

Here is one way, using implicitplot since you had that.

(It might be possible to produce a nicer surface directly with plot3d, say, instead of generating a collection of polygons as implicitplot does.)

 > > > > > >  I'm supposing that you wanted the patch projected only onto the upper part of the sphere, where z>0. You could change that, to use -sqrt(...) instead. Or you could generate both. Eg, Projection_acc.mw

## with an event...

Following dharr's comment about the piecewise, a dsolve-numeric event could also be used.

min_problem_ac.mw

Alternate kinds of events are possible.

## coeffs, CoefficientList...

Yes, the terms are taken in the order in which they are currently stored.

 > restart;
 > m := [\$1..3]: > c := coeffs(P1, R, 't'): [c], [t]; > PolynomialTools:-CoefficientList(P1, R); > sort(P1,order=plex(R),ascending); > c:= coeffs(P1, R, 't'): [c], [t]; > PolynomialTools:-CoefficientList(P1, R); Note that if the order of terms didn't matter then there'd be less point to the optional side-effect assignment to the 3rd argument 't'.

If you'd prefer you could use the CoefficientList command, for which the order is fixed and only the coefficients returned (because the order of the terms is implied). There is a cross-reference to that command from the Help page for coeffs.

ps. These aspects have not changed since Maple 2015.

## one way...

Here is one way to handle your example,

```S := "124e34e243e45e56e76f34e45e23ea12e98e34e43":

parse~(StringTools:-Split(S,cat(seq("A".."z"))));

[124, 34, 243, 45, 56, 76, 34, 45, 23, 12, 98, 34, 43]```

## one way...

In Maple 2023.0 the edges themselves are represented as CURVES plotting substructures. That's different from, say Maple 2015.

That means that the edge lines will always be rendered on top of the arrow heads (which are POLYGONS substructures). You probably don't want to see such an effect; it's not pretty, if the colored arrow head appears below a differently colored edge line.

One possible approach is to split the edge lines at the arrow head, using some undefined value(s).

You could modify this to change all/several arrows at once.

 > restart;
 > with(GraphTheory):
 > G := Digraph({[1, 2], [2, 3], [3,4], [4,5], [5,1]}):
 > GP := DrawGraph(G, stylesheet=[vertexborder=false,vertexpadding=5,edgecolor = "black",vertexcolor="MistyRose",edgethickness=2],size=[250,250]); > changearrowhead:=proc(P,ind::posint:=1)   local C,i,L,otherR,R,T,temp,this;   R:=seq(`if`(op(i,P)::And(specfunc(POLYGONS),                            satisfies(p->op([1,1,1],p)=3)),i,NULL),          i=1..nops(P));   this := R[ind];   otherR := seq(`if`(op(i,P)::And(specfunc(CURVES)),i,NULL),          i=1..nops(P));   T:=op(plots:-display(op(this,P),overrideoption,_rest));   L := op(1,T);   temp:=subsop(this=op(0,T)(op(T),T[2..]),P);   C := op([otherR[ind],1],temp);   C := [C,         [(L[1,1]+L[3,1])/2,(L[1,2]+L[3,2])/2],         [L[2,1],undefined], [L[2,1],L[2,2]], C];   subsop([otherR[ind],1]=C,temp); end proc:
 > new1:=changearrowhead(GP, 2, color=red, style=polygon): new1;  >

This whole approach depends on the fact that the order of the POLYGONS and CURVES substructures match. That might not be set in stone for the future...

I just wrote this off the cuff. It's not mint-clean (option names unprotected). I didn't test thoroughly.

## mapping a mapped operation...

You have fasteners as a list of Vectors.

You are trying to map some mapped operation (that itself maps across the Vector entries) across the list. That can be done using map or elementwise operations with a nested syntax (including in 2D Input mode).

Your examples could also be handled by targeted substitution (evalindets).

[Sorry, my worksheet fails to display inline here right now.]

## Interpolation...

You could construct an interpolating function from the x-y-z data, which could compute a z-value for any x,y pair within range.

Then you can make any usual kind of plot, and add coloring, options, etc.

For example, using the very small collection of data originally provided, see below.

I forced 20 contour values, but you could just as easily specifiy the contour values in steps of 100 ( ie, seq(...,100) instead of seq(...,numelems=20) ).

 > restart;
 > data := [[177.80000,0.,967.3266667],[169.80000,0.,963.5900000],          [100.00000,0.,188.8933333],[176.92000,17.703000,967.3266667],          [174.27000,35.230000,967.3200000],[169.90000,52.407000,967.3566667],          [163.84000,69.064000,967.3366667],[156.15000,85.034000,967.3100000],          [146.91000,100.16000,967.3366667],[136.20000,114.29000,967.3666667]]:
 > F := Interpolation:-Interpolate(data[..,1..2],data[..,3]):
 > P2:=plots:-contourplot(F,min(data[..,1])..max(data[..,1]),min(data[..,2])..max(data[..,2]),                        contours=[seq(min(data[..,3])..max(data[..,3])*(1-1e-9),numelems=20)],                        thickness=2, colorscheme=["Blue","Red"]):
 > plots:-display(   plottools:-transform((x,y)->[x,y,0])(P2),   plots:-surfdata(Matrix(data),source=irregular,                   style=point,shading=zhue,symbolsize=15,symbol=solidcircle) ); > P2; 