26 #include "casadi_find.hpp"
36 std::string
Find::disp(
const std::vector<std::string>& arg)
const {
37 return "find(" + arg.at(0) +
")";
40 int Find::eval(
const double** arg,
double** res, casadi_int* iw,
double* w)
const {
41 const double* x = arg[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());
49 void Find::eval_mx(
const std::vector<MX>& arg, std::vector<MX>& res)
const {
50 res[0] =
find(arg[0]);
54 std::vector<std::vector<MX> >& fsens)
const {
55 for (casadi_int d=0; d<fsens.size(); ++d) {
61 std::vector<std::vector<MX> >& asens)
const {
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 {
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()) {
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.
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
Find(const MX &x)
Constructor.
int sp_forward(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity forward.
int sp_reverse(bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity backwards.
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res) const override
Evaluate symbolically (MX)
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.
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.
casadi_int nnz(casadi_int i=0) const
const MX & dep(casadi_int ind=0) const
dependencies - functions that have to be evaluated before this one
void set_sparsity(const Sparsity &sparsity)
Set the sparsity.
void set_dep(const MX &dep)
Set unary dependency.
static Sparsity scalar(bool dense_scalar=true)
Create a scalar sparsity pattern *.
unsigned long long bvec_t
std::vector< casadi_int > find(const std::vector< T > &v)
find nonzeros