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

Trying to use Measurements to differentiate respect to a unitful quantity. #151

Closed
martinmestre opened this issue Sep 12, 2023 · 12 comments
Closed

Comments

@martinmestre
Copy link

Hi,
I have tried with the following:

x = (98.1 ± 12.7)u"km"
y = (105.4 ± 25.6)*u"km"
Measurements.derivative.(x^2*y^2+2*x^3*y, [x, y])
ERROR: MethodError: no method matching derivative(::Quantity{Measurement{Float64}, 𝐋^4, Unitful.FreeUnits{(km^4,), 𝐋^4, nothing}}, ::Quantity{Measurement{Float64}, 𝐋, Unitful.FreeUnits{(km,), 𝐋, nothing}})
Stacktrace:
 [1] _broadcast_getindex_evalf
   @ ./broadcast.jl:683 [inlined]
 [2] _broadcast_getindex
   @ ./broadcast.jl:656 [inlined]
 [3] getindex
   @ ./broadcast.jl:610 [inlined]
 [4] copy
   @ ./broadcast.jl:912 [inlined]
 [5] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, typeof(Measurements.derivative), Tuple{Quantity{Measurement{Float64}, 𝐋^4, Unitful.FreeUnits{(km^4,), 𝐋^4, nothing}}, Vector{Quantity{Measurement{Float64}, 𝐋, Unitful.FreeUnits{(km,), 𝐋, nothing}}}}})
   @ Base.Broadcast ./broadcast.jl:873
 [6] top-level scope
   @ REPL[28]:1

I would like to know how could I modify the code.
Besides I would like to know if giving gradients with units to DifferentialEquations is possible and if it has time penalty.
Thank you very much

@LebedevRI
Copy link

Measurements.derivative.(x^2*y^2+2*x^3*y, [x, y])

That is a weird call, where did you get it?
derivative takes either a single Measurement argument + tag, or two Measurement arguments:

# Derivative and Gradient
derivative(a::Measurement{F},
tag::Tuple{T, T, UInt64}) where {F<:AbstractFloat, T<:AbstractFloat} =
get(a.der, tag, zero(F))
"""
derivative(x::Measurement, y::Measurement)
Return the value of the partial derivative of `x` with respect to the
independent measurement `y`, calculated on the nominal value of `y`. Return
zero if `x` does not depend on `y`.
Use `Measurements.derivative.(x, array)` to calculate the gradient of `x` with respect to an
array of independent measurements.
"""
derivative(a::Measurement, b::Measurement) =
derivative(a, (b.val, b.err, b.tag))

I'm not sure what you are trying to do, more generally.
#144 might be relevant.

@martinmestre
Copy link
Author

martinmestre commented Sep 19, 2023

Hi, thanks for the answer. I took the example from here, it is this example:
Measurements.derivative.(log1p(x) + y^2 - cos(x/y), [x, y, z])
Am I interpreting something wrongly?
My objective is to use Measurements.jl as a AD tool for functions that depend on unitful quantities.

@LebedevRI

This comment was marked as outdated.

@LebedevRI
Copy link

As it can be seen in
https://github.com/JuliaPhysics/Measurements.jl/blob/55332725ad6826cef711103de7a2413c3b1b1fd6/ext/MeasurementsUnitfulExt.jl
only the main public interface is unit-aware.

@martinmestre
Copy link
Author

martinmestre commented Sep 19, 2023

Hi. I do not understand which functions are part of the main public interface and which not. Thanks

@LebedevRI
Copy link

Hi. I do not understand what it means.

The error you are getting is correct, Measurements.jl does not support
calling derivative() with an Unitful arguments, only with Measurements.

@martinmestre
Copy link
Author

martinmestre commented Sep 19, 2023

I thought that measurements could have units, like

x = (98.1 ± 12.7)u"km"
y = (105.4 ± 25.6)*u"km"

Do you say that derivative() only accepts measurements without units?

@LebedevRI
Copy link

I thought that measurements could have units, like

x = (98.1 ± 12.7)u"km"
y = (105.4 ± 25.6)*u"km"

Do you say that derivative() only accepts measurements without units?

The thing is that is not a measurement with units,
it's a Measurement-typed value inside of an AbstractUnit-typed value,
and derivative() is missing special-handling to unwrap
the outer AbstractUnit type to get to the actual Measurement inside.

@martinmestre
Copy link
Author

Ok, just to understand, could you give me an example of a "measurement with units" that Measurement.jl understands ?

@LebedevRI
Copy link

All normal mathematical operations work, only the functionality that is defined in https://github.com/JuliaPhysics/Measurements.jl/blob/c12fb10d3deef0251d671028dfccb3faa9c880dd/src/utils.jl does not.

@LebedevRI
Copy link

#152 kind-of helps,
but i'm not sure yet how to actually deal with unit mismatch in derivative().

@martinmestre
Copy link
Author

Thank you very much

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

2 participants