Simple function classes

Challenge: functions with parameters

Representing a function as a class

The self variable

Another function class example

Alternative function class implementations

Making classes without the class construct

Closures

More examples on classes

Bank accounts

Phone book

A circle

Special methods

The call special method

Example: Automagic differentiation

Example: Automagic integration

Turning an instance into a string

Example: Phone book with special methods

Adding objects

Example: Class for polynomials

Arithmetic operations and other special methods

Special methods for string conversion

Example: Class for vectors in the plane

Some mathematical operations on vectors

Implementation

Usage

Example: Class for complex numbers

Implementation

Illegal operations

Mixing complex and real numbers

Dynamic, static, strong, weak, and duck typing

Special methods for "right" operands

Inspecting instances

Static methods and attributes

Summary

Chapter topics

Example: interval arithmetic

Exercises

Exercise 1: Make a function class

Exercise 2: Add a data attribute to a class

Exercise 3: Add functionality to a class

Exercise 4: Make classes for a rectangle and a triangle

Exercise 5: Make a class for quadratic functions

Exercise 6: Make a class for straight lines

Exercise 7: Flexible handling of function arguments

Exercise 8: Wrap functions in a class

Exercise 9: Flexible handling of function arguments

Exercise 10: Deduce a class implementation

Exercise 11: Implement special methods in a class

Exercise 12: Make a class for summation of series

Exercise 13: Apply a numerical differentiation class

Exercise 14: Implement an addition operator

Exercise 15: Implement in-place `+=`

and `-=`

operators

Exercise 16: Implement a class for numerical differentiation

Exercise 17: Examine a program

Exercise 18: Modify a class for numerical differentiation

Exercise 19: Make a class for the Heaviside function

Exercise 20: Make a class for the indicator function

Exercise 21: Make a class for piecewise constant functions

Exercise 22: Speed up repeated integral calculations

Exercise 23: Apply a class for polynomials

Exercise 24: Find a bug in a class for polynomials

Exercise 25: Implement subtraction of polynomials

Exercise 26: Test the functionality of pretty print of polynomials

Exercise 27: Vectorize a class for polynomials

Exercise 28: Use a dict to hold polynomial coefficients

Exercise 29: Extend class Vec2D to work with lists/tuples

Exercise 30: Extend class Vec2D to 3D vectors

Exercise 31: Use NumPy arrays in class Vec2D

Exercise 32: Impreciseness of interval arithmetics

Exercise 33: Make classes for students and courses

Exercise 34: Find local and global extrema of a function

Exercise 35: Find the optimal production for a company

References

A class packs a set of data (variables) together with a set of functions operating on the data. The goal is to achieve more modular code by grouping data and functions into manageable (often small) units. Most of the mathematical computations in this document can easily be coded without using classes, but in many problems, classes enable either more elegant solutions or code that is easier to extend at a later stage. In the non-mathematical world, where there are no mathematical concepts and associated algorithms to help structure the problem solving, software development can be very challenging. Classes may then improve the understanding of the problem and contribute to simplify the modeling of data and actions in programs. As a consequence, almost all large software systems being developed in the world today are heavily based on classes.

Programming with classes is offered by most modern programming languages, also Python. In fact, Python employs classes to a very large extent, but one can use the language for lots of purposes without knowing what a class is. However, one will frequently encounter the class concept when searching books or the World Wide Web for Python programming information. And more important, classes often provide better solutions to programming problems. This document therefore gives an introduction to the class concept with emphasis on applications to numerical computing. More advanced use of classes, including inheritance and object orientation, is treated in the document Object-oriented programming [1].

The folder `src/class` contains all the program examples from
the present document.