Jorge Calvo
2012-07-05 19:36:08 UTC
Hello there:
I am trying to define a function which would take the name of another function as a parameter and use it somehow. My goal is to implement the bisection, secant, and Newton's method procedures, but at the moment I am trying to understand the proper syntax that will be needed in all of these methods. As a very simple example, I tried:
(%i1) testfun(f, a) := f(a) $
This works as long as f(x) is not defined globally:
(%i2) testfun(sin, 3.0);
(%o2) .1411200080598672
However, if f(x) is defined globally, then my testfun uses the global function and not the one indicated by the parameter:
(%i3) f(x) := x^2 - x $
(%i4) testfun(sin, 3.0);
(%o4) 6.0
I have fixed this by using apply instead:
(%i5) testfun(f, a) := apply(f, [a]) $
(%i6) testfun(sin, 3.0);
(%o2) .1411200080598672
However, I wonder if this is the best way to do this. Is there a way to pass a function (rather than just its name) as an argument?
Thanks in advance!
Jorge
--
Dr. Jorge Alberto Calvo
Associate Professor of Mathematics
Department of Mathematics and Physics
Ave Maria University
Phone: (239) 280-1608
Email: ***@avemaria.edu
Web: http://sites.google.com/site/jorgealbertocalvo
I am trying to define a function which would take the name of another function as a parameter and use it somehow. My goal is to implement the bisection, secant, and Newton's method procedures, but at the moment I am trying to understand the proper syntax that will be needed in all of these methods. As a very simple example, I tried:
(%i1) testfun(f, a) := f(a) $
This works as long as f(x) is not defined globally:
(%i2) testfun(sin, 3.0);
(%o2) .1411200080598672
However, if f(x) is defined globally, then my testfun uses the global function and not the one indicated by the parameter:
(%i3) f(x) := x^2 - x $
(%i4) testfun(sin, 3.0);
(%o4) 6.0
I have fixed this by using apply instead:
(%i5) testfun(f, a) := apply(f, [a]) $
(%i6) testfun(sin, 3.0);
(%o2) .1411200080598672
However, I wonder if this is the best way to do this. Is there a way to pass a function (rather than just its name) as an argument?
Thanks in advance!
Jorge
--
Dr. Jorge Alberto Calvo
Associate Professor of Mathematics
Department of Mathematics and Physics
Ave Maria University
Phone: (239) 280-1608
Email: ***@avemaria.edu
Web: http://sites.google.com/site/jorgealbertocalvo