Skip to content

Problem 1: Abstract and Algebra (200pts)

Object-oriented programming (OOP) is the art of managing complexity of hierarchies. In this problem, you will implement a hierarchy of algebraic structures called semirings using OOP.

A semiring is a set of objects supporting two binary operations, typically called "addition" and "multiplication", satisfying the following properties:

  • There is an identity element for addition, called zero. That is, there exists an element 0 such that for any element \( a \), \( a + 0 = a \).
  • There is an identity element for multiplication, called one. That is, there exists an element 1 such that for any element \( a \), \( a * 1 = a \).
  • Each element has an additive inverse (negation). That is, for any element \( a \), there exists an element -a such that \( a + (-a) = 0 \).

Different mathematical objects can form semirings, forming a hierarchy of classes in OOP. Specifically, in this problem, you will implement the following class hierarchy:

面向对象编程(OOP)是管理层次结构复杂性的艺术。 在本问题中,您将使用 OOP 实现一个称为半环的代数结构层次结构。

半环是一组支持两种二元运算(通常称为“加法”和“乘法”)的对象,满足以下性质:

  • 加法有一个单位元,称为零元。 即,存在一个元素 0,使得对于任何元素 \( a \)\( a + 0 = a \)
  • 乘法有一个单位元,称为幺元。 即,存在一个元素 1,使得对于任何元素 \( a \)\( a * 1 = a \)
  • 每个元素都有一个加法逆元(负元)。 即,对于任何元素 \( a \),存在一个元素 -a,使得 \( a + (-a) = 0 \)

不同的数学对象可以形成半环,在 OOP 中形成一个类层次结构。 具体来说,在本问题中,您将实现以下类层次结构:

Semiring
  ├── Matrix
  └── Polynomial
      └── Number

Here, Semiring is the base class providing the interfaces:

在这里,Semiring 是提供接口的基类:

class Semiring:
    """A base class representing objects that can form a semiring."""

    def add(self, other):
        """Returns the sum of self and other."""
        pass

    def mult(self, other):
        """Returns the product of self and other."""
        pass

    def negative(self):
        """Returns the additive inverse of self."""
        pass

    def zero(self):
        """Returns the additive identity element."""
        pass

    def one(self):
        """Returns the multiplicative identity element."""
        pass

In Problem 1.1, you will implement a certain subclass of semiring, the Matrix class representing 3x3 matrices.

In Problem 1.2, you will implement another subclass, the Polynomial class representing univariate polynomials of integer coefficients. Here, although the definition of addition and multiplication for polynomials differs farly from that of matrices, they still follow the same semiring interface. Specifically, you will also implement the Number class representing numbers as a special case of zero-degree polynomials.

Finally in Problem 1.3, you will implement some methods using the Semiring interfaces. These abstract methods should work for any subclass of Semiring, demonstrating the power of abstraction in OOP.

在问题 1.1 中,您将实现半环的一个特定子类,即表示 3x3 矩阵Matrix 类。

在问题 1.2 中,您将实现另一个子类,即表示具有整数系数的单变量多项式Polynomial 类。在这里,尽管多项式的加法和乘法的定义与矩阵的定义有很大不同,但它们仍然遵循相同的半环接口。 具体来说,您还将实现 Number 类,它将数字表示为零次多项式的一个特例。

最后在问题 1.3 中,您将使用 Semiring 接口实现一些方法。这些抽象方法应该适用于 Semiring 的任何子类,展示了 OOP 中抽象的力量。