Wednesday, October 28, 2015

Notes on Harmonic Mean, Geometric Mean, Complex Numbers

GmeanHmean

A Rate Problem that can be solved with Harmonic Mean

Leslie drives X miles due north at 15 mph, then drives X miles due west at 30 mph, then drives X miles due south at 45 mph, then drives X miles due east at 50 mph, and arrives back at her starting point. What was Leslie's average speed for the trip?

The total distance driven is 4X. The time taken to drive the first leg was X/15 hours. Similarly, the other legs took X/30, X/45, and X/50 hours. Therefore,

$Average\ Speed\ =\frac{Total\ Distance}{Total\ Time}=\frac{4X}{\frac{X}{15}+\frac{X}{30}+\frac{X}{45}+\frac{X}{50}}$

The X's in the numerator and denominator cancel.

$Average\ Speed\ =\frac{4}{\frac{1}{15}+\frac{1}{30}+\frac{1}{45}+\frac{1}{50}}$

In [1]:
4/(1/15+1/30+1/45+1/50)
Out[1]:
28.125

This is the harmonic mean of the four speeds. The same result could be obtained with the hmean() function.

In [2]:
from scipy.stats.mstats import *
hmean([15,30,45,50])
Out[2]:
28.125


A Problem that uses Geometric Mean

Leslie has staked out a rectangular plot of land measuring 6 meters by 15 meters for a garden. Then she decides to change the shape of her garden to be a square, but she would like it to have the identical area as the original rectangular garden. What should be the side length of the new square-shaped garden?

The original garden had an area of 6x15 square meters.

In [3]:
6*15
Out[3]:
90

A square with this area would have side length sqrt(90)

In [4]:
import math
math.sqrt(90)
Out[4]:
9.486832980505138

This is the geometric mean of the two lengths. The same result could be obtained with the gmean() function.

In [5]:
gmean([6,15])
Out[5]:
9.4868329805051381


A few calculations with Complex Numbers

In [6]:
a=2+3j
In [7]:
a.real
Out[7]:
2.0
In [8]:
a.imag
Out[8]:
3.0
In [9]:
b=-4+5j
In [10]:
a+b
Out[10]:
(-2+8j)
In [11]:
a-b
Out[11]:
(6-2j)
In [12]:
a*b
Out[12]:
(-23-2j)
In [13]:
a/b
Out[13]:
(0.17073170731707318-0.5365853658536586j)

For Complex Numbers, use cmath functions

In [14]:
import cmath
In [15]:
cmath.exp(-3-1j)
Out[15]:
(0.02690006784157161-0.041894373450204546j)
In [16]:
cmath.sqrt(-3)
Out[16]:
1.7320508075688772j