Solved

# Can you simplify this mathematical algorithm

Posted on 2014-11-19
182 Views
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
``````
0
Question by:JasonMewes
• 3
• 2

LVL 84

Accepted Solution

ozo earned 500 total points
ID: 40451983
What do Ra,Ia,Rb,RI represent?
Unless you can establish some relationship between the inputs, there may not be much more you can do than to reduce half of the trigonometric functions to sqrt functions.
Or if you can transform your inputs so that instead of working with Ma,Mb,Pa,Pb,
you're working with inputs that correspond to  Ma*sin(Pa),Ma*cos(Pa),Mb*sin(Pb),Mb*cos(Pb),
But that may depend on refactoring other parts of the algorithm that use this part.
0

Author Comment

ID: 40452008
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?
0

Author Comment

ID: 40452018
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
0

LVL 84

Expert Comment

ID: 40452126
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))
0

Author Comment

ID: 40452169
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.
0

LVL 16

Expert Comment

ID: 40452943
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
0

## Featured Post

### Suggested Solutions

Okay. So what exactly is the problem here? How often have we come across situations where we need to know if two strings are 'similar' but not necessarily the same? I have, plenty of times. Until recently, I thought any functionality like that wo…
Have you ever thought of installing a power system that generates solar electricity to power your house? Some may say yes, while others may tell me no. But have you noticed that people around you are now considering installing such systems in their …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…