acer

32622 Reputation

29 Badges

20 years, 43 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by acer

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

Download loop_functional.mw

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.

You could call collect on an expression containing placeholders for names nu[1],nu[2],u[1],u[2],u[3], and then subsitute for those placeholders.

collect_complete_ac.mw

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.)

restart; with(plots)

Equations := [-.1272463007-.1364614350*y+.5478028265*y^2-.4626165419*x+0.7714146226e-1*y*x+.6671835197*x^2, 0.9582956998e-1-.5439311444*y+.5969331379*y^2+.2711291996*x-.1314652750*y*x+.4978275516*x^2, 0.4404070985e-1-.1903242613*y+.5210473801*y^2+.5305119366*x-0.9202813426e-1*y*x+.6327990414*x^2]

P := implicitplot(max(op(Equations)), x = -.3 .. 0, y = .1 .. .5, filledregions, coloring = [red, white], scaling = constrained, gridrefine = 3)

tP := subsindets(P, And(specfunc(POLYGONS), satisfies(proc (p) options operator, arrow; has(p, COLOUR(RGB, 1.00000000, 1.00000000, 1.00000000)) end proc)), proc () options operator, arrow; NULL end proc)

S := (plottools:-transform(proc (x, y) options operator, arrow; [x, y, sqrt(1-x^2-y^2)] end proc))(tP)

display(plottools:-sphere([0, 0, 0], 1, color = gray, transparency = .8, style = surface), S)

Download Projection_ac.mw

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

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.

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

restart;

m := [$1..3]:

P1 := add(m[k]*(R)^(k-1), k=1..3);

3*R^2+2*R+1

c := coeffs(P1, R, 't'): [c], [t];

[3, 2, 1], [R^2, R, 1]

PolynomialTools:-CoefficientList(P1, R);

[1, 2, 3]

sort(P1,order=plex(R),ascending);

1+2*R+3*R^2

c:= coeffs(P1, R, 't'): [c], [t];

[1, 2, 3], [1, R, R^2]

PolynomialTools:-CoefficientList(P1, R);

[1, 2, 3]

