1083 | | """ |
1084 | | Return the p-norm of this vector, where p can be a real number |
1085 | | `\geq 1`, Infinity, or a symbolic expression. If `p=2` (default), |
1086 | | this is the usual Euclidean norm; if p=Infinity, this is the |
1087 | | maximum norm; if `p=1`, this is the taxicab (Manhattan) norm. |
1088 | | |
1089 | | .. SEEALSO:: |
1090 | | |
1091 | | - :func:`sage.misc.functional.norm` |
1092 | | |
| 1083 | r""" |
| 1084 | Return the `p`-norm of ``self``. |
| 1085 | |
| 1086 | INPUT: |
| 1087 | |
| 1088 | - ``p`` - defalt: 2 - ``p`` can be a real number greater than 1, |
| 1089 | infinity (``oo`` or ``Infinity``), or a symbolic expression. |
| 1090 | |
| 1091 | - `p=1`: the taxicab (Manhattan) norm |
| 1092 | - `p=2`: the usual Euclidean norm (the default) |
| 1093 | - `p=\infty`: the maximum entry (in absolute value) |
| 1094 | |
| 1095 | .. note:: |
| 1096 | |
| 1097 | See also :func:`sage.misc.functional.norm` |
| 1098 | |
| 1130 | |
| 1131 | Notice that the result may be a symbolic expression, owing to |
| 1132 | the necessity of taking a square root (in the default case). |
| 1133 | These results can be converted to numerical values if needed. :: |
| 1134 | |
| 1135 | sage: v = vector(ZZ, [3,4]) |
| 1136 | sage: nrm = v.norm(); nrm |
| 1137 | 5 |
| 1138 | sage: nrm.parent() |
| 1139 | Rational Field |
| 1140 | |
| 1141 | sage: v = vector(QQ, [3, 5]) |
| 1142 | sage: nrm = v.norm(); nrm |
| 1143 | sqrt(34) |
| 1144 | sage: nrm.parent() |
| 1145 | Symbolic Ring |
| 1146 | sage: numeric = N(nrm); numeric |
| 1147 | 5.83095189484... |
| 1148 | sage: numeric.parent() |
| 1149 | Real Field with 53 bits of precision |
| 1150 | |
| 1151 | TESTS: |
| 1152 | |
| 1153 | The value of ``p`` must be greater than, or |
| 1154 | equal to, one. :: |
| 1155 | |
| 1156 | sage: v = vector(QQ, [1,2]) |
| 1157 | sage: v.norm(0.99) |
| 1158 | Traceback (most recent call last): |
| 1159 | ... |
| 1160 | ValueError: 0.990000 is not greater than or equal to 1 |
1667 | | |
1668 | | |
1669 | | - ``right`` - vector of the same degree as self. It |
1670 | | need not be in the same vector space as self, as long as the |
1671 | | coefficients can be multiplied. |
1672 | | |
1673 | | |
| 1707 | |
| 1708 | - ``right`` - a vector of the same degree as ``self``. |
| 1709 | It does not need to belong to the same parent as ``self``, |
| 1710 | so long as the necessary products and sums are defined. |
| 1711 | |
| 1712 | OUTPUT: |
| 1713 | |
| 1714 | If ``self`` and ``right`` are the vectors `\vec{x}` and `\vec{y}`, |
| 1715 | of degree `n`, then this method returns |
| 1716 | |
| 1717 | .. math:: |
| 1718 | |
| 1719 | \sum_{i=1}^{n}x_iy_i |
| 1720 | |
| 1721 | .. note:: |
| 1722 | |
| 1723 | The :meth:`inner_product` is a more general version of |
| 1724 | this method, and the :meth:`hermitian_inner_product` |
| 1725 | method may be more appropriate if your vectors |
| 1726 | have complex entries. |
| 1727 | |
1681 | | |
1682 | | :: |
1683 | | |
1684 | | sage: W = VectorSpace(GF(3),3) |
1685 | | sage: w = W([0,1,2]) |
1686 | | sage: w.dot_product(v) |
| 1735 | |
| 1736 | The vectors may be from different vector spaces, |
| 1737 | provided the necessary operations make sense. |
| 1738 | Notice that coercion will generate a result of |
| 1739 | the same type, even if the order of the |
| 1740 | arguments is reversed.:: |
| 1741 | |
| 1742 | sage: v = vector(ZZ, [1,2,3]) |
| 1743 | sage: w = vector(FiniteField(3), [0,1,2]) |
| 1744 | sage: ip = w.dot_product(v); ip |
| 1751 | sage: ip.parent() |
| 1752 | Finite Field of size 3 |
| 1753 | |
| 1754 | The dot product of a vector with itself is the 2-norm, squared. :: |
| 1755 | |
| 1756 | sage: v = vector(QQ, [3, 4, 7]) |
| 1757 | sage: v.dot_product(v) - v.norm()^2 |
| 1758 | 0 |
| 1759 | |
| 1760 | TESTS: |
| 1761 | |
| 1762 | The second argument must be a free module element. :: |
| 1763 | |
| 1764 | sage: v = vector(QQ, [1,2]) |
| 1765 | sage: v.dot_product('junk') |
| 1766 | Traceback (most recent call last): |
| 1767 | ... |
| 1768 | TypeError: right must be a free module element |
| 1769 | |
| 1770 | The degrees of the arguments must match. :: |
| 1771 | |
| 1772 | sage: v = vector(QQ, [1,2]) |
| 1773 | sage: w = vector(QQ, [1,2,3]) |
| 1774 | sage: v.dot_product(w) |
| 1775 | Traceback (most recent call last): |
| 1776 | ... |
| 1777 | ArithmeticError: degrees (2 and 3) must be the same |
2094 | | """ |
2095 | | Returns the inner product of self and other, with respect to the |
2096 | | inner product defined on the parent of self. |
2097 | | |
| 2174 | r""" |
| 2175 | Returns the inner product of ``self`` and ``right``, |
| 2176 | possibly using an inner product matrix from the parent of ``self``. |
| 2177 | |
| 2178 | INPUT: |
| 2179 | |
| 2180 | - ``right`` - a vector of the same degree as ``self`` |
| 2181 | |
| 2182 | OUTPUT: |
| 2183 | |
| 2184 | If the parent vector space does not have an inner product |
| 2185 | matrix defined, then this is the usual dot product |
| 2186 | (:meth:`dot_product`). If ``self`` and ``right`` are |
| 2187 | considered as single column matrices, `\vec{x}` and `\vec{y}`, |
| 2188 | and `A` is the inner product matrix, then this method computes |
| 2189 | |
| 2190 | .. math:: |
| 2191 | |
| 2192 | \left(\vec{x}\right)^tA\vec{y} |
| 2193 | |
| 2194 | where `t` indicates the transpose. |
| 2195 | |
| 2196 | .. note:: |
| 2197 | |
| 2198 | If your vectors have complex entries, the |
| 2199 | :meth:`hermitian_inner_product` may be more |
| 2200 | appropriate for your purposes. |
| 2201 | |
2099 | | |
2100 | | sage: I = matrix(ZZ,3,[2,0,-1,0,2,0,-1,0,6]) |
2101 | | sage: M = FreeModule(ZZ, 3, inner_product_matrix = I) |
2102 | | sage: (M.0).inner_product(M.0) |
| 2203 | |
| 2204 | sage: v = vector(QQ, [1,2,3]) |
| 2205 | sage: w = vector(QQ, [-1,2,-3]) |
| 2206 | sage: v.inner_product(w) |
| 2207 | -6 |
| 2208 | sage: v.inner_product(w) == v.dot_product(w) |
| 2209 | True |
| 2210 | |
| 2211 | The vector space or free module that is the parent to |
| 2212 | ``self`` can have an inner product matrix defined, which |
| 2213 | will be used by this method. This matrix will be passed |
| 2214 | through to subspaces. :: |
| 2215 | |
| 2216 | sage: ipm = matrix(ZZ,[[2,0,-1], [0,2,0], [-1,0,6]]) |
| 2217 | sage: M = FreeModule(ZZ, 3, inner_product_matrix = ipm) |
| 2218 | sage: v = M([1,0,0]) |
| 2219 | sage: v.inner_product(v) |
| 2224 | sage: w = M([1,3,-1]) |
| 2225 | sage: v = M([2,-4,5]) |
| 2226 | sage: w.row()*ipm*v.column() == w.inner_product(v) |
| 2227 | True |
| 2228 | |
| 2229 | Note that the inner product matrix comes from the parent of ``self``. |
| 2230 | So if a vector is not an element of the correct parent, the result |
| 2231 | could be a source of confusion. :: |
| 2232 | |
| 2233 | sage: V = VectorSpace(QQ, 2, inner_product_matrix=[[1,2],[2,1]]) |
| 2234 | sage: v = V([12, -10]) |
| 2235 | sage: w = vector(QQ, [10,12]) |
| 2236 | sage: v.inner_product(w) |
| 2237 | 88 |
| 2238 | sage: w.inner_product(v) |
| 2239 | 0 |
| 2240 | sage: w = V(w) |
| 2241 | sage: w.inner_product(v) |
| 2242 | 88 |
| 2243 | |
| 2244 | .. note:: |
| 2245 | |
| 2246 | The use of an inner product matrix makes no restrictions on |
| 2247 | the nature of the matrix. In particular, in this context it |
| 2248 | need not be symmetric and positive-definite. |
| 2249 | |
| 2250 | TESTS: |
| 2251 | |
| 2252 | Most error handling occurs in the :meth:`dot_product` method. |
| 2253 | But with an inner product defined, this method will check |
| 2254 | that the input is a vector or free module element. :: |
| 2255 | |
| 2256 | sage: W = VectorSpace(RDF, 2, inner_product_matrix = matrix(RDF, 2, [1.0,2.0,3.0,4.0])) |
| 2257 | sage: v = W([2.0, 4.0]) |
| 2258 | sage: v.inner_product(5) |
| 2259 | Traceback (most recent call last): |
| 2260 | ... |
| 2261 | TypeError: right must be a free module element |
| 2262 | |