| 60 | |
| 61 | template <typename T, int Dim> |
| 62 | MatrixT<T, Dim+1, 1> VectorizedLogSumExp(const MatrixT<T, Dynamic, Dim> &LinearExponents, |
| 63 | const MatrixT<T, Dynamic, 1> &Scaling) |
| 64 | { |
| 65 | /** [linear term Dim x 1; nonlinear term 1 x 1] */ |
| 66 | MatrixT<T, Dim+1, 1> LSE; |
| 67 | |
| 68 | /** compute squared errors */ |
| 69 | const MatrixT<T, Dynamic, 1> Exponents = -0.5 * LinearExponents.rowwise().squaredNorm(); |
| 70 | const MatrixT<T, Dynamic, 1> SquaredError = Exponents + Scaling.array().log().matrix(); |
| 71 | |
| 72 | /** find maximum probability (minimum of squared error) */ |
| 73 | Index MaxIndex; |
| 74 | SquaredError.maxCoeff(&MaxIndex); |
| 75 | |
| 76 | /** pass max directly */ |
| 77 | LSE.template head<Dim>() = LinearExponents.row(MaxIndex); |
| 78 | |
| 79 | /** apply log sum exp for the other components */ |
| 80 | LSE(Dim) = ScaledLogSumExp<T>(Exponents.array() - Exponents(MaxIndex), Scaling.array()); |
| 81 | |
| 82 | return LSE; |
| 83 | } |
| 84 | |
| 85 | } |
| 86 | |