logsumexp.cpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #include "logsumexp.hpp"
27 namespace casadi {
28 
30  set_dep(A);
32  }
33 
34  std::string LogSumExp::disp(const std::vector<std::string>& arg) const {
35  return "logsumexp(" + arg.at(0) + ")";
36  }
37 
38  void LogSumExp::eval_mx(const std::vector<MX>& arg, std::vector<MX>& res,
39  const std::vector<bool>& unique) const {
40  res[0] = logsumexp(arg[0]);
41  }
42 
43  void LogSumExp::ad_forward(const std::vector<std::vector<MX> >& fseed,
44  std::vector<std::vector<MX> >& fsens) const {
45  MX max = mmax(dep(0));
46  MX expmm = exp(dep(0)-max);
47  MX s = sum1(expmm);
48  for (casadi_int d=0; d<fsens.size(); ++d) {
49  MX v = project(fseed[d][0], dep().sparsity());
50  fsens[d][0] = dot(v, expmm)/s;
51  }
52  }
53 
54  void LogSumExp::ad_reverse(const std::vector<std::vector<MX> >& aseed,
55  std::vector<std::vector<MX> >& asens) const {
56  MX max = mmax(dep(0));
57  MX expmm = exp(dep(0)-max);
58  MX s = sum1(expmm);
59  for (casadi_int d=0; d<aseed.size(); ++d) {
60  asens[d][0] += expmm*aseed[d][0]/s;
61  }
62  }
63 
64  int LogSumExp::eval(const double** arg, double** res, casadi_int* iw, double* w) const {
65  return eval_gen<double>(arg, res, iw, w);
66  }
67 
68  template<typename T>
69  int LogSumExp::eval_gen(const T** arg, T** res, casadi_int* iw, T* w) const {
70  res[0][0] = casadi_logsumexp(arg[0], dep(0).nnz());
71  return 0;
72  }
73 
75  const std::vector<casadi_int>& arg,
76  const std::vector<casadi_int>& res,
77  const std::vector<bool>& arg_is_ref,
78  std::vector<bool>& res_is_ref) const {
79  // Perform operation inplace
80  g << g.workel(res[0]) << " = "
81  << g.logsumexp(g.work(arg[0], dep(0).nnz(), arg_is_ref[0]), dep(0).nnz())
82  << "\n";
83  }
84 
85 } // namespace casadi
Helper class for C code generation.
std::string logsumexp(const std::string &A, casadi_int n)
std::string work(casadi_int n, casadi_int sz, bool is_ref) const
std::string workel(casadi_int n) const
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
Definition: logsumexp.cpp:34
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res, const std::vector< bool > &unique={}) const override
Evaluate symbolically (MX)
Definition: logsumexp.cpp:38
int eval_gen(const T **arg, T **res, casadi_int *iw, T *w) const
Evaluate the function (template)
Definition: logsumexp.cpp:69
LogSumExp(const MX &A)
Constructor.
Definition: logsumexp.cpp:29
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
Definition: logsumexp.cpp:43
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
Definition: logsumexp.cpp:54
void generate(CodeGenerator &g, const std::vector< casadi_int > &arg, const std::vector< casadi_int > &res, const std::vector< bool > &arg_is_ref, std::vector< bool > &res_is_ref) const override
Generate code for the operation.
Definition: logsumexp.cpp:74
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
Definition: logsumexp.cpp:64
const Sparsity & sparsity() const
Get the sparsity.
Definition: mx_node.hpp:410
casadi_int nnz(casadi_int i=0) const
Definition: mx_node.hpp:427
const MX & dep(casadi_int ind=0) const
dependencies - functions that have to be evaluated before this one
Definition: mx_node.hpp:392
void set_sparsity(const Sparsity &sparsity)
Set the sparsity.
Definition: mx_node.cpp:224
void set_dep(const MX &dep)
Set unary dependency.
Definition: mx_node.cpp:228
MX - Matrix expression.
Definition: mx.hpp:92
static Sparsity dense(casadi_int nrow, casadi_int ncol=1)
Create a dense rectangular sparsity pattern *.
Definition: sparsity.cpp:1028
The casadi namespace.
Definition: archiver.cpp:28
T1 casadi_logsumexp(const T1 *x, casadi_int n)
T dot(const std::vector< T > &a, const std::vector< T > &b)