At 11:54 AM 8/8/01 +0100, Hugh Williams wrote:
>I have always believed that if a (user defined) Derive function is called
>with fewer arguments than in the function definition that the calling
>arguments are mapped onto the leftmost definitional arguments. E.g.If
>foo(a,b,c,d) := ...... is called as foo(x,y) then a:=x and b:=y leaving c
>and d as local variables for the function to use.
>
>This seems not to be the complete story as in a paper I am reviewing seems
>to get the assignment a:=x and (!!) c:=y.
>
>I have tried shuffling the argument order of some functions I have written
>and they all fall over as I would expect.
>
>It may be relevant that the function is a program (foo(a,b,c,d) := PROG(...)
>) and that b is assigned to before any mention of c in the program text.
>
>Can anyone explain what is going on please as I cannot find anything in
>Derive help files. I am interested in the general situation as much as the
>particular.
>
>The following lines are the function definition (Gauss2) and suitable data.
>The call Gauss2(a,c) works correctly!
>
> gauss2(a, i, c, q) := PROG(IF(DET(a) = 0, RETURN "no unique solution"), i
>:= IDENTITY_MATRIX(DIM(a)), p := DIM(a) + 1, q := 2·DIM(a), a := APPEND(a`,
>i`)`, a := APPEND(a`, c`)`, a := ROW_REDUCE(a), RETURN ["inverse is:", a COL
>[p, ..., q], "solution is:", a COL [q + 1]])
>
>a := [3, 4, 5; 7, -2, 2; 9, 2, 3]
>
>c := [3; 4; 7]
>
>Thanks in advance for any help.
>
>Hugh Williams
>
Hugh, the thing is this: When you call Gauss(a,c), then a becomes your
matrix a and i becomes c. So far so good. This value of i is completely
irrelevant here, as it gets a new one right at the beginning of the program
by the assignment
i := IDENTITY_MATRIX(DIM(a))
When runnung through the program step by step, Derive encounters eventually
the assignment
a := APPEND(a`, c`)`
Now, what should it to do here? After all, there has not been passed any
value for the third placeholder c in the function call. What is the most
reasonable thing it could do, in fact, what it is expected to do? Right! It
should take the value of the global variable c.
So who is to blame here? Certainly not Derive. It is the programmer, who
should put the auxiliary variable i where it belongs in the function
definition, that is at the third place
gauss2(a, c, i, q) := ...
By the way, as it wasn't you who programmed it, I may be allowed to say
that this is one of the ugliest programs I have ever seen in my life anyway...
Cheers,
Johann
|