2010-04-29 22:29:35 -07:00
|
|
|
import numpy as np
|
2010-04-24 17:26:44 -07:00
|
|
|
|
|
|
|
|
2009-12-27 09:48:27 -08:00
|
|
|
def rms(errors):
|
2011-09-17 14:38:49 -07:00
|
|
|
"""
|
|
|
|
root mean square calculation
|
|
|
|
"""
|
|
|
|
|
|
|
|
# slow pure python way for reference:
|
|
|
|
# r = 0.0
|
|
|
|
# for i in errors:
|
|
|
|
# r += np.power(i, 2)
|
|
|
|
# r = np.sqrt(r / len(errors))
|
|
|
|
# return r
|
2011-05-26 13:40:02 -07:00
|
|
|
|
2011-09-17 14:38:49 -07:00
|
|
|
return np.sqrt((errors ** 2).mean())
|
2011-05-26 13:40:02 -07:00
|
|
|
|
2010-01-30 11:15:00 -08:00
|
|
|
|
2011-03-23 23:26:27 -07:00
|
|
|
def baker_exact_2D(X):
|
2011-09-17 14:38:49 -07:00
|
|
|
"""
|
|
|
|
the exact function (2D) used from baker's article (for testing,
|
|
|
|
slightly modified)
|
|
|
|
"""
|
|
|
|
x, y = X
|
|
|
|
|
|
|
|
answer = np.power((np.sin(x * np.pi) * np.cos(y * np.pi)), 2)
|
|
|
|
return answer
|
2011-05-29 12:22:44 -07:00
|
|
|
|
2011-03-22 13:20:48 -07:00
|
|
|
|
2011-03-23 23:26:27 -07:00
|
|
|
def friendly_exact_2D(X):
|
2011-09-17 14:38:49 -07:00
|
|
|
"""
|
|
|
|
A friendlier 2D func
|
|
|
|
"""
|
|
|
|
x, y = X
|
|
|
|
answer = 1.0 + x * x + y * y
|
|
|
|
return answer
|
|
|
|
|
2010-03-18 22:18:59 -07:00
|
|
|
|
2011-03-23 23:26:27 -07:00
|
|
|
def baker_exact_3D(X):
|
2011-09-17 14:38:49 -07:00
|
|
|
"""
|
|
|
|
the exact function (3D) used from baker's article (for testing)
|
|
|
|
"""
|
|
|
|
x, y, z = X
|
|
|
|
answer = np.power((np.sin(x * np.pi / 2.0) * np.sin(y * np.pi / 2.0) *
|
|
|
|
np.sin(z * np.pi / 2.0)), 2)
|
|
|
|
return answer
|
|
|
|
|
2011-09-27 14:23:42 -07:00
|
|
|
|
|
|
|
def exact_me(points, f):
|
|
|
|
a = np.array([f(i) for i in points])
|
2011-09-17 14:39:01 -07:00
|
|
|
return a
|
|
|
|
|
2010-04-23 08:58:30 -07:00
|
|
|
|
2011-03-23 23:26:27 -07:00
|
|
|
def friendly_exact_3D(X):
|
2011-09-17 14:38:49 -07:00
|
|
|
x, y, z = X
|
|
|
|
return 1 + x * x + y * y + z * z
|
|
|
|
|
2011-03-02 22:44:08 -08:00
|
|
|
|
2011-05-24 12:45:39 -07:00
|
|
|
def scipy_exact_2D(X):
|
2011-09-17 14:38:49 -07:00
|
|
|
x, y = X
|
|
|
|
return x * (1 - x) * np.cos(4 * np.pi * x) *\
|
|
|
|
np.sin(4 * np.pi * y ** 2) ** 2
|
|
|
|
|
2011-05-24 12:45:39 -07:00
|
|
|
|
2011-03-28 21:11:38 -07:00
|
|
|
def improved_answer(answer, exact):
|
2011-09-17 16:03:07 -07:00
|
|
|
if not answer.error:
|
2011-09-17 14:38:49 -07:00
|
|
|
# was probably just a linear interpolation
|
|
|
|
return False
|
|
|
|
|
2011-09-17 16:03:07 -07:00
|
|
|
if np.abs(answer.final - exact) <= np.abs(answer.qlin - exact):
|
2011-09-17 14:38:49 -07:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2011-09-27 14:23:42 -07:00
|
|
|
|
|
|
|
def identical_points(a, b):
|
2011-09-17 14:39:01 -07:00
|
|
|
return all(set(j[i] for j in a) \
|
|
|
|
== set(j[i] for j in b) for i in xrange(len(a[0])))
|
2011-05-20 10:13:17 -07:00
|
|
|
|
2011-09-27 14:23:42 -07:00
|
|
|
|
2011-05-18 11:57:09 -07:00
|
|
|
def improved(qlin, err, final, exact):
|
2011-09-17 14:38:49 -07:00
|
|
|
if np.abs(final - exact) <= np.abs(qlin - exact):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|