# Path: attitude/display/plot/cov_types/regressions.py
# def hyperbola(cov, axes, means, xvals, n=1,level=1):
# 
#     cov1 = dot(axes.T,cov,axes)
#     # Plot hyperbola
#     a = N.sqrt(cov[0]) #hyp_axes[0]
#     b = N.sqrt(cov[-1]) #hyp_axes[-1]
# 
#     #a = N.sqrt(cov[0])#1[0]) #hyp_axes[0]
#     #b = N.sqrt(cov[-1])#1[-1]) #hyp_axes[-1]
#     u = lambda x: N.arcsinh(x/a)
#     y = lambda x: b*N.cosh(u(x))
# 
#     def y(x):
#         return level*b*N.sqrt(x**2/(a**2*n)+1/n)
# 
#     # Top
#     vals = N.array([xvals,y(xvals)]).transpose()
#     t = dot(vals,axes).T+means[:,N.newaxis]
#     # Btm
#     vals = N.array([xvals,-y(xvals)]).transpose()
#     b = dot(vals,axes).T+means[:,N.newaxis]
#     return t[0],b[1],t[1]
# 
# Path: attitude/orientation/test_pca.py
# def test_rotation():
# def test_error_angles():
#     def __vec_angle(i):
# def test_solid_angle():
#         def func(theta):
# def test_recovery_from_axes():
# def test_variance_equivalence():
# def test_data_matrix_multiplication():
# def test_hyperbola_axes():
# def test_pca_regression_variable():
# def test_pca_recovery():
# def test_builtin_recovery():
# def __do_component_planes(fit,component):
# def test_component_planes():
# def test_grouped_plane():
# def test_singular_values():
# def test_normal_errors():
#     MTM = dot(M_bar.transpose(),M_bar)/(n-1)
#     X = fit.arr[:,:2]
# 
# Path: attitude/geom/util.py
# def dot(*matrices):
#     return reduce(N.dot, matrices)
# 
# def augment_tensor(matrix, ndim=None):
#     """
#     Increase the dimensionality of a tensor,
#     splicing it into an identity matrix of a higher
#     dimension. Useful for generalizing
#     transformation matrices.
#     """
#     s = matrix.shape
#     if ndim is None:
#         ndim = s[0]+1
#     arr = N.identity(ndim)
#     arr[:s[0],:s[1]] = matrix
#     return arr
# 
# def plane(normal,offset=0):
#     # This only works in Hessian-Normal form
#     return N.append(normal,offset).view(Plane)
# 
# Path: attitude/error/axes.py
# def sampling_axes(fit, **kw):
#     """
#     Hyperbolic axis lengths based on sample-size
#     normal statistics.
# 
#     Note: legacy method that simply wraps the
#     `statistical_axes` function.
#     """
#     return statistical_axes(fit, method='sampling', **kw)
# 
# Path: attitude/geom/conics.py
# def conic(x):
#     return N.array(x).view(Conic)


import numpy as N
from scipy.stats import chi2
from .plot.cov_types.regressions import hyperbola
from ..orientation.test_pca import random_pca
from ..geom.util import dot, augment_tensor, plane
from ..error.axes import sampling_axes
from ..geom.conics import conic
    cov = sv**2/(n-1)

    xvals = N.linspace(-100,100,100)
    level = chi2.ppf(0.95,n-3)

    # use only direction of maximum angular
    # variation
    cov2d = cov[1:]

    res1 = simple_hyperbola(cov2d,xvals, n, N.sqrt(level))
    res2 = hyperbola(
        cov2d,
        N.identity(2), # rotation
        N.array([0,0]), # mean
        xvals,
        n=n,
        level=N.sqrt(level))
    # Get only top values
    res2 = (res2[0],res2[-1])

    for a,b in zip(res1,res2):
        assert N.allclose(a,b)

    # Scale axes before sending to plotting function
    cov[-1] = level*cov[-1]/n
    res3 = simple_hyperbola(cov[1:],xvals)
    for a,b in zip(res1,res3):
        assert N.allclose(a,b)

def test_dual_conic():

