Joe Riel

9660 Reputation

23 Badges

20 years, 22 days

MaplePrimes Activity


These are replies submitted by Joe Riel

I haven't actually tested it, but suspect that the call to floor(log[10](n)/3.) would be better handled by iquo(ilog10(n),3).  Or use the length function.  Or do away with the open-loop test and terminate when r gets small enough.

I haven't actually tested it, but suspect that the call to floor(log[10](n)/3.) would be better handled by iquo(ilog10(n),3).  Or use the length function.  Or do away with the open-loop test and terminate when r gets small enough.

I was going to mention that that could be an advantage of convert/+, however, one can safely use a global variable with add (which permits it to be inlined). That is

mysum := proc(L::list) option inline; add(x, x in L) end proc:

is, so far as I can ascertain, completely safe and works properly regardless whether x is assigned.  Are there exceptions? Note that this is not the case with seq:

myseq := proc(L::list) option inline; seq(x, x in L) end proc:

This fails with the list [g(x)].

myseq([g(x)]);
Error, too many levels of recursion

The variable x should be declared as local, which precludes inlining the procedure.

I was going to mention that that could be an advantage of convert/+, however, one can safely use a global variable with add (which permits it to be inlined). That is

mysum := proc(L::list) option inline; add(x, x in L) end proc:

is, so far as I can ascertain, completely safe and works properly regardless whether x is assigned.  Are there exceptions? Note that this is not the case with seq:

myseq := proc(L::list) option inline; seq(x, x in L) end proc:

This fails with the list [g(x)].

myseq([g(x)]);
Error, too many levels of recursion

The variable x should be declared as local, which precludes inlining the procedure.

I see that one can directly convert from a table to `+`, the call to entries is not needed:

convert(eval(T), `+`)

Note that the eval is necessary, otherwise the table itself is returned.  This is different than when convert/list is called:

convert(T, 'list');  # eval is not required

I see that one can directly convert from a table to `+`, the call to entries is not needed:

convert(eval(T), `+`)

Note that the eval is necessary, otherwise the table itself is returned.  This is different than when convert/list is called:

convert(T, 'list');  # eval is not required

Thanks.  I had previously done that, but didn't see the link.  I wonder why?  That seems a common occurrence; my search pattern misses what is clearly there. Maybe I've learned to filter too much stuff...

You can use VectorCalculus:-Tangent:

restart;
f := x^3 - x^2 - y^3 + y^2:
Tf := VectorCalculus:-Tangent(<x,y,f>,x=1,y=1);

Note that the tangent is shifted so that the tangent point occurs at the origin [in the shifted axes].  If you want to plot the plane so that it touches the surface, you will need to shift it back into place:

# shift tangent plane to tangent point
Tf2 := eval(Tf, [x=x-1,y=y-1]);
fplt := plot3d(f,x=0..2,y=0..2):
Tplt := plot3d(Tf2,x=0..2,y=0..2, color=red, transparency=0.8):
plots[display3d](fplt,Tplt);

You can use VectorCalculus:-Tangent:

restart;
f := x^3 - x^2 - y^3 + y^2:
Tf := VectorCalculus:-Tangent(<x,y,f>,x=1,y=1);

Note that the tangent is shifted so that the tangent point occurs at the origin [in the shifted axes].  If you want to plot the plane so that it touches the surface, you will need to shift it back into place:

# shift tangent plane to tangent point
Tf2 := eval(Tf, [x=x-1,y=y-1]);
fplt := plot3d(f,x=0..2,y=0..2):
Tplt := plot3d(Tf2,x=0..2,y=0..2, color=red, transparency=0.8):
plots[display3d](fplt,Tplt);

Just a note.  The entries (indices) command provide a 'nolist' option that returns the results without enclosing each entry in a list, so you can forego the map(op, ...) call:

return convert([entries(T,'nolist')], `+`);

I prefer, however,

return add(T[k], k=1..i-1)

Just a note.  The entries (indices) command provide a 'nolist' option that returns the results without enclosing each entry in a list, so you can forego the map(op, ...) call:

return convert([entries(T,'nolist')], `+`);

I prefer, however,

return add(T[k], k=1..i-1)

Yes, excellent point.  I'm embarrassed that I didn't notice this; I wasn't thinking of a symbolic list.  If the elements in the list are numeric, then this is not an issue.  Of course, the proper method is to use add, but the original poster explicitly ruled that out.  Your method is a nifty workaround, but I don't see any practical reasons to prefer it over add.

The reason that the do loop is O(n^2) with symbolic elements is that the k'th pass through the loop creates a partial summation with k distinct elements: a1 + a2 + ... + ak. This summand is stored, at least temporarily, in the Maple simplification table.  So n passes creates a total of n objects with sizes from 1 to n, hence the total memory requirement is n*(n+1)/2, which is O(n^2).

Yes, excellent point.  I'm embarrassed that I didn't notice this; I wasn't thinking of a symbolic list.  If the elements in the list are numeric, then this is not an issue.  Of course, the proper method is to use add, but the original poster explicitly ruled that out.  Your method is a nifty workaround, but I don't see any practical reasons to prefer it over add.

The reason that the do loop is O(n^2) with symbolic elements is that the k'th pass through the loop creates a partial summation with k distinct elements: a1 + a2 + ... + ak. This summand is stored, at least temporarily, in the Maple simplification table.  So n passes creates a total of n objects with sizes from 1 to n, hence the total memory requirement is n*(n+1)/2, which is O(n^2).

I'll add some comments to Robert's code:

plot(x*exp(-x),x=0..2);    # generate the plot
P:= %:                     # assign the plot structure to the variable P.
pts:= op([1,1],P):         # Pick out the listlist of points from the plot structure.  
# added line, to verify pts:
pts[..3];                  # print the first three points of pts
   [[0., 0.], [0.0217971541666666657, 0.0213271789060863227],
     [0.0435943083333333314, 0.0417346738582774560]]
Now we want to convert the list of sublists to a sequence of the second element (the y-value) of each point
and then pass this sequence to max, which returns the maximum value.  Robert combines these steps into a one-liner.
maxvalue:= max(op(map(p -> p[2], pts)));
Here is a slightly simpler alternative.  We don't need to call op to convert the list to a sequence because max (at least in Maple 12) operates on lists as well as sequences.  Also, rather than mapping a user-created procedure (p->p[2]) that returns the second item of each sublist, I'll use the builtin procedure op with map2.
 
maxvalue := max(map2(op,2,pts));

I'll add some comments to Robert's code:

plot(x*exp(-x),x=0..2);    # generate the plot
P:= %:                     # assign the plot structure to the variable P.
pts:= op([1,1],P):         # Pick out the listlist of points from the plot structure.  
# added line, to verify pts:
pts[..3];                  # print the first three points of pts
   [[0., 0.], [0.0217971541666666657, 0.0213271789060863227],
     [0.0435943083333333314, 0.0417346738582774560]]
Now we want to convert the list of sublists to a sequence of the second element (the y-value) of each point
and then pass this sequence to max, which returns the maximum value.  Robert combines these steps into a one-liner.
maxvalue:= max(op(map(p -> p[2], pts)));
Here is a slightly simpler alternative.  We don't need to call op to convert the list to a sequence because max (at least in Maple 12) operates on lists as well as sequences.  Also, rather than mapping a user-created procedure (p->p[2]) that returns the second item of each sublist, I'll use the builtin procedure op with map2.
 
maxvalue := max(map2(op,2,pts));
First 128 129 130 131 132 133 134 Last Page 130 of 195