(*how to make mathematica recognize x as x^1??
I want f[x]to output {x,1}
In[44]:= f[a_^n_] := {a, n};
In[43]:= f[x]
Out[43]= f[x]
In[45]:= f[x^2]
Out[45]= {x, 2}
(*how to make mathematica recognize x as x^1??
I want f[x]to output {x,1}
In[44]:= f[a_^n_] := {a, n};
In[43]:= f[x]
Out[43]= f[x]
In[45]:= f[x^2]
Out[45]= {x, 2}
f[a_^n_] := {a, n}
f[a_] := {a, 1}
Now f[x^2] returns {x,2} and f[x] returns {x,1}
Functions can be “overloaded” – you create different patterns to match for the same function.
Regards
Appreciate your reply! I was wondering if there is any other way apart from overloading the function.
Cause in my application, this small issue doubles my number of test cases that I have to hard code.
Thanks
f[a_^(n_: 1)] := {a, n};
This will also work. Makes n optional with a default of 1.
Perfect!