389
389
return x*(x - 1)*(x - 2)
392
return x**4 + x**2 + 1
391
394
# Test exceptions
392
395
assert_raises(ValueError, herme.hermefit, [1], [1], -1)
393
396
assert_raises(TypeError, herme.hermefit, [[1]], [1], 0)
397
400
assert_raises(TypeError, herme.hermefit, [1], [1, 2], 0)
398
401
assert_raises(TypeError, herme.hermefit, [1], [1], 0, w=[[1]])
399
402
assert_raises(TypeError, herme.hermefit, [1], [1], 0, w=[1, 1])
403
assert_raises(ValueError, herme.hermefit, [1], [1], [-1,])
404
assert_raises(ValueError, herme.hermefit, [1], [1], [2, -1, 6])
405
assert_raises(TypeError, herme.hermefit, [1], [1], [])
402
408
x = np.linspace(0, 2)
405
411
coef3 = herme.hermefit(x, y, 3)
406
412
assert_equal(len(coef3), 4)
407
413
assert_almost_equal(herme.hermeval(x, coef3), y)
414
coef3 = herme.hermefit(x, y, [0, 1, 2, 3])
415
assert_equal(len(coef3), 4)
416
assert_almost_equal(herme.hermeval(x, coef3), y)
409
418
coef4 = herme.hermefit(x, y, 4)
410
419
assert_equal(len(coef4), 5)
411
420
assert_almost_equal(herme.hermeval(x, coef4), y)
421
coef4 = herme.hermefit(x, y, [0, 1, 2, 3, 4])
422
assert_equal(len(coef4), 5)
423
assert_almost_equal(herme.hermeval(x, coef4), y)
424
# check things still work if deg is not in strict increasing
425
coef4 = herme.hermefit(x, y, [2, 3, 4, 1, 0])
426
assert_equal(len(coef4), 5)
427
assert_almost_equal(herme.hermeval(x, coef4), y)
413
429
coef2d = herme.hermefit(x, np.array([y, y]).T, 3)
414
430
assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
431
coef2d = herme.hermefit(x, np.array([y, y]).T, [0, 1, 2, 3])
432
assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
416
434
w = np.zeros_like(x)
420
438
wcoef3 = herme.hermefit(x, yw, 3, w=w)
421
439
assert_almost_equal(wcoef3, coef3)
440
wcoef3 = herme.hermefit(x, yw, [0, 1, 2, 3], w=w)
441
assert_almost_equal(wcoef3, coef3)
423
443
wcoef2d = herme.hermefit(x, np.array([yw, yw]).T, 3, w=w)
424
444
assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
445
wcoef2d = herme.hermefit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w)
446
assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
425
447
# test scaling with complex values x points whose square
426
448
# is zero when summed.
427
449
x = [1, 1j, -1, -1j]
428
450
assert_almost_equal(herme.hermefit(x, x, 1), [0, 1])
451
assert_almost_equal(herme.hermefit(x, x, [0, 1]), [0, 1])
452
# test fitting only even Legendre polynomials
453
x = np.linspace(-1, 1)
455
coef1 = herme.hermefit(x, y, 4)
456
assert_almost_equal(herme.hermeval(x, coef1), y)
457
coef2 = herme.hermefit(x, y, [0, 2, 4])
458
assert_almost_equal(herme.hermeval(x, coef2), y)
459
assert_almost_equal(coef1, coef2)
431
462
class TestCompanion(TestCase):