Larssend
Larssend

Reputation: 147

Substitute variable with value, but don't evaluate

Suppose I have the following expressions:

(%i1) (8*x)*(log(x) / log(10));

(%i2) X^2;

Now, because I want to find out what constant value I can pick to make the statement %i1 is O(%i2) true, I evaluate them in a loop like so:

for a:1 thru 10 do print(%i1, "=", ev(%i1, x=a), %i2, "=", ev(%i2, numer, x=a));

The output is:

8 x log(x)          2
---------- = 0.0 , x  = 1 
 log(10)

8 x log(x)                        2
---------- = 4.816479930623698 , x  = 4 
 log(10)

8 x log(x)                        2
---------- = 11.45091011327189 , x  = 9 
 log(10)

8 x log(x)                        2
---------- = 19.26591972249479 , x  = 16 
 log(10)

8 x log(x)                        2
---------- = 27.95880017344075 , x  = 25 
 log(10)

8 x log(x)                        2
---------- = 37.35126001841489 , x  = 36 
 log(10)

8 x log(x)                        2
---------- = 47.32549024079837 , x  = 49 
 log(10)

8 x log(x)                        2
---------- = 57.79775916748438 , x  = 64 
 log(10)

8 x log(x)                        2
---------- = 68.70546067963139 , x  = 81 
 log(10)

8 x log(x)           2
---------- = 80.0 , x  = 100 
 log(10)

I want to make the output easier to eyeball, something like:

8 1 log(1)          2
---------- = 0.0 , 1  = 1 
 log(10)

8 2 log(2)                        2
---------- = 4.816479930623698 , 2  = 4 
 log(10)

8 3 log(3)                        2
---------- = 11.45091011327189 , 3  = 9 
 log(10)

[snip]

8 10 log(10)           2
---------- = 80.0 , 10  = 100 
 log(10)

How can I tell Maxima to substitute the value of a for x in every iteration of the loop without evaluating the expression?

I've searched the manual, but I didn't find anything seemingly relevant.

Upvotes: 0

Views: 833

Answers (2)

Robert Dodier
Robert Dodier

Reputation: 17595

A lot of operations in Maxima are carried out by a process called "simplification", which means applying identities to make a "simpler" expression. E.g. 1 + 1 simplifies to 2, sin(0) simplifies to 0, etc.

In order to get the effect you want, we must disable simplification in general, so that expressions are evaluated but not simplified. But to get the numerical values, we need to enable simplification just for those results.

Here's something to do that.

(%i16) simp : false $
(%i17) for x in [1,2,3,4,5]
         do print (ev(%i1) = ev(%i1, simp, numer), ev(%i2) = ev(%i2, simp));
       log(1)          2
(8 1) (-------) = 0.0 1  = 1
       log(10)
       log(2)                        2
(8 2) (-------) = 4.816479930623698 2  = 4
       log(10)
       log(3)                       2
(8 3) (-------) = 11.4509101132719 3  = 9
       log(10)
       log(4)                        2
(8 4) (-------) = 19.26591972249479 4  = 16
       log(10)
       log(5)                        2
(8 5) (-------) = 27.95880017344075 5  = 25
       log(10)
(%o17)                               done

Note that I wrote for x in [1, 2, 3, 4, 5] ... instead of for x:1 thru 5 .... That's because the latter uses arithmetic, which requires simplification. Try it both ways, I think you'll see the difference, and it is very enlightening, I believe.

Nota bene I've used the same values of %i1 and %i2 as you.

Upvotes: 1

slitvinov
slitvinov

Reputation: 5768

Use "empty" function:

(%i1) display2d: false $
(%i2) prefix("") $
(%i3) almost_subst(a, x, e):= subst(""(a), x, e) $
(%i4) almost_subst(10, x, 8*x*log(x)/log(10));
(%o4) (8* 10*log( 10))/log(10)

Upvotes: 1

Related Questions