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

Matrix evaluation for Taylor1 and TaylorN polynomials #144

Merged
merged 7 commits into from
Jan 12, 2018

Conversation

blas-ko
Copy link
Contributor

@blas-ko blas-ko commented Dec 29, 2017

Straightforward name! This allows the user to evaluate Taylor-element matrices via evaluate or the function feature for evaluation A(x).

@coveralls
Copy link

coveralls commented Dec 29, 2017

Coverage Status

Coverage decreased (-0.2%) to 97.168% when pulling 6e19f27 on blas-ko:matrix_evaluation into 10ab42c on JuliaDiff:master.

@PerezHz
Copy link
Contributor

PerezHz commented Jan 5, 2018

Really nice! Works like a charm! 👏

@PerezHz
Copy link
Contributor

PerezHz commented Jan 5, 2018

travis failure on nightly (macOS) seems to be related to a test which asks for identical equality (test/onevariable.jl line 456):

    @test A*B==Y

The occasional failure of this test is a recurrent issue which has been usually handled by restarting tests, but perhaps it could be fixed if we use an approximate equality, i.e. something like:

    @test norm(A*B-Y,1)  eps(max(norm(A*B,1), norm(Y,1)))
    #or something like: 
    @test norm(A*B-Y,1)  eps()*max(norm(A*B,1), norm(Y,1))

Note that I didn't use isapprox, since A, B and Y are Vector{Taylor1}. So an additional suggestion is to extend isapprox methods for Vector{Taylor1}, etc.

@blas-ko
Copy link
Contributor Author

blas-ko commented Jan 5, 2018

Really nice! Works like a charm! 👏

Many thanks! =)

The occasional failure of this test is a recurrent issue which has been usually handled by restarting tests, but perhaps it could be fixed if we use an approximate equality.

Nice that you noticed this! I actually had no idea on why travis was failing!

So an additional suggestion is to extend isapprox methods for Vector{Taylor1}

Maybe this suffices:

import Base: norm, isapprox

norm(v::Vector{T},p::Real=2) where T <: AbstractSeries = norm(norm.(v,p),p)

function isapprox(x::Vector{T}, y::Vector{S}; rtol::Real=rtoldefault(T,S), atol::Real=0.0,
        nans::Bool=false) where {T<:AbstractSeries, S<:AbstractSeries}

    x == y || norm(x-y,1) <= atol + rtol*max(norm(x,1), norm(y,1)) ||
        (nans && isnan(x) && isnan(y))
end

I've tried this with silly examples and does the job, but I'm not sure is the best way to go.

@PerezHz
Copy link
Contributor

PerezHz commented Jan 5, 2018

@blas-ko do you think a similar thing could be done for 1 and 2 dimensional SubArrays? That would be helpful when working with TaylorIntegration outputs...

@PerezHz
Copy link
Contributor

PerezHz commented Jan 6, 2018

I've tried this with silly examples and does the job, but I'm not sure is the best way to go.

Well, it LGTM! I think for the moment being you could try that, unless @lbenet or @dpsanders have some other suggestions 😄

@PerezHz
Copy link
Contributor

PerezHz commented Jan 6, 2018

Upon adding your proposal to the corresponding code files, and substituting @test A*B == Y by either

        @test A*B  Y atol=0.0 rtol=eps()

or

        @test isapprox(A*B, Y, atol=0.0, rtol=eps())

tests pass 👍

@PerezHz
Copy link
Contributor

PerezHz commented Jan 6, 2018

I mean, tests pass locally

@lbenet
Copy link
Member

lbenet commented Jan 8, 2018

Thanks for the contribution, LGTM!

I've just restarted the travis job in mac for Julia 0.7 which was failing. Let's see if it passes now and then I'll merge...

@blas-ko
Copy link
Contributor Author

blas-ko commented Jan 8, 2018

I've just restarted the travis job in mac for Julia 0.7 which was failing. Let's see if it passes now and then I'll merge...

I'm about to push changes suggested by @PerezHz, so it includes SubArray types in evaluation! Also, should I add isapprox extension in this same PR?

@lbenet
Copy link
Member

lbenet commented Jan 8, 2018

Ok. Then, I'll wait for the next test round with travis!

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.1%) to 97.241% when pulling 62bb86b on blas-ko:matrix_evaluation into 10ab42c on JuliaDiff:master.

@lbenet
Copy link
Member

lbenet commented Jan 8, 2018

Tests are not passing in Julia nightly on Mac, seemingly with an unrelated issue with the matrix multiplication tests; I'm restarting that test...

@lbenet
Copy link
Member

lbenet commented Jan 8, 2018

By some reason i do not understand, tests fail locally on nighties on a mac with Julia 0.7.0-DEV.3329. The problem appears in matrix multiplication (@test A*B==Y).

Maybe we should allow failures in Julia nightly.

cc: @dpsanders

@blas-ko
Copy link
Contributor Author

blas-ko commented Jan 9, 2018

By some reason i do not understand, tests fail locally on nighties on a mac with Julia 0.7.0-DEV.3329

