rank1.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 "rank1.hpp"
27 namespace casadi {
28 
29  Rank1::Rank1(const MX& A, const MX& alpha, const MX& x, const MX& y) {
30  set_dep({A, alpha, x, y});
32  }
33 
34  std::string Rank1::disp(const std::vector<std::string>& arg) const {
35  return "rank1(" + arg.at(0) + ", " + arg.at(1)
36  + ", " + arg.at(2) + ", " + arg.at(3) + ")";
37  }
38 
39  void Rank1::eval_mx(const std::vector<MX>& arg, std::vector<MX>& res,
40  const std::vector<bool>& unique) const {
41  res[0] = rank1(arg[0], arg[1], arg[2], arg[3]);
42  }
43 
44  void Rank1::ad_forward(const std::vector<std::vector<MX> >& fseed,
45  std::vector<std::vector<MX> >& fsens) const {
46  for (casadi_int d=0; d<fsens.size(); ++d) {
47  MX v = project(fseed[d][0], sparsity());
48  v = rank1(v, fseed[d][1], dep(2), dep(3));
49  v = rank1(v, dep(1), fseed[d][2], dep(3));
50  v = rank1(v, dep(1), dep(2), fseed[d][3]);
51  fsens[d][0] = v;
52  }
53  }
54 
55  void Rank1::ad_reverse(const std::vector<std::vector<MX> >& aseed,
56  std::vector<std::vector<MX> >& asens) const {
57  for (casadi_int d=0; d<aseed.size(); ++d) {
58  asens[d][1] += bilin(aseed[d][0], dep(2), dep(3));
59  asens[d][2] += dep(1) * mtimes(aseed[d][0], dep(3));
60  asens[d][3] += dep(1) * mtimes(aseed[d][0].T(), dep(2));
61  asens[d][0] += aseed[d][0];
62  }
63  }
64 
65  int Rank1::eval(const double** arg, double** res, casadi_int* iw, double* w) const {
66  return eval_gen<double>(arg, res, iw, w);
67  }
68 
69  int Rank1::eval_sx(const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w) const {
70  return eval_gen<SXElem>(arg, res, iw, w);
71  }
72 
73  template<typename T>
74  int Rank1::eval_gen(const T** arg, T** res, casadi_int* iw, T* w) const {
75  if (arg[0]!=res[0]) casadi_copy(arg[0], dep(0).nnz(), res[0]);
76  casadi_rank1(res[0], sparsity(), *arg[1], arg[2], arg[3]);
77  return 0;
78  }
79 
80  int Rank1::sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
81  /* If not inline, copy to result */
82  if (arg[0]!=res[0]) std::copy(arg[0], arg[0]+dep(0).nnz(), res[0]);
83 
84  /* Get sparsities */
85  casadi_int ncol_A = sparsity().size2();
86  const casadi_int *colind_A = sparsity().colind(), *row_A = sparsity().row();
87 
88  /* Loop over the columns of A */
89  casadi_int cc, rr, el;
90  for (cc=0; cc<ncol_A; ++cc) {
91  /* Loop over the nonzeros of A */
92  for (el=colind_A[cc]; el<colind_A[cc+1]; ++el) {
93  /* Get row */
94  rr = row_A[el];
95 
96  /* Add the multiple */
97  res[0][el] |= *arg[1] | arg[2][rr] | arg[3][cc];
98  }
99  }
100  return 0;
101  }
102 
103  int Rank1::sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
104  /* Get sparsities */
105  casadi_int ncol_A = sparsity().size2();
106  const casadi_int *colind_A = sparsity().colind(), *row_A = sparsity().row();
107 
108  /* Loop over the columns of A */
109  casadi_int cc, rr, el;
110  for (cc=0; cc<ncol_A; ++cc) {
111  /* Loop over the nonzeros of A */
112  for (el=colind_A[cc]; el<colind_A[cc+1]; ++el) {
113  /* Get row */
114  rr = row_A[el];
115 
116  /* Add the multiple */
117  *arg[1] |= res[0][el];
118  arg[2][rr] |= res[0][el];
119  arg[3][cc] |= res[0][el];
120  }
121  }
122 
123  // Clear seeds
124  copy_rev(arg[0], res[0], nnz());
125  return 0;
126  }
127 
129  const std::vector<casadi_int>& arg,
130  const std::vector<casadi_int>& res,
131  const std::vector<bool>& arg_is_ref,
132  std::vector<bool>& res_is_ref) const {
133  // Copy first argument if not inplace
134  if (arg[0]!=res[0] || arg_is_ref[0]) {
135  g << g.copy(g.work(arg[0], nnz(), arg_is_ref[0]),
136  nnz(),
137  g.work(res[0], nnz(), false)) << "\n";
138  }
139 
140  // First argument of casadi_rank1 is non-const
141  std::string a = g.work(res[0], dep(0).nnz(), false);
142  std::string b = g.work(arg[2], dep(2).nnz(), arg_is_ref[2]);
143  std::string c = g.work(arg[3], dep(3).nnz(), arg_is_ref[3]);
144 
145  // Perform operation inplace
146  g << g.rank1(a, sparsity(), g.workel(arg[1]), b, c) << "\n";
147  }
148 
149 } // namespace casadi
Helper class for C code generation.
std::string work(casadi_int n, casadi_int sz, bool is_ref) const
std::string copy(const std::string &arg, std::size_t n, const std::string &res)
Create a copy operation.
std::string rank1(const std::string &A, const Sparsity &sp_A, const std::string &alpha, const std::string &x, const std::string &y)
Rank-1 update.
std::string workel(casadi_int n) const
static void copy_rev(bvec_t *arg, bvec_t *res, casadi_int len)
Propagate sparsities backwards through a copy operation.
Definition: mx_node.cpp:1308
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
const Sparsity & sparsity() const
Get the sparsity pattern.
Definition: mx.cpp:612
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: rank1.cpp:128
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
Definition: rank1.cpp:69
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
Definition: rank1.cpp:65
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res, const std::vector< bool > &unique={}) const override
Evaluate symbolically (MX)
Definition: rank1.cpp:39
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
Definition: rank1.cpp:44
int sp_forward(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity forward.
Definition: rank1.cpp:80
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
Definition: rank1.cpp:34
int sp_reverse(bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity backwards.
Definition: rank1.cpp:103
Rank1(const MX &A, const MX &alpha, const MX &x, const MX &y)
Constructor.
Definition: rank1.cpp:29
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
Definition: rank1.cpp:55
int eval_gen(const T **arg, T **res, casadi_int *iw, T *w) const
Evaluate the function (template)
Definition: rank1.cpp:74
The basic scalar symbolic class of CasADi.
Definition: sx_elem.hpp:75
casadi_int size2() const
Get the number of columns.
Definition: sparsity.cpp:128
const casadi_int * row() const
Get a reference to row-vector,.
Definition: sparsity.cpp:164
const casadi_int * colind() const
Get a reference to the colindex of all column element (see class description)
Definition: sparsity.cpp:168
The casadi namespace.
Definition: archiver.cpp:28
unsigned long long bvec_t
void casadi_copy(const T1 *x, casadi_int n, T1 *y)
COPY: y <-x.
void casadi_rank1(T1 *A, const casadi_int *sp_A, T1 alpha, const T1 *x)
Adds a multiple alpha/2 of the outer product mul(x, trans(x)) to A.