Re: Apologies to Vincze (symbolic differentiation on the 35S) Message #3 Posted by Peter Niessen on 16 Sept 2007, 1:19 p.m., in response to message #2 by Vincze
Vincze, my friend,
there actually is hope!
So, to find out how "deep" an expression is in the stack, on has to
count how many operators and operands there are. E.g.
(x+1) * x
x 1 + x *
Actually, every two valued operator needs two operands, so the mumber
of operands should exceed the number of operators by 1:
expression x 1 + x *
#operators 2 1
#operands 3 2 1
When seeing that the number of operands exceeds the number of
operators by 1, we know that the expression is terminated.
Thus, we can now write our differentiation function. Here I chose a
C(++)like language which differentiates the expression between the
positions from and to.
std::string diff (std::string expression, int from, int to) {
switch (expression[from]) {
case "constant": // constant is easily differentiated, is 0
return "0";
break;
case "x":
return "1"; // variable diffs to 1
break;
case "+":
eox_1 = find_end_of_expression (expression, from + 1);
eox_2 = find_end_of_expression (expression, eox_1 + 1);
return diff (expression, from + 1, eox_1)
+ diff (expression, eox_1 + 1, eox_2)
+ "+";
break;
case "" // basically same as 
case "*": eox_1 = find_end_of_expression (expression, from + 1);
eox_2 = find_end_of_expression, eox_1 + 1);
ex1 = copy (expression, from + 1, eox_1);
ex2 = copy (expression, eox_1 + 1, eox_2);
return diff (ex1, from + 1, eox_1) + ex2 + "*"
+ ex1 + diff (ex2, eox_1 + 1, eox_2) + "*" + "+";
break;
// and so on for /, sin, cos, ...
}
}
As an implementation, one could use the indirect registers for
"expression", coding +, , *, / as .2, 1.2, 2.2, 3.2, sin, cos, tan, exp,
ln as .1, 1.1, 2.1, 3.1, ... so that the fractional part gives the
valuedness of the operator. The variables would be represented by A =
1, ... Z=26 (you can't have numerical literals, I'm afraid).
Our example would thus become (using A to store 1)
clear text x 1 + x *
I 1 2 3 4 5
(I) 24 1 .2 24 2.2
Now we have all the ingredients... On the 32S with only 6 levels of XEQ nesting, this would not be very much fun, but on the 35s with its 20 levels, one actually stands a chance.
Happy programming, whoever takes this challenge!
Cheers, Peter.
