# Algorithms for Various Trigonometric Power Sums
## 5 Twisted Trigonometric Power Sums
Moreover, the \(l\!=\!1\) and \(l\!=\!2\) cases can be combined into one statement by separating the \(i=\upsilon\) term in the second term of (68) and expressing it as  
\[l\sum_{i=\upsilon+1}^{j}(-1)^{i}\binom{j}{i}S^{+}_{0,l}(i-\upsilon,m) = l\sum_{i=\upsilon+1}^{j}(-1)^{i}\binom{j}{i}\left(2^{2\upsilon-2i }m\binom{2i-2\upsilon}{i-\upsilon}+2^{1+2\upsilon-2i}m\right. \tag{85}\] \[\times \sum_{p=1}^{\lfloor(i-\upsilon)/(lm)\rfloor}(-1)^{plm}\binom{2i- 2\upsilon}{i-\upsilon-pml}-\frac{1+(-1)^{l}}{4}\Bigg{)}.\]  
Although the above equation is elegant from a mathematical point of view, it makes a module less readable rather than considering the \(l\!=\!1\) and \(l\!=\!2\) cases separately, which can be handled by employing a conditional If statement. Consequently, we shall create a module that will calculate the entire last term on the rhs of (75) for both the Dowker and Gardner-Fisher cases in addition to both their non-alternating (\(sgn=+1\)) and alternating versions (\(sgn=-1\)). Thus, we shall evaluate  
\[U^{\pm}_{l,v,j}(m)=\sum_{i=v+1}^{j}(-1)^{i}\binom{j}{i}\sum_{k=1}^{lm-1}(\pm 1)^ {k}\sin^{2i-2v}\Big{(}\frac{k\pi}{lm}\Big{)}\,. \tag{86}\]  
The above result can be regarded as a different type of trigonometric power sum. A Mathematica module that is capable of calculating \(U^{\pm}_{l,v,j}(m)\) appears below:  
U[sgn_l_v_j_m_]= Module[[H], H = 0; total = 0;  
Do[1 = 0;Which[sgn == 1&&( == 1),  
e1 = 2\({}^{\wedge}\)(1-2(i-v))m Sum[(-1)\({}^{\wedge}\)(p m) Binomial[2 (i\(-\)v),i\(-\)v\(-\)p m], {p,1,Floor[(i\(-\)v)/m]}]  
+2\({}^{\wedge}\)(\(-2(i-v)\))m Binomial[2(i\(-\)v),i\(-\)v];  
(sgn == 1)&&(1 == 2),  
e1 = 2\({}^{\wedge}\)(1-2(i-v)) m Sum[Binomial[2 (i\(-\)v),i\(-\)v\(-\)2p m],{p,1,Floor[(i\(-\)v)/(2m)]}]  
+2\({}^{\wedge}\)(\(-2\) (i\(-\)v))m Binomial[2 (i\(-\)v),i\(-\)v]-1/2,  
(sgn == -1)&&(l == 1),  
e1 = -Binomial[2 (i\(-\)v),i\(-\)v](1 + (-1)\({}^{\wedge}\)m)/2\({}^{\wedge}\)(2 (i\(-\)v) + 1);  
Do[If[Exp[2(i\(-\)v\(-\)k)] \(\pi\)/m] == -1,H = (m + 1),H = (1 + (-1)\({}^{\wedge}\)m)/2\({}^{\wedge}\);  
e2 = (-1)\({}^{\wedge}\)(i-v + k) Binomial[2 (i\(-\)v),k]H/2\({}^{\wedge}\)(2(i\(-\)v)\(-\)1); e1 = e1 + e2,{k,0,(i\(-\)v)}];  
(sgn == -1)&&(l == 2),  
e1 = -Binomial[2 (i\(-\)v),i\(-\)v](1 + (-1)\({}^{\wedge}\)m)/2\({}^{\wedge}\)(2 (i\(-\)v) + 1);  
Do[If[ Exp[(i\(-\)v\(-\)k)] \(\pi\)/m] == -1,H = (m + 1),H = (1 + (-1)\({}^{\wedge}\)(i\(-\)v + m\(-\)k))/2];  
e2 = (-1)\({}^{\wedge}\)(i-v + k) Binomial[2 (i\(-\)v),k]H/2\({}^{\wedge}\)(2(i\(-\)v)\(-\)1);  
If[] == 2,e1 = (-1)\({}^{\wedge}\)i Binomial[j,i](2 e1 - (-1)\({}^{\wedge}\)m),  
e1 = (-1)\({}^{\wedge}\)i Binomial[j,i] e1];totval = totalv + e1,{i,v + i,j}];  
Print[\"For l=\",l,\", sgn=\",sgn,\",\"]=\",\"=\",\",\" and m=\",m,\", the sum of the basic trigonometric power sums in U\({}^{\pm}_{\mathrm{r},j,l}\) is equal to \",totval]]]. After setting both the values of H, given by (57), and \(U^{\pm}_{l,v,j}(m)\) or totval to zero initially, the code encounters a Do loop, whose purpose is to evaluate the summation over \(i\) in (86). Within the Do loop, the module encounters a Which instruction, which is required to handle all four types of basic trigonometric power sums according to the values of \(sgn\) and \(l\). The first case in the Which instruction corresponds to the non-alternating Dowker case, where the value of \(S^{\pm}_{0,1}(i-v,m)\) given by e1 is calculated in accordance with the upper signed version of (76). The second case is the non-alternating Gardner-Fisher sum, where e1 is evaluated according to (82). The next two cases in the Which instruction handle both types of alternating sums. This is where H figures in the evaluation of e1. As indicated previously, both these cases require another Do loop, since H is conditional on the value of \(\exp(i(2-l)\pi/m)\). After e1 has been calculated for the particular values of \(sgn\) and \(l\), the code continues with the first Do loop by testing the upper limit of the inner sum. That is, for the \(l=2\) case, the code takes account of the fact that the upper limit is \(lm-1\) as in (77).