-
Notifications
You must be signed in to change notification settings - Fork 53
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
Fuse operations and methods for mixed types #95
Conversation
In == we avoid using fixorder...
…onstructors Also, renamed check_taylor1_order! -> resize_coeffs1! and check_hpoly_order! -> resize_coeffsHP!
Pushed another commit to increase coverage |
To my surprise (at least in Julia 0.5), this PR seems to solve the cases described in #62 and #78 related to the order of loading Here is an example: julia> using TaylorSeries, ArbFloats
julia> t1=Taylor1([ArbFloat(2), ArbFloat(3)])
2 + 3 t + 𝒪(t²)
julia> exp(t1)
7.389056098930650227230427 + 22.16716829679195068169128 t + 𝒪(t²) and the other way around julia> using ArbFloats, TaylorSeries
julia> t1=Taylor1([ArbFloat(2), ArbFloat(3)])
2 + 3 t + 𝒪(t²)
julia> exp(t1)
7.389056098930650227230427 + 22.16716829679195068169128 t + 𝒪(t²)
@PerezHz @JeffreySarnoff @fhenneke Can you confirm this is indeed the case? |
Wow, quite unexpected! On my machine, the order of loading seems to be fixed for ArbFloats, ForwardDiff, and ValidatedNumerics! |
@dpsanders Do you want to take a look on this PR? (Sorry for making it so complicate.) Tests pass and have been enlarged to cover other situations (#94), including unexpected ones (#62 and #78). |
Yes, but I won't be able to take a proper look at it until Sunday. |
Take your time. You have always good comments and insight! Thanks! |
Hmm, I see that not using It's also a bit unfortunate that what was now relatively simple code for |
I agree, though your idea in #96 may help a little bit... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general looks very good.
There are a few suggestions to simplify syntax, and a few places where it feels like some refactoring could be useful, which I have pointed out.
But these can also be postponed for another PR.
@@ -8,27 +8,35 @@ | |||
|
|||
## Auxiliary function ## | |||
|
|||
function check_taylor1_order!{T<:Number}(coeffs::Array{T,1}, order::Int) | |||
""" | |||
resize_coeffs1!{T<Number}(coeffs::Array{T,1}, order::Int) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the changes to be able to handle Taylor
s of different orders, is this function necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved part of the constructor of Taylor1
to this function to simplify the code. I'm not sure how to answer the question, since this function resizes the coefficients if you ask for a larger order. For instannce, you may call Taylor1([0,1],10)
and at some point this function is called to resize the coefficients to size 11.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My point is that there will no longer be any need to resize the coefficients, ever!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I truly hope so!
src/auxiliary.jl
Outdated
@inbounds coeffs[i] = zero(coeffs[1]) | ||
end | ||
nothing | ||
coeffs[lencoef+1:order+1] .= zero(coeffs[1]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`coeffs[lencoef+1:end]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it should work... I'll push another commit with this change.
|
||
|
||
# A `Number` which is not an `AbstractSeries` | ||
const NumberNotSeries = Union{setdiff(subtypes(Number), [AbstractSeries])...} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is clever!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
R = promote_type(T,S) | ||
numVars = get_numvars() | ||
suma = convert(TaylorN{R}, a) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why there is this convert
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type stability; if you simply use assignment (suma = a
) it may change type inside the loop.
suma = convert(TaylorN{R}, a) | ||
|
||
for nv = 1:numVars | ||
@inbounds for nv = 1:numVars | ||
suma = horner(suma, (nv, vals[nv])) | ||
end | ||
|
||
return suma.coeffs[1].coeffs[1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels like this is unnecessary and suma
should just be of the final type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe. The way it is done here is that a TaylorN
polynomial is evaluated substituting each variable, one by one; maybe it's not the best way of doing it...
src/arithmetic.jl
Outdated
end | ||
function $f(a::HomogeneousPolynomial) | ||
v = similar(a.coeffs) | ||
v .= $f(a.coeffs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two lines can be reduced to v = $f(a.coeffs)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess so... Thanks for the catch!
end | ||
R = eltype(aux) | ||
coeffs = Array{HomogeneousPolynomial{Taylor1{R}}}(a.order+1) | ||
coeffs .= a.coeffs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two lines should be equivalent to a convert
?
@inbounds aux = a * b.coeffs[1] | ||
R = typeof(aux) | ||
coeffs = Array{R}(length(b.coeffs)) | ||
coeffs .= a .* b.coeffs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels like these three lines form a recurring pattern that should be factored out into a function somehow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess they appear like that to assure type stability.
@inbounds aux = a * b.coeffs[1] | ||
R = typeof(aux) | ||
coeffs = Array{R}(length(b.coeffs)) | ||
coeffs .= a .* b.coeffs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a lot of repetition here with the previous definitions.
@@ -262,7 +292,7 @@ Inputs are the `kcoef`-th coefficient, and the vectors of the expansion coeffici | |||
""" | |||
function mulHomogCoef{T<:Number}(kcoef::Int, ac::Array{T,1}, bc::Array{T,1}) | |||
kcoef == 0 && return ac[1] * bc[1] | |||
coefhomog = zero(T) | |||
coefhomog = zero(ac[1]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
zero(T)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as some comment above; the difference appears for Taylor1{TaylorN{T}}
...
I'll revise the code and push another commit. |
@dpsanders Thanks for your review! And, again, I'm sorry that this PR became so extended and messy. I just pushed another commit which address many of your observations, but not all. As you suggest, this can be part of another commit. Once we get green lights, I think this should be merged. Somewhere, which i can't find now, you also suggested that the tests should reflect more the file that they are actually testing. I will reorganize them as well, but in another PR. |
Got green lights. Merging. |
Nice! |
This PR began as an attempt to get rid of the use
fixorder
, which creates copies of Taylor1 objects. This has been accomplished to a certain extent using dot-operatotions and dot-functions. But I couldn't get rid of some cases.While working on this, #92 appeared followed by #94. I think I have solved this now by introducing new methods, and restricting more carefully some methods according to their methods.