Download coeffs_ord.mw

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.

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]

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[1]);
  temp:=subsop(this=op(0,T[1])(op(T[1]),T[2..]),P);
  C := op([otherR[ind],1],temp);
  C := [C[1],
        [(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[2]];
  subsop([otherR[ind],1]=C,temp);
end proc:

new1:=changearrowhead(GP, 2, color=red, style=polygon):
new1;

new2:=changearrowhead(new1, 4, color=gold, style=polygonoutline);

 

Download changearrowhead0.mw

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.

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.]

Download elementwise_ac.mw

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;

 

Download interp_cont_data.mw

Using Maple 18.02, since that's the version in which your attachment was last saved.

restart

kernelopts(version)

`Maple 18.02, X86 64 LINUX, Oct 20 2014, Build ID 991181`

A[m] := (x/a)^(i+1)*(1-x/a)^2;B[n] := (y/b)^(i+1)*(1-y/b)^2;w := c[i]*A[m]*B[n];

(x/a)^(i+1)*(1-x/a)^2

(y/b)^(i+1)*(1-y/b)^2

c[i]*(x/a)^(i+1)*(1-x/a)^2*(y/b)^(i+1)*(1-y/b)^2

TPE := `assuming`([(1/2)*(int(int(D__11*(diff(w, x, x))^2+2*D__12*(diff(w, x, x))*(diff(w, y, y))+4*D__66*(diff(w, x, y))^2+D__22*(diff(w, y, y))^2-2*q__0*w, x = 0 .. a), y = 0 .. b))], [i > 0]);

2*c[i]*(-512*a^4*b^4*i^8*q__0-6144*a^4*b^4*i^7*q__0-29184*a^4*b^4*i^6*q__0+36*D__11*b^4*i^8*c[i]+8*D__12*a^2*b^2*i^8*c[i]+36*D__22*a^4*i^8*c[i]+16*D__66*a^2*b^2*i^8*c[i]-69120*a^4*b^4*i^5*q__0+612*D__11*b^4*i^7*c[i]+168*D__12*a^2*b^2*i^7*c[i]+612*D__22*a^4*i^7*c[i]+336*D__66*a^2*b^2*i^7*c[i]-82368*a^4*b^4*i^4*q__0+4257*D__11*b^4*i^6*c[i]+1482*D__12*a^2*b^2*i^6*c[i]+4257*D__22*a^4*i^6*c[i]+2964*D__66*a^2*b^2*i^6*c[i]-38016*a^4*b^4*i^3*q__0+15570*D__11*b^4*i^5*c[i]+7068*D__12*a^2*b^2*i^5*c[i]+15570*D__22*a^4*i^5*c[i]+14136*D__66*a^2*b^2*i^5*c[i]+9824*a^4*b^4*i^2*q__0+31959*D__11*b^4*i^4*c[i]+19386*D__12*a^2*b^2*i^4*c[i]+31959*D__22*a^4*i^4*c[i]+38772*D__66*a^2*b^2*i^4*c[i]+13920*a^4*b^4*i*q__0+36198*D__11*b^4*i^3*c[i]+29352*D__12*a^2*b^2*i^3*c[i]+36198*D__22*a^4*i^3*c[i]+58704*D__66*a^2*b^2*i^3*c[i]+3150*a^4*b^4*q__0+20448*D__11*b^4*i^2*c[i]+19048*D__12*a^2*b^2*i^2*c[i]+20448*D__22*a^4*i^2*c[i]+38096*D__66*a^2*b^2*i^2*c[i]+4320*D__11*b^4*i*c[i]-3648*D__12*a^2*b^2*i*c[i]+4320*D__22*a^4*i*c[i]-7296*D__66*a^2*b^2*i*c[i]-8064*D__12*a^2*b^2*c[i]-16128*D__66*a^2*b^2*c[i])/(a^3*b^3*(256*i^14+7680*i^13+103936*i^12+837888*i^11+4472800*i^10+16609536*i^9+43796912*i^8+81956400*i^7+106195721*i^6+88876434*i^5+38200637*i^4-3705948*i^3-13260492*i^2-5974560*i-907200))

 

You could rearrange that result with various kinds of simplification. For example,

 

collect(numer(TPE), [a, b], factor)/factor(denom(TPE))

((-4*c[i]*q__0*(2*i+7)*(2*i-1)*(2*i+5)^2*(2*i+3)^2*(2*i+1)^2*b^4+18*c[i]^2*D__22*i*(2*i+5)*(2*i+1)*(i+3)*(i+2)*(i+1)*(i+4)^2)*a^4+4*c[i]^2*(2*i+7)*(2*i-1)*(i+4)^2*(i+3)^2*(i+2)^2*(D__12+2*D__66)*b^2*a^2+18*c[i]^2*D__11*i*(2*i+5)*(2*i+1)*(i+3)*(i+2)*(i+1)*(i+4)^2*b^4)/(a^3*b^3*(2*i+7)*(2*i-1)*(2*i+5)^2*(2*i+3)^2*(2*i+1)^2*(i+4)^2*(i+3)^2*(i+2)^2)

collect(TPE, [D__11, D__12, D__22, D__66], factor)

18*(i+1)*i*b*c[i]^2*D__11/((2*i+7)*(2*i+5)*(2*i+1)*(2*i-1)*(i+3)*(i+2)*(2*i+3)^2*a^3)+4*c[i]^2*D__12/(b*a*(2*i+5)^2*(2*i+3)^2*(2*i+1)^2)+18*(i+1)*i*a*c[i]^2*D__22/((2*i+7)*(2*i+5)*(2*i+1)*(2*i-1)*(i+3)*(i+2)*(2*i+3)^2*b^3)+8*c[i]^2*D__66/(b*a*(2*i+5)^2*(2*i+3)^2*(2*i+1)^2)-4*a*b*q__0*c[i]/((i+4)^2*(i+3)^2*(i+2)^2)

Download total_PE_ac.mw

You can insert a new execution group after the current line using either the keyboard shortcut Ctl j, or the main menubar item,
   Insert -> Execution Group -> After Cursor

The keyboard shortcut for inserting before the cursor is Ctl k.

The keyboard shortcut for splitting an Execution Group at the current cursor position is the F3 key.

You can see a listing of the shortcuts on MS-Windows by looking at the Help page for topic
   worksheet,reference,hotwin
Here's the link to that in the online Help (version Maple 2013, not your Maple 18, but these particular shortcuts are unchanged).

For "Mac" (OS X) the variants are shown on the Help page with topic,
   worksheet,reference,hotmac
For example, Cmd j and Cmd k

That can be attained directly by Matrix indexing, eg.

with(GraphTheory):

g:=Graph({{2,3},{1,2}}):

foo:=AdjacencyMatrix(g);

foo := Matrix(3, 3, {(1, 1) = 0, (1, 2) = 1, (1, 3) = 0, (2, 2) = 0, (2, 3) = 1, (3, 3) = 0}, storage = triangular[upper], order = C_order, shape = [symmetric])

ind := [3,1,2];

[3, 1, 2]

foo[ind,ind];

Matrix([[0, 0, 1], [0, 0, 1], [1, 1, 0]])

Download matrix_indexing_ex.mw

If I understand the question correctly then here are two more ways, using reasonably terse syntax.

restart;

A := <<1, 5, 9, 13> | <2, 6, 10, 14> | <3, 7, 11, 15> | <4, 8, 12, 16>>;

Matrix(4, 4, {(1, 1) = 1, (1, 2) = 2, (1, 3) = 3, (1, 4) = 4, (2, 1) = 5, (2, 2) = 6, (2, 3) = 7, (2, 4) = 8, (3, 1) = 9, (3, 2) = 10, (3, 3) = 11, (3, 4) = 12, (4, 1) = 13, (4, 2) = 14, (4, 3) = 15, (4, 4) = 16})

B := <17,18,19,20>;

Vector(4, {(1) = 17, (2) = 18, (3) = 19, (4) = 20})

`<|>`(B$4)/~A

Matrix(4, 4, {(1, 1) = 17, (1, 2) = 17/2, (1, 3) = 17/3, (1, 4) = 17/4, (2, 1) = 18/5, (2, 2) = 3, (2, 3) = 18/7, (2, 4) = 9/4, (3, 1) = 19/9, (3, 2) = 19/10, (3, 3) = 19/11, (3, 4) = 19/12, (4, 1) = 20/13, (4, 2) = 10/7, (4, 3) = 4/3, (4, 4) = 5/4})

`<|>`(seq(B/~A[..,i],i=1..4))

Matrix(4, 4, {(1, 1) = 17, (1, 2) = 17/2, (1, 3) = 17/3, (1, 4) = 17/4, (2, 1) = 18/5, (2, 2) = 3, (2, 3) = 18/7, (2, 4) = 9/4, (3, 1) = 19/9, (3, 2) = 19/10, (3, 3) = 19/11, (3, 4) = 19/12, (4, 1) = 20/13, (4, 2) = 10/7, (4, 3) = 4/3, (4, 4) = 5/4})

Download mvdivelem_ex.mw

Those don't use any functionality new to only recent Maple versions.

ps. If you haven't already assigned the number of columns of Matrix A (here, the value 4) to some name, then it can be obtained as op([1,2],A) .

This is fiddly, but...

Typesetting:-mrow(Typesetting:-mo("Y"),
                  Typesetting:-mspace(width="-1.2em"),
                  Typesetting:-mo("&#x25CB;",mathsize=18));

"Y &cir;"

Download ts_negmspace.mw

Here is an example that you might find useful.

I'm using Maple here. I don't understand what you might want that's make it different for Maple Flow.

And I'm not sure what generality you're after.

restart;

x := T -> (A-1/T)/C;

proc (T) options operator, arrow; (A-1/T)/C end proc

y := x -> sqrt(1/27*8^3/C^3+1/4*x^2);

proc (x) options operator, arrow; sqrt((512/27)/C^3+(1/4)*x^2) end proc

R := (x,y) -> exp((y-1/2*x)^(1/3)-(y+1/2*x)^(1/3));

proc (x, y) options operator, arrow; exp((y-(1/2)*x)^(1/3)-(y+(1/2)*x)^(1/3)) end proc

R(x(T),y(x(T)));

exp(((1/18)*(6144/C^3+81*(A-1/T)^2/C^2)^(1/2)-(1/2)*(A-1/T)/C)^(1/3)-((1/18)*(6144/C^3+81*(A-1/T)^2/C^2)^(1/2)+(1/2)*(A-1/T)/C)^(1/3))

# The same result
R( (x,y@x)(T) );

exp(((1/18)*(6144/C^3+81*(A-1/T)^2/C^2)^(1/2)-(1/2)*(A-1/T)/C)^(1/3)-((1/18)*(6144/C^3+81*(A-1/T)^2/C^2)^(1/2)+(1/2)*(A-1/T)/C)^(1/3))

comp := t -> R( (x,y@x)(t) ):

comp(T);

exp(((1/18)*(6144/C^3+81*(A-1/T)^2/C^2)^(1/2)-(1/2)*(A-1/T)/C)^(1/3)-((1/18)*(6144/C^3+81*(A-1/T)^2/C^2)^(1/2)+(1/2)*(A-1/T)/C)^(1/3))

Download comp_ex2.mw

I'll mention that I'm not really sure that I've understood you precise goal. Here is a variant that might be closer to what you want.

restart;

x := T -> (A-1/T)/C;

proc (T) options operator, arrow; (A-1/T)/C end proc

y := x -> sqrt(1/27*8^3/C^3+1/4*x^2);

proc (x) options operator, arrow; sqrt((512/27)/C^3+(1/4)*x^2) end proc

Rexpr := exp((y-1/2*x)^(1/3)-(y+1/2*x)^(1/3));

exp((y-(1/2)*x)^(1/3)-(y+(1/2)*x)^(1/3))

R := unapply(subs(x='x(t)',y='y(x(t))',Rexpr),t);

proc (t) options operator, arrow; exp((y(x(t))-(1/2)*x(t))^(1/3)-(y(x(t))+(1/2)*x(t))^(1/3)) end proc

R(T);

exp(((1/18)*(6144/C^3+81*(A-1/T)^2/C^2)^(1/2)-(1/2)*(A-1/T)/C)^(1/3)-((1/18)*(6144/C^3+81*(A-1/T)^2/C^2)^(1/2)+(1/2)*(A-1/T)/C)^(1/3))

Download comp_ex2b.mw

Yet another possibility is that all your shown operators are actually starting off for you as just expressions, and you're trying to figure out how best to turn it all into the final (partially composed) operator.

For instance, you originally showed R as,
   R := T -> exp((y-1/2*x)^(1/3)-(y+1/2*x)^(1/3));
where the parameter of that operator is T, which is not used within the body. As it stands that doesn't make syntactic sense. I can't tell whether you want that attempt to be fixed or to be accomodated.

Yet another possibility is that you're mostly trying to get (expressions or) operators to be legible when displayed. In that case you might be interested in commands such as PDEtools:-declare, so that the functional dependence of x & y upon T is visually suppressed. 

Please let us know what you're really after.

First 49 50 51 52 53 54 55 Last Page 51 of 339