[Anfang] [Hauptseite] [Suchen] [LDFM]
I'm trying to extract a function contained inside of a floor function.
For example, I have the function:
f := floor(1 / 2 + 1 / 2 * sqrt(1 + 120 * x)) - 1
I want to extract 1 / 2 + 1 / 2 * sqrt(1 + 120 * x) from inside of the floor function and then combine it with the existing (-1) that is outside of the floor function. I have written the following code to do this, but I believe there must be a more efficient and cleaner way.
MY CURRENT CODE:
NOTE: Although my example function f contains a floor function, in general the function f may not (it depends on the particular problem)
pieces := [op(f)]:
numops := nops(pieces):
nofloor := true:
for i from 1 to numops while nofloor do
if has(pieces, floor) then
nofloor := false:
floorpiece := i:
fi:
od:
if not(nofloor) then
pieces[floorpiece] := op(pieces[floorpiece]):
f := sum(pieces[k], k = 1 .. numops):
fi:
Robert Israel (26.3.01)
It looks to me like all you need is
> eval(f, {floor = (t -> t)});
Carl DeVore (26.3.01)
You just need to substitute the identity function for the floor:
eval(f, floor= eval(_->_));
Note that if f contains multiple nested floors, the above command will remove them all. If you only want to remove the outermost floor, I'll think of something else. Let me know.
Norbert Roth (27.3.01)
This should work:
> restart;
> # a bit more complex polynom. why? see below...
> f := floor(1 / 2 + 1 / 2 * sqrt(1 + 120 * x) )- 1+sin(1 / 2) /
/ +sin( 1 / 2 * sqrt(1 + 120 * x));
> # declaration of two procedures
> # the first one splitting the expression up
> # into pieces and then invoking the second one
> fstrip:=proc(f,fname) global pieces: local i:
> pieces:=[op(f)]:
> seq(fstrip_a(pieces,i,fname), i=1..nops(pieces)):
> sum(pieces[k],k=1.. nops(pieces)):
> end:
> # the following procedure does the required extraction:
> fstrip_a:=proc(part,i,fname) global pieces:
> if has(pieces[i],fname) then pieces:=subsop(i=op(pieces[i]),pieces):fi:
> end:
> # e.g. for extracting the 'floor' subexpressions of f,
> # you may use:
> f1:=fstrip(f,floor);
^ ^^^^^
| |__________________extracting expression (e.g.
| sin, cos, ....,
| see Maple's 'has' functionality
|
|________________ function from which to extract
I haven't done much testing, so please let me know if there are problems.
Also I don't know if this is faster or clearer, or whatever ;-)
Douglas B. Meade (27.3.01)
Assuming your expression has at most one term with the floor function, the following command will return the argument of the floor command:
> op(select(has,f,floor));
Actually, this works only if the coefficient of floor is 1. If the coefficient of floor is anything else, you will need to take more care in creating the output. I'll leave the implementation of this to another MUGger.
Douglas Wilhelm Harder (27.3.01)
This finds all floor functions and replaces them with a procedure which just returns its first argument.
f := floor(1 / 2 + 1 / 2 * sqrt(1 + 120 * x)) - 1; f := eval( f, floor = proc(x) x end );
If you're worried about not touching things like the deriviative of the floor function, i.e., floor(1,x), then you have to be more selective:
f := eval( f, floor = proc(x) if nargs = 1 then x else 'floor'(args) fi end );
[Anfang] [Hauptseite] [Suchen] [LDFM]