t> MAPLE DATA STRUCTURE SURGERY
b>
t> The next step after learning how to create Maple commands and
t> expressions is to learn a little more about the internal structure of
t> these objects. Doing so helps you understand the way Maple works,
t> as well as giving you the ability to manipulate output created by Maple
t> for inclusion in further calculations.
b>
c1>
t> Determining Data Types
b>
t> Each object in Maple has a type associated with it. These types range
t> from integer, to list, to `+` (a sum of objects). Understanding the
t> type of an object is often imperative to using the object in a
t> calculation. (Many Maple commands only take certain object types as
t> input.) As well, when investigating the elements that make up an object
t> (as we do later this chapter), it is important to know the object's
t> type. The command whattype allows us to determine an object's type.
b>
x> whattype(34/57);
x> whattype([1,2,3,4,5]);
x> whattype((x+3)*(y-4));
c1>
t> Keep in mind that while you can always query the type of an object,
t> there is no way in Maple to stipulate that a certain variable will
t> always be of a certain type (i.e., there is no strong typing in Maple).
t> For example, there is no way to say that the variable j will always be
t> of type integer.
b>
t> In many cases, especially in programming with Maple, there are times
t> when you want to do different calculations dependant upon the type
t> of a certain variable. The type command allows you to query whether a
t> variable is of a specified type.
b>
c1>
x> greetings := `hello there`;
x> type(greetings, integer);
x> type(greetings, string);
h> greetings := 'greetings';
c1>
t> Two other useful tools are the hastype command, which tells you whether
t> an object contains a subobject of the given type, and the has command,
t> which tells you if a certain subobject is contained in an object. Some
t> examples are worth a thousand words.
b>
c1>
x> hastype((x+1/2)*exp(3), fraction);
x> hastype(x^2+3*x+5, `*`);
x> has(x^2+3*x+5, 3);
x> has(x^2+3*x+5, 2*x);
t> While these examples are fairly obvious, hastype and has are invaluable
t> when dealing with very large objects.
c1>
b>
t> Questions
b>
c2>
q> Determine the type of the equation: x = y+1 
a> whattype(x = y+1);
c2>
q> Determine if the result of int(exp(-x^2),x) contains a fraction.
a> hastype(int(exp(-x^2),x), fraction);
eoq>
c1>
t> Dissecting Data Structures
b>
t> Each Maple object has an internal structuring that allows you to
t> examine and extract individual elements (operands) that make up that
t> object. Being able to do this is handy when dealing with large objects
t> or objects that contain one crucial piece of information. There are
t> several commands in Maple that manipulate selected parts of objects.
b>
t> Internally, Maple breaks down each object into logical subobjects,
t> which are then again broken down into even smaller subobjects, until
t> basic elements are reached. Pictorially, this creates a branching,
t> tree-like structure. The nops command tells you how many first-level
t> operands there are in a command, and the op command displays these
t> operands in the form of an expression sequence.
b>
c1>
x> object := 3*x^2 + 2*x - 3;
x> nops(object);
x> op(object);
c1>
t> The op command also selects individual operands out of an object, 
t> and used recursively, can delve even deeper into an object.
b>
x> object := x^3*exp(1) - 34/Pi;
x> op(1, object);
x> op(1, op(1, object));
c1>
t> The subsop command exchanges a specified operand of an expression for
t> a new operand. The following example doubles the current numerator of
t> a fraction (i.e., its first operand).
b>
x> myfrac := (a+b)/(c+d);
x> subsop(1=2*op(1,myfrac), myfrac);
h> myfrac := 'myfrac';
t> For more information, see the help file for subsop.
b>
t> Questions
b>
c2>
q> Determine of what type the second operand of x^2+exp(1)-3 is. 
a> whattype(op(2, x^2+exp(1)-3));
c2>
q> Create a single command that will automatically display the last operand
q> of the expression "object" (created earlier). (Hint: use a combination of
q> the nops and op commands.)
a> op(nops(object), object);
eoq>
h> object := 'object';
eof>
