9 9 CHAPTER 4 Operations and Constructions It is beneficial to separate the two concepts of _o_p_e_r_a_t_i_n_g _o_n _o_b_j_e_c_t_s and _c_o_n_s_t_r_u_c_t_i_n_g _o_b_j_e_c_t_s, and yet mix them together in use. When we operate on objects, we produce a result which is apparently different from what we started with. ____________________________________________________ (c1) 2+3; (d1) 7 5 ____________________________________________________ When we construct objects, we make a less obvious "change". ____________________________________________________ (c2) x+3; (d2) 7 x+3 ____________________________________________________ Apparently the "+" in the first example disappeared in the process of simplification into an internal representa- tion. The "+" in the second one got "absorbed" _i_n_t_o the internal representation, and was printed out. Although it is usually not apparent to the user, some constructors are always absorbed. 9 9Operations and Constructions 4-2 Operations and Constructions 4-1 ____________________________________________________ (c1) x/y; (d1) 79 y7x_ 9 ________________________________________________________ However, as indicated in the previous chapter, x/y is represented internally by a construction resembling more x*y8-19. Because people prefer it, the display program re- formats the internal form to resemble the illustrated display. Sometimes we want to prevent constructors from being absorbed. In this example, we use the _d_i_f_f or _d_e_r_i_v_a_t_i_v_e operator. Sometimes we get what we want by means of a "quote" (apostrophe or "'") which provides a level of "pro- tection" against operation. ____________________________________________________ (c1) diff(f(x),x) + 2*x; /* This diff is a construction since f is unknown */ (d1) 79 dx7d__9f(x) + 2x (c2) diff(x^2,x) + 2*x; /* This diff is an operator. */ (d2) 7 4x (c3) 'diff(x^2,x) + 2*x; /*This diff is a constructor. Note the "'". */ (d3) 79 dx7d__9(x829) + 2x ____________________________________________________ To make constructors into operators when possible, the _e_v command, which is described in more detail later, can be used: 9 9 Printed: May 31, 1986 Operations and Constructions 4-2 ____________________________________________________ (c4) ev(d3,diff); (d4) 7 4x ____________________________________________________ Many other operators serve dual purposes. They can be "quoted" in this way, or as is sometimes referred to in this manual, made into "nouns". They can subsequently be evaluated by ev and "verbified".* _1._1. _A_s_s_i_g_n_m_e_n_t Assignment to variables is fundamental. We treat it in two ways. First informally: the ":" operation is "infix", and has left- and right- hand sides. The left side is not, in gen- eral, evaluated, but used as a "name". EXAMPLE: x:2*3+sin(0); assigns to the name x, the value 6. That is, the "right hand side" of the assignment statement is evaluated and simplified. Even if x had had a value, say y, before that, y would not be changed. A variable can be assigned a new value at any time. The value of a variable can be a number, a matrix, a list, or any MACSYMA expression. If a variable has never been assigned a value (i.e. is used as an indeterminate) then it just represents itself. Some simple examples of assignment follow. (The comments in parentheses are not typed to or by MACSYMA). MACSYMA automatically assigns labels ci to the user's input lines and di to the output lines. You can use these labels as though they were the usual sort of assigned variable. box; c c c l l l. line-label expression comment _ (c1) a:16$ (integer) (c2) lambda: -3/37$ (rational number) (c3) x:d1; (x is assigned the value of d1) (d3) 16 (c4) rho:sigma; (since sigma has no value at this time rho is assigned the sym- bol sigma) (d4) sigma (c5) sigma: .005$ (floating point) (c6) rho; (rho still has its old value since (d6) sigma it hasn't been reas- now, more formally, Operations and Constructions 4-3 : ( l_name,'r_any) This is usually written l_name : r_any. RETURNS: r_any. SIDE EFFECT: l_name now has the value r_any. NOTE: if l_name is subscripted, e.g. a[i,j], then i and j are evaluated. A different operator, "::" described below totally evaluates the left-hand side. Since the value assigned may be any expression, and assignments themselves have values, it is possible to make multi- ple assignments: Thus a:b:c:x+1 assigns x+1 to a, b, and c. The MACSYMA variable _v_a_l_u_e_s[DEFAULT: []] gives a list of all your atomic variables which have been bound (i.e. have been assigned values). ::( l_name,r_any) (e.g. x :: y) NOTE: the _v_a_l_u_e of the quantity on the left, which must evaluate to an atomic variable or subscripted variable is used. Thus continuing with the above examples: signed a new one) box; l l l. (c7) rho::lambda$ (Note that the :: causes the value of (c8) sigma; lambda, i.e. -3/37, to be assigned to (d8) -3/37 the value of rho, There are many variables which have already been assigned values. These are referred to as MACSYMA variables or options. They provide a means for communicating between the user and the system in the execution of commands, which serves to influence, in a fashion more "global" in nature than the parameters of the command, the system behavior. It is a good idea to be aware of these and other built-in names when you choose variable names yourself, or when you program new function. The index to this manual distinguishes these MACSYMA variables and options with a colon (":") between the vari- able and its default value. 9 9 Printed: May 31, 1986 Operations and Constructions 4-4 _1._2. _F_u_n_c_t_i_o_n_s A function of a fixed number of arguments can be defined in MACSYMA by using the := operator. The left side of a function definition consists of the name of the func- tion followed by the list of formal parameters enclosed in parentheses. The right side consists of the function body. When a function is called, the formal parameters will be bound to the actual arguments, any free variables in the function body will take on the values which they have at the time of the call, and the function body will be evaluated. It is permissible to define functions which are recursive to an arbitrary depth. It is possible to pass function names as formal parameters, and it is possible to have name con- flicts resulting in subtle problems. The MACSYMA variable _f_u_n_c_t_i_o_n_s[DEFAULT: []] is a list of all user defined non-subscripted functions. The MACSYMA functions _d_i_s_p_f_u_n or _g_r_i_n_d may be used to display the definition of a function. These are described in a subsequent section. The following example was especially constructed to illustrate obscure points concerning the use of variables which are not parameters. It is included for historical rea- sons. i.e. sigma.) 9 9 Printed: May 31, 1986 Operations and Constructions 4-5 ____________________________________________________ (c1) f(x):=x^2+y$ (c2) f(2); (d2) 7 y + 4 (c3) y:7$ (c4) f(2); (d4) 7 11 /* If we now define another function, using y as a "dummy" variable, the effect of MACSYMA's dynamic binding scheme can be seen. Each time y is evaluat- ed, it will use the most recent binding. */ (c5) g(y,z):=f(z)+3*y; (d5) 7 g(y,z):=f(z) + 3y (c6) g(2*y+z,-.5); (d6) 7 3(z + 14) + z + 14.25 (c7) functions; (d7) 7 [f(x), g(y, z)] ____________________________________________________ To continue the obscure example (something we even more firmly advise against), we give some more explanation of how this function g is being evaluated: The value of y is of particular importance. Note the following sequence of events: (1) The arguments to g are evaluated giving z+14 and -.5. (y has the value 7) (2) G is then invoked and has its formal parameters bound. Y to z+14 (the first argument) and z to -.5 (the second argument). The evaluation of g then causes f to be invoked on the argument -.5 9 9 Printed: May 31, 1986 Operations and Constructions 4-6 (3) F has its formal parameter x bound to -.5 and returns the result of the evaluation x829+y with the current bindings which gives z+14.25. (4) The evaluation of g continues with 3y which yields 3(z+14). This is added to the result from (3) and returned. 9 9 Printed: May 31, 1986