You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lib/node_modules/@stdlib/math/base/special/polygamma/lib/polycotpi.js
+54-43
Original file line number
Diff line number
Diff line change
@@ -100,6 +100,44 @@ function zeros( len ) {
100
100
returnout;
101
101
}// end FUNCTION zeros()
102
102
103
+
/**
104
+
* Updates the derivatives table.
105
+
*
106
+
* @private
107
+
* @param {PositiveInteger} n - derivative
108
+
*/
109
+
functioncalculateDerivatives(n){
110
+
varnoffset;// offset for next row
111
+
varoffset;// 1 if the first cos power is 0; otherwise 0
112
+
varncols;// how many entries there are in the current row
113
+
varmcols;// how many entries there will be in the next row
114
+
varmo;// largest order of the polynomial of cos terms
115
+
varso;// order of the sin term
116
+
varco;// order of the cosine term in entry "j"
117
+
vari;
118
+
varj;
119
+
vark;
120
+
121
+
for(i=table.length-1;i<n-1;i++){
122
+
offset=(i&1)|0;
123
+
so=(i+2)|0;
124
+
mo=(so-1)|0;
125
+
ncols=((mo-offset)/2)|0;
126
+
noffset=offset ? 0 : 1;
127
+
mcols=((mo+1-noffset)/2)|0;
128
+
table.push(zeros(mcols+1));
129
+
for(j=0;j<=ncols;j++){
130
+
co=((2*j)+offset)|0;
131
+
k=((co+1)/2)|0;
132
+
table[i+1][k]+=((co-so)*table[i][j])/(so-1);
133
+
if(co){
134
+
k=((co-1)/2)|0;
135
+
table[i+1][k]+=(-co*table[i][j])/(so-1);
136
+
}
137
+
}
138
+
}
139
+
}
140
+
103
141
104
142
// MAIN //
105
143
@@ -125,7 +163,7 @@ function zeros( len ) {
125
163
*
126
164
* - Note that there are many different ways of representing this derivative thanks to the many trigonometric identities available. In particular, the sum of powers of cosines could be replaced by a sum of cosine multiple angles, and, indeed, if you plug the derivative into Mathematica, this is the form it will give. The two forms are related via the Chebeshev polynomials of the first kind and \\( T_n(\cos(x)) = \cos(n x) \\). The polynomial form has the great advantage that all the cosine terms are zero at half integer arguments - right where this function has it's minimum - thus avoiding cancellation error in this region.
127
165
*
128
-
* - And finally, since every other term in the polynomials is zero, we can save space by only storing the non-zero terms. This greatly complexifies subscripting the tables in the calculation, but halves the storage space (and complexity for that matter).
166
+
* - And finally, since every other term in the polynomials is zero, we can save space by only storing the non-zero terms. This greatly increases complexity when subscripting the tables in the calculation, but halves the storage space (and complexity for that matter).
129
167
*
130
168
*
131
169
* @private
@@ -135,21 +173,11 @@ function zeros( len ) {
135
173
* @returns {number} n'th derivative
136
174
*/
137
175
functionpolycotpi(n,x,xc){
138
-
varnextMaxColumns;
139
-
varmaxCosOrder;
140
-
varmaxColumns;
141
-
varnextOffset;
142
176
varpowTerms;
143
-
varcosOrder;
144
-
varsinOrder;
145
-
varcolumn;
146
-
varoffset;
147
-
varindex;
148
177
varidx;
149
178
varout;
150
179
varsum;
151
180
varc;
152
-
vari;
153
181
vars;
154
182
155
183
s=(abs(x)<abs(xc)) ? sinpi(x) : sinpi(xc);
@@ -180,34 +208,18 @@ function polycotpi( n, x, xc ) {
180
208
case12:
181
209
returnPI12*c*polyval12(c*c)/pow(s,13.0);
182
210
}
183
-
// We'll have to compute the coefficients up to n, complexity is O(n^2) which we don't worry about as the values are computed once and then cached. However, if the final evaluation would have too many terms just bail out right away:
184
-
if(n/2>MAX_SERIES_ITERATIONS){
185
-
debug('The value of n is so large that we\'re unable to compute the result in reasonable time.');
211
+
// We'll have to compute the coefficients up to `n`, complexity is O(n^2) which we don't worry about as the values are computed once and then cached. However, if the final evaluation would have too many terms just bail out right away:
212
+
if(n/2>MAX_SERIES_ITERATIONS){
213
+
debug('The value of `n` is so large that we\'re unable to compute the result in reasonable time.');
186
214
returnNaN;
187
215
}
188
-
index=n-1;
189
-
if(index>=table.length){
190
-
for(i=table.length-1;i<index;++i){
191
-
offset=(i&1)|0;// 1 if the first cos power is 0, otherwise 0.
192
-
sinOrder=(i+2)|0;// Order of the sin term
193
-
maxCosOrder=(sinOrder-1)|0;// Largest order of the polynomial of cos terms
194
-
maxColumns=((maxCosOrder-offset)/2)|0;// How many entries there are in the current row.
195
-
nextOffset=offset ? 0 : 1;
196
-
nextMaxColumns=((maxCosOrder+1-nextOffset)/2)|0;// How many entries there will be in the next row
197
-
table.push(zeros(nextMaxColumns+1));
198
-
for(column=0;column<=maxColumns;++column){
199
-
cosOrder=((2*column)+offset)|0;// Order of the cosine term in entry "column"
0 commit comments