I would like to define a new operator, say:
$\text{stard}[f[x]] = \lim_{h\rightarrow 0}\left(\frac{f[x+h]}{f[x]}\right)^{\frac{1}{h}}$
I think it must be done with an upvalue, but how do I do it?
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityI would like to define a new operator, say:
$\text{stard}[f[x]] = \lim_{h\rightarrow 0}\left(\frac{f[x+h]}{f[x]}\right)^{\frac{1}{h}}$
I think it must be done with an upvalue, but how do I do it?
The usual way is to specify both the expression and the relevant variables.
Clear[stard]
stard[expr_, x_] := Module[{h, result},
result = Limit[((expr /. x -> (x + h))/expr)^(1/h), h -> 0];
result /; Head[result] =!= Limit]
stard[x^2, x]
(* Exp[2/x] *)
This is what builtin functions do too, e.g. Integrate[expr, x]
or FourierTransform[expr, t, ω]
.
Update: Here's a version which can do multiple steps in one evaluation. The most complex part of this is the error checking. The order of definitions is crucial.
Clear[stard]
stard[expr_, {x_, 0}] := expr
stard[expr_, {x_, 1}] := stard[expr, x]
stard[expr_, {x_, n_Integer?Positive}] :=
Module[{part},
part = stard[expr, {x, n - 1}];
stard[part, x] /; Head[part] =!= stard
]
stard[expr_, Except[_List, x_]] :=
Module[{h, result},
result = Limit[((expr /. x -> (x + h))/expr)^(1/h), h -> 0];
result /; Head[result] =!= Limit
]
Example:
stard[Sin[x], {x, 3}]
(* E^(2 Cot[x] Csc[x]^2) *)
Derivative[n][f][x]
is the full form of D[f[x],{x,n}]
, so, in general, you can use any of them.
$\endgroup$
Derivative[1][f][x]
. The latter isn't the FullForm
of the former. What I meant was that D
operates on an expression, e.g. D[x^2,x]
. There's no need to define a function first. Derivative
operates on a function, and doesn't require specifying a variable.
$\endgroup$
This is a product derivative. Using the solution from here,
ProductD[f_, x_] := ProductD[f, {x, 1}];
ProductD[f_, {x_, k_Integer?NonNegative}] := Exp[D[Log[f], {x, k}]]
ProductD[Sin[x], {x, 3}]
(* E^(2 Cot[x] Csc[x]^2) *)