ajdmom.poly

Class Poly, a customized dictionary, derived from UserDict

I defined a new class Poly to extend the UserDict class from the Python Standard Library collections, within which I defined an attribute keyfor and the following magic methods:

Magic Method

Example

Function

__add__()

poly1 + poly2

Addition between two polynomials

__sub__()

poly1 - poly2

Subtraction between two polynomials

__mul__()

poly1 * poly2

Multiplication between two polynomials

__rmul__()

c * poly2

Reversely multiply by a constant

__pow__()

poly ** 2

Raise a polynomial to power n

It should be noted that:

  • Addition and subtraction are only for polynomials having the same keyfor attribute.

  • The original poly or polys remain unchanged after these operations, all of which return a new poly.

Besides, I defined following methods:

Method

Function

merge()

Merge with another poly having the same keyfor attribute

add_keyval()

Insert a new key-val or add val to the existing key

remove_zero()

Remove any item having value 0

set_keyfor()

Set the keyfor attribute for the poly

mul_poly()

Multiply by another poly of different type and return a new one

is_exact_type()

Check whether the supplied argument is a Poly with the same keyfor

write_to_txt()

Write the poly to a txt file with the keyfor as the title line

write_to_csv()

Write the poly to a csv file with the keyfor as the title line

const_one()

Constant equivalent to 1 in real numbers

const_zero()

Constant equivalent to 0 in real numbers

Note that merge() (in-place) is different from the magic method __add__() (not-in-place). The former changes the original poly, while the latter does not.

Classes

Poly([dict])

Class for different versions of "Polynomial"

Functions

kv(i, key)

get key[i] where key is a tuple