Skip to content

Add ComponentArrays extension #407

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

Merged
merged 20 commits into from
Apr 23, 2025
Merged

Add ComponentArrays extension #407

merged 20 commits into from
Apr 23, 2025

Conversation

ErikQQY
Copy link
Collaborator

@ErikQQY ErikQQY commented Mar 17, 2025

Replace: #345
Fix: #344

So in #344, the reason why ComponentArray failed with DenseEuclideanMetric is that the dense matrix in DenseEuclideanMetric doesn't have the same axes as the input, and when we do linear algebra operations on two arrays with different axes, the result is just an ordinary Julia array, for example:

julia> r_init = ComponentArray(; a=randn(5), b=randn(5))
ComponentVector{Float64}(a = [-0.8058038744185181, -0.08413831281388894, 0.7453256076416268, -0.03469137039683826, -0.2517275575798834], b = [-1.5630129100605425, 1.7031219617336555, -1.0997337141360415, 0.4409537319539482, 0.19944559484881833])

julia> getaxes(r_init)
(Axis(a = ViewAxis(1:5, Shaped1DAxis((5,))), b = ViewAxis(6:10, Shaped1DAxis((5,)))),)

julia> M = rand(10, 10);

julia> axes(M)
(Base.OneTo(10), Base.OneTo(10))

julia> M * r_init # clearly the axes of M and r_init don't match
10-element Vector{Float64}:
 -0.06957219975578224
 -0.24499462421604584
  0.26535313109443504
 -0.05499884350331631
 -0.11871623121978324
 -1.132138726898246
 -0.6221912360430668
 -0.7051768915767198
  1.3780763510373664
 -0.9263200146682737

But it's basically user's obligation to ensure consistent input data structure, so this PR ensures the input from user are axes-matching.

Some notes:

  • See the tests for example uses of ComponentArrays.
  • The code changes in src/adaptation/massmatrix.jl are not related to fixing ComponentArrays.

Copy link
Member

@yebai yebai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with ComponentArrays. Can @mhauru, @penelopeysm, or @sunxd3 take a look, please?

@ErikQQY ErikQQY requested a review from devmotion April 17, 2025 09:52
@ErikQQY ErikQQY requested a review from yebai April 17, 2025 13:04
yebai
yebai previously approved these changes Apr 17, 2025
Copy link
Member

@yebai yebai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work, @ErikQQY!

I only checked the main code (/src/) but didn't look at the tests carefully.

@yebai yebai requested a review from penelopeysm April 17, 2025 13:33
@yebai
Copy link
Member

yebai commented Apr 17, 2025

@ErikQQY, can you update the PR summary to explain what the issue was preventing the use of ComponentArrays and how this PR fix it? It is not self-evident from the code changes.

@ErikQQY ErikQQY closed this Apr 17, 2025
@ErikQQY ErikQQY reopened this Apr 17, 2025
@yebai
Copy link
Member

yebai commented Apr 23, 2025

@penelopeysm, can you also review this please?

Copy link
Member

@penelopeysm penelopeysm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very happy apart from the single comment!

@yebai yebai merged commit 7067a90 into TuringLang:main Apr 23, 2025
14 of 17 checks passed
@yebai
Copy link
Member

yebai commented Apr 23, 2025

Excellent work @ErikQQY!

@ErikQQY ErikQQY deleted the qqy/component branch April 23, 2025 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Component Arrays with DenseEuclideanMetric fails
4 participants