Joe Riel

9660 Reputation

23 Badges

20 years, 4 days

MaplePrimes Activity


These are answers submitted by Joe Riel

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:

I haven't been able to duplicate your first problem, about the merged yellow regions.  I assume you mean the drawing of tree T = SpanningTree(K33). 

Your suggestion, for enumerating neighbors, isn't workable with a set because the first element is not well-defined. You could create a helper proc:

conn := (node, neighbors) -> `{}`~(node,neighbors):
conn(1, {2,3,4,5});
                                   {{1,2},{1,3},{1,4},{1,5}}

 

It isn't the case that Maple necessarily uses 0 and 1 for false and true when computing conditionals. However, for your purposes, you might be able to use ?evalhf, depending on the boolean expression. For example,

evalhf(3 < Pi);
                      1.0

Note that the result is float, not an integer. 

Possibly the cleanest way to do this is the following

A := r*sin(m):
Aval := unapply('evalf'(A),r,m); 
                                 Aval := (r,m) -> evalf(r*sin(m))

 Enclosing evalf in forward-quotes delays its evaluation so that unapply can then see it and include it in the generated procedure.

@TylerCMW An event corresponds to a condition that halts the integrator.  This is used to modify the state equations that are being integrated.  For example, when the current through an ideal-diode crosses zero, an event occurs and the equations are changed to correspond to the new configuration (say diode off rather than diode on).

It is possible that the new configuration causes another event to immediately fire, which then reconfigures the model to the previous configuration, which trigger the original event, and so on.  The error message you are seeing may correspond to such an occurrence. 

There are usually ways to avoid this.  It would be helpful to have the actual model that is causing the problem.

You have an expression.  Try

x -> your_expression

There is a better way to do what you want; try using the ?min procedure.

Try

int(f, 0..1);
                      1/4 

The way to do this is to expand this in terms of the vector components.  For example,

Y := [y1,y2]:
deq := diff(Y(t),t) = [t^2,exp(t)]:
ini := Y(0) = [-1,1]:
(lhs-rhs)(deq), (lhs-rhs)(ini);
          2   /d       \            /d       \
       [-t  + |-- y1(t)|, -exp(t) + |-- y2(t)|], [1 + y1(0), -1 + y2(0)]
              \dt      /            \dt      /

op~([%]);
           2   /d       \            /d       \
        [-t  + |-- y1(t)|, -exp(t) + |-- y2(t)|, 1 + y1(0), -1 + y2(0)]
               \dt      /            \dt      /

dsol := dsolve(%);
                                     3
                                    t
                  dsol := {y1(t) = ---- - 1, y2(t) = exp(t)}
                                    3

eval(Y(t), dsol);
                                 3
                                t
                              [---- - 1, exp(t)]
                                3

First 51 52 53 54 55 56 57 Last Page 53 of 114