 Don't ask to ask, just ask PerlMonks

### Using PDL for rotations

 on Apr 29, 2008 at 16:30 UTC Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Ok, for a certain program I am writing (protein folding), I need to be able perform rotations. I was first planning to do it manually by defining rotation matrices and change of basis matrices etc but I think pdl might save me time. However I am not sure how to use its use PDL::Transform to do so. Here is a piece of code that I was using to experiment with pdl
```use PDL;
use PDL::Transform;

@a = [[1,0,0],[0,1,0],[0,0,1]];

\$c= pdl @a;

\$e = t_rot(45,45,45);

\$c = \$e * \$c

print \$c;
I was hoping this would rotate my 1,1,1 vector in all directions by 45 degrees but it gives the error. "Hash given as a pdl - but not {PDL} key!". I am not able to understand what this error is for? Also I have seen no tutorial where these rotationa matrices are explained so I would appreciate any help, thanks.

Replies are listed 'Best First'.
Re: Using PDL for rotations
by pc88mxer (Vicar) on Apr 29, 2008 at 16:38 UTC
t_rot takes a single argument, so you need to use (but see update below):
```\$e = t_rot([45,45,45]);
The other thing is that \$e will be a transform object which requires you to use it like this:
```\$d = \$e->apply(\$c);
The final thing is that matrix multiplication is the x operator whereas * is component-wise multiplication.

Update: There's a bug in the code/documentation for t_rot. The work-around is to use t_linear and also specify a dims argument:

```\$e = t_linear({ rot => [45,45,45], dims => 3});
The problem can be demonstrated by setting \$PDL::Transform::debug = 1;:
```\$PDL::Transform::debug = 1;

\$e = t_linear({ rot => [45,45,45] });
# the above will emit:
#   PDL::Transform::Linear: assuming 2-D transform (set dims option to
+ change)
Note from THE FUTURE: this bug was in fact still there! And impressively, there was not a single test for any of the rotation stuff. Now there is a single test, which also caught another recently-introduced bug. The code will now use the number of elements in the rotation if neither dims nor matrix are supplied.

Both bugs now have fixes, which will be in PDL 2.081.

Okay one more question, upon doing rotations perl some times returns infitesmaly small numbers instead of zero (where it should be zero, as I check them my-self). Is there a simple way to have perl return them zero by changing precision somehow? Thanks...
Floating-point math can get ugly little rounding errors. You're seeing them here.

No need to set any precision, simply make sure the magnitude of the values are above some \$eps (epsilon) or set them to zero.

Like so:  \$pdl = \$pdl * ( (abs \$pdl) > \$eps);

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://683514]
Approved by pc88mxer
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2023-02-04 02:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?