|
489 | 489 | \begin{itemize}%
|
490 | 490 | \item \dunder{eq} implements the functionality of~\pythonilIdx{==}, as we already discussed before.%
|
491 | 491 | %
|
492 |
| -\item \dunder{ne} implements the functionality of~\pythonilIdx{!=}.% |
| 492 | +\item \dunder{ne} implements the functionality of~\pythonil{!=}\pythonIdx{"!=}.% |
493 | 493 | %
|
494 | 494 | \item \dunder{lt} implements the functionality of~\pythonilIdx{<}.%
|
495 | 495 | %
|
|
503 | 503 | Implementing equality and inequality is rather easy, since our fractions are all normalized.
|
504 | 504 | For two fractions~\pythonil{x} and~\pythonil{y}, it holds only that~\pythonil{x == y} if \pythonil{x.a == y.a} and \pythonil{x.b == y.b}.
|
505 | 505 | \dunder{eq} is thus quickly implemented.
|
506 |
| -\dunder{ne} is its complement for the \pythonilIdx{!=}~operator. |
| 506 | +\dunder{ne} is its complement for the \pythonil{!=}\pythonIdx{"!=}~operator. |
507 | 507 | \pythonil{x != y} is \pythonil{True} if either \pythonil{x.a != y.a} or \pythonil{x.b != y.b}.
|
508 | 508 |
|
509 | 509 | The other four comparison methods can be implemented by remembering how we used the common \pgls{denominator} for addition and subtraction.
|
|
1557 | 1557 | .3 \dunder{rfloordiv}\DTcomment{right-integer-divide: \pythonil{a // b}\pythonIdx{//}~$\cong$~\pythonil{b.\_\_rfloordiv\_\_(a)}, if \pythonil{a.\_\_floordiv\_\_(b)} yields~\pythonilIdx{NotImplemented}}.
|
1558 | 1558 | .3 \dunder{pow}\DTcomment{exponential: \pythonil{a ** b}\pythonIdx{**}~$\cong$~\pythonil{a.\_\_pow\_\_(b)}}.
|
1559 | 1559 | .3 \dunder{rpow}\DTcomment{right-exponential: \pythonil{a ** b}\pythonIdx{**}~$\cong$~\pythonil{b.\_\_rpow\_\_(a)}, if \pythonil{a.\_\_pow\_\_(b)} yields~\pythonilIdx{NotImplemented}}.
|
1560 |
| -.3 \dunder{matmul}\DTcomment{matrix-multiply: \pythonil{a @ b}\pythonIdx{@}~$\cong$~\pythonil{a.\_\_matmul\_\_(b)}}. |
1561 |
| -.3 \dunder{rmatmul}\DTcomment{right-ma, see \cref{sec:arithmeticsAndOrder}trix-multiply: \pythonil{a @ b}\pythonIdx{@}~$\cong$~\pythonil{b.\_\_rmatmul\_\_(a)}, if \pythonil{a.\_\_matmul\_\_(b)} yields~\pythonilIdx{NotImplemented}}. |
| 1560 | +.3 \dunder{matmul}\DTcomment{matrix-multiply: \pythonil{a @ b}\pythonIdx{"@}~$\cong$~\pythonil{a.\_\_matmul\_\_(b)}}. |
| 1561 | +.3 \dunder{rmatmul}\DTcomment{right-ma, see \cref{sec:arithmeticsAndOrder}trix-multiply: \pythonil{a @ b}\pythonIdx{"@}~$\cong$~\pythonil{b.\_\_rmatmul\_\_(a)}, if \pythonil{a.\_\_matmul\_\_(b)} yields~\pythonilIdx{NotImplemented}}. |
1562 | 1562 | .3 \dunder{divmod}\DTcomment{compute the result of an integer division as well as the remainder and return them as 2-tuple: \pythonil{divmod(a, b)}\pythonIdx{divmod}~$\cong$~\pythonil{a.\_\_divmod\_\_(b)}}.
|
1563 | 1563 | .3 \dunder{rdivmod}\DTcomment{right-sided \pythonilIdx{divmod}: \pythonil{divmod(a, b)}\pythonIdx{divmod}~$\cong$~\pythonil{b.\_\_divmod\_\_(a)}, if \pythonil{a.\_\_divmod\_\_(b)} yields~\pythonilIdx{NotImplemented}}.
|
1564 | 1564 | .3 \dunder{round}\DTcomment{round: \pythonil{round(a)}\pythonIdx{round}~$\cong$~\pythonil{a.\_\_round\_\_()}~\cite{PEP3141}}.
|
|
1573 | 1573 | .3 \dunder{imod}\DTcomment{in-place modulo division: \pythonil{a \%= b}\pythonIdx{\%=}~$\cong$~\pythonil{a.\_\_imod\_\_(b)}, returns~\pythonilIdx{self}}.
|
1574 | 1574 | .3 \dunder{ifloordiv}\DTcomment{in-place integer division: \pythonil{a //= b}\pythonIdx{//=}~$\cong$~\pythonil{a.\_\_ifloordiv\_\_(b)}, returns~\pythonilIdx{self}}.
|
1575 | 1575 | .3 \dunder{ipow}\DTcomment{in-place exponentiation: \pythonil{a **= b}\pythonIdx{**=}~$\cong$~\pythonil{a.\_\_ipow\_\_(b)}, returns~\pythonilIdx{self}}.
|
1576 |
| -.3 \dunder{imatmul}\DTcomment{in-place matrix multiplication: \pythonil{a @= b}\pythonIdx{@=}~$\cong$~\pythonil{a.\_\_imatmul\_\_(b)}, returns~\pythonilIdx{self}}. |
| 1576 | +.3 \dunder{imatmul}\DTcomment{in-place matrix multiplication: \pythonil{a @= b}\pythonIdx{"@=}~$\cong$~\pythonil{a.\_\_imatmul\_\_(b)}, returns~\pythonilIdx{self}}. |
1577 | 1577 | }%
|
1578 | 1578 | %
|
1579 | 1579 | \caption{An overview of the dunder\pythonIdx{dunder} methods in \python\ (Part~2).}%
|
|
1655 | 1655 |
|
1656 | 1656 | While we implemented several arithmetic dunder methods for our \pythonil{Fractions} class in \cref{sec:arithmeticsAndOrder}, \cref{fig:pythonDunder:2} shows us that there are \emph{many} more.
|
1657 | 1657 | The unary methods \dunder{abs}, \dunder{minus}, and \dunder{pos} allow us to implement the computation of the absolute value and the negated variant of a number, as well as the rather obscure unary plus.
|
1658 |
| -There are dunder methods for basically all binary arithmetic operators, ranging from \pythonilIdx{+}, \pythonilIdx{-}, \pythonilIdx{*}, \pythonilIdx{/}, \pythonilIdx{//}, \pythonilIdx{\%}, to~\pythonilIdx{**} and even the matrix multiplication operator~\pythonilIdx{@}. |
| 1658 | +There are dunder methods for basically all binary arithmetic operators, ranging from \pythonilIdx{+}, \pythonilIdx{-}, \pythonilIdx{*}, \pythonilIdx{/}, \pythonilIdx{//}, \pythonilIdx{\%}, to~\pythonilIdx{**} and even the matrix multiplication operator~\pythonil{@}\pythonIdx{"@}. |
1659 | 1659 | Interestingly, these methods always exist in two variants, the plain one and one prefixed with~\inQuotes{\pythonil{r}.}
|
1660 | 1660 | For example, when evaluating the expression~\pythonil{a + b}, \python\ will look whether \pythonil{a}~implements \dunder{add}.
|
1661 | 1661 | If so, it will call \pythonil{a.\_\_add\_\_(b)}.
|
|
0 commit comments