Ejemplo n.º 1
0
def test_cholesky_inplace_inplace_F_order(test_mat):
    """test cholesky_inplace works F ordering """
    A = test_mat.A.copy()
    chol1 = test_mat.chol.copy()

    #Test doing cholesky in place with cholesky_inplace
    test_chol_inplace1 = A.copy('F')
    cholesky_inplace(test_chol_inplace1,inplace=True,lower=True)

    atol_loc3 = np.max(np.abs(chol1))*atol_rel_use

    assert check_is_cholesky(test_chol_inplace1,A,lower=True,atol_rel=atol_rel_use,rtol=rtol_use)
    assert np.allclose(chol1,test_chol_inplace1,atol=atol_loc3,rtol=rtol_use)
    test_chol_inplace1 = None
Ejemplo n.º 2
0
 def project_covar(self, v):
     r"""get the variance  (v.T).C_lw.v where v=\frac{\partial\bar{\delta}}{\delta_\alpha} in the given geometry"""
     result = np.zeros((v.shape[1], v.shape[1]))
     itr_ll = 0
     for ll in range(0, self.n_l):
         n_k = self.C_compact[ll].shape[0]
         res = cholesky_inplace(self.C_compact[ll],
                                inplace=False,
                                lower=True,
                                clean=False)
         n_break = n_k
         for _m_itr in range(0, self.lm_map[ll, 2].size):
             rhs = spl.blas.dtrmm(1.,
                                  res,
                                  v[itr_ll:itr_ll + n_break],
                                  trans_a=True,
                                  lower=True)
             result = spl.blas.dsyrk(1.,
                                     rhs,
                                     1.,
                                     result,
                                     lower=True,
                                     trans=True)
             itr_ll += n_k
         res = None
     return result
Ejemplo n.º 3
0
    def get_cov_cholesky_inv(self,
                             inplace=False,
                             copy_output=False,
                             internal=False):
        """get inverse of lower triangular cholesky decomposition of covariance
                inputs:
                    inplace: if True, will mutate _internal_mat to be the inverse cholesky decompostion of the covariance matrix
                    copy_output: whether to copy the output matrix, to be safe from mutating _internal_mat later
                    internal: should be True only if being called from another FisherMatrix routine, do not need to clean upper triangle"""

        #if _internal_mat changes must change internal state
        if inplace and not internal:
            self.switch_rep(REP_CHOL_INV)

        if self.internal_state == REP_CHOL_INV:
            if not self.silent:
                print("FisherMatrix ", id(self),
                      " cholesky decomposition inv retrieved from cache")
            result = self._internal_mat
        else:
            if not self.silent:
                print("FisherMatrix ", id(self),
                      " cholesky decomposition inv cache miss")
            if self.internal_state == REP_CHOL:
                result = invert_triangular(self._internal_mat,
                                           lower=True,
                                           inplace=inplace,
                                           clean=False)
            elif self.internal_state == REP_FISHER:
                result = np.asfortranarray(np.rot90(self._internal_mat, 2))
                result = np.rot90(
                    cholesky_inplace(result,
                                     inplace=inplace,
                                     lower=False,
                                     clean=False), 2)
                result = np.asfortranarray(result)
            elif self.internal_state == REP_COVAR:
                result = get_inv_cholesky(self._internal_mat,
                                          lower=True,
                                          inplace=inplace,
                                          clean=False)
            else:
                raise ValueError("FisherMatrix " + str(id(self)) +
                                 ": unrecognized internal state " +
                                 str(self.internal_state))

        if inplace:
            self._internal_mat = result
            self.internal_state = REP_CHOL_INV

        if not internal:
            result = clean_triangle(result, lower=True, inplace=True)

        if copy_output:
            return result.copy()
        else:
            return result
Ejemplo n.º 4
0
 def get_cov_cholesky_array(self):
     """get cholesky decomposition of covariance matrix as an array"""
     result = np.zeros((self.C_id.shape[0], self.C_id.shape[0]), order='F')
     itr_ll = 0
     for ll in range(0, self.n_l):
         n_k = self.C_compact[ll].shape[0]
         res = cholesky_inplace(self.C_compact[ll],
                                inplace=False,
                                lower=True)
         for _m_itr in range(0, self.lm_map[ll, 2].size):
             result[itr_ll:itr_ll + n_k, itr_ll:itr_ll + n_k] = res
             itr_ll += n_k
     return result
