From: Barton W. <wi...@un...> - 2014-03-01 12:40:54
|
>> Substituting a lambda form for a function has been called my favorite trick. It is. >Thank you very much! I will try to understand this trick. Maybe I shouldn't call it a trick--substitution of one function for another is a fairly natural operation. Often it is convenient for the substituted function to be a lambda form (anonymous function), but it doesn't have to be. If the substituted function isn't a lambda form, it might be necessary to call ev on the result: (%i36) f(x,y) := max(x,y)$ (%i37) subst(g = f, g(2,4)); (%o37) f(2,4) (%i38) ev(%); (%o38) 4 When substituting a lambda form for a function, the lambda form is automatically evaluated. This is inconsistent--maybe soon this behavior will be changed. To substitute for +, *, ^, ^^, enclose the operator in double quotes; for example to convert from non-commutative multiplication to commutative multiplication, use (%i49) subst("." = "*",a.b.a); (%o49) a^2*b Internally, a-b is a + (-1 * b), so substitution for "-" might not work as expected: (%i54) subst("-" = f,a-b); (%o54) a-b (%i55) subst("+" = f,a-b); (%o55) f(a,-b) One thing that consistently trips me up is substitution for noun form. Substitution for integrate doesn't seem to work (%i67) subst('integrate = lambda([e,x,a,b], quad_qag(e,x,a,b,6)), integrate(x^x,x,0,1)); (%o67) integrate(x^x,x,0,1) But substitution for the noun form of integrate does (%i68) subst(nounify('integrate) = lambda([e,x,a,b], quad_qag(e,x,a,b,6)), integrate(x^x,x,0,1)); (%o68) [0.78343051069213,2.1976896391359236*10^-9,549,0] --Barton |