Re: mathematics on sequences

new topic     » goto parent     » topic index » view thread      » older message » newer message

On Sun, 06 Mar 2005 17:58:54 -0800, Hayden McKay
<guest at rapideuphoria.com> wrote:
> 
> posted by: Hayden McKay <hmck1 at dodo.com.au>
> 
> As you readers probably know I'm currently takeing a crash coarse with
> matrices and
> 2d-3d rotation. Any way I found some thing interesting and I was wondering if
> someone could explain the reason why.

Let me just say that while euphoria sequences look like they'd be
really good for work with matrices, they are not, except for a few
cases like adding a single vector to a single vector.

> below are two test cases of a routine that would rotate
> (x) left--right and (y) up--down

If you are doing 3d rotations, I recommend using rotation matrices:
http://en.wikipedia.org/wiki/Rotation_matrix
General form to convert from model-coordinates to world-coordinates is this:
W = M*R + P
W -> world-coordinates (where the point ends up)
M -> model-coordinates (where the point is relative to your model)
R -> rotation matrix (the angle of the model w.r.t world coordinates)
P -> position     ( of the model in world-coordinates )

Do this for every point in your model, for instance a cube has 8 points.
(But you probably know this already)

You can't do this with the built-in euphoria functions.
M*R is a matrix multiplication (cross product?) .. whereas euphoria
would give you  { M[1]*R[1], M[2]*R[2], etc }

I suggest you write a routine to do M*R+P for you, and make it as
efficient as you can. This is an oft-used function.


> test_1() benchmarked a wopping ~0.66xxxxxx while test_2() came in at
> ~0.06xxxxx on my pentium III 550. You would assume test_1() to be the
> fastset but this is not the case.

Hmm... that makes sense.

> function test_1()
>     sequence row_1, row_2
> 
>     row_1=repeat(matrix[1],10000)*coords
>     row_2=repeat(matrix[1],10000)*coords
>     coords=(row_1+row_2)
>     return coords
> 
> end function

Lessee, you have to allocate a huge amount of space for row_1, and
row_2... then you have to fill each with 10000 matrix[1] instances...
then you have to multiply each instance by 10000...
Then, you have to allocate a huge amount of space for coords, and copy
row_1[i]+row_2[i] to each space in the coords sequence.
It's horribly inefficient... 

> function test_2()
> 
>     for i=1 to 10000 do
>         coords[i][1]=(coords[i][1]*matrix[1][1])+(coords[i][1]*matrix[2][1])
>         coords[i][2]=(coords[i][2]*matrix[1][2])+(coords[i][1]*matrix[2][2])
>     end for
>     return coords
> 
> end function

This may seem slower, but it is atomic... all the processing is done
in one step, and the result assigned once. That's why it's faster.

> t=time()
> s=test_1()
> ?t-time()
> t=time()
> s=test_2()
> ?t-time()

Also, there's some delay involved when euphoria starts up - what
happens if you rearrange test_1 and test_2?

-- 
MrTrick

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu