Link to home
Start Free TrialLog in
Avatar of JasonMewes
JasonMewes

asked on

Can you simplify this mathematical algorithm

Is it possible to refactor this mathematical algorithm to minimize the use of trigonometric functions?

input:
  z      :=  0 to ½PI
  Ma, Mb :=  0 to ∞
  Pa, Pb :=  0 to 2PI

algorithm:
  r = Ma * sin(Pa)
  i = Mb * sin(Pb)
  Sr = r + i
  Dr = r - i
  r = Ma * cos(Pa)
  i = Mb * cos(Pb)
  Si = r + i
  Di = r - i
  r = -cos(z)
  i = -sin(z)
  Tr = r * Si + i * Dr
  Ti = i * Si - r * Dr

output:
  Ra :=  Sr + Tr
  Ia :=  Ti + Di
  Rb :=  Sr - Tr
  RI :=  Ti - Di

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JasonMewes
JasonMewes

ASKER

Errors (for reference, no effect on question/solution):
Line 3 - bounds are actually -∞ to +∞
Line 24 - the name RI is actually Ib

Ra, Ia, Rb, Ib represent the four needed outputs of the algorithm.
There is no relationship between the inputs, only the bounds stated.

Other parts cannot be refactored, as inputs are external and outputs at this stage must be as specified.

SQRT is costlier than SIN and COS in this case.

Essentially you are saying that this is already as optimized as it can get?
Not possible to use identities or relationships (such as sin(A + B) = sin(A)*cos(B) + cos(A)*sin(B)) to reduce?
Given your (ozo) track record I trust your opinion implicitly!
This probably does not help:

Ma, Pa = magnitude, phase pair A
Mb, Pb = magnitude, phase pair B

Ra, Ia = complex number out A
Rb, Ib = complex number out B
I'm not sure if this comes out simpler, but
Tr = -(Ma * cos(z-Pa) + Mb * cos(z+Pb))
Ti = -(Ma * sin(z-Pa) + Mb * sin(z+Pb))
Since Sr and Di (requiring four sin/cos to calculate) are still required to calculate output - it seems calculating Tr and Ti this way would add additional processing.
This page shows a history of how trig functions have been computed in the past and how complicated it becomes.  If you read this you will probably be convinced to just use the functions defined in program.  That is probably the simplest way.

http://www.clarku.edu/~djoyce/trig/compute.html