Giter Club home page Giter Club logo

dfdr's People

Contributors

codacy-badger avatar konrad1991 avatar mailund avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

dfdr's Issues

Handle calls to user-defined functions.

If an expression contains a call to a function, then we need to use the chain rule. That means we need to compute the derivative of this function. We need to find it and do this, but we can't do it in the rewriting when we compute a derivative, we need to put that code into the function we create, otherwise, we are evaluating df/dx in a context that might be different from when we later call the derivative.

The issue is similar to how global variables are handled. Now, we just leave them be in expressions, and that works because df/dx is evaluated in the same environment as f. We can't do the same thing with derived functions. If we refer to a function g, we can't compute dg/dx when we compute df/dx, we need to compute it when we call df/dx.

Using curly braces for the function body leads to an error

Hello,

I'm using R-4.2.2 with Linux Mint. When I enclose the body of a function with curly braces {}, I can't apply the dfdr functions to this function; I get the error "function { is not supported". That sounds strange because there are such examples in the documentation.

Derivatives for entire functions

Could the approach in the function 'd' be used for more complicated functions. For instance:

f <- function(y) {
y[1] = a + y[1]*b
y[2] = y[2]*y[1]
return(y)
}

Is it actually automatic differentiation in forward or reverse mode?

Handle parentheses

Right now, I don't handle parentheses at all. This is a bit silly, but that is how it is. I just didn't include ( in the parsing of calls.

Using raw variables for getting derivatives

Code such as

dfdx <- d(f, x)

looks better than

dfdx <- d(f, "x")

It is simple enough to handle the former with substitute(), but then it is harder to use programmatically. With tidy-eval, though, it should be easy enough to implement it.

Do partial evaluation and simplification

It should be easy enough to figure out how to simplify constant expressions. There is a little more involved in doing partial evaluation, having to do with references to scopes around f that makes expressions mutable.

Compute gradients and hessians

It should be pretty straightforward to automatically compute the gradient and hessian of a function if we can compute the derivative with respect to any individual parameter.

Add a function for the computation of the gradient

Hello,

Thank you for this amazing package.

I have the function f below. I'm wondering whether we can add the function d to the list of supported functions in order to compute the gradient of f, in the same way we can do it for derivatives.

d <- function(x, y ,z) 
  sqrt((-sin(x) * sin(y) + cos(x) * cos(z))^2 + (-sin(y) * sin(z) + cos(y) * cos(x))^2 + (-sin(z) * sin(x) + cos(z) * cos(y))^2)

f <- function(x, y, z)
  (
    cos(
      x - (-sin(x) * sin(y) + cos(x) * cos(z)) / d(x, y, z)
    )
    * sin(
      y - (-sin(y) * sin(z) + cos(y) * cos(x)) / d(x, y, z)
    )
    + cos(
      y - (-sin(y) * sin(z) + cos(y) * cos(x)) / d(x, y, z)
    )
    * sin(
      z - (-sin(z) * sin(x) + cos(z) * cos(y)) / d(x, y, z)
    )
    + cos(
      z - (-sin(z) * sin(x) + cos(z) * cos(y)) / d(x, y, z)
    )
    * sin(
      x - (-sin(x) * sin(y) + cos(x) * cos(z)) / d(x, y, z)
    )
  ) * (
    (
      cos(
        x + (-sin(x) * sin(y) + cos(x) * cos(z)) / d(x, y, z)
      )
      * sin(
        y + (-sin(y) * sin(z) + cos(y) * cos(x)) / d(x, y, z)
      )
      + cos(
        y + (-sin(y) * sin(z) + cos(y) * cos(x)) / d(x, y, z)
      )
      * sin(
        z + (-sin(z) * sin(x) + cos(z) * cos(y)) / d(x, y, z)
      )
      + cos(
        z + (-sin(z) * sin(x) + cos(z) * cos(y)) / d(x, y, z)
      )
      * sin(
        x + (-sin(x) * sin(y) + cos(x) * cos(z)) / d(x, y, z)
      )
    )
  )

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.