@PerezHz noted that for the A*B == Y test "the occasional failure of this test is a recurrent issue which has been usually handled by restarting tests, but perhaps it could be fixed if we use an approximate equality".

Is it ok if I push the isapprox extension for vectors and see if this solves the inconsistency? @lbenet

@lbenet
Copy link
Member

lbenet commented Jan 9, 2018

I guess what you mean is to replace line 452 in test/onevariable.jl (which is the problematic test) by something like

@test reduce(&, A*B .≈ Y )

Is that what you mean?

In any case, just go ahead and let's see how travis reacts.

@dpsanders
Copy link
Contributor

Can't you just do evaluate.(A, 3)?

@blas-ko
Copy link
Contributor Author

blas-ko commented Jan 9, 2018

Can't you just do evaluate.(A, 3)?

Yes, but it's much slower and it doesn't have the function feature A(3.0)

using TaylorSeries, BenchmarkTools
t = Taylor1(5)
A = [t 2t ; 3t 4t]

@btime evaluate.(A,3.0)
  4.385 μs (24 allocations: 1.19 KiB)

@btime evaluate(A,3.0)
  282.176 ns (12 allocations: 592 bytes)

@dpsanders
Copy link
Contributor

dpsanders commented Jan 9, 2018

Ah OK, thanks.

Note that you need $ in the call to @btime:

@btime evaluate.($A, 3.0)

@@ -449,7 +456,7 @@ end
A_mul_B!(Y,A,B)

# do we get the same result when using the `A*B` form?
@test A*B==Y
@test A*BY
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks @blas-ko for this change! I'd only insist in specifying both atol and rtol as:

@test A*B ≈ Y atol=0.0 rtol=eps()

otherwise, since we are being consistent with Base.isapprox, the default rtol value is sqrt(eps()), which is ~1e-8

@PerezHz
Copy link
Contributor

PerezHz commented Jan 9, 2018

I guess what you mean is to replace line 452 in test/onevariable.jl (which is the problematic test) by something like @test reduce(&, A*B .≈ Y )

note that @blas-ko defined an isapprox method for vectors of polynomials, so now we can actually do A*B ≈ Y where both the LHS and RHS are vectors of polynomials

@lbenet
Copy link
Member

lbenet commented Jan 9, 2018

Why not using for the test broadcasting? In current master, the following works:

julia> using TaylorSeries

julia> a = Taylor1(randn(4));

julia> a  a
true

julia> [a] .≈ [a]   # the warnings are related to Julia 0.7
┌ Warning: `rtoldefault(x, y)` is deprecated, use `rtoldefault(x, y, 0)` instead.
│   caller = isapprox at other_functions.jl:189 [inlined]
└                                                  @ Core other_functions.jl:189
1-element BitArray{1}:
 true

julia> [a a] .≈ [a a]
┌ Warning: `rtoldefault(x, y)` is deprecated, use `rtoldefault(x, y, 0)` instead.
│   caller = isapprox at other_functions.jl:189 [inlined]
└                                                  @ Core other_functions.jl:189
1×2 BitArray{2}:
 true  true

The result of .≈ is some sort of array; this is what motivated in my previous comment to suggest @test reduce(&, A*B .≈ Y ), without the need of extending isapprox.

julia> reduce(&, [a a] .≈ [a a])
true

julia> reduce(&, [a a] .≈ [a 2a])
┌ Warning: `Array{T}(m::Int) where T` is deprecated, use `Array{T}(uninitialized, m)` instead.
│   caller = *(::Int64, ::Taylor1{Float64}) at arithmetic.jl:215
└                                               @ TaylorSeries arithmetic.jl:215
false

Does extending isapprox make things faster as you showed above for evaluate?

@coveralls
Copy link

coveralls commented Jan 9, 2018

Coverage Status

Coverage decreased (-0.2%) to 97.171% when pulling c22ac64 on blas-ko:matrix_evaluation into 10ab42c on JuliaDiff:master.

@PerezHz
Copy link
Contributor

PerezHz commented Jan 9, 2018

We could do both:

@test reduce(&, A*B .≈ Y )
@test A*B  Y atol=0.0 rtol=eps()

I agree that for fixing this test we have two solutions and one doesn't involve defining new methods. I just thought that it'd be meaningful to define isapprox for vectors of polynomials

@blas-ko
Copy link
Contributor Author

blas-ko commented Jan 12, 2018

I'll push the broadcasting approach on the tests. Do you think is a good idea to extend isapprox as @PerezHz suggested, @lbenet ? I included such extension in my last commit, I can delete that and fix the tests as suggested.

@lbenet
Copy link
Member

lbenet commented Jan 12, 2018

Let's leave it as it is now, and,if it is not necessary, we'll remove it eventually.

@lbenet lbenet merged commit 34f7a48 into JuliaDiff:master Jan 12, 2018
@lbenet
Copy link
Member

lbenet commented Jan 12, 2018

Merged! Thanks a lot!

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.

5 participants