# Path: attitude/display/plot/cov_types/regressions.py
# def hyperbola(cov, axes, means, xvals, n=1,level=1):
# 
#     cov1 = dot(axes.T,cov,axes)
#     # Plot hyperbola
#     a = N.sqrt(cov[0]) #hyp_axes[0]
#     b = N.sqrt(cov[-1]) #hyp_axes[-1]
# 
#     #a = N.sqrt(cov[0])#1[0]) #hyp_axes[0]
#     #b = N.sqrt(cov[-1])#1[-1]) #hyp_axes[-1]
#     u = lambda x: N.arcsinh(x/a)
#     y = lambda x: b*N.cosh(u(x))
# 
#     def y(x):
#         return level*b*N.sqrt(x**2/(a**2*n)+1/n)
# 
#     # Top
#     vals = N.array([xvals,y(xvals)]).transpose()
#     t = dot(vals,axes).T+means[:,N.newaxis]
#     # Btm
#     vals = N.array([xvals,-y(xvals)]).transpose()
#     b = dot(vals,axes).T+means[:,N.newaxis]
#     return t[0],b[1],t[1]
# 
# Path: attitude/orientation/test_pca.py
# def test_rotation():
# def test_error_angles():
#     def __vec_angle(i):
# def test_solid_angle():
#         def func(theta):
# def test_recovery_from_axes():
# def test_variance_equivalence():
# def test_data_matrix_multiplication():
# def test_hyperbola_axes():
# def test_pca_regression_variable():
# def test_pca_recovery():
# def test_builtin_recovery():
# def __do_component_planes(fit,component):
# def test_component_planes():
# def test_grouped_plane():
# def test_singular_values():
# def test_normal_errors():
#     MTM = dot(M_bar.transpose(),M_bar)/(n-1)
#     X = fit.arr[:,:2]
# 
# Path: attitude/geom/util.py
# def dot(*matrices):
#     return reduce(N.dot, matrices)
# 
# def augment_tensor(matrix, ndim=None):
#     """
#     Increase the dimensionality of a tensor,
#     splicing it into an identity matrix of a higher
#     dimension. Useful for generalizing
#     transformation matrices.
#     """
#     s = matrix.shape
#     if ndim is None:
#         ndim = s[0]+1
#     arr = N.identity(ndim)
#     arr[:s[0],:s[1]] = matrix
#     return arr
# 
# def plane(normal,offset=0):
#     # This only works in Hessian-Normal form
#     return N.append(normal,offset).view(Plane)
# 
# Path: attitude/error/axes.py
# def sampling_axes(fit, **kw):
#     """
#     Hyperbolic axis lengths based on sample-size
#     normal statistics.
# 
#     Note: legacy method that simply wraps the
#     `statistical_axes` function.
#     """
#     return statistical_axes(fit, method='sampling', **kw)
# 
# Path: attitude/geom/conics.py
# def conic(x):
#     return N.array(x).view(Conic)


# Path: attitude/display/test_hyperbolic_errors.py
import numpy as N
from scipy.stats import chi2
from .plot.cov_types.regressions import hyperbola
from ..orientation.test_pca import random_pca
from ..geom.util import dot, augment_tensor, plane
from ..error.axes import sampling_axes
from ..geom.conics import conic

    cov = sv**2/(n-1)

    xvals = N.linspace(-100,100,100)
    level = chi2.ppf(0.95,n-3)

    # use only direction of maximum angular
    # variation
    cov2d = cov[1:]

    res1 = simple_hyperbola(cov2d,xvals, n, N.sqrt(level))
    res2 = hyperbola(
        cov2d,
        N.identity(2), # rotation
        N.array([0,0]), # mean
        xvals,
        n=n,
        level=N.sqrt(level))
    # Get only top values
    res2 = (res2[0],res2[-1])

    for a,b in zip(res1,res2):
        assert N.allclose(a,b)

    # Scale axes before sending to plotting function
    cov[-1] = level*cov[-1]/n
    res3 = simple_hyperbola(cov[1:],xvals)
    for a,b in zip(res1,res3):
        assert N.allclose(a,b)

def test_dual_conic():

    fit = random_pca()