stan-dev / math

The Stan Math Library is a C++ template library for automatic differentiation of any order using forward, reverse, and mixed modes. It includes a range of built-in functions for probabilistic modeling, linear algebra, and equation solving.
https://mc-stan.org
BSD 3-Clause "New" or "Revised" License
752 stars 188 forks source link

L1 and L2 norms (Euclidean and taxicab lengths) #2562

Open bob-carpenter opened 3 years ago

bob-carpenter commented 3 years ago

Description

We should have L1 and L2 norm functions in the Stan language. They have to be implemented in the math library first, which is what this PR is for. We want the functions, but ideally we want custom derivatives.

real norm1(vector x);
real norm1(row_vector x);

real norm2(vector x);
real norm2(row_vector x);

The definitions are:

norm1(x)  =def=  sum(abs(x))
norm2(x)  =def=  sqrt(sum(square(x)))

The derivatives are:

d/dx norm1(x) = signum(x)
d/dx norm2(x) = x / norm2(x)

Current Version:

v4.1.0

tvrsimhan commented 3 years ago

I would like to work on this issue!

bob-carpenter commented 3 years ago

Hi, @tvrsimhan. That'd be great. We accept pull requests for issues. Please let us know if you have any questions about how to implement it or test it (questions on the issue are fine).

There are two possible ways to do this: either by adding the functions with inefficient autodiff implementations or by adding custom gradients. We'd accept the former, but the advantage in compute speed is from the latter.

tvrsimhan commented 3 years ago

Thank you, @bob-carpenter! In which directory should this be implemented? I read through the guide but I couldn't locate those directories. Also, please point me to any other links I can refer to! I'll try my best and get back to you with any questions I have!

I'll try to add custom gradients, but I'll get back to you if I have any trouble with it!

bob-carpenter commented 3 years ago

It should follow the other functions. That means a primitive (double) implementation in stan/math/prim/fun and then one with custom gradients in stan/math/rev/fun. The primitive version needs thorough testing, then the reverse mode should be tested in the mix directory for autodiff like the other matrix functions.

You can see how dot_self is implemented as it has the same signature:

tvrsimhan commented 3 years ago

Thank you for your guidance! I'll do my best!

yizhang-yiz commented 2 years ago

@tvrsimhan any update on this issue? Thanks.

SteveBronder commented 2 years ago

@tvrsimhan the stan math docs have some examples to get started with as well

https://mc-stan.org/math/getting_started.html

rok-cesnovar commented 2 years ago

Ah sorry, was too quick to close. #2636 addressed the first point of the issue, the second one is still open. Reopening.