Message Boards Message Boards

0
|
8376 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Add a constant to some list elements?

Posted 8 years ago

I have a list of phase angles, all of which are less than 0. I want to apply some sort of transformation, to keep them in the range -180 to + 180 degrees, by adding 360 degrees where necessary. So for example, if these where the phase angles in degrees

phases = Table[-45 n , {n, 0, 6}]
{0, -45, -90, -135, -180, -225, -270}

I want the set

{0, -45, -90, -135, -180, 135, 90}

where 360 was added to -225 to get 135, and 360 was added to -270 to get 90.

I have two questions:

1) How do I solve the simple case where only 360 degrees may needs to be added - never more than 360? I'd appreciate an example, even though the next question would solve this problem too.

2) How, if the need arose, which I can't see happening in the short term, could I add or subtract the appropriate number of 360's, so that any angle in the range -Infinity to +Infinity would always be transformed to the range -180 to +180?

I have access to Mathematica 7, but nothing later.

POSTED BY: David Kirkby
3 Replies
Posted 8 years ago

Thank you both. Much appreciated.

POSTED BY: David Kirkby

In answer to your first question, you can use a list of conditional replacement rule:

phases = {0, -45, -90, -135, -180, -225, -270}
phases /. {x_ /; x < -180 -> x + 360, x_ /; x > +180 -> x - 360}

gives: {0, -45, -90, -135, -180, 135, 90}

For the general case, I think the best is to build a function that does the job an Map the function on the list. Based for example on the function x - Floor[x] or Mod we can sculpt the desired function that maps the full interval in -180/+180 after a few steps (a graphics for visualization helps a lot). After a few iterations I got:

360 (-1/2 + (x - 180)/360 - Floor[(x - 180)/360]

Which simplified by MMA in:

f[x_] = x + 360 (Ceiling[1/2 - x/360] - 1)

The function Mod[x, 360, 180] - 360 does the same job

Then you map the function to the list:

f /@ phases

and get tje same result for any value ouside the range -180/+180

In[76]:= phases = Table[-45 n, {n, 0, 18}]

Out[76]= {0, -45, -90, -135, -180, -225, -270, -315, -360, -405, \
-450, -495, -540, -585, -630, -675, -720, -765, -810}

then:

In[77]:= f /@ phases

Out[77]= {0, -45, -90, -135, -180, 135, 90, 45, 0, -45, -90, -135, \
-180, 135, 90, 45, 0, -45, -90}

This should work even with MMA 2.2 :)

Christian

POSTED BY: Christian Neel
phases = Table[-45 n, {n, 0, 6}]
Mod[phases, 360, -180]

{0, -45, -90, -135, -180, 135, 90}
POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract