Adept Quick Reference Stack Member Functions Constructors: All Functions and Types Are Placed in the Adept Namespace

Adept Quick Reference Stack Member Functions Constructors: All Functions and Types Are Placed in the Adept Namespace

Adept Quick Reference Stack member functions Constructors: All functions and types are placed in the adept namespace. Stack stack; Construct and activate immediately Stack stack(false); Construct in inactive state Member functions: Header files .new_recording() Clear any existing differential statements adept.h Include if only scalar automatic differentiation is required .pause_recording() Pause recording (ADEPT_PAUSABLE_RECORDING needed) adept_arrays.h Include if array capabilities are needed as well .continue_recording() Continue recording adept_fortran.h Interface to Fortran 2018 array descriptors .is_recording() Is Adept currently recording? adept_optimize.h Minimization algorithms, e.g. Levenberg-Marquardt .forward() Perform forward-mode differentiation adept_source.h Include entire Adept library, so linking to library not required .compute_tangent_linear() ...as above .reverse() Perform reverse-mode differentiation Scalar types .compute_adjoint() ...as above .independent(x) Declare an independent variable (active scalar or array) Real Passive scalar type used for differentiation (usually double) .independent(xptr,n) Declare n independent scalar variables starting at xptr aReal Active scalar of underlying type Real .dependent(y) Declare a dependent variable (active scalar or array) adouble, afloat Active scalars of underlying type double and float .dependent(yptr,n) Declare n dependent scalar variables starting at yptr .jacobian() Return Jacobian matrix Basic reverse-mode workflow .jacobian(jacptr) Place Jacobian matrix into jacptr (column major) .jacobian(jacptr,false) Place Jacobian matrix into jacptr (row major) Stack stack; Object to store derivative information .clear_gradients() Clear gradients set with set_gradient function aVector x = {1.0, 2.0}; Initialize independent (input) variables (C++11) .clear_independents() Clear independent variables stack.new_recording(); Start a new recording .clear_dependents() Clear dependent variables aReal J = algorithm(x); Any complicated algorithm here .n_independents() Number of independent variables declared J.set_gradient(1.0); Seed adjoint of cost function .n_dependents() Number of dependent variables declared stack.reverse(); Perform reverse-mode differentiation .print_status() Print status of Stack to standard output Vector dJ_dx = x.get_gradient(); Return gradients of output with respect to inputs .print_statements() Print list of differential statements .print_gradients() Print current values of gradients Basic Jacobian workflow .activate() Activate the stack Stack stack; Object to store derivative information .deactivate() Deactivate the stack aVector x = {1.0, 2.0}; Initialize independent (input) variables (C++11) .is_active() Is the stack currently active? stack.new_recording(); Start a new recording .memory() Return number of bytes currently used aVector y = algorithm(x); Algorithm with vector output .preallocate_statements(n) Preallocate space for n statements stack.independent(x); Declare independent variables .preallocate_operations(n) Preallocate space for n operations stack.dependent(y); Declare dependent variables Matrix dy_dx = stack.jacobian(); Compute Jacobian matrix Query functions in adept namespace aReal member functions active_stack() Return pointer to currently active Stack object version() Return std::string with Adept version number The first three functions below also work with active array arguments, where g would be of the configuration() Return std::string describing Adept configuration equivalent passive array type: have_matrix_multiplication() Adept compiled with matrix multiplication (BLAS)? .set_gradient(g) Initialize gradient to g have_linear_algebra() Adept compiled with linear-algebra (LAPACK)? .get_gradient() After forward or reverse pass, return gradient set_max_blas_threads(n) Set maximum threads for matrix operations .get_gradient(g) As above, but writing gradient to g max_blas_threads() Get maximum threads for matrix operations .add_derivative_dependence(a,p) Add p×δa to the stack is_thread_unsafe() Global Stack object is not thread-local? .append_derivative_dependence(a,p) Append +p×δa to the stack Dense dynamic array types Array resize and link member functions Vector, Matrix, Array3D, Array4D... Array7D Arrays of type Real .clear() Return array to original empty state intVector, intMatrix, intArray3D... intArray7D Arrays of type int .resize(3,4) Resize array discarding data boolVector, boolMatrix, boolArray3D... boolArray7D Arrays of type bool .resize(dimensions(3,4)) ...as above floatVector, floatMatrix, floatArray3D... floatARray7D Arrays of type float .resize_row_major(3,4) Resize with row-major storage (default) aVector, aMatrix, aArray3D... aArray7D Active arrays of type Real .resize_column_major(3,4) Resize with column-major storage Define new dynamic array types as follows: .resize(M.dimensions()) Resize to same as M typedef Array<short,2,false> shortMatrix; .resize_contiguous(...) Resize guaranteeing contiguous storage typedef Array<float,3,true> afloatArray3D; N >>= M; Discard existing data and link to array on right-hand-side Dense fixed-size array types Array query member functions Vector2, Vector3, Vector4 Passive vectors of fixed length 2–4 ::rank Number of array dimensions Matrix22, Matrix33, Matrix44 Passive matrices of fixed size 2×2, 3×3, 4×4 .empty() Return true if array is empty, false otherwise aVector2, aVector3, aVector4 Active vectors of fixed length 2–4 .dimensions() Return an object that can be used to resize other arrays aMatrix22, aMatrix33, aMatrix44 Active matrices of fixed size 2×2, 3×3, 4×4 .dimension(i) Return length of dimension i (0 based) .size() Return total number of elements Define new fixed array types as follows: .data() Return pointer to underlying passive data typedef FixedArray<short,false,2,4> shortMatrix24; .const_data() Return const pointer to underlying data typedef FixedArray<Real,true,3,3,3> aArray333; Special square matrix types Array filling M = 1.0; Fill all elements of array with the same number SymmMatrix, aSymmMatrix Symmetric matrix M << 1.0, 2.0, 3.0, 4.0; Fill first four elements of array DiagMatrix, aDiagMatrix Diagonal matrix M = {{1.0,2.0},{3.0,4.0}}; Fill 2×2 matrix (C++11) TridiagMatrix, aTridiagMatrix Tridiagonal matrix PentadiagMatrix, aPentadiagMatrix Pentadiagonal matrix LowerMatrix, aLowerMatrix Lower-triangular matrix Array indexing and slicing UpperMatrix, aUpperMatrix Upper-triangular matrix Dense arrays can be indexed/sliced using the function-call operator with as many arguments as there are dimensions (e.g. index a matrix with M(i,j)). In all cases a slice can be used as an Dense dynamic array constructors lvalue or rvalue. If all arguments are scalars then a single element of the array is extracted. The following special values are available: Matrix M; Create an empty matrix of type Real end The last element of the dimension being indexed Matrix N(M); Create matrix sharing data with existing matrix end-1 Penultimate element of indexed dimension (any integer arithmetic possible) Matrix N = M; ...as above If one or more argument is a regular index range then the return type will be an Array pointing Matrix N(3,4); Create matrix with size 3×4 to part of the original array. For every scalar argument, its rank will be reduced by one compared Matrix N(dimensions(3,4)); ...as above to the original array. The available ranges are: Matrix N(M.dimensions()); Create matrix with the same size as M __ All elements of indexed dimension Matrix N(ptr,dimensions(3,4)); Create 3×4 matrix sharing data from pointer ptr range(ibeg,iend) Contiguous range from ibeg to iend Matrix N = log(M); Create matrix containing copy of right-hand-side stride(ibeg,iend,istride) Strided range (istride can be negative but not zero) Matrix N = {{1.0,2.0},{3.0,4.0}}; Create 2×2 matrix from initializer list (C++11) If any of the arguments is a irregular index range (such as an intVector containing an arbitrary list of indices) then the return type will be an IndexedArray. If used as an lvalue, it will modify the original array, but if passed into a function receiving an Array type then any modifications inside the function will not affect the original array. Passing arrays to and from functions Expansion functions There are three ways an array can be received as an argument to a function: spread<d>(M,n) Replicate M array expression n times along dimension d Matrix& For an array that might be resized in the function outer_product(x,y) Return rank-2 outer product from two rank-1 arguments Matrix For an array or array slice to be modified inside the function const Matrix& For a read-only array, array slice or array expression Matrix multiplication and linear algebra Member functions returning lvalue transpose(M) Transpose matrix or 2D matrix expression matmul(M,N) Matrix multiply, where at least one argument must be a matrix, and The functions in this section return an Array that links to the original data and can be used on orientation of any vector arguments is inferred the left- or right-hand-side of an assignment. The following only work on dynamic or fixed-size M ** N Shortcut for matmul; precedence is the same as normal multiply dense arrays: inv(M) Inverse of square matrix .subset(ibeg0,iend0,ibeg1,iend1,...) Contiguous subset solve(A,x) Solve system of linear equations .permute(i0,i1,...) Permute dimensions .diag_matrix() For vector, return DiagMatrix Preprocessor variables .soft_link() The following works on any matrix: The following can be defined to change the behaviour of your code: .T() Transpose of matrix ADEPT_STACK_THREAD_UNSAFE

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    4 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us