POV-Ray : Newsgroups : povray.text.tutorials : Summing over indices of an array Server Time
29 Mar 2024 02:49:43 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[0]+elem[1]+elem[2]+... 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


Post a reply to this message

From: Mike Williams
Subject: Re: Summing over indices of an array
Date: 22 Jan 2005 03:06:26
Message: <l3taCGA5lg8BFw4s@econym.demon.co.uk>
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[0]+elem[1]+elem[2]+... 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


Post a reply to this message

From: Christoph Hormann
Subject: Re: Summing over indices of an array
Date: 22 Jan 2005 03:30:02
Message: <cst2rq$36v$1@chho.imagico.de>
Mike Williams wrote:
> 
> I think you have to use a loop:
> 

Bah, you surely don't have to:

#macro Add(i)
   #if (i>0)
     #local R=Add(i-1)+elem[i];
   #else
     #local R=elem[i];
   #end
   R
#end

#declare Sum=Add(n);

;-)

Christoph

-- 
POV-Ray tutorials, include files, Sim-POV,
HCR-Edit and more: http://www.tu-bs.de/~y0013390/
Last updated 23 Sep. 2004 _____./\/^>_*_<^\/\.______


Post a reply to this message

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] gmxde> 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 -


Post a reply to this message

From: moocow
Subject: Re: Summing over indices of an array
Date: 2 Jan 2006 20:05:00
Message: <web.43b9cd3b4b45696074508c90@news.povray.org>
Christoph Hormann <chr### [at] gmxde> wrote:
> Mike Williams wrote:
> >
> > I think you have to use a loop:
> >
>
> Bah, you surely don't have to:
>
> #macro Add(i)
>    #if (i>0)
>      #local R=Add(i-1)+elem[i];
>    #else
>      #local R=elem[i];
>    #end
>    R
> #end
>
> #declare Sum=Add(n);
>
> ;-)
>
> 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) }
?


Post a reply to this message

From: nomail
Subject: Re: Summing over indices of an array
Date: 3 Jan 2006 00:45:00
Message: <web.43ba0e404b456960963fc0330@news.povray.org>
"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!


Post a reply to this message

From: moocow
Subject: Re: Summing over indices of an array
Date: 3 Jan 2006 01:10:00
Message: <web.43ba14da4b45696074508c90@news.povray.org>
> 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


Post a reply to this message

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