Comments (9)
Given the sparsity patterns for matrix and the desired elements
https://coin-or.github.io/CppAD/doc/sparse_rc.htm
I would use row_major (or col_major) order to, in one pass of over the elements, determine which elements of the desired pattern are in the matrix pattern.
Perhaps it would be useful to write a general purpose function like this:
intersect_rc = intersection(first_rc, second_rc)
where _rc denotes a row and column sparsity pattern.
from cppadcodegen.
I have improved the error message for this case; see CppAD commit
coin-or/CppAD@30e4e4a
from cppadcodegen.
CppAD should give an error from a know soruce for this case and report the cause.
The problem is that the subset of elements requested must all be in the sparsity pattern.
This requirement is documented under the heading 'subset' on
https://coin-or.github.io/CppAD/doc/sparse_jac.htm
'All of the row, column pairs in subset must also appear in pattern ; i.e., they must be possibly non-zero. '
from cppadcodegen.
Okay so I correctly understood what the problem is. What's the best way to assure they are in the pattern?
from cppadcodegen.
Elements that are not in the pattern have value zero, so you do not need to calculate them.
The reason I did not handle this case is that it seems like a good cross check (that the pattern and subset of the pattern are consistent).
from cppadcodegen.
I guess what I'm asking is how to check if those values are zero and don't have to be added to my custom sparsity with setCustomSparseJacobianElements
. So something like this:
// Jacobian sparsity
std::vector<size_t> rows;
std::vector<size_t> cols;
for (size_t row = 0; row < jacobian_t::RowsAtCompileTime; row++)
{
for (size_t col = 0; col < jacobian_t::ColsAtCompileTime; col++)
{
if (/* (row, col) not already in pattern */)
{
rows.push_back(row);
cols.push_back(col);
}
}
}
Or if there's a way to edit the current pattern so that I can remove the values that are not needed.
from cppadcodegen.
Okay with your help I ended up with something that works:
// sparsity pattern for the identity matrix
size_t nr = x.size();
size_t nc = x.size();
size_t nnz_in = x.size();
CppAD::sparse_rc<std::vector<size_t>> pattern_in(nr, nc, nnz_in);
for (size_t k = 0; k < nnz_in; k++)
{
size_t r = k;
size_t c = k;
pattern_in.set(k, r, c);
}
CppAD::sparse_rc<std::vector<size_t>> pattern_out;
f_.for_jac_sparsity(pattern_in, false, false, false, pattern_out);
const std::vector<size_t> &row(pattern_out.row());
const std::vector<size_t> &col(pattern_out.col());
// reduced Jacobian sparsity
std::vector<size_t> rows;
std::vector<size_t> cols;
for (size_t i = 0; i < row.size(); i++)
{
if (col[i] < desired_column_count)
{
rows.push_back(row[i]);
cols.push_back(col[i]);
}
}
cgen.setCustomSparseJacobianElements(rows, cols);
This is not actually faster than just computing the full dense Jacobian
with all the parameter derivatives in it and then discarding the unused elements. Was this to be expected?
from cppadcodegen.
I am not sure how this all plays out in the context of CppADCodeGen, but in the context of CppAD,
it would be better to use the dynamic parameter option for the parameters that you are not taking
derivatives w.r.t.; see
https://coin-or.github.io/CppAD/doc/new_dynamic.htm
In addition, I only expect sparse versions to be faster when the matrices are large
and sparse. If you have such a case, in the context of CppAD, where the dense Jacobian is as fast as the sparse one, you could open an issue about it for CppAD; see
https://github.com/coin-or/CppAD/issues
from cppadcodegen.
I've been using CppAD parameters as well. The reason why I switched to code generation is the 10x speedup (even with the full Jacobian) that is absolutely necessary for my application.
The matrix is indeed pretty small with 20 independent parameters of which 10 are parameters, and 10 outputs. So the slower sparse Jacobian is not surprising.
I'll be very happy the day CppADCodegen impements parameters, but I understand it's a pretty big change and will take time.
Thanks again for your help.
from cppadcodegen.
Related Issues (20)
- CppADCodeGen support for Eigen `.transpose()` operation HOT 6
- Found CppAD version '' but at least version '20200000.1' is required HOT 5
- Loops reusing intermediate results HOT 1
- Using LLVM to improve CppAD HOT 4
- Question about wrong type argument error
- Examples on using CppADCodeGen with Eigen HOT 5
- Use CppADCodeGen with variable number of independent variable arrays HOT 2
- GreaterThanZero cannot be called for non-parameters for eigen determinant HOT 2
- Code generation with if/else statements HOT 18
- Large computational graphs fail at link time HOT 4
- cppad_ipopt dependency HOT 3
- Calling CppAD::ipopt::solve from CppADCodeGen HOT 8
- How to get the value of a CppADCodeGen scalar type HOT 1
- Generating Jacobians as Tensors HOT 12
- icpc build fails for dynamic_linux.cpp example HOT 1
- make build_tests fails HOT 1
- Supporting runtime compilation and dynamic linking in MacOSX M1,2 chips HOT 2
- model->Domain() returns size for previous model
- Running the test program Jacobian module listwith cppAD library gives a lot of errors HOT 6
- error when complie function library
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cppadcodegen.