help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: defining a function from 2 other ones


From: Geordie McBain
Subject: Re: defining a function from 2 other ones
Date: Sat, 18 Aug 2007 13:04:06 +1000

On Thu, 2007-08-16 at 13:42 -0400, James Sherman Jr. wrote:
> While it would be cool to be able to define + in such a way as to take
> two functions f and g, and have it output a new function h in such an
> intuitive manner, I'm pretty sure that it can't be done in octave.
> (Actually, I'm not sure what languages this would be possible, Lisp
> maybe?  I'm not sure how you would go about dynamically defining a
> function). 

This idiom is more natural in Lisp, but you can do it in Octave too.  I
attach a rudimentary implementation, addfns.m, using which:

%<---
octave:27> f = @ (x) x+x; g = @ (x) x*x;
octave:28> addfns (f, g) (3)
ans =  15
octave:29> f = @ (x, y) x + y; g = @ (x, y) x * y;
octave:30> addfns (f, g) (2, 3)
ans =  11
%<---

The arguments do have to match up though, so for the original poster's
problem, I guess:
%<---
octave:37> f = @ (x, p, k) p * x; g = @ (x, p, k) x .^ k;
octave:38> h = addfns (f, g);
%<---

Hope this helps,

Geordie McBain

Attachment: addfns.m
Description: Text Data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]