-
Notifications
You must be signed in to change notification settings - Fork 51
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
Add getindex
, setindex!
methods for ::Colon
#101
Conversation
…sts; add new ::UnitRange setindex! method for Taylor1
I don't really understand why you would want to be able to modify a Taylor expansion. |
Well, for example, when solving an ODE using Taylor's method, it is useful to allocate a |
Just to make it clear (which it wasn't for me), this PR allows the following to work: julia> y[1:5] = rand(5)
5-element Array{Float64,1}:
0.996061
0.430487
0.776109
0.207677
0.353624 This is pretty useful, as @PerezHz points out, for in-place evaluations (in TaylorIntegration.jl). |
Can you extend this even further, to allow dot-operations that avoid allocations? Following with your example, it would be nice that the following would work: julia> y = sin( Taylor1(16) );
julia> y[1:5] .= rand.()
5-element Array{Float64,1}:
0.654379
0.412568
0.460999
0.806723
0.885951
julia> y[1:5]
5-element Array{Float64,1}:
0.0
1.0
0.0
-0.166667
0.0 The last line shows that it didn't work. Yet, if I do the same with julia> y.coeffs[1:5] .= rand.()
5-element SubArray{Float64,1,Array{Float64,1},Tuple{UnitRange{Int64}},true}:
0.340261
0.761345
0.913692
0.926201
0.570581
julia> y[1:5]
5-element Array{Float64,1}:
0.340261
0.761345
0.913692
0.926201
0.570581 |
Ok, so the problem was that while the syntax getindex(a::Taylor1, n::UnitRange) = a.coeffs[n] In order to fix this, what I did in the last commit that I just pushed was to modify the getindex(a::Taylor1, n::UnitRange) = view(a.coeffs, n) and analogously for With this change, now everything should be working properly: julia> y = sin(t)
1.0 t - 0.16666666666666666 t³ + 0.008333333333333333 t⁵ - 0.0001984126984126984 t⁷ + 2.7557319223985893e-6 t⁹ - 2.505210838544172e-8 t¹¹ + 1.6059043836821616e-10 t¹³ - 7.647163731819817e-13 t¹⁵ + 𝒪(t¹⁷)
julia> y[1:5] .= rand.()
5-element SubArray{Float64,1,Array{Float64,1},Tuple{UnitRange{Int64}},true}:
0.915906
0.239288
0.232181
0.246147
0.473498
julia> y
0.9159064943913973 + 0.239287582828001 t + 0.23218121042262174 t² + 0.24614747149358918 t³ + 0.47349817383751724 t⁴ + 0.008333333333333333 t⁵ - 0.0001984126984126984 t⁷ + 2.7557319223985893e-6 t⁹ - 2.505210838544172e-8 t¹¹ + 1.6059043836821616e-10 t¹³ - 7.647163731819817e-13 t¹⁵ + 𝒪(t¹⁷)
julia> y[1:5]
5-element SubArray{Float64,1,Array{Float64,1},Tuple{UnitRange{Int64}},true}:
0.915906
0.239288
0.232181
0.246147
0.473498 4 out of 4 travis tests are passing on my fork (PerezHz/TaylorSeries.jl); but just noticed that 1 out 4 travis tests failed on the JuliaDiff/TaylorSeries.jl repo (only julia 0.6 on OS X failed) |
I restarted the tests, and now they all pass. Coverage indicates that there are few methods of Aside from this, LGTM! |
Thanks!
Sure, actually I'm working on it already! |
Just finished adding the missing tests for julia> using TaylorSeries;
julia> p = set_variables("x", numvars=2, order=3);
julia> x = sin(p[1]+p[2]*p[1])+p[2]^2-(1-p[1])^3
- 1.0 + 4.0 x₁ - 3.0 x₁² + 1.0 x₁ x₂ + 1.0 x₂² + 0.8333333333333334 x₁³ + 𝒪(‖x‖⁴)
julia> x[1]
- 1.0
julia> x.coeffs[1] = HomogeneousPolynomial(1)^2
1.0 x₁²
julia> x[1]
1.0 x₁²
julia> x.coeffs[:] = HomogeneousPolynomial(1)^2
1.0 x₁²
julia> x
1.0 x₁² + 1.0 x₁² + 1.0 x₁² + 1.0 x₁² + 𝒪(‖x‖⁴) I would think that this is some unwanted behaviour for julia> using TaylorSeries
julia> p = set_variables("x", numvars=2, order=3)
2-element Array{TaylorSeries.TaylorN{Float64},1}:
1.0 x₁ + 𝒪(‖x‖⁴)
1.0 x₂ + 𝒪(‖x‖⁴)
julia> x = sin(p[1]+p[2]*p[1])+p[2]^2-(1-p[1])^3
- 1.0 + 4.0 x₁ - 3.0 x₁² + 1.0 x₁ x₂ + 1.0 x₂² + 0.8333333333333334 x₁³ + 𝒪(‖x‖⁴)
julia> x[:] = HomogeneousPolynomial(1)^3
1.0 x₁³
julia> x[1] = HomogeneousPolynomial(1)^2
1.0 x₁²
julia> x[2:3] = HomogeneousPolynomial(2)^2
1.0 x₂²
julia> x
1.0 x₁² + 1.0 x₂² + 1.0 x₂² + 1.0 x₁³ + 𝒪(‖x‖⁴) Has this been discussed before? |
LGTM. Merging. |
Thanks! |
This PR is aimed at solving the issue mentioned in #100. It adds
getindex
andsetindex!
methods for::Colon
, along with corresponding tests. It also adds a newsetindex!
method forTaylor1
, which allows the user to do:Similar new methods for
setindex!
have been added forTaylorN
andHomogeneousPolynomial
. Tests are passing in travis. Feedback is welcome!