     POV-Ray : Newsgroups : povray.text.tutorials : Summing over indices of an array Server Time24 Jul 2021 13:16:39 EDT (-0400) Summing over indices of an array (Message 1 to 7 of 7)         From: Max Ulbrich Subject: Summing over indices of an array Date: 22 Jan 2005 02:07:52 Message: <41f1fbc8\$1@news.povray.org>    ```Hi,

I just trying to get started with Povray and try to sum elements of an
array.
If I use elem+elem+elem+... it works fine.
If I try sum(i,1,n,elem[i]) I get an error.
What is the usual way to get it done? I cannot find help in the Povray
tutorial from the povray web page. Is there a good online tutorial that
explains easy things and has lots of examples?
Thanks,
Max
```    From: Mike Williams Subject: Re: Summing over indices of an array Date: 22 Jan 2005 03:06:26 Message:    ```Wasn't it Max Ulbrich who wrote:
>Hi,
>
>I just trying to get started with Povray and try to sum elements of an
>array.
>If I use elem+elem+elem+... it works fine.
>If I try sum(i,1,n,elem[i]) I get an error.

sum() only works inside a function{} and array[] references don't work
inside a function{}. So, whether you're trying to do the sum inside or
outside a function{} it's not going to work.

I think you have to use a loop:

#declare i=1;
#declare Sum=0;
#while (i<=n)
#declare Sum = Sum + elem[i];
#declare i=i+1;
#end

--
Mike Williams
Gentleman of Leisure
```    From: Christoph Hormann Subject: Re: Summing over indices of an array Date: 22 Jan 2005 03:30:02 Message:    ```Mike Williams wrote:
>
> I think you have to use a loop:
>

Bah, you surely don't have to:

#if (i>0)
#else
#local R=elem[i];
#end
R
#end

;-)

Christoph

--
POV-Ray tutorials, include files, Sim-POV,
HCR-Edit and more: http://www.tu-bs.de/~y0013390/
Last updated 23 Sep. 2004 _____./\/^>_*_<^\/\.______
```    From: Warp Subject: Re: Summing over indices of an array Date: 23 Jan 2005 08:27:58 Message: <41f3a65e@news.povray.org>    ```Christoph Hormann <chr### [at] gmx de> wrote:
> Bah, you surely don't have to:

But using a recursive macro is an awfully inefficient way of doing that
(at least with the current SDL parser).

--
#macro M(A,N,D,L)plane{-z,-9pigment{mandel L*9translate N color_map{[0rgb x]
[1rgb 9]}scale<D,D*3D>*1e3}rotate y*A*8}#end M(-3<1.206434.28623>70,7)M(
-1<.7438.1795>1,20)M(1<.77595.13699>30,20)M(3<.75923.07145>80,99)// - Warp -
```    From: moocow Subject: Re: Summing over indices of an array Date: 2 Jan 2006 20:05:00 Message:    ```Christoph Hormann <chr### [at] gmx de> wrote:
> Mike Williams wrote:
> >
> > I think you have to use a loop:
> >
>
> Bah, you surely don't have to:
>
>    #if (i>0)
>    #else
>      #local R=elem[i];
>    #end
>    R
> #end
>
>
> ;-)
>
> Christoph
>
> --
> POV-Ray tutorials, include files, Sim-POV,
> HCR-Edit and more: http://www.tu-bs.de/~y0013390/
> Last updated 23 Sep. 2004 _____.//^>_*_<^/.______

What if I want to define a function as a sum of other functions?

#declare X = array[N]
//set X's

#declare f = function{ sum(i, 0, N-1, pow(.5, pow( vlength(<x,y,z> - X[i]),
2))) }

I can't think of how to make that work.

Also, why these restrictions? Why can't I sum outside a function or use
arrays inside? Why can't I do:
#define f1 = function { f1(x,y,z) + g(x,y,z) }
?
```    From: nomail Subject: Re: Summing over indices of an array Date: 3 Jan 2006 00:45:00 Message:    ```"moocow" <nomail@nomail> wrote:
> What if I want to define a function as a sum of other functions?

There is nothing preventing you from doing it.

> #declare X = array[N]
> //set X's
>
> #declare f = function{ sum(i, 0, N-1, pow(.5, pow( vlength(<x,y,z> - X[i]),
> 2))) }
>
> I can't think of how to make that work.

RTFM.

> Also, why these restrictions?

Why is the sky blue? The primary use for functions is in isosurfaces,
functions are not a macro replacement.

> Why can't I sum outside a function or

The SDL supports means to compute the sum via a macro.

> use arrays inside?

Arrays exist while parsing, functions are evaluated while rendering.
Functions are not macros!

> Why can't I do:
> #define f1 = function { f1(x,y,z) + g(x,y,z) }
> ?

This cannot work as it creates an infinite recursion!
```    From: moocow Subject: Re: Summing over indices of an array Date: 3 Jan 2006 01:10:00 Message:    ```> RTFM.

I couldn't find that in the documentation. Could you give me a link?

> Arrays exist while parsing, functions are evaluated while rendering.
> Functions are not macros!

Thanks.

> > Why can't I do:
> > #define f1 = function { f1(x,y,z) + g(x,y,z) }
> > ?
>
> This cannot work as it creates an infinite recursion!

Only if you assume functions are evaluated lazily, and don't unroll all
function references into a closed form. Apparently that's how POV works.

For all I knew, it could be the same as with scalar values:
#define f = function(x) { x }
//f(x) = x
#define f = function(x) { f(x) + 1 }
//f(x) = x+1
```        