Ejemplo n.º 5
0
def test_cholesky_inplace_not_inplace_C_order(test_mat):
    """test cholesky_inplace works C ordering """
    A = test_mat.A.copy()
    chol1 = test_mat.chol.copy()
    #Test doing cholesky not in place with C ordering
    test_chol_inplace2 = A.copy('C')
    test_chol_inplace2_res = cholesky_inplace(test_chol_inplace2,inplace=False,lower=True)

    atol_loc1 = np.max(np.abs(A))*atol_rel_use
    atol_loc3 = np.max(np.abs(chol1))*atol_rel_use

    assert check_is_cholesky(test_chol_inplace2_res,A,lower=True,atol_rel=atol_rel_use,rtol=rtol_use)
    assert np.allclose(chol1,test_chol_inplace2_res,atol=atol_loc3,rtol=rtol_use)
    assert np.allclose(test_chol_inplace2,A,atol=atol_loc1,rtol=rtol_use)
Ejemplo n.º 6
0
def test_cholesky_inplace_inplace_C_order_nonfatal(test_mat):
    """test cholesky_inplace works C ordering with error recovery"""
    A = test_mat.A.copy()
    chol1 = test_mat.chol.copy()
    #Test doing cholesky in place with C ordering (should cause warning unless also F ordering)
    test_chol_inplace4 = A.copy('C')

    atol_loc1 = np.max(np.abs(A))*atol_rel_use
    atol_loc3 = np.max(np.abs(chol1))*atol_rel_use

    with warnings.catch_warnings(record=True) as w:
        if not test_chol_inplace4.flags['F']:
            warnings.simplefilter("always")
            test_chol_inplace4_res = cholesky_inplace(test_chol_inplace4,inplace=True,fatal_errors=False)
            #check if it caused the warning
            assert len(w)==1
            assert issubclass(w[-1].category,RuntimeWarning)
            assert check_is_cholesky(test_chol_inplace4_res,A,atol_rel=atol_rel_use,rtol=rtol_use,lower=True)
            assert np.allclose(chol1,test_chol_inplace4_res,atol=atol_loc3,rtol=rtol_use)
            assert np.allclose(test_chol_inplace4,A,atol=atol_loc1,rtol=rtol_use)
    test_chol_inplace4 = None
    test_chol_inplace4_res = None
Ejemplo n.º 7
0
    def perturb_and_project_covar(self, perturb_v, project_v, perturb_sigma2s):
        r"""get the variance  (v.T).C_lw.v where v=\frac{\partial\bar{\delta}}{\delta_\alpha} in the given geometry"""
        perturb_v = np.ascontiguousarray(perturb_v.T)
        denom_result = np.zeros((perturb_v.shape[1], perturb_v.shape[1]),
                                order='F')
        covar_result = np.zeros((project_v.shape[1], project_v.shape[1]),
                                order='F')
        itr_ll = 0
        for ll in range(0, self.n_l):
            n_k = self.C_compact[ll].shape[0]
            res = cholesky_inplace(self.C_compact[ll],
                                   inplace=False,
                                   lower=True,
                                   clean=False)
            n_break = n_k
            for _m_itr in range(0, self.lm_map[ll, 2].size):
                rhs1 = spl.blas.dtrmm(1.,
                                      res,
                                      perturb_v[itr_ll:itr_ll + n_break],
                                      trans_a=True,
                                      lower=True)
                rhs2 = spl.blas.dtrmm(1.,
                                      res,
                                      project_v[itr_ll:itr_ll + n_break],
                                      trans_a=True,
                                      lower=True)
                denom_result = spl.blas.dsyrk(1.,
                                              rhs1,
                                              1.,
                                              denom_result,
                                              lower=True,
                                              trans=True)
                covar_result = spl.blas.dsyrk(1.,
                                              rhs2,
                                              1.,
                                              covar_result,
                                              lower=True,
                                              trans=True)
                rhs1 = None
                rhs2 = None
                itr_ll += n_k
            res = None

        mult_mat = np.asfortranarray(np.diag(
            1. / perturb_sigma2s)) + denom_result
        denom_result = None
        mult_mat_chol_inv = get_cholesky_inv(mult_mat,
                                             lower=True,
                                             inplace=False,
                                             clean=True)
        mult_mat = None
        pert_in = spl.blas.dtrmm(1.,
                                 mult_mat_chol_inv,
                                 perturb_v.T,
                                 side=False,
                                 lower=True,
                                 trans_a=True)
        mult_mat_chol_inv = None

        proj_out = np.zeros_like(project_v, order='F')
        itr_ll = 0
        for ll in range(0, self.n_l):
            n_k = self.C_compact[ll].shape[0]
            res = self.C_compact[ll]
            n_break = n_k
            for _m_itr in range(0, self.lm_map[ll, 2].size):
                proj_out[itr_ll:itr_ll + n_break] = spl.blas.dsymm(
                    1., res, project_v[itr_ll:itr_ll + n_break], lower=True)
                itr_ll += n_k
            res = None
        rhs_req = np.asfortranarray(np.dot(pert_in, proj_out))
        mit_result = spl.blas.dsyrk(
            -1., rhs_req, 1., covar_result, lower=True,
            trans=True)  #covar_result-np.dot(rhs_req.T,rhs_req)
        return covar_result, mit_result
