stan-dev / stan-dev/math

Issue with `.trace` on a `.val()` expression fixed with `.val_op()`

Open
#2,268 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
839
Forks
220
Avg merge
2d 4h
Merged PRs (30d)
14

Description

Description

Here's the code:

TEST(Whatever, whatever3) {
  using stan::math::var;
  Eigen::Matrix<var, -1, 1> a(3);
  Eigen::VectorXd b(3);

  a << 1, 1, 1;

  b << 0.001, 0.01, 0.1;

  std::cout << "val no trace: " << a.val().transpose() * b << std::endl;
  std::cout << "val trace: " << (a.val().transpose() * b).trace() << std::endl;
  std::cout << "val_op no trace: " << a.val_op().transpose() * b << std::endl;
  std::cout << "val_op trace: " << (a.val_op().transpose() * b).trace() << std::endl;
}

Output is:

val no trace: 0.111
val trace: 0.001
val_op no trace: 0.111
val_op trace: 0.111

The correct output is 0.111.

So it looks like with .trace() and .val() only the first element of the expression is being evaluated but .val_op() makes it all the way through.

Current Version:

v3.4.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the provided C++ example and comparing the .val() and .val_op() paths for the Eigen expression involving .trace(). Done means the trace expression produces the full expected value, 0.111, rather than only the first element.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.