summaryrefslogtreecommitdiff log msg author committer range
blob: a388f4e031d62036c67b94c35b6f6804800a6452 (plain)
 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 ``` ``````/* Attempts to find a minimax polynomial of degree n for f via Remez algorithm. The Remez algorithm appears to be slightly shot, but the initial step of approximating by Chebyshev nodes works, and is usually good enough. */ { chebyshev_node(a, b, k, n) = my(p, m, c); p = (b + a)/2; m = (b - a)/2; c = cos(Pi * (2*k - 1)/(2*n)); return(p + m*c); } { remez_step(f, n, a, b, x) = my(M, xx, bvec, k); M = matrix(n + 2, n + 2); bvec = vector(n + 2); for (k = 1, n + 2, xx = x[k]; for (j = 1, n + 1, M[k,j] = xx^(j - 1); ); M[k, n + 2] = (-1)^k; bvec[k] = f(xx); ); return(mattranspose(M^-1 * mattranspose(bvec))); } { p_eval(n, v, x) = my(s, k); s = 0; for (k = 1, n + 1, s = s + v[k]*x^(k - 1) ); return(s); } { err(f, n, v, x) = return(abs(f(x) - p_eval(n, v, x))); } { find_M(f, n, v, a, b, depth) = my(X, gran, l, lnext, len, xprev, xcur, xnext, yprev, ycur, ynext, thisa, thisb, k, j); gran = 10000 * depth; l = listcreate(); xprev = a - (b - a)/gran; yprev = err(f, n, v, xprev); xcur = a; ycur = err(f, n, v, xprev); xnext = a + (b - a)/gran; ynext = err(f, n, v, xprev); for (k = 2, gran, xprev = xcur; yprev = ycur; xcur = xnext; ycur = ynext; xnext = a + k*(b - a)/gran; ynext = err(f, n, v, xnext); if(ycur > yprev && ycur > ynext, listput(l, [xcur, abs(ycur)]),); ); vecsort(l, 2); if(length(l) < n + 2 || l[1][2] < 2^(-2000), return("q"),); len = length(l); lnext = listcreate(); for(j = 1, n + 2, thisa = l[j][1] - (b-a)/gran; thisb = l[j][1] + (b-a)/gran; xprev = thisa - (thisb - a)/gran; yprev = err(f, n, v, xprev); xcur = thisa; ycur = err(f, n, v, xprev); xnext = thisa + (thisb - thisa)/gran; ynext = err(f, n, v, xprev); for (k = 2, gran, xprev = xcur; yprev = ycur; xcur = xnext; ycur = ynext; xnext = thisa + k*(thisb - thisa)/gran; ynext = abs(f(xnext) - p_eval(n, v, xnext)); if(ycur > yprev && ycur > ynext, listput(lnext, xcur),); ); ); vecsort(lnext, cmp); listkill(l); X = vector(n + 2); for (k = 1, min(n + 2, length(lnext)), X[k] = lnext[k]); listkill(lnext); vecsort(X); return(X); } { find_minimax(f, n, a, b) = my(c, k, j); c = vector(n + 2); for (k = 1, n + 2, c[k] = chebyshev_node(a, b, k, n + 2); ); for(j = 1, 100, v = remez_step(f, n, a, b, c); print("v = ", v); c = find_M(f, n, v, a, b, j); if(c == "q", return,); print("c = ", c); ); } { sinoverx(x) = return(if(x == 0, 1, sin(x)/x)); } { tanoverx(x) = return(if(x == 0, 1, tan(x)/x)); } { atanxoverx(x) = return(if(x == 0, 1, atan(x)/x)); } { cotx(x) = return(1/tanoverx(x)); } print("\n"); print("Minimaxing sin(x) / x, degree 6, on [-Pi/(4 * 256), Pi/(4 * 256)]:"); find_minimax(sinoverx, 6, -Pi/1024, Pi/1024) print("\n"); print("(You'll need to add a 0x0 at the beginning to make a degree 7...\n"); print("\n"); print("---\n"); print("\n"); print("Minimaxing cos(x), degree 7, on [-Pi/(4 * 256), Pi/(4 * 256)]:"); find_minimax(cos, 7, -Pi/1024, Pi/1024) print("\n"); print("---\n"); print("\n"); print("Minmimaxing tan(x) / x, degree 6, on [-Pi/(4 * 256), Pi/(4 * 256)]:"); find_minimax(tanoverx, 6, -Pi/1024, Pi/1024) print("\n"); print("(You'll need to add a 0x0 at the beginning to make a degree 7...\n"); print("\n"); print("---\n"); print("\n"); print("Minmimaxing x*cot(x), degree 8, on [-Pi/(4 * 256), Pi/(4 * 256)]:"); find_minimax(cotx, 8, -Pi/1024, Pi/1024) print("\n"); print("(Take the first v, and remember to divide by x)\n"); print("\n"); print("---\n"); print("\n"); print("Minmimaxing tan(x) / x, degree 10, on [0, 15.5/256]:"); find_minimax(tanoverx, 10, 0, 15.5/256) print("\n"); print("(You'll need to add a 0x0 at the beginning to make a degree 11...\n"); print("\n"); print("---\n"); print("\n"); print("Minmimaxing atan(x) / x, degree 12, on [0, 15.5/256]:"); find_minimax(atanxoverx, 12, 0, 1/16) print("\n"); print("(You'll need to add a 0x0 at the beginning to make a degree 13...\n"); print("\n"); print("---\n"); print("Remember that there's that extra, ugly E term at the end of the vector that you want to lop off.\n"); ``````