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

Dimensional correctness test with unknown constants (with unknown units) #653

Open
MilesCranmer opened this issue Jun 3, 2023 · 1 comment

Comments

@MilesCranmer
Copy link

MilesCranmer commented Jun 3, 2023

I want to determine if an expression, with arbitrary constants, is dimensionally correct. The constants essentially have "wildcard" units which one would solve for to make an expression consistent. How can I do this with Unitful.jl?

For example,

  • $a_1 x - a_2 y$ , is dimensionally correct, for any dimensions in $x$ and $y$, because $a_1$ and $a_2$ are free parameters.
  • $\cos(a_1*x)$ is dimensionally correct, as the $a_1$ can cancel out the dimensions of $x$.
  • But, $\cos(a_1*x - y)$ is not dimensionally correct if $y$ has units which cannot be cancelled out.
  • $\cos(a_1*x) * a_2 - y$ is dimensionally correct, as the $a_2$ can give the result of the $\cos$ the same units as $y$ has.

I am wondering what the best way to approach this with Unitful.jl is?

@MilesCranmer
Copy link
Author

Okay I think I got it working: MilesCranmer/SymbolicRegression.jl#220. The trick is to define a wrapper type:

"""
    WildcardDimensionWrapper{T}

A wrapper for `Quantity{T}` that allows for a "wildcard" unit, indicating
there is a free constant whose dimensions are not yet determined.
Also stores a flag indicating whether an expression is dimensionally consistent.
"""
Base.@kwdef struct WildcardDimensionWrapper{T}
    val::Quantity{T} = one(Quantity{T})
    wildcard::Bool = false
    violates::Bool = false
end

and propagate wildcard through a depth-first tree traversal evaluation.

However, it is slow. I think this is due to the type instability in val, as the units are variable at each node in an expression. Is there any way I can get around this? Or maybe a type-stable version of Unitful.jl I could use?

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

No branches or pull requests

1 participant