









 
 




 
 


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


 
 




 
 


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


 
 




 
 


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(i1)+elem[i];
#else
#local R=elem[i];
#end
R
#end
#declare Sum=Add(n);
;)
Christoph

POVRay tutorials, include files, SimPOV,
HCREdit and more: http://www.tubs.de/~y0013390/
Last updated 23 Sep. 2004 _____./\/^>_*_<^\/\.______
Post a reply to this message


 
 




 
 


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


 
 




 
 


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(i1)+elem[i];
> #else
> #local R=elem[i];
> #end
> R
> #end
>
> #declare Sum=Add(n);
>
> ;)
>
> Christoph
>
> 
> POVRay tutorials, include files, SimPOV,
> HCREdit and more: http://www.tubs.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, N1, 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


 
 




 
 


"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, N1, 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


 
 




 
 


> 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


 
 




 

