Remove "small" "trailing" coefficients from a polynomial. "Small" means "small in absolute value" and is controlled by the parameter `tol`; "trailing" means highest order coefficient(s), e.g., in ``[0, 1, 1, 0, 0]`` (which represents ``0 + x + x**2 + 0*x**3 + 0*x**4``) both the
(c, tol=0)
| 158 | |
| 159 | |
| 160 | def trimcoef(c, tol=0): |
| 161 | """ |
| 162 | Remove "small" "trailing" coefficients from a polynomial. |
| 163 | |
| 164 | "Small" means "small in absolute value" and is controlled by the |
| 165 | parameter `tol`; "trailing" means highest order coefficient(s), e.g., in |
| 166 | ``[0, 1, 1, 0, 0]`` (which represents ``0 + x + x**2 + 0*x**3 + 0*x**4``) |
| 167 | both the 3-rd and 4-th order coefficients would be "trimmed." |
| 168 | |
| 169 | Parameters |
| 170 | ---------- |
| 171 | c : array_like |
| 172 | 1-d array of coefficients, ordered from lowest order to highest. |
| 173 | tol : number, optional |
| 174 | Trailing (i.e., highest order) elements with absolute value less |
| 175 | than or equal to `tol` (default value is zero) are removed. |
| 176 | |
| 177 | Returns |
| 178 | ------- |
| 179 | trimmed : ndarray |
| 180 | 1-d array with trailing zeros removed. If the resulting series |
| 181 | would be empty, a series containing a single zero is returned. |
| 182 | |
| 183 | Raises |
| 184 | ------ |
| 185 | ValueError |
| 186 | If `tol` < 0 |
| 187 | |
| 188 | See Also |
| 189 | -------- |
| 190 | trimseq |
| 191 | |
| 192 | Examples |
| 193 | -------- |
| 194 | >>> from numpy.polynomial import polyutils as pu |
| 195 | >>> pu.trimcoef((0,0,3,0,5,0,0)) |
| 196 | array([0., 0., 3., 0., 5.]) |
| 197 | >>> pu.trimcoef((0,0,1e-3,0,1e-5,0,0),1e-3) # item == tol is trimmed |
| 198 | array([0.]) |
| 199 | >>> i = complex(0,1) # works for complex |
| 200 | >>> pu.trimcoef((3e-4,1e-3*(1-i),5e-4,2e-5*(1+i)), 1e-3) |
| 201 | array([0.0003+0.j , 0.001 -0.001j]) |
| 202 | |
| 203 | """ |
| 204 | if tol < 0: |
| 205 | raise ValueError("tol must be non-negative") |
| 206 | |
| 207 | [c] = as_series([c]) |
| 208 | [ind] = np.nonzero(np.abs(c) > tol) |
| 209 | if len(ind) == 0: |
| 210 | return c[:1]*0 |
| 211 | else: |
| 212 | return c[:ind[-1] + 1].copy() |
| 213 | |
| 214 | def getdomain(x): |
| 215 | """ |