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

Fuse operations and methods for mixed types #95

Merged
merged 12 commits into from
Mar 13, 2017
Merged

Fuse operations and methods for mixed types #95

merged 12 commits into from
Mar 13, 2017

Conversation

lbenet
Copy link
Member

@lbenet lbenet commented Mar 10, 2017

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.

@coveralls
Copy link

coveralls commented Mar 10, 2017

Coverage Status

Coverage decreased (-2.07%) to 96.204% when pulling fff6d00 on fuse_operations into b7eb18f on master.

@lbenet
Copy link
Member Author

lbenet commented Mar 10, 2017

Pushed another commit to increase coverage

@lbenet
Copy link
Member Author

lbenet commented Mar 10, 2017

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 TaylorSeries and another package.

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?

@coveralls
Copy link

coveralls commented Mar 10, 2017

Coverage Status

Coverage decreased (-1.4%) to 96.847% when pulling 61af53a on fuse_operations into b7eb18f on master.

@PerezHz
Copy link
Contributor

PerezHz commented Mar 10, 2017

Wow, quite unexpected! On my machine, the order of loading seems to be fixed for ArbFloats, ForwardDiff, and ValidatedNumerics!

@coveralls
Copy link

coveralls commented Mar 10, 2017

Coverage Status

Coverage decreased (-0.05%) to 98.222% when pulling 025cba8 on fuse_operations into b7eb18f on master.

@coveralls
Copy link

coveralls commented Mar 10, 2017

Coverage Status

Coverage increased (+0.4%) to 98.626% when pulling e23dec7 on fuse_operations into b7eb18f on master.

@lbenet
Copy link
Member Author

lbenet commented Mar 10, 2017

@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).

@dpsanders
Copy link
Contributor

Yes, but I won't be able to take a proper look at it until Sunday.
If you prefer to merge now then go ahead.

@lbenet
Copy link
Member Author

lbenet commented Mar 11, 2017

Take your time. You have always good comments and insight! Thanks!

@dpsanders
Copy link
Contributor

Hmm, I see that not using fixorder does complicate the code quite a lot.

It's also a bit unfortunate that what was now relatively simple code for Taylor1 is mixed up with more complicated code for TaylorN?

@lbenet
Copy link
Member Author

lbenet commented Mar 12, 2017

I agree, though your idea in #96 may help a little bit...

Copy link
Contributor

@dpsanders dpsanders left a 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)
Copy link
Contributor

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 Taylors of different orders, is this function necessary?

Copy link
Member Author

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.

Copy link
Contributor

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!

Copy link
Member Author

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])
Copy link
Contributor

Choose a reason for hiding this comment

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

`coeffs[lencoef+1:end]

Copy link
Member Author

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])...}
Copy link
Contributor

Choose a reason for hiding this comment

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

This is clever!

Copy link
Member Author

Choose a reason for hiding this comment

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

Somehow beyond my understanding, I think this is at the core why problems reported in #62 and #94 are solved.... or Julia-magic.

R = promote_type(T,S)
numVars = get_numvars()
suma = convert(TaylorN{R}, a)
Copy link
Contributor

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.

Copy link
Member Author

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]
Copy link
Contributor

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.

Copy link
Member Author

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...

end
function $f(a::HomogeneousPolynomial)
v = similar(a.coeffs)
v .= $f(a.coeffs)
Copy link
Contributor

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).

Copy link
Member Author

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
Copy link
Contributor

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
Copy link
Contributor

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.

Copy link
Member Author

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
Copy link
Contributor

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])
Copy link
Contributor

Choose a reason for hiding this comment

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

zero(T).

Copy link
Member Author

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}}...

@lbenet
Copy link
Member Author

lbenet commented Mar 13, 2017

I'll revise the code and push another commit.

@lbenet
Copy link
Member Author

lbenet commented Mar 13, 2017

@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.

@coveralls
Copy link

coveralls commented Mar 13, 2017

Coverage Status

Coverage increased (+0.4%) to 98.625% when pulling 60c9421 on fuse_operations into b7eb18f on master.

@lbenet
Copy link
Member Author

lbenet commented Mar 13, 2017

Got green lights. Merging.

@dpsanders
Copy link
Contributor

Nice!

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.

4 participants