Ejemplo n.º 8
0
 def __init__(self, fisher_in):
     """fisher_in: the fisher matrix"""
     self.fab = fisher_in
     self.cov = ch_inv(self.fab, cholesky_given=False, lower=True)
     self.chol_cov = cholesky_inplace(self.cov, inplace=False, lower=True)
     self.chol_cov_i = invert_triangular(self.chol_cov, lower=True)
Ejemplo n.º 9
0
 def __init__(self, cov_in):
     """cov_in: the covariance matrix"""
     self.cov = cov_in
     self.chol_cov = cholesky_inplace(self.cov, inplace=False, lower=True)
     self.chol_cov_i = invert_triangular(self.chol_cov, lower=True)
     self.fab = ch_inv(self.cov, cholesky_given=False)
Ejemplo n.º 10
0
    def get_cov_cholesky(self,
                         inplace=False,
                         copy_output=False,
                         internal=False):
        """get lower triangular cholesky decomposition of covariance
                inputs:
                    inplace: if True, will mutate _internal_mat to be the cholesky decompostion of the covariance matrix
                    copy_output: whether to copy the output matrix, to be safe from mutating _internal_mat later
                    internal: should be True only if being called from another FisherMatrix routine, do not need to clean upper triangle"""

        #if _internal_mat changes must change internal state
        if inplace and not internal:
            self.switch_rep(REP_CHOL)

        if self.internal_state == REP_CHOL:
            if not self.silent:
                print(
                    "FisherMatrix ",
                    str(id(self)) + ": cholesky stored, size: " +
                    str(self._internal_mat.nbytes / 10**6) + " megabytes")
            result = self._internal_mat
        else:
            if self.internal_state == REP_CHOL_INV:
                if not self.silent:
                    print(
                        "FisherMatrix " + str(id(self)),
                        ": getting cholesky of covariance from its inverse, size: "
                        + str(self._internal_mat.nbytes / 10**6) + " mb")
                result = invert_triangular(self._internal_mat,
                                           lower=True,
                                           inplace=inplace,
                                           clean=False)
            elif self.internal_state == REP_COVAR:
                if not self.silent:
                    print(
                        "FisherMatrix " + str(id(self)),
                        ": getting cholesky from covariance directly: " +
                        str(self._internal_mat.nbytes / 10**6) + " mb")
                result = cholesky_inplace(self._internal_mat,
                                          inplace=inplace,
                                          lower=True,
                                          clean=False)
            elif self.internal_state == REP_FISHER:
                if not self.silent:
                    print(
                        "FisherMatrix " + str(id(self)),
                        ": getting cholesky of covariance from fisher, size: "
                        + str(self._internal_mat.nbytes / 10**6) + " mb")
                result = get_cholesky_inv(self._internal_mat,
                                          lower=True,
                                          inplace=inplace,
                                          clean=False)
            else:
                raise ValueError("FisherMatrix " + str(id(self)) +
                                 ": unrecognized internal state " +
                                 str(self.internal_state))

            if not self.silent:
                print(
                    "FisherMatrix " + str(id(self)),
                    ": found cholesky decomposition of covariance matrix, size: "
                    + str(result.nbytes / 10**6) + " megabytes")

        if inplace:
            self._internal_mat = result
            self.internal_state = REP_CHOL

        #if not being requested internally to FisherMatrix, make sure the upper triangle is all zeros
        if not internal:
            result = clean_triangle(result, lower=True, inplace=True)

        if copy_output:
            return result.copy()
        else:
            return result
