Coder Social home page Coder Social logo

lubkoll / funcy Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 20.7 MB

This will become the successor of FunG, a stable automatic differentiation library that admits highly efficient evaluation of derivatives of functions operating on arbitrary mathematical types

Home Page: https://lubkoll.github.io/funcy/

License: GNU General Public License v3.0

CMake 1.89% Shell 0.94% C++ 96.36% C 0.51% Dockerfile 0.07% Python 0.22%

funcy's Introduction

CI Coverage Status Documentation

funcy - automatic differentiation for biomechanical modeling

Installation

funcy is header-only and requires no installation. To make it available in the local cmake-registry you still may find it useful install funcy using cmake.

Install funcy via cmake

mkdir build
cd build
cmake .. -DFuncy_BuildTest=ON -DCMAKE_INSTALL_PREFIX=<install-prefix>
cmake --build . --target install

To include funcy in your project add this to your CMakeLists.txt:

find_package(Funcy REQUIRED)
target_link_libraries(<target> PRIVATE Funcy::Funcy)

Using funcy

The function alt text with funcy:

#include <funcy/funcy.h>

int main()
{
    using namespace funcy;
    // qualified function to sqrt to distinguish from the c-function sqrt in math.h, commonly included in the c++-header cmath.
    const auto y = funcy::sqrt(1.);
    auto f = finalize( pow<3>(y) + sin(y) );

    std::cout << "f(3) = " << f(3) << std::endl;
    f.update(4);
    std::cout << "f'(4) = " << f.d1() << std::endl;
    std::cout << "f'''(4) = " << f.d3() << std::endl;
}

The function finalize simplifies usage of f, taking advantage of the fact that it depends on only one variable and is defined on a one-dimensional space. The following examples illustrate usage without finalize and with vector and matrix-valued arguments.

A model for nonlinear heat transfer alt text:

#include <funcy/funcy.h>

template <class Vector>
auto heatModel(double c, double d, double u0, const Vector& du0)
{
    using funcy::variable;
    const auto u = variable<0>(u0);
    const auto du = variable<0>(du0);
    return (c+d*squared(u))*du;
}

int main()
{
    auto f = heatModel(c, d, u, du);
    // update function value
    f.template update<0>(std::make_tuple(u, du));

    std::cout << "f(u, du) = " << f() << std::endl;
    // derivative with respect to variable with id 0
    std::cout << "f'(4) = " << f.template d1<0>(std::make_tuple(v,dv)) << std::endl;
}

A complex biomechanical model for adipose tissue, based on isotropic and anisotropic matrix invariants alt text:

#include <funcy/funcy.h>

template <class Matrix>
auto adiposeTissue(double cCells, double k1, double k2, double kappa,
                   const Matrix& M const Matrix& F, int n = dim<Matrix>())
{
    using funcy::finalize;
    using namespace funcy::linalg;

    auto aniso = kappa*i1(F) + (1-3*kappa) * i4(F,M) - 1;
    auto materialLaw = cCells*( i1(F) - n ) + (k1/k2) * ( exp(k2*squared(aniso)) - 1 );

    return finalize( materialLaw( strainTensor(F) ) );
}

int main()
{
    // update function value
    f.update(du);

    std::cout << "f(du) = " << f() << std::endl;
    std::cout << "f''(du)(dv,dw) = " << f.d2(dv,dw) << std::endl;
}

Citing

L. Lubkoll: FunG - Automatic differentiation for invariant-based modeling. Archive of Numerical Software, vol. 5, no. 1, 2017, pp. 169-192, DOI: 10.11588/ans.2017.1.27477

Documentation

Available here: Documentation

Compatibility

funcy can work with any type that satisfies basic arithmetic requirements as described in requirements. Particular support has been implemented for scalars, vectors and matrices, enabling funcy to be used with all popular matrix libraries.

Optimization Strategies

For details, and performance comparisons with other AD-libraries, see FunG - Automatic differentiation for invariant-based modeling.

Publications:

Published:

  • L. Lubkoll: FunG - Automatic differentiation for invariant-based modeling. Archive of Numerical Software, vol. 5, no. 1, 2017, pp. 169-192, DOI: 10.11588/ans.2017.1.27477
  • L. Lubkoll, A. Schiela, M. Weiser: An affine covariant composite step method for optimization with PDEs as equality constraints.
    Opt. Meth. Softw., 32(5), pp. 1132-1161, 2016, (Preprint)
  • L. Lubkoll: An Optimal Control Approach to Implant Shape Design: Modeling, Analysis and Numerics.
    Dissertation, Universität Bayreuth, 2015

Preprints:

funcy's People

Contributors

lubkoll avatar abstruse-goose avatar mmlanger avatar

Stargazers

Kai Yan avatar

Watchers

James Cloos avatar  avatar

funcy's Issues

Is funcy thread safe?

I ran a test comparing a single-thread and multi-thread execution (OpenMP), the results didn't match.

Can I have a brief explanation on how you compare the average computation time between different program?

In your paper about FunG you compare the computation speed between multiple programs. I can see there is a huge difference between your program and another traditional autodiff library. Is it possible to release the method or program that how you preform the speed testing between this seven software? I implemented FunG into CppAD and find the ranking can be much differnt comparing your ranking in the paper.(For example Adolc is much faster but FADBAD is almost the slowest one) I am also trying to compare my autodiff program with funG using std::chrono::high_resolution_clock. I can share my library with you if you want to take a look.
Best,

How to calculate a series using FunG?

I am trying to test the following algorithm using FunG something like

begin code

auto u = FunG::Pow<3>(1.) + FunG::Sin(1.)(FunG::Sqrt(1.));
u = u + u^2; #something like this
auto f_fung_opt = FunG::finalize(u);
for(auto i=0; i<iter; ++i)
{
f_fung_opt.update(x);
f = f_fung_opt();
dfdx = f_fung_opt.d1(1.);
x *= scaling;
}

end code

Is there anyway to do test like this in funG? It looks like FunG cannot support "=" and some operator in my experience.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.