Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>cos</code> is expensive, especially 4 in a row. You appear to be computing cos(n<em>a+b) where b is a constant and n is a small integer. This means you can precompute cos(b) and sin(b) and at runtime compute just cos(hp) and sin(hp). You can get cos(n</em>a+b) by making repeated use of</p> <pre><code>cos(a+b) = cos(a)*cos(b)-sin(a)*sin(b) </code></pre> <p>You'll be trading a couple of <code>sin</code>s and <code>cos</code>s for some multiplications and additions, almost certainly worthwhile.</p> <p>You can do better if you're feeling ambitious. You're getting <code>hp</code> indirectly from an <code>atan2</code>. The pattern <code>trig-function(rational-function(inverse-trig-function(x)))</code> can frequently be replaced by some combination of polynomials and roots which are faster to evaluate than trig functions.</p> <p>I don't know how <code>pow</code> is implemented in Java, but if it uses logs, you may be better off getting <code>Cp7</code> using <code>Cp2=Cp*Cp;Cp4=Cp2*Cp2;Cp7=Cp4*Cp2*Cp;</code></p> <p>Update: Getting a bit more speculative right now as I don't have time to actually rewrite the code. The power optimization and the trig optimization are actually the same thing in disguise! The trig optimization is a version of the power optimization applied to complex numbers. What's more, the line</p> <pre><code>double dH = 2 * Math.sqrt(CpProd) * Math.sin(dhp / 2); </code></pre> <p>is part of a complex number square root operation. This makes me think that a large chunk of this code could actually be written to use complex numbers eliminating almost all of the trig functions. I don't know how your complex number arithmetic is though...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload