|
| 1 | +/* |
| 2 | + * Copyright 2021 Huawei Technologies Co., Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <iostream> |
| 18 | +#include <sstream> |
| 19 | + |
| 20 | +#include <alp.hpp> |
| 21 | +#include <graphblas/utils/iscomplex.hpp> // use from grb |
| 22 | +#ifdef DEBUG |
| 23 | +#include "../tests/utils/print_alp_containers.hpp" |
| 24 | +#endif |
| 25 | + |
| 26 | +namespace alp { |
| 27 | + |
| 28 | + namespace algorithms { |
| 29 | + |
| 30 | + /** |
| 31 | + * Given a general matrix H perform an inplace Householder reflections in order to |
| 32 | + * eliminate column elements H[i+d:,i] (below diagonal or subdigonal), H = U H. |
| 33 | + * |
| 34 | + * @tparam D Data element type |
| 35 | + * @tparam Ring Type of the semiring used in the computation |
| 36 | + * @tparam Minus Type minus operator used in the computation |
| 37 | + * @tparam Divide Type of divide operator used in the computation |
| 38 | + * @param[in,out] U updated orthogonal matrix |
| 39 | + * @param[in,out] H updated general matrix with column |
| 40 | + * elements H[i+d:,i] eliminated |
| 41 | + * @param[in] i column to eliminate |
| 42 | + * @param[in] d offset from diagonal to eliminate, default 0 |
| 43 | + * @param[in] ring A semiring for operations |
| 44 | + * @return RC SUCCESS if the execution was correct |
| 45 | + */ |
| 46 | + template< |
| 47 | + typename MatH, |
| 48 | + typename MatU, |
| 49 | + typename IndexType, |
| 50 | + typename D = typename MatH::value_type, |
| 51 | + class Ring = Semiring< operators::add< D >, operators::mul< D >, identities::zero, identities::one >, |
| 52 | + class Minus = operators::subtract< D >, |
| 53 | + class Divide = operators::divide< D >, |
| 54 | + std::enable_if_t< |
| 55 | + structures::is_a< typename MatH::structure, structures::General >::value && |
| 56 | + structures::is_a< typename MatU::structure, structures::Orthogonal >::value && |
| 57 | + is_semiring< Ring >::value && |
| 58 | + is_operator< Minus >::value && |
| 59 | + is_operator< Divide >::value |
| 60 | + > * = nullptr |
| 61 | + > |
| 62 | + RC elminate_below_ith_diag( |
| 63 | + const IndexType i, |
| 64 | + MatH &H, |
| 65 | + MatU &U, |
| 66 | + const IndexType d = 0, |
| 67 | + const Ring &ring = Ring(), |
| 68 | + const Minus &minus = Minus(), |
| 69 | + const Divide ÷ = Divide() |
| 70 | + ) { |
| 71 | + RC rc = SUCCESS; |
| 72 | + |
| 73 | + const Scalar< D > zero( ring.template getZero< D >() ); |
| 74 | + const Scalar< D > one( ring.template getOne< D >() ); |
| 75 | + |
| 76 | + const IndexType m = nrows( H ); |
| 77 | + const IndexType n = ncols( H ); |
| 78 | + |
| 79 | + // v=copy(A0[i+d:,i]) |
| 80 | + auto a = get_view( H, utils::range( i + d, m ), i ); |
| 81 | + Vector< D > v( m - ( i + d ) ); |
| 82 | + rc = rc ? rc : alp::set( v, a ); |
| 83 | + |
| 84 | + // alpha=v[0]/abs(v[0]) |
| 85 | + Scalar< D > alpha( zero ); |
| 86 | + auto v0 = get_view( v, utils::range( 0, 1 ) ); |
| 87 | + rc = rc ? rc : foldl( alpha, v0, ring.getAdditiveMonoid() ); |
| 88 | + rc = rc ? rc : foldl( alpha, Scalar< D >( std::abs( *alpha ) ), divide ); |
| 89 | + |
| 90 | + // alpha=alpha*norm(v) |
| 91 | + Scalar< D > norm_v1( zero ); |
| 92 | + rc = rc ? rc : norm2( norm_v1, v, ring ); |
| 93 | + rc = rc ? rc : foldl( alpha, norm_v1, ring.getMultiplicativeOperator() ); |
| 94 | + |
| 95 | + // v[0]=v[0]-alpha |
| 96 | + rc = rc ? rc : foldl( v0, alpha, minus ); |
| 97 | + |
| 98 | + // v=v/norm(v) |
| 99 | + Scalar< D > norm_v2( zero ); |
| 100 | + rc = rc ? rc : norm2( norm_v2, v, ring ); |
| 101 | + rc = rc ? rc : foldl( v, norm_v2, divide ); |
| 102 | + |
| 103 | + //P1=zeros((m-(i+d),m-(i+d))).astype(complex) |
| 104 | + //P1=P1-2*outer(v,conjugate(v)) |
| 105 | + auto vvh = outer( v, ring.getMultiplicativeOperator() ); |
| 106 | + Matrix< D, typename decltype( vvh )::structure, Dense > reflector( m - ( i + d ) ); |
| 107 | + rc = rc ? rc : alp::set( reflector, vvh ); |
| 108 | + rc = rc ? rc : foldl( reflector, Scalar< D > ( -2 ), ring.getMultiplicativeOperator() ); |
| 109 | + |
| 110 | + // A0=P.dot(A0) |
| 111 | + auto Hupdate = get_view( H, utils::range( i + d, m ), utils::range( 0, n ) ); |
| 112 | + Matrix< D, structures::General, Dense > Temp1( m - ( i + d ) , n ); |
| 113 | + rc = rc ? rc : alp::set( Temp1, Hupdate ); |
| 114 | + rc = rc ? rc : mxm( Hupdate, reflector, Temp1, ring ); |
| 115 | + |
| 116 | + // Uk=Uk.dot(P) |
| 117 | + auto Uupdate = get_view< structures::OrthogonalColumns >( U, utils::range( 0, m ), utils::range( i + d, m ) ); |
| 118 | + Matrix< D, structures::OrthogonalColumns, Dense > Temp2( m, m - ( i + d ) ); |
| 119 | + rc = rc ? rc : alp::set( Temp2, Uupdate ); |
| 120 | + rc = rc ? rc : mxm( Uupdate, Temp2, reflector, ring ); |
| 121 | + |
| 122 | + return rc; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Computes Householder (inplace) bidiagonalisation of general matrix \f$H = U B V \f$ |
| 127 | + * where \a H is general (complex or real), |
| 128 | + * \a U orthogonal, \a B is bidiagonal and \a V orthogonal. |
| 129 | + * |
| 130 | + * @tparam D Data element type |
| 131 | + * @tparam Ring Type of the semiring used in the computation |
| 132 | + * @tparam Minus Type minus operator used in the computation |
| 133 | + * @tparam Divide Type of divide operator used in the computation |
| 134 | + * @param[in,out] U updated orthogonal matrix |
| 135 | + * @param[in,out] V updated orthogonal matrix |
| 136 | + * @param[in,out] H input general matrix, output bidiagonal matrix (B) |
| 137 | + * @param[in] ring A semiring for operations |
| 138 | + * @return RC SUCCESS if the execution was correct |
| 139 | + * |
| 140 | + */ |
| 141 | + template< |
| 142 | + typename MatH, |
| 143 | + typename D = typename MatH::value_type, |
| 144 | + typename MatU, |
| 145 | + typename MatV, |
| 146 | + class Ring = Semiring< operators::add< D >, operators::mul< D >, identities::zero, identities::one >, |
| 147 | + class Minus = operators::subtract< D >, |
| 148 | + class Divide = operators::divide< D >, |
| 149 | + std::enable_if_t< |
| 150 | + is_matrix< MatH >::value && |
| 151 | + is_matrix< MatU >::value && |
| 152 | + is_matrix< MatV >::value && |
| 153 | + structures::is_a< typename MatH::structure, structures::General >::value && |
| 154 | + structures::is_a< typename MatU::structure, structures::Orthogonal >::value && |
| 155 | + structures::is_a< typename MatV::structure, structures::Orthogonal >::value && |
| 156 | + is_semiring< Ring >::value && |
| 157 | + is_operator< Minus >::value && |
| 158 | + is_operator< Divide >::value |
| 159 | + > * = nullptr |
| 160 | + > |
| 161 | + RC householder_bidiag( |
| 162 | + MatU &U, |
| 163 | + MatH &H, |
| 164 | + MatV &V, |
| 165 | + const Ring &ring = Ring(), |
| 166 | + const Minus &minus = Minus(), |
| 167 | + const Divide ÷ = Divide() |
| 168 | + ) { |
| 169 | + RC rc = SUCCESS; |
| 170 | + |
| 171 | + const Scalar< D > zero( ring.template getZero< D >() ); |
| 172 | + const Scalar< D > one( ring.template getOne< D >() ); |
| 173 | + |
| 174 | + const size_t m = nrows( H ); |
| 175 | + const size_t n = ncols( H ); |
| 176 | + |
| 177 | + // check sizes |
| 178 | + if( |
| 179 | + ( ncols( U ) != nrows( H ) ) || |
| 180 | + ( ncols( H ) != nrows( V ) ) |
| 181 | + ) { |
| 182 | + std::cerr << "Incompatible sizes in householder_bidiag.\n"; |
| 183 | + return FAILED; |
| 184 | + } |
| 185 | + |
| 186 | + //for i in range(min(n,m)): |
| 187 | + for( size_t i = 0; i < std::min( n, m ); ++i ) { |
| 188 | + // eliminate column elements below ith diagonal element |
| 189 | + if( i < std::min( n, m - 1 ) ) { |
| 190 | + rc = rc ? rc : elminate_below_ith_diag( i, H, U, static_cast< size_t >( 0 ), ring, minus, divide ); |
| 191 | + } |
| 192 | + // eliminate row elements to the right from (i+1)th diagonal element |
| 193 | + if( i < std::min( n - 2, m ) ) { |
| 194 | + auto HT = get_view< alp::view::transpose >( H ); |
| 195 | + auto VT = get_view< alp::view::transpose >( V ); |
| 196 | + rc = rc ? rc : elminate_below_ith_diag( i, HT, VT, static_cast< size_t >( 1 ), ring, minus, divide ); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + return rc; |
| 201 | + |
| 202 | + } |
| 203 | + } // namespace algorithms |
| 204 | +} // namespace alp |
0 commit comments