smbinterp/interp/tools.py

89 lines
1.9 KiB
Python
Raw Normal View History

import os
2010-10-23 12:49:15 -07:00
import inspect
import numpy as np
import logging
log = logging.getLogger("interp")
2009-12-27 09:48:27 -08:00
def rms(errors):
"""
root mean square calculation
"""
2011-05-26 13:40:02 -07:00
# 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
return np.sqrt((errors**2).mean())
2011-03-23 23:26:27 -07:00
def baker_exact_2D(X):
"""
the exact function (2D) used from baker's article (for testing, slightly
modified)
"""
x ,y = X
# TODO: this is not baker's function!! this is:
# np.power(np.sin(x*np.pi/2.0) * np.sin(y*np.pi/2.0),2)
2011-03-22 13:20:48 -07:00
answer = np.power((np.sin(x * np.pi) * np.cos(y * np.pi)), 2)
log.debug(answer)
return answer
2011-03-23 23:26:27 -07:00
def friendly_exact_2D(X):
2011-03-22 13:20:48 -07:00
"""
A friendlier 2D func
"""
x ,y = X
answer = 1.0 + x*x + y*y
log.debug(answer)
return answer
2011-03-23 23:26:27 -07:00
def baker_exact_3D(X):
"""
the exact function (3D) used from baker's article (for testing)
"""
x = X[0]
y = X[1]
z = X[2]
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)
log.debug(answer)
return answer
2011-03-23 23:26:27 -07:00
def friendly_exact_3D(X):
2011-03-02 22:44:08 -08:00
x,y,z = X
return 1 + x*x + y*y + z*z
def scipy_exact_2D(X):
x,y = X
return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
def improved_answer(answer, exact):
if not answer['error']:
2011-05-02 20:56:41 -07:00
# was probably just a linear interpolation
return False
2011-03-22 13:20:48 -07:00
2010-10-29 11:42:05 -07:00
log.debug('qlin: %s' % answer['qlin'])
log.debug('error: %s' % answer['error'])
log.debug('final: %s' % answer['final'])
log.debug('exact: %s' % exact)
if np.abs(answer['final'] - exact) <= np.abs(answer['qlin'] - exact):
2010-10-29 11:42:05 -07:00
log.debug(":) improved result")
return True
else:
2010-10-29 11:42:05 -07:00
log.debug(":( damaged result")
return False
2011-05-20 10:13:17 -07:00
2011-05-18 11:57:09 -07:00
def improved(qlin, err, final, exact):
if np.abs(final - exact) <= np.abs(qlin - exact):
return True
else:
return False
def percent_improvement(answer, exact):
2011-05-17 20:07:23 -07:00
return np.abs(answer['error']) / exact