mercredi 6 novembre 2019

Can We Define Every or Most Mathematical Concepts Using Object Oriented Design?

Mathematics has a lot of concepts and definitions of concepts. Such as "Set" is a basic mathematical concept, and it is defined by the set class in most programming languages. While those mathematical concepts have properties and we can derive other properties from those basic properties. I start to wonder if it's possible to define all or most mathematical concepts using Object-Oriented Programming? So that we can eventually translate a math textbook into an Object-Oriented program. For Example, this is an OOP representation of several definitions from Measure Theory:

class Event:
    def __init__(self, probability, name):
        self.probability = probability
        self.name = name


    def __repr__(self):
        return f"{self.name} has probability {self.probability}"


    def __setattr__(self, key, value):
        if key == 'probability' and value >1:
            raise Exception('Probability must be smaller than 1')
        super().__setattr__(key, value)

class Events(set):
    def add(self, *args):
        for arg in args:
            if not isinstance(arg,Event):
                raise TypeError('You should add Event')
        else:
            super(Events, self).add(*args)

    def probability(self):
        result = 0
        for event in self:
            result += event.probability
        return result


class Space(Events):
    def __init__(self, seq=()):
        total_probability = 0
        for arg in seq:
            total_probability += arg.probability
        if total_probability != 1:
            raise Exception('This space is not complete')
        else:
            super(Space, self).__init__(seq)

Aucun commentaire:

Enregistrer un commentaire