POV-Ray : Newsgroups : povray.newusers : sum over array of functions Server Time
22 Dec 2024 06:39:52 EST (-0500)
  sum over array of functions (Message 1 to 3 of 3)  
From: Norman
Subject: sum over array of functions
Date: 18 Feb 2013 09:40:01
Message: <web.51223ca9839c96992224e72f0@news.povray.org>
Hello!

I would like to create a function (used inthe definition of an isosurface) which
itself consists of a sum of functions. The number of summands in this sum
function should be declarable.

How this can be done for merges, intersections or differences via min() and
max(), respectively, was pointed out in a very old thread here:
http://news.povray.org/povray.newusers/thread/%3C3d69631b@news.povray.org%3E/
and is demonstrated in iso_cgs.inc as mentioned in that thread.

However, this seems not to work with sum().

If I do


#declare f_test = array [2]{
  function (x) { (x)*1 },
  function (x) { (x)*2 }};

#declare f_summe = function(x) {sum(i,0,1,f_test[i](x))};


I get a "Parse Error: Expected 'numeric expression', undeclared identifier 'i'
found instead". Is there any workaround to do the job?

Additonally, I would like to mention that it is not possible to use
dimension_size(f_test,1)-1
as final value expression of the sum, like in

#declare f_summe = function(x)
{sum(i,0,dimension_size(f_test,1)-1,f_test[i](x))};

yielding a "Parse Error: Expected 'operand', array identifier found instead. Is
this a bug, feature or am I doing something wrong?
Anyway, I can work around this, but not the non-working sum above.

Help is greatly appreciated!


Post a reply to this message

From: Warp
Subject: Re: sum over array of functions
Date: 18 Feb 2013 10:08:23
Message: <512243e7@news.povray.org>
Norman <nomail@nomail> wrote:
> #declare f_test = array [2]{
>   function (x) { (x)*1 },
>   function (x) { (x)*2 }};

> #declare f_summe = function(x) {sum(i,0,1,f_test[i](x))};

It has been a long time since I dealt with user-defined functions,
but I seem to remember that SDL arrays are not supported in them.

-- 
                                                          - Warp


Post a reply to this message

From: clipka
Subject: Re: sum over array of functions
Date: 18 Feb 2013 10:23:36
Message: <51224778$1@news.povray.org>
Am 18.02.2013 15:37, schrieb Norman:

> If I do
>
>
> #declare f_test = array [2]{
>    function (x) { (x)*1 },
>    function (x) { (x)*2 }};
>
> #declare f_summe = function(x) {sum(i,0,1,f_test[i](x))};
>
>
> I get a "Parse Error: Expected 'numeric expression', undeclared identifier 'i'
> found instead". Is there any workaround to do the job?

Yup:

#declare f_summe = function(x) {
   #for(i,0,1)
     + f_test[i](x)
   #end
   }

> Additonally, I would like to mention that it is not possible to use
> dimension_size(f_test,1)-1
> as final value expression of the sum, like in
>
> #declare f_summe = function(x)
> {sum(i,0,dimension_size(f_test,1)-1,f_test[i](x))};
>
> yielding a "Parse Error: Expected 'operand', array identifier found instead. Is
> this a bug, feature or am I doing something wrong?

Neither of the three - it's simply a limitation in what you can use 
within a function and what not. Those limitations are necessary because 
functions need to be (potentially) evaluated at run time. These 
limitations include (but are not limited to):

- Functions can't access SDL variables. What actually happens when you 
place an SDL variable in a function is that the SDL variable is 
evaluated /immediately/; e.g.:

   #declare a = 1;
   #declare f = function(x) { a * x }
   #declare a = 2;
   #debug concat(str(f(1),0,-1),"\n")

will yield "1", not "2".

- Functions can only deal with scalar expressions. No vectors, no 
arrays, no nothing.


> Anyway, I can work around this, but not the non-working sum above.

Note that the workaround for the sum() problem works fine with 
dimension_size.


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.