Question: How to extract numerical values from an expression, elegantly?

I have an expression of the form X=a/f^b where a and b are both numeric. How do I elegantly extract the numerical values of "a" and "b" given X? See below for my kludge solution.

Problem: extract two numerical values from an expression. Surely there is a more elegant way?
Here is the form of the expression.

restart;
X:=a/f^b;

a/f^b

(1)

Find a and b when they are numbers.
X1:=subs(a=1.234,b=6.789,X);

1.234/f^6.789

(2)

indets(X1);
fterm:=op(2,%);
log(fterm);This sometimes works and yields 6.789*log(f) but I've never been able to figure out when it is going to work and when it won't work.
#log(fterm,f);This never works

{f, 1/f^6.789}

 

1/f^6.789

 

ln(1/f^6.789)

(3)

b:=-op(2,fterm);

6.789

(4)

a:=X1*f^b;

1.234

(5)

Isn't there a better way? This requires that the "op" indices won't ever change and that is asking a lot if this is embedded in a larger program.


 

Download Extract.mw

 

Please Wait...