Writing code to read or write text files can be tricky because the exact contents of a file cannot be viewed in a text editor if the file contains non-printable characters such as line feeds or carriage returns. This simple utility program will take a filename as a command line argument and print out its exact contents, including descriptions of any non-printable or whitespace characters.

Continue reading# Complex Numbers in Python – An Introduction

The Python language provides comprehensive support for complex numbers, with basic functionality being included with the core language and more advanced features included in the cmath module.

This article will demonstrate the basic operations such as arithmetic which does not require any additional modules. I will also go a bit further than that by writing code to carry out these operations without using the Python operators; of course you wouldn't do this in production code but it does provide an insight into how these operations and complex numbers in general work.

Continue reading# Plotting Taylor Series

Previous posts have included an SVG library, memoization of factorials and Taylor Polynomials. In this post I will bring these all together to plot various sine waves created using Taylor Polynomials.

Taylor Polynomials are used to approximate functions, in this case sine, to any level of accuracy. We can plot these levels to show how they become increasingly accurate, which is the subject of this post.

Continue reading# Logarithmic Plots

The majority of data can easily be plotted on a graph with equal intervals on the axes, for example 1, 2, 3 or 100, 200, 300 etc.. Some data, typically that which increases or decreases exponentially, cannot comfortably be graphed on such a scale without squashing the data up so much at one end that it becomes incomprehensible. The solution to this problem is to use a logarithmic scale.

Continue reading# Calculating Sines with Taylor Polynomials

In this post I will write a Python function to calculate sines using Taylor polynomials. It is impossible to calculate these directly but they can be approximated to any accuracy using this method, and I will show how a Taylor series converges to ever-increasing accuracy.

Continue reading# Normal Distribution

One of the most useful bits of number-crunching you can do is to calculate the probability distribution of a set of data in the earnest hope that it will be a reasonable fit for one of the recognised distributions such as the normal distribution. In this project I will write a Python class to calculate the normal distribution for a given data set.

Continue reading# Trigonometric Memoization

I recently posted an article on memoization of factorials and mentioned that another possible use for memoization was with trigonometric values. In this article I will do just that, but this project is significantly more complex than the previous one for factorials.

Continue reading# Memoization of Factorials

I am currently working on an article about calculating sines and cosines using Taylor Polynomials. These make heavy use of factorials so I started thinking about ways to streamline the process.

This post consists of a simple project using memoization with a lookup table to pre-calculate factorials and store them for future use.

Continue reading# Logarithms: A Practical Use

This is a simple Python project demonstrating a useful application of logarithms. The logarithm of a number to a specified base is the power to which the base must be raised to get the number. Since their invention by John Napier in the 17th century until a few decades ago slide rules and books of log tables were used to simplify multiplication by turning it into a process of addition. Modern science, technology and engineering all depended on that simple idea.

Continue reading# Creating SVG Drawings

In this post I will develop a simple Python class to create and save an SVG (Scalable Vector Graphics) file, complete with a few examples of its use.

Continue reading