mmin.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 "mmin.hpp"
27 
28 namespace casadi {
29 
30  MMin::MMin(const MX& x) {
31  set_dep(x);
33  }
34 
35  std::string MMin::disp(const std::vector<std::string>& arg) const {
36  return "min(" + arg.at(0) + ")";
37  }
38 
39  MMax::MMax(const MX& x) {
40  set_dep(x);
42  }
43 
44  std::string MMax::disp(const std::vector<std::string>& arg) const {
45  return "max(" + arg.at(0) + ")";
46  }
47 
48  int MMin::eval(const double** arg, double** res, casadi_int* iw, double* w) const {
49  if (!res[0]) return 0;
50  res[0][0] = casadi_mmin(arg[0], dep(0).nnz(), dep(0).is_dense());
51  return 0;
52  }
53 
54  int MMax::eval(const double** arg, double** res, casadi_int* iw, double* w) const {
55  if (!res[0]) return 0;
56  res[0][0] = casadi_mmax(arg[0], dep(0).nnz(), dep(0).is_dense());
57 
58  return 0;
59  }
60 
61  int MMin::eval_sx(const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w) const {
62  if (!res[0]) return 0;
63  res[0][0] = casadi_mmin(arg[0], dep(0).nnz(), dep(0).is_dense());
64 
65  return 0;
66  }
67 
68  int MMax::eval_sx(const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w) const {
69  if (!res[0]) return 0;
70  res[0][0] = casadi_mmax(arg[0], dep(0).nnz(), dep(0).is_dense());
71 
72  return 0;
73  }
74 
75  void MMin::eval_mx(const std::vector<MX>& arg, std::vector<MX>& res) const {
76  res[0] = mmin(arg[0]);
77  }
78 
79  void MMax::eval_mx(const std::vector<MX>& arg, std::vector<MX>& res) const {
80  res[0] = mmax(arg[0]);
81  }
82 
84  const std::vector<casadi_int>& arg,
85  const std::vector<casadi_int>& res,
86  const std::vector<bool>& arg_is_ref,
87  std::vector<bool>& res_is_ref) const {
88  g << g.workel(res[0]) << " = "
89  << g.mmin(g.work(arg[0], dep(0).nnz(), arg_is_ref[0]), dep(0).nnz(), dep(0).is_dense())
90  << ";\n";
91  }
92 
94  const std::vector<casadi_int>& arg,
95  const std::vector<casadi_int>& res,
96  const std::vector<bool>& arg_is_ref,
97  std::vector<bool>& res_is_ref) const {
98  g << g.workel(res[0]) << " = "
99  << g.mmax(g.work(arg[0], dep(0).nnz(), arg_is_ref[0]), dep(0).nnz(), dep(0).is_dense())
100  << ";\n";
101  }
102 
103  void MMin::ad_forward(const std::vector<std::vector<MX> >& fseed,
104  std::vector<std::vector<MX> >& fsens) const {
105  MX m = shared_from_this<MX>()==dep(0);
106  MX N = sum2(sum1(m));
107  for (casadi_int d=0; d<fsens.size(); ++d) {
108  fsens[d][0] = dot(fseed[d][0], m) / N;
109  }
110  }
111 
112  void MMin::ad_reverse(const std::vector<std::vector<MX> >& aseed,
113  std::vector<std::vector<MX> >& asens) const {
114  MX m = shared_from_this<MX>()==dep(0);
115  MX N = sum2(sum1(m));
116  for (casadi_int d=0; d<aseed.size(); ++d) {
117  asens[d][0] += (aseed[d][0]/N)*m;
118  }
119  }
120 
121  void MMax::ad_forward(const std::vector<std::vector<MX> >& fseed,
122  std::vector<std::vector<MX> >& fsens) const {
123  MX m = shared_from_this<MX>()==dep(0);
124  MX N = sum2(sum1(m));
125  for (casadi_int d=0; d<fsens.size(); ++d) {
126  fsens[d][0] = dot(fseed[d][0], m) / N;
127  }
128  }
129 
130  void MMax::ad_reverse(const std::vector<std::vector<MX> >& aseed,
131  std::vector<std::vector<MX> >& asens) const {
132  MX m = shared_from_this<MX>()==dep(0);
133  MX N = sum2(sum1(m));
134  for (casadi_int d=0; d<aseed.size(); ++d) {
135  asens[d][0] += (aseed[d][0]/N)*m;
136  }
137  }
138 
139 } // namespace casadi
Helper class for C code generation.
std::string mmax(const std::string &x, casadi_int n, bool is_dense)
mmax
std::string work(casadi_int n, casadi_int sz, bool is_ref) const
std::string mmin(const std::string &x, casadi_int n, bool is_dense)
mmin
std::string workel(casadi_int n) const
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate symbolically (SX)
Definition: mmin.cpp:68
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res) const override
Evaluate symbolically (MX)
Definition: mmin.cpp:79
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
Definition: mmin.cpp:130
MMax(const MX &x)
Constructor.
Definition: mmin.cpp:39
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: mmin.cpp:93
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
Definition: mmin.cpp:121
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate numerically.
Definition: mmin.cpp:54
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
Definition: mmin.cpp:44
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate symbolically (SX)
Definition: mmin.cpp:61
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
Definition: mmin.cpp:103
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res) const override
Evaluate symbolically (MX)
Definition: mmin.cpp:75
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: mmin.cpp:83
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
Definition: mmin.cpp:35
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
Definition: mmin.cpp:112
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate numerically.
Definition: mmin.cpp:48
MMin(const MX &x)
Constructor.
Definition: mmin.cpp:30
casadi_int nnz(casadi_int i=0) const
Definition: mx_node.hpp:389
const MX & dep(casadi_int ind=0) const
dependencies - functions that have to be evaluated before this one
Definition: mx_node.hpp:354
void set_sparsity(const Sparsity &sparsity)
Set the sparsity.
Definition: mx_node.cpp:222
void set_dep(const MX &dep)
Set unary dependency.
Definition: mx_node.cpp:226
MX - Matrix expression.
Definition: mx.hpp:92
The basic scalar symbolic class of CasADi.
Definition: sx_elem.hpp:75
static Sparsity scalar(bool dense_scalar=true)
Create a scalar sparsity pattern *.
Definition: sparsity.hpp:153
The casadi namespace.
Definition: archiver.cpp:28
T1 casadi_mmax(const T1 *x, casadi_int n, T1 is_dense)
T dot(const std::vector< T > &a, const std::vector< T > &b)
T1 casadi_mmin(const T1 *x, casadi_int n, casadi_int is_dense)