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

add! does not work to add two TaylorN #156

Open
dpsanders opened this issue Feb 23, 2018 · 11 comments
Open

add! does not work to add two TaylorN #156

dpsanders opened this issue Feb 23, 2018 · 11 comments

Comments

@dpsanders
Copy link
Contributor

dpsanders commented Feb 23, 2018

julia> x, y = set_variables("x y")
2-element Array{TaylorSeries.TaylorN{Float64},1}:
  1.0 x + 𝒪(‖x‖⁷)
  1.0 y + 𝒪(‖x‖⁷)

julia> s = (x+y)^3
 1.0+ 3.0 x² y + 3.0 x y² + 1.0+ 𝒪(‖x‖⁷)

julia> TaylorSeries.add!(s, 3x)
ERROR: MethodError: no method matching add!(::TaylorSeries.TaylorN{Float64}, ::TaylorSeries.TaylorN{Float64})
Closest candidates are:
  add!(::TaylorSeries.TaylorN, ::TaylorSeries.TaylorN, ::Int64) at /Users/dpsanders/.julia/v0.6/TaylorSeries/src/arithmetic.jl:121
  add!(::TaylorSeries.TaylorN, ::TaylorSeries.TaylorN, ::TaylorSeries.TaylorN, ::Int64) at /Users/dpsanders/.julia/v0.6/TaylorSeries/src/arithmetic.jl:129
  add!(::TaylorSeries.TaylorN, ::TaylorSeries.TaylorN, ::Union{Complex, Real}, ::Int64) at /Users/dpsanders/.julia/v0.6/TaylorSeries/src/arithmetic.jl:133
  ...
@dpsanders
Copy link
Contributor Author

Maybe I'm misunderstanding what add! is for, but I actually need this!

@lbenet
Copy link
Member

lbenet commented Feb 23, 2018

The basic idea of add!(res, a, b, k) is to modify res::TaylorN (for your example), by computing the addition of a and b (both TaylorNs) for the coefficient k, so it computes res[k]=a[k]+b[k] as an in-place operation. There are different methods for add!.

I am not sure what are you after, but the following works:

julia> TaylorSeries.add!(s, 3x, 1)

julia> s
 3.0 x + 1.0+ 3.0 x² y + 3.0 x y² + 1.0+ 𝒪(‖x‖⁷)

@dpsanders
Copy link
Contributor Author

I want to add a TaylorSeries t in place to another one s (modifying s) via add!(s, t).

@dpsanders
Copy link
Contributor Author

I don't understand what the coefficient k is for, e.g.

julia> s = (x + y)^2

julia> TaylorSeries.add!(s, x, 1)

julia> s
 1.0 x + 1.0 x² + 2.0 x y + 1.0 y² + 𝒪(‖x‖⁷)

julia> TaylorSeries.add!(s, x, 2)

julia> s
 1.0 x + 𝒪(‖x‖⁷)

@lbenet
Copy link
Member

lbenet commented Feb 23, 2018

The coefficient k is the only you compute. So you need something like

for k in 0:s.order
    TaylorSeries.add!(s, s, t, k)
end
s

For example:

julia> s = (x + y)^2
 1.0+ 2.0 x y + 1.0+ 𝒪(‖x‖⁷)

julia> t = (x-y)^2
 1.0- 2.0 x y + 1.0+ 𝒪(‖x‖⁷)

julia> for k in 0:s.order
           TaylorSeries.add!(s, s, t, k)
       end

julia> s
 2.0+ 2.0+ 𝒪(‖x‖⁷)

julia> t
 1.0- 2.0 x y + 1.0+ 𝒪(‖x‖⁷)

@dpsanders
Copy link
Contributor Author

dpsanders commented Feb 23, 2018 via email

@lbenet
Copy link
Member

lbenet commented Feb 23, 2018

I'm not sure what are you after, but in the example above, this is what you get with s+t.

@dpsanders
Copy link
Contributor Author

dpsanders commented Feb 23, 2018 via email

@lbenet
Copy link
Member

lbenet commented Feb 23, 2018

It does work, though not as an in-place operation.

julia> using TaylorSeries

julia> x, y = set_variables("x y")
2-element Array{TaylorSeries.TaylorN{Float64},1}:
  1.0 x + 𝒪(‖x‖⁷)
  1.0 y + 𝒪(‖x‖⁷)

julia> s = (x + y)^2
 1.0+ 2.0 x y + 1.0+ 𝒪(‖x‖⁷)

julia> t = (x-y)^2
 1.0- 2.0 x y + 1.0+ 𝒪(‖x‖⁷)

julia> s = s+t
 2.0+ 2.0+ 𝒪(‖x‖⁷)

@dpsanders
Copy link
Contributor Author

dpsanders commented Feb 23, 2018 via email

@lbenet
Copy link
Member

lbenet commented Feb 23, 2018

Currently there is no specific method that does that. I guess you could add add!(s,t,u) (with no integer coefficient) to do the for-loop illustrated above.

The current use of all those in-place functions (add!, mul!, sin!, ...) is to optimize the allocations in TaylorIntegration.jl. I've got pretty far with that, but there are few issues which are unresolved; see this PR.

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