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) const {
40  res[0] = rank1(arg[0], arg[1], arg[2], arg[3]);
41  }
42 
43  void Rank1::ad_forward(const std::vector<std::vector<MX> >& fseed,
44  std::vector<std::vector<MX> >& fsens) const {
45  for (casadi_int d=0; d<fsens.size(); ++d) {
46  MX v = project(fseed[d][0], sparsity());
47  v = rank1(v, fseed[d][1], dep(2), dep(3));
48  v = rank1(v, dep(1), fseed[d][2], dep(3));
49  v = rank1(v, dep(1), dep(2), fseed[d][3]);
50  fsens[d][0] = v;
51  }
52  }
53 
54  void Rank1::ad_reverse(const std::vector<std::vector<MX> >& aseed,
55  std::vector<std::vector<MX> >& asens) const {
56  for (casadi_int d=0; d<aseed.size(); ++d) {
57  asens[d][1] += bilin(aseed[d][0], dep(2), dep(3));
58  asens[d][2] += dep(1) * mtimes(aseed[d][0], dep(3));
59  asens[d][3] += dep(1) * mtimes(aseed[d][0].T(), dep(2));
60  asens[d][0] += aseed[d][0];
61  }
62  }
63 
64  int Rank1::eval(const double** arg, double** res, casadi_int* iw, double* w) const {
65  return eval_gen<double>(arg, res, iw, w);
66  }
67 
68  int Rank1::eval_sx(const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w) const {
69  return eval_gen<SXElem>(arg, res, iw, w);
70  }
71 
72  template<typename T>
73  int Rank1::eval_gen(const T** arg, T** res, casadi_int* iw, T* w) const {
74  if (arg[0]!=res[0]) casadi_copy(arg[0], dep(0).nnz(), res[0]);
75  casadi_rank1(res[0], sparsity(), *arg[1], arg[2], arg[3]);
76  return 0;
77  }
78 
79  int Rank1::sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
80  /* If not inline, copy to result */
81  if (arg[0]!=res[0]) std::copy(arg[0], arg[0]+dep(0).nnz(), res[0]);
82 
83  /* Get sparsities */
84  casadi_int ncol_A = sparsity().size2();
85  const casadi_int *colind_A = sparsity().colind(), *row_A = sparsity().row();
86 
87  /* Loop over the columns of A */
88  casadi_int cc, rr, el;
89  for (cc=0; cc<ncol_A; ++cc) {
90  /* Loop over the nonzeros of A */
91  for (el=colind_A[cc]; el<colind_A[cc+1]; ++el) {
92  /* Get row */
93  rr = row_A[el];
94 
95  /* Add the multiple */
96  res[0][el] |= *arg[1] | arg[2][rr] | arg[3][cc];
97  }
98  }
99  return 0;
100  }
101 
102  int Rank1::sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
103  /* Get sparsities */
104  casadi_int ncol_A = sparsity().size2();
105  const casadi_int *colind_A = sparsity().colind(), *row_A = sparsity().row();
106 
107  /* Loop over the columns of A */
108  casadi_int cc, rr, el;
109  for (cc=0; cc<ncol_A; ++cc) {
110  /* Loop over the nonzeros of A */
111  for (el=colind_A[cc]; el<colind_A[cc+1]; ++el) {
112  /* Get row */
113  rr = row_A[el];
114 
115  /* Add the multiple */
116  *arg[1] |= res[0][el];
117  arg[2][rr] |= res[0][el];
118  arg[3][cc] |= res[0][el];
119  }
120  }
121 
122  // Clear seeds
123  copy_rev(arg[0], res[0], nnz());
124  return 0;
125  }
126 
128  const std::vector<casadi_int>& arg,
129  const std::vector<casadi_int>& res,
130  const std::vector<bool>& arg_is_ref,
131  std::vector<bool>& res_is_ref) const {
132  // Copy first argument if not inplace
133  if (arg[0]!=res[0] || arg_is_ref[0]) {
134  g << g.copy(g.work(arg[0], nnz(), arg_is_ref[0]),
135  nnz(),
136  g.work(res[0], nnz(), false)) << "\n";
137  }
138 
139  // First argument of casadi_rank1 is non-const
140  std::string a = g.work(res[0], dep(0).nnz(), false);
141  std::string b = g.work(arg[2], dep(2).nnz(), arg_is_ref[2]);
142  std::string c = g.work(arg[3], dep(3).nnz(), arg_is_ref[3]);
143 
144  // Perform operation inplace
145  g << g.rank1(a, sparsity(), g.workel(arg[1]), b, c) << "\n";
146  }
147 
148 } // 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:1247
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
const Sparsity & sparsity() const
Get the sparsity pattern.
Definition: mx.cpp:592
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:127
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
Definition: rank1.cpp:68
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
Definition: rank1.cpp:64
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:43
int sp_forward(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity forward.
Definition: rank1.cpp:79
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res) const override
Evaluate symbolically (MX)
Definition: rank1.cpp:39
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:102
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:54
int eval_gen(const T **arg, T **res, casadi_int *iw, T *w) const
Evaluate the function (template)
Definition: rank1.cpp:73
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.