multiple_output.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 "multiple_output.hpp"
27 #include "function_internal.hpp"
28 #include "casadi_misc.hpp"
29 #include "serializing_stream.hpp"
30 
31 namespace casadi {
32 
34  }
35 
37  }
38 
39  MX MultipleOutput::get_output(casadi_int oind) const {
40  MX this_ = shared_from_this<MX>();
41  // No need for an OutputNode if sparsity is fully sparse
42  if (this_->sparsity(oind).nnz()==0) return MX(this_->sparsity(oind));
43  return MX::create(new OutputNode(this_, oind));
44  }
45 
46  OutputNode::OutputNode(const MX& parent, casadi_int oind) : oind_(oind) {
47  set_dep(parent);
48 
49  // Save the sparsity pattern
50  set_sparsity(dep(0)->sparsity(oind));
51  }
52 
54  }
55 
56  std::string OutputNode::disp(const std::vector<std::string>& arg) const {
57  return arg.at(0) + "{" + str(oind_) + "}";
58  }
59 
62  s.pack("OutputNode::oind", oind_);
63  }
64 
66  s.unpack("OutputNode::oind", oind_);
67  }
68 
69 } // namespace casadi
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
Node class for MX objects.
Definition: mx_node.hpp:51
friend class MX
Definition: mx_node.hpp:52
const Sparsity & sparsity() const
Get the sparsity.
Definition: mx_node.hpp:372
const MX & dep(casadi_int ind=0) const
dependencies - functions that have to be evaluated before this one
Definition: mx_node.hpp:354
virtual void serialize_body(SerializingStream &s) const
Serialize an object without type information.
Definition: mx_node.cpp:523
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 MX create(MXNode *node)
Create from node.
Definition: mx.cpp:67
MX get_output(casadi_int oind) const override
Get an output.
MultipleOutput()
Constructor.
~MultipleOutput() override
Destructor.
OutputNode(const MX &parent, casadi_int oind)
Constructor.
~OutputNode() override
Destructor.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
casadi_int oind_
Output index.
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
Helper class for Serialization.
void pack(const Sparsity &e)
Serializes an object to the output stream.
casadi_int nnz() const
Get the number of (structural) non-zeros.
Definition: sparsity.cpp:148
The casadi namespace.
Definition: archiver.cpp:28
std::string str(const T &v)
String representation, any type.