Ejemplo n.º 11
0
    def contract_covar(self,
                       v1,
                       v2,
                       identical_inputs=False,
                       return_fisher=False,
                       destructive=False):
        """calculates (v1.T).covariance.v2 for getting variance/projecting to another basis
            inputs:
                v1,v2: vectors with one dimension aligned with _internal_mat
                identical_inputs: if True, assume v2=v1 and ignore v2 completely
                return_fisher: if True, return a FisherMatrix object
                destructive: if True, destroy the internal representation of self for a performance gain
        """
        if not self.silent:
            print("FisherMatrix " + str(id(self)) + ": contracting covariance")
        if return_fisher and not identical_inputs:
            raise ValueError(
                'cannot get FisherMatrix object if inputs not identical')

        if self.internal_state == REP_COVAR:
            if identical_inputs:
                right_res = spl.blas.dsymm(1.,
                                           self._internal_mat,
                                           v1,
                                           lower=True)
            else:
                right_res = spl.blas.dsymm(1.,
                                           self._internal_mat,
                                           v2,
                                           lower=True)

            if destructive:
                self._internal_mat = None
            result = np.dot(v1.T, right_res)

            right_res = None
            if identical_inputs and not return_fisher:
                result = mirror_symmetrize(result, lower=True, inplace=True)
        else:
            if self.internal_state == REP_FISHER:
                chol_fisher = cholesky_inplace(self._internal_mat,
                                               lower=True,
                                               inplace=destructive,
                                               clean=False)
                if destructive:
                    self._internal_mat = None
                res1 = spl.solve_triangular(chol_fisher,
                                            v1,
                                            lower=True,
                                            check_finite=DEBUG)
                if not identical_inputs:
                    res2 = spl.solve_triangular(chol_fisher,
                                                v2,
                                                lower=True,
                                                check_finite=DEBUG)
                chol_fisher = None
            elif self.internal_state == REP_CHOL_INV:
                res1 = spl.solve_triangular(self._internal_mat,
                                            v1,
                                            lower=True,
                                            check_finite=DEBUG,
                                            trans=True)
                if not identical_inputs:
                    res2 = spl.solve_triangular(self._internal_mat,
                                                v2,
                                                lower=True,
                                                check_finite=DEBUG,
                                                trans=True)
            elif self.internal_state == REP_CHOL:
                res1 = spl.blas.dtrmm(1.,
                                      self._internal_mat,
                                      v1,
                                      lower=True,
                                      trans_a=True)
                if not identical_inputs:
                    res2 = spl.blas.dtrmm(1.,
                                          self._internal_mat,
                                          v2,
                                          lower=True,
                                          trans_a=True)
            else:
                raise ValueError('unknown internal state ' +
                                 str(self.internal_state))

            if destructive:
                self._internal_mat = None

            if identical_inputs:
                result = spl.blas.dsyrk(1.,
                                        np.asfortranarray(res1),
                                        lower=True,
                                        trans=True,
                                        overwrite_c=True)
                res1 = None
                if not return_fisher:
                    result = mirror_symmetrize(result,
                                               lower=True,
                                               inplace=True)
            else:
                result = np.dot(res1.T, res2)
                res1 = None
                res2 = None
        if return_fisher:
            return FisherMatrix(result,
                                input_type=REP_COVAR,
                                initial_state=REP_COVAR,
                                fix_input=False,
                                silent=self.silent)
        else:
            return result