anonymous functions?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

anonymous functions?

Han-Wen Nienhuys-6
Hi there!

Please bear with me, I'm a newbie user. I want to build a simple
meshless PDE solver in octave, but right at the start I get stuck.
I'm trying to write a function to compute a gram-matrix, but I can't
figure out how to pass an anonymous function to the quad procedure.

What I am hoping|trying to do:


        function G =  grammatrix (funcs)
          l = length (funcs)
          G = zeros (l,l)
          i = 0
          j = 0
          for f = funcs
            for g =  funcs
              function p = prod(t)
                p = f(t) * g(t)
              endfunction
              G(i,j) = quad (prod,0, 1, [0, 0.01], [])
              j++
              endfor
            i++
          endfor
        endfunction





--

Han-Wen Nienhuys   |   [hidden email]    | <a href="http://www.cs.uu.nl/Šþhanwen/">http://www.cs.uu.nl/Šþhanwen/



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------


Reply | Threaded
Open this post in threaded view
|

Re: anonymous functions?

Etienne Grossmann-3

  Hello,

From: Han-Wen Nienhuys <[hidden email]>
#  Please bear with me, I'm a newbie user. I want to build a simple
#  meshless PDE solver in octave, but right at the start I get stuck.
#  I'm trying to write a function to compute a gram-matrix, but I can't
#  figure out how to pass an anonymous function to the quad procedure.

#  What I am hoping|trying to do:


#          function G =  grammatrix (funcs)
#            l = length (funcs)
#            G = zeros (l,l)
#            i = 0
#            j = 0
#            for f = funcs
#              for g =  funcs
#                function p = prod(t)
#                  p = f(t) * g(t)
#                endfunction
#                G(i,j) = quad (prod,0, 1, [0, 0.01], [])
#                j++
#                endfor
#              i++
#            endfor
#          endfunction

  for f = funcs
     ....
  end

  loops over the columns of funcs :

octave:52> for ii = reshape (1:6,2,3), ii, end
ii =

  1
  2

ii =

  3
  4

ii =

  5
  6

  which may not be what you expected.

  You may do

  eval (["function p = my_prod(t)ŠÜn"ŠÜ
          "  p = f(t) * g(t);ŠÜn"ŠÜ
          "endfunctionŠÜn"]);

  but f(t) will not be visible (see the man pages on local and global
variables); by default, all vars are local.

  Also, you don't get an anonymous func : it's called "my_prod". (note
that "prod" is a builtin function). Do you usually program in perl (a
good language for anonymous functions)?

  Sorry for not being very helpful,

  Etienne









-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------


Reply | Threaded
Open this post in threaded view
|

Re: anonymous functions?

Han-Wen Nienhuys-6
[hidden email] writes:
>   eval (["function p = my_prod(t)ŠÜn"ŠÜ
>           "  p = f(t) * g(t);ŠÜn"ŠÜ
>           "endfunctionŠÜn"]);
>
>   but f(t) will not be visible (see the man pages on local and global
> variables); by default, all vars are local.

As far as I can tell, the only way to do what I want involves crude
hacks (assembling strings, and loading or evalling them). In that
case, I'll revert to using Mathematica (sorry).

>   Also, you don't get an anonymous func : it's called "my_prod". (note
> that "prod" is a builtin function). Do you usually program in perl (a
> good language for anonymous functions)?

Usually in Scheme or Python.

--

Han-Wen Nienhuys   |   [hidden email]    | <a href="http://www.cs.uu.nl/Šþhanwen/">http://www.cs.uu.nl/Šþhanwen/



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------


Reply | Threaded
Open this post in threaded view
|

Re: anonymous functions?

Johan Kullstam-3
Han-Wen Nienhuys <[hidden email]> writes:

> [hidden email] writes:
> >   eval (["function p = my_prod(t)ŠÜn"ŠÜ
> >           "  p = f(t) * g(t);ŠÜn"ŠÜ
> >           "endfunctionŠÜn"]);
> >
> >   but f(t) will not be visible (see the man pages on local and global
> > variables); by default, all vars are local.
>
> As far as I can tell, the only way to do what I want involves crude
> hacks (assembling strings, and loading or evalling them). In that
> case, I'll revert to using Mathematica (sorry).
>
> >   Also, you don't get an anonymous func : it's called "my_prod". (note
> > that "prod" is a builtin function). Do you usually program in perl (a
> > good language for anonymous functions)?
>
> Usually in Scheme or Python.

if you like lisp, you might want to try matlisp.

<URL:http://matlisp.sourceforge.net/>

--
J o h a n  K u l l s t a m
[[hidden email]]
Don't Fear the Penguin!



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------