Joe Riel

9530 Reputation

23 Badges

20 years, 26 days

MaplePrimes Activity


These are answers submitted by Joe Riel

Assuming the procedure is still assigned, print it, then copy the output to an input region.  The problem with doing so is that copying it invariably screws up the formatting, so its hard to read. The following routine can be used to avoid that problem:

PrintProc := proc( P :: {name,string,procedure})
local opacity,p;
    p := P;
    if p :: string then
        opacity := kernelopts('opaquemodules'=true);
        p := eval(parse(p));
        kernelopts('opaquemodules'=opacity);
    end if;
    p := debugopts('procdump'=p);
    p := StringTools:-RegSubs("\n *[0-9]+"="\n", p);
    printf("%s\n", p);
end proc:

You might be able to substitute by doing

p := proc() local i,y; y := (i=30); end proc;
pnew := subs((i=30) = (i=50), eval(p));

Addendum: the above subs won't work (as explained in the followup) because i is local to p.

Following pagan's lead, here's another manual approach. I used the naked differentials, but removed them before integrating. Years ago I wrote a Maple package that could integrate such one-forms...some day I should revive it.

deq := A=B*x*d(y)/d(x);
                              B x d(y)
                          A = --------
                                d(x)  
isolate(deq,x);
                          x       A   
                         ---- = ------
                         d(x)   B d(y)
map(`/`,%);
                         d(x)   B d(y)
                         ---- = ------
                          x       A   
eval(%,d=1);
                             1   B
                             - = -
                             x   A
int(lhs(%),x=x1..x2) = int(rhs(%),y=y1..y2) assuming positive;
                                    B (y2 - y1)
                 -ln(x1) + ln(x2) = -----------
                                         A     
combine(%,ln) assuming positive;
                        /x2\   B (y2 - y1)
                      ln|--| = -----------
                        \x1/        A     

Maple doesn't normally handle "naked" differentials (dx, dy), though some packages may permit it.  You could formulate the original equation as a standard Maple differential equation, then use ?dsolve to solve it:

deq := deq := A=B*x*diff(y(x),x):
dsolve({deq, y(x1)=y1});

                               A ln(x)        A ln(x1)
                        y(x) = ------- + y1 - --------
                                  B              B



 

You cannot  necessarily do so.  To see that, do

eq := A[p]*v+C[L]*(M[L]*v*s+B[p]*v+F[L])/A[p]+V[t]*s*(M[L]*v*s+B[p]*v+F[L])/((4*beta)*A[p]) = k[q]*xv-k[qp]*(M[L]*v*s+B[p]*v+F[L])/A[p]:

ex := (lhs-rhs)(eq):
with(LargeExpressions):
collect(ex,[v,xv],Veil[K]);
                                             1/4 K[1] v - k[q] xv + 1/4 K[2]

The above expression is equal to zero, but v/xv cannot be isolated to a constant. 

In control theory one is frequently interested in the derivative, dv/dxv, which can be computed via

isolate(%,v);
tf := diff(%,xv);
Unveil[K](rhs(tf));

I'm not sure what you want to do, but global variables are usually not the best approach. You can assign s to be local to g:

g := proc(y,a,b)
local s;
    s := x -> x+a+b;
    s(y);
end proc:
g(a,b,c);
                  a+b+c

combine(f,radical,symbolic);

Acer's method is nicer than mine. The problem with these approaches, as I hinted at, is that they don't really work with any normal flow. That is, one can use them to show a particular expansion, but doing so will require duplicating the input when assigning.  One way to avoid that is to modify the procedure so that it prints the expansion while returning the value. For example

show:=proc(expr::uneval)
local val;
val := eval(expr);
subsindets(expr,And(name,satisfies(t->type(eval(t),constant))),
``@eval) = val;
val;
end proc:

a := 3: b := 4:
y := show(a+b^2);
(3)+(4)^2 = 19
y := 19

It would be helpful if you used text rather than pictures.  Then we could cut and paste and not have to attempt to retype the entire input.

When computing a definite sum it is usually best to use ?add rather than ?sum.  Doing so here eliminates the problem. 

The cause of the bug is subtle; an internal loop is called with

for n in vars ... end do

where vars is assigned, say, T[3].  Maple assigns 3, rather than T[3], to n.  The problem is easily fixed in the code; I'll submit an SCR.

There isn't a really good way to do this.  There are various kludges that sort of work in the simple case but not so much for more complicated stuff. Here is one approach.

 macro(show=(eval(%,params)=expand(eval(%,params)))):

params := {NULL
           , a = ``(5)
           , b = ``(6)
           , c = ``(100)
          }:


a+b; show;
                                     a + b

                                (5) +  (6) = 11

a*b-c^2; show;
                                          2
                                   a b - c

                                            2
                           (5)  (6) -  (100)  = -9970

Add forward-quotes:

seq('(seq(i,i=1..10) , seq(j,j=9..2,-1))' ,k=1..4);
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 2, 3,

    4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2

There is no option to do that.  The easiest way to recompute the entire worksheet is to click the !!! button on the toolbar.

plt1 := pointplot(x,y1):
plt2 := pointplot(x,y2):
plots:-display(plt1,plt2);

A couple of the assignments use square-brackets as delimiters. Square brackets create lists, they cannot be used as ordinary parentheses.  Fix those then try again.

Parameters that change value during a simulation should not be relied upon---they may work, but may also give invalid results.  Technically they should be rejected by the parser; that they are not is a deficiency.

The ?binarytree example is intended to explain how to create a Maple package---not how to effectively represent a tree in Maple (there is also a typo in the code that suggests the author is a C-programmer). The data structure used in the example---nested unevaluated function calls---may not be particularly efficient depending on the usage. The problem is that inert functions are immutable. Changing the value of a single leaf in the tree requires reallocating memory for much of the tree.  Creating a tree using the given insert routine is quite inefficient. 

The usual way around this is to use one of Maple's mutable data structures: tables, rtables, or records.  Here I reproduce part of the functionality of the BinaryTree module using a packed record for each node, with a separate record for the tree. 

module BinaryTree()

export Delete
, Insert
, Lookup
, New
;

local CreateLeaf;

option package;

New := proc()
Record('root'=NULL);
end proc;

CreateLeaf := proc(key:=NULL,val:=NULL)
Record['packed'](':-left'=NULL, ':-right'=NULL, ':-key'=key, ':-value'=val);
end proc:

Insert := proc(tree,key,val:=NULL)
local branch,nkey,node;
node := tree;
branch := ':-root';
while node[branch] <> NULL do
node := node[branch];
nkey := node:-key;
if key < nkey then branch := ':-left';
elif key > nkey then branch := ':-right';
else
error "duplicate keys in tree";
end if;
end do;
node[branch] := CreateLeaf(key,val);
return tree;
end proc;

Lookup := proc(tree, key::numeric)
local nkey, node;
node := tree:-root;
do
if node = NULL then
error "key not found in tree";
end if;
nkey := node:-key;
if key < nkey then node := node:-left;
elif key > nkey then node := node:-right;
else
return node:-value;
end if;
end do;
end proc;

Delete := proc(tree, key::numeric)
local branch,child,node,parent;
node := tree;
branch := ':-root';
do
parent := node;
node := parent[branch];
if node = NULL then
error "key not found in tree";
end if;
nkey := node:-key;
if key < nkey then branch := ':-left';
elif key > nkey then branch := ':-right';
elif node:-left = NULL then
parent[branch] := node:-right;
break;
elif node:-right = NULL then
parent[branch] := node:-left;
break;
else
# find node in left branch with largest key
parent := node;
child := parent:-left;
while child:-right <> NULL do
parent := child;
child := parent:-right;
end do;
# copy its contents to the deleted node
node:-key := child:-key;
node:-value := child:-value;
# point parent to left child of child
parent:-right := child:-left;
break;
end if;
end do;
return tree;
end proc;

end module:
First 50 51 52 53 54 55 56 Last Page 52 of 114