MCPcopy
hub / github.com/numpy/numpy / polysub

Function polysub

numpy/lib/_polynomial_impl.py:867–920  ·  view source on GitHub ↗

Difference (subtraction) of two polynomials. .. note:: This forms part of the old polynomial API. Since version 1.4, the new polynomial API defined in `numpy.polynomial` is preferred. A summary of the differences can be found in the :doc:`transition guide </refe

(a1, a2)

Source from the content-addressed store, hash-verified

865
866@array_function_dispatch(_binary_op_dispatcher)
867def polysub(a1, a2):
868 """
869 Difference (subtraction) of two polynomials.
870
871 .. note::
872 This forms part of the old polynomial API. Since version 1.4, the
873 new polynomial API defined in `numpy.polynomial` is preferred.
874 A summary of the differences can be found in the
875 :doc:`transition guide </reference/routines.polynomials>`.
876
877 Given two polynomials `a1` and `a2`, returns ``a1 - a2``.
878 `a1` and `a2` can be either array_like sequences of the polynomials&#x27;
879 coefficients (including coefficients equal to zero), or `poly1d` objects.
880
881 Parameters
882 ----------
883 a1, a2 : array_like or poly1d
884 Minuend and subtrahend polynomials, respectively.
885
886 Returns
887 -------
888 out : ndarray or poly1d
889 Array or `poly1d` object of the difference polynomial&#x27;s coefficients.
890
891 See Also
892 --------
893 polyval, polydiv, polymul, polyadd
894
895 Examples
896 --------
897
898 .. math:: (2 x^2 + 10 x - 2) - (3 x^2 + 10 x -4) = (-x^2 + 2)
899
900 >>> import numpy as np
901
902 >>> np.polysub([2, 10, -2], [3, 10, -4])
903 array([-1, 0, 2])
904
905 """
906 truepoly = (isinstance(a1, poly1d) or isinstance(a2, poly1d))
907 a1 = atleast_1d(a1)
908 a2 = atleast_1d(a2)
909 diff = len(a2) - len(a1)
910 if diff == 0:
911 val = a1 - a2
912 elif diff > 0:
913 zr = NX.zeros(diff, a1.dtype)
914 val = NX.concatenate((zr, a1)) - a2
915 else:
916 zr = NX.zeros(abs(diff), a2.dtype)
917 val = a1 - NX.concatenate((zr, a2))
918 if truepoly:
919 val = poly1d(val)
920 return val
921
922
923@array_function_dispatch(_binary_op_dispatcher)

Callers 2

__sub__Method · 0.70
__rsub__Method · 0.70

Calls 2

atleast_1dFunction · 0.90
poly1dClass · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…