Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Container construction utilities #692

Closed
bob-carpenter opened this issue Dec 17, 2017 · 2 comments · Fixed by #1636
Closed

Container construction utilities #692

bob-carpenter opened this issue Dec 17, 2017 · 2 comments · Fixed by #1636

Comments

@bob-carpenter
Copy link
Contributor

bob-carpenter commented Dec 17, 2017

Summary:

Provide functions to construct commonly used containers.

Description:

Here are suggested signatures:

// [1/K, ..., 1/K]'
vector uniform_simplex(int K);
// [1, ..., 1]'
vector ones_vector(int K);
// [0, ..., 0]'
vector zeros_vector(int K);
// [c, ..., c]'
vector constant_vector(double c);
// diag_matrix(ones_vector(K))
matrix identity_matrix(int K);
// [low, ..., high]' with equal spacing between values in ...
vector set_spaced_vector(real low, real high, int K);
// [0, ..., 1, ..., 0]'  with 1 in k-th position (indexing from 1)
vector one_hot_vector(int k, int K);

These all need to validate sizes are non-negative and validate other bounds like the index being in ragne for one_hot_vector.

We could also have versions for _row_vector and _array, and _int_array.

This lines up with what's available in Eigen [we already have rep_vector for their constant vectors and specific RNGs].

Additional Information:

These only require double-based implementations in C++, because in Stan there are only integer arguments, so there can't be any derivatives. For example, this function will have to be added to stan/math/prim/mat/fun/uniform_simplex.hpp and then included in the header for prim/mat.hpp.

Eigen::VectorXd uniform_simplex(long K) {
  // validate that K is positive
  // return K-vector filled with 1.0 / K (the 1.0 avoids int division)

The implementation of uniform_simplex can call const_vector.

Current Version:

v2.17.0

@mcol
Copy link
Contributor

mcol commented Jan 21, 2020

Having added the vector version, I'm now going to add the rest. I won't be adding an int_array version of uniform_simplex and set_spaced, as I don't think they make sense (the latter should be called sequence or similar, if it doesn't exist already).

I have a question: do we need row vector and array versions of uniform_simplex? If so, @bob-carpenter do you have a suggestion as for naming?

@bob-carpenter
Copy link
Contributor Author

bob-carpenter commented Jan 21, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants