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)
| 142 | |
| 143 | |
| 144 | def trimcoef(c, tol=0): |
| 145 | """ |
| 146 | Remove "small" "trailing" coefficients from a polynomial. |
| 147 | |
| 148 | "Small" means "small in absolute value" and is controlled by the |
| 149 | parameter `tol`; "trailing" means highest order coefficient(s), e.g., in |
| 150 | ``[0, 1, 1, 0, 0]`` (which represents ``0 + x + x**2 + 0*x**3 + 0*x**4``) |
| 151 | both the 3-rd and 4-th order coefficients would be "trimmed." |
| 152 | |
| 153 | Parameters |
| 154 | ---------- |
| 155 | c : array_like |
| 156 | 1-d array of coefficients, ordered from lowest order to highest. |
| 157 | tol : number, optional |
| 158 | Trailing (i.e., highest order) elements with absolute value less |
| 159 | than or equal to `tol` (default value is zero) are removed. |
| 160 | |
| 161 | Returns |
| 162 | ------- |
| 163 | trimmed : ndarray |
| 164 | 1-d array with trailing zeros removed. If the resulting series |
| 165 | would be empty, a series containing a single zero is returned. |
| 166 | |
| 167 | Raises |
| 168 | ------ |
| 169 | ValueError |
| 170 | If `tol` < 0 |
| 171 | |
| 172 | Examples |
| 173 | -------- |
| 174 | >>> from numpy.polynomial import polyutils as pu |
| 175 | >>> pu.trimcoef((0,0,3,0,5,0,0)) |
| 176 | array([0., 0., 3., 0., 5.]) |
| 177 | >>> pu.trimcoef((0,0,1e-3,0,1e-5,0,0),1e-3) # item == tol is trimmed |
| 178 | array([0.]) |
| 179 | >>> i = complex(0,1) # works for complex |
| 180 | >>> pu.trimcoef((3e-4,1e-3*(1-i),5e-4,2e-5*(1+i)), 1e-3) |
| 181 | array([0.0003+0.j , 0.001 -0.001j]) |
| 182 | |
| 183 | """ |
| 184 | if tol < 0: |
| 185 | raise ValueError("tol must be non-negative") |
| 186 | |
| 187 | [c] = as_series([c]) |
| 188 | [ind] = np.nonzero(np.abs(c) > tol) |
| 189 | if len(ind) == 0: |
| 190 | return c[:1] * 0 |
| 191 | else: |
| 192 | return c[:ind[-1] + 1].copy() |
| 193 | |
| 194 | def getdomain(x): |
| 195 | """ |