My objective is to define an opeartor op
such that for every rational function $f(c)$ of a fixed symbol c, $op(f(c) x)=f(c) op(x)$. What is a good way to implement this?
$\begingroup$
$\endgroup$
1
-
$\begingroup$ Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. $\endgroup$– Community BotCommented Jul 25 at 15:27
Add a comment
|
1 Answer
$\begingroup$
$\endgroup$
2
An alternative solution using UpValues, and defining the operator as linear:
ClearAll[op, x]
SetAttributes[op, HoldAll]
(* Define the linearity property using UpValues *)
op /: op[a_ + b_] := op[a] +op[b]
(* Define the commuting property using UpValues *)
op /: op[f_*expx_] /; FreeQ[expx, c] := f*op[expx]
Test:
op[(c^2 + 3)/(c + 1)*x^2]
(*output*)
((3+c^2)*op[x^2])/(1+c)
and for linearity:
op[(c^2 + 3)/(c + 1)*x^2+ c x]
(* output *)
c*op[x]+((3+c^2)*op[x^2])/(1+c)
-
$\begingroup$ Thank you for your answer. It appears that x is declared as a special variable instead of c. Is it possible to go the other way around? $\endgroup$ Commented Jul 28 at 18:51
-
$\begingroup$ Sure!, I edited the code
op /: op[f_*expx_] /; FreeQ[expx, c] := f*op[expx]
, so that the special variable declared is c. $\endgroup$– FercaCommented Jul 28 at 21:06