From ba2c54635d9ef0336029b7457120adb38d3d4067 Mon Sep 17 00:00:00 2001 From: Vedant2005goyal Date: Sat, 11 Jul 2026 05:32:57 +0000 Subject: [PATCH] This PR adds reverse mode pullback for the vector for basic std::vector initializations. Example:- std::vector v(size); --- include/clad/Differentiator/STLBuiltins.h | 14 +++++++++ test/Features/VectorPullback.cpp | 37 +++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 test/Features/VectorPullback.cpp diff --git a/include/clad/Differentiator/STLBuiltins.h b/include/clad/Differentiator/STLBuiltins.h index b35471130..7b6ee4445 100644 --- a/include/clad/Differentiator/STLBuiltins.h +++ b/include/clad/Differentiator/STLBuiltins.h @@ -687,7 +687,21 @@ void constructor_pullback(const ::std::array& arr, for (size_t i = 0; i < N; ++i) (*d_arr)[i] += (*d_this)[i]; } +template +void constructor_pullback(typename ::std::vector::size_type count, + ::std::vector* d_this, + typename ::std::vector::size_type* d_count) { + d_this->clear(); +} +template +void constructor_pullback( + typename ::std::vector::size_type count, + const typename ::std::vector::allocator_type& alloc, + ::std::vector* d_this, typename ::std::vector::size_type* d_count, + typename ::std::vector::allocator_type* d_alloc) { + d_this->clear(); +} // tuple forward mode template diff --git a/test/Features/VectorPullback.cpp b/test/Features/VectorPullback.cpp new file mode 100644 index 000000000..cb543bef0 --- /dev/null +++ b/test/Features/VectorPullback.cpp @@ -0,0 +1,37 @@ +// RUN: %cladclang -std=c++17 -O0 -I%S/../../include/ %s -o %t +// RUN: %t | %filecheck_exec %s + +#include "clad/Differentiator/Differentiator.h" +#include "clad/Differentiator/STLBuiltins.h" +#include +#include +#include + +double f(double x) { + std::vector v(5); + return x * x; +} + +double f1(double x) { + std::allocator alloc; + std::vector v(5, alloc); + return x * x * x; +} + +int main() { + + auto df = clad::gradient(f); + auto df1 = clad::gradient(f1); + + double dx1 = 0.0; + df.execute(3.0, &dx1); + std::cout << "Diff result: " << dx1 << "\n"; + //CHECK-EXEC: Diff result: 6 + + double dx2 = 0.0; + df1.execute(2.0, &dx2); + std::cout << "Diff result: " << dx2 << "\n"; + //CHECK-EXEC: Diff result: 12 + + return 0; +} \ No newline at end of file