sorry for my bad English
One way to make a Derivative is through tables rather by a selection of cases, analyzing the parts of the expression
Code:
// version 0.2 Jun 6 2016 by COMPSYSTEMS COPYLEFT inv(©)
#cas
diff_table(xpr,var):=
BEGIN
LOCAL nparts, operator, part1, part2;
LOCAL xprSameVar;
//purge(var);
//print("");
//CASE 1: if the expression is a variable name or identifier
if (type(xpr)==DOM_IDENT) then
// CASE 2: if the expression is equal to the variable, example diff(x,x)=1, otherwise diff(x,y)=0
return when(xpr==var,1,0);
end;
// number of parts of the expression
nparts:=part(xpr);
//operator
operator:=part(xpr,0);
//CASE 3: diff(k,v)=0
//print(nparts); print(operator); wait;
if (nparts==0) then
return 0; // xpr=pi, i, numbers
end;
if (nparts>1) then
part1:=part(xpr,1);
part2:=part(xpr,2);
//print(part1);print(part2);wait;
else
part1:=part(xpr,1);
//print(part1); wait;
end;
xprSameVar:= (string(part1)==string(var));
//CASE 4: diff(-f(v),v)=0 // NEG(xpr)
if (operator=="-") then
return -1*diff_table(part1,var);
end;
//CASE 5: diff(k*f(v),v)=0 // NEG(xpr)
if (operator="*" and type(part1)==DOM_INT) then
return part1*diff_table(part2,var);
end;
//CASE 6: diff(|f(v)|,v) with f(v)=v
if (operator=="abs" and xprSameVar) then
return sign(var); // assuming a function from R -> R
//return var/abs(var); // Alternate Form
end;
//CASE 7: diff(|f(v)|,v)
if (operator=="abs" and !(xprSameVar)) then
return sign(var)*diff_table(part1,var);
end;
// //CASE 8: diff(√(f(v)),v)
// if operator=="√" and xprSameVar then
// return 1/(2*√(var));
// end;
// //CASE 9: diff(√(v),v)
// if operator=="√" and !(xprSameVar) then
// return diff_table(part1,var)/(2*√(var));
// end;
//CASE 8: diff(ln(v),v)
if (operator=="ln" and xprSameVar) then
return 1/var;
end;
//CASE 9: diff(ln(f(v)),v)
if (operator=="ln" and !(xprSameVar)) then
return diff_table(part1,var)/part1;
end;
//fun.trig
//CASE 10: diff(sin(v),v)
if (operator="sin" and xprSameVar) then
return cos(var);
end;
//CASE 11: diff(sin(f(v)),v)
if (operator="sin" and !(xprSameVar)) then
return cos(part1)*diff_table(p1,var);
end;
//CASE 12: diff(cos(v),v)
if (operator="cos" and xprSameVar) then
return sin(var);
end;
//CASE 13: diff(cos(f(v)),v)
if (operator="cos" and !(xprSameVar)) then
return sin(part1)*diff_table(part1,var);
end;
//CASE 14: diff(tan(v),v)
if (operator="tan" and xprSameVar) then
return sec(var)^2 ; // alternate form (1/cos(x))^2
end;
//CASE 15: diff(tan(f(v)),v)
if (operator="tan" and !(xprSameVar)) then
return sec(part1)^2*diff_table(part1,var);
end;
//CASE 16: diff(sin^-1(v),v)
if (operator=="asin" and xprSameVar) then
return 1/√(1-var^2);
end;
//CASE 17: diff(sin^-1(f(v)),v)
if (operator=="asin" and !(xprSameVar)) then
return diff_table(part1,var)/√(1-part1^2);
end;
//CASE 18: diff(cos^-1(v),v)
if (operator=="acos" and xprSameVar) then
return 1/√(1-var^2);
end;
//CASE 19: diff(cos^-1(f(v)),v)
// acos(-x) -> ((π+2*asin(x))/2)
// if (operator=="acos" and !(xprSameVar)) then
// return -1*diff_table(part1,var)/√(1-part1^2);
// end;
//CASE 20: diff(atan^-1(v),v)
if (operator=="atan" and xprSameVar) then
return 1/(1+var^2);
end;
//CASE 21: diff(atan^-1(f(v)),v)
if (operator=="atan" and !(xprSameVar)) then
return diff_table(part1,var)/(1+part1^2);
end;
//CASE : diff(f+g,v)
if operator="+" then
return diff_table(part1,var)+diff_table(part2,var);
end;
if operator="/" and nparts≠0
Return (part2*diff_table(part1,var)-part1*diff_table(part2,var))/(part2^2);
end;
// codifying
// ...
return Done;
END;
#end
Examples
CASE 1:
diff_table(x,x) -> 1
CASE 2:
diff_table(x,y) -> 0
diff_table(y,x) -> 0
CASE 3:
diff_table(i,x) -> 0
diff_table(√(-1),x) -> 0
diff_table(5,x) -> 0
diff_table(PI,x) -> 0
diff_table(-1,x) -> 0
CASE 4:
diff_table(-x,x) -> -1
CASE 5:
diff_table(-3*x,x) -> -3
diff_table(3*x,x) -> 3
diff_table(+3*x,x) -> 3
CASE 6:
diff_table(abs(x),x) -> sign(x)
CASE 7:
diff_table(abs(-x),x) -> sign(x)
CASE 8:
diff_table(ln(x),x) -> 1/x
CASE 9:
diff_table(ln(-x),x) -> 1/x
CASE 10:
diff_table(sin(x),x) -> cos(x)
CASE 11:
diff_table(sin(-x),x) -> -cos(x)
CASE 12:
diff_table(cos(x),x) -> -sin(x)
CASE 13:
diff_table(cos(x),x) -> -sin(x)
CASE 14:
diff_table(tan(x),x)-> (1/cos(x))^2 = sec(x)^2
CASE 15:
diff_table(tan(-x),x)-> -(1/cos(x))^2 = -sec(x)^2
CASE 16:
diff_table(asin(x),x) -> 1/√(1-x^2)
CASE 17:
diff_table(asin(x),x) -> -1/√(1-x^2)
CASE 18:
diff_table(acos(x),x) -> -1/√(1-x^2)
CASE 20:
diff_table(atan(x),x) -> 1/(1+x^2)
CASE 21:
diff_table(atan(-x),x) -> -1/(1+x^2)
CASE ...:
writing ...
PD: for Bernard How is internally code of differentiation in xcas?