casadi_find.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 "casadi_find.hpp"
27 
28 namespace casadi {
29 
30  Find::Find(const MX& x) {
31  casadi_assert_dev(x.is_column());
32  set_dep(x);
34  }
35 
36  std::string Find::disp(const std::vector<std::string>& arg) const {
37  return "find(" + arg.at(0) + ")";
38  }
39 
40  int Find::eval(const double** arg, double** res, casadi_int* iw, double* w) const {
41  const double* x = arg[0];
42  casadi_int nnz = dep(0).nnz();
43  casadi_int k=0;
44  while (k<nnz && *x++ == 0) k++;
45  res[0][0] = k<nnz ? static_cast<double>(dep(0).row(k)) : static_cast<double>(dep(0).size1());
46  return 0;
47  }
48 
49  void Find::eval_mx(const std::vector<MX>& arg, std::vector<MX>& res) const {
50  res[0] = find(arg[0]);
51  }
52 
53  void Find::ad_forward(const std::vector<std::vector<MX> >& fseed,
54  std::vector<std::vector<MX> >& fsens) const {
55  for (casadi_int d=0; d<fsens.size(); ++d) {
56  fsens[d][0] = 0;
57  }
58  }
59 
60  void Find::ad_reverse(const std::vector<std::vector<MX> >& aseed,
61  std::vector<std::vector<MX> >& asens) const {
62  }
63 
64  int Find::sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
65  res[0][0] = 0; // pw constant
66  return 0;
67  }
68 
69  int Find::sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
70  res[0][0] = 0; // pw constant
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  casadi_int nnz = dep(0).nnz();
80  g.local("i", "casadi_int");
81  g.local("cr", "const casadi_real", "*");
82  g << "for (i=0, cr=" << g.work(arg[0], nnz, arg_is_ref[0]) << "; i<" << nnz
83  << " && *cr++==0; ++i) {}\n"
84  << g.workel(res[0]) << " = ";
85  if (dep(0).is_dense()) {
86  g << "i;\n";
87  } else {
88  // The row is in position 1+1+2+i (colind has length 2)
89  g << "i<" << nnz << " ? " << g.sparsity(dep(0).sparsity()) << "[4+i] : "
90  << dep(0).size1() << "\n";
91  }
92  }
93 
94 } // namespace casadi
Helper class for C code generation.
std::string work(casadi_int n, casadi_int sz, bool is_ref) const
void local(const std::string &name, const std::string &type, const std::string &ref="")
Declare a local variable.
std::string workel(casadi_int n) const
std::string sparsity(const Sparsity &sp, bool canonical=true)
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
Definition: casadi_find.cpp:40
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
Definition: casadi_find.cpp:53
Find(const MX &x)
Constructor.
Definition: casadi_find.cpp:30
int sp_forward(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity forward.
Definition: casadi_find.cpp:64
int sp_reverse(bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity backwards.
Definition: casadi_find.cpp:69
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
Definition: casadi_find.cpp:36
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
Definition: casadi_find.cpp:60
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res) const override
Evaluate symbolically (MX)
Definition: casadi_find.cpp:49
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: casadi_find.cpp:74
bool is_column() const
Check if the matrix is a column vector (i.e. size2()==1)
casadi_int nnz() const
Get the number of (structural) non-zero elements.
casadi_int size1() const
Get the first dimension (i.e. number of rows)
const Sparsity & sparsity() const
Get the sparsity.
Definition: mx_node.hpp:372
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
static Sparsity scalar(bool dense_scalar=true)
Create a scalar sparsity pattern *.
Definition: sparsity.hpp:153
The casadi namespace.
Definition: archiver.cpp:28
unsigned long long bvec_t
std::vector< casadi_int > find(const std::vector< T > &v)
find nonzeros