# Transforming a function into a sequence of mutable operations on objects

**URL:** <https://discourse.julialang.org/t/transforming-a-function-into-a-sequence-of-mutable-operations-on-objects/21508>\
**Category:** General Usage\
**Created:** [March 5, 2019, 8:05pm UTC](https://discourse.julialang.org/t/transforming-a-function-into-a-sequence-of-mutable-operations-on-objects/21508 "2019-03-05T20:05:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [March 5, 2019, 8:05pm UTC](https://discourse.julialang.org/t/transforming-a-function-into-a-sequence-of-mutable-operations-on-objects/21508/1 "2019-03-05T20:05:37Z")

</div>

The following example comes up in the Taylor method for integrating differential equations, but the question keeps coming up over and over again.

Suppose I have something like the following, which is over-simplified, in particular with the wrong formula for `*` for simplicity, but contains the kernel of the question:

```julia
struct Taylor
    coeffs::Vector{Float64}
end 

add!(z::Taylor, x::Taylor, y::Taylor, n) = z.coeffs[n] = x.coeffs[n] + y.coeffs[n]
mul!(z::Taylor, x::Taylor, y::Taylor, n) = z.coeffs[n] = x.coeffs[n] * y.coeffs[n]

x = Taylor(zeros(5))
y = Taylor(zeros(5))

```

Now if a user specifies a polynomial function like  
`f(x, y) = x*x + y`,  
we need to transform it into code that produces finally a new `Taylor` object, but in order to do so, it must calculate the coefficients of the new object element by element.

Currently in `TaylorIntegration.jl`, there is a macro `@taylorize` that does this by direct and painful code generation.  
My question is if there is a better method, e.g. using operator overloading or creating an Intermediate Representation, e.g. using ModelingToolkit.jl, and doing something clever with that?

```julia
z1 = copy(x)
z2 = copy(y)
for n in 1:length(coeffs.x)
    mul!(z1, x, x, n)
    add!(z2, z1, y, n)
end

```

We really do need to do this element by element because of how the Taylor method works. Sowe need some way of persistently recording which intermediate objects get created along the way and modifying those later.

I have a rather different approach outlined in [https://github.com/dpsanders/LazyTaylorSeries.jl](https://github.com/dpsanders/LazyTaylorSeries.jl), but I think it would be very useful to see any suggestions that people can make about this kind of coding pattern.

cc @lbenet

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [March 5, 2019, 8:55pm UTC](https://discourse.julialang.org/t/transforming-a-function-into-a-sequence-of-mutable-operations-on-objects/21508/2 "2019-03-05T20:55:12Z")

</div>

> [@dpsanders](#):
>
> Taylor method for integrating differential equations

I am certainly not a julia expert, but maybe using an aproach like forwardDiff can work, they have a struct that stores a number `<:Real` and the derivative, and they overload the base operations with this struct:  
(the following code is a modification of the [dual number implementation in forwardDiff.jl](http://www.juliadiff.org/ForwardDiff.jl/latest/dev/how_it_works.html)

```julia
struct TaylorNumber{V,N<:Real} <: Real
number::N
coeffs::Vector{V}

```

and then import the base operators:

```julia
import Base: *
function *(x::taylorNumber,y::taylorNumber)
return taylorNumber(x.value+y.value,taylorMul(x.coeff,y.coeff)
end 

```

doing this, using a function of the form f(x,y) where x,y are `Real`, can automatically generate such coefficients, please correct me if am wrong.

I totally recomend reading that source code, they know what they are doing, i am not

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [March 5, 2019, 11:34pm UTC](https://discourse.julialang.org/t/transforming-a-function-into-a-sequence-of-mutable-operations-on-objects/21508/3 "2019-03-05T23:34:28Z")

</div>

Thanks, I am aware of `ForwardDiff.jl`; actually I think I might need something more like `ReverseDiff.jl`, which builds up a tape of the operations that you can play back later. (Or maybe even Cassette.jl…)

Unfortunately the difficulty here is that I need to refer back to the `same` intermediate result later to modify it.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 6, 2019, 3:35am UTC](https://discourse.julialang.org/t/transforming-a-function-into-a-sequence-of-mutable-operations-on-objects/21508/4 "2019-03-06T03:35:10Z")

</div>

This sounds a bit like the LazyExpressions from Parametron.jl: [Parametron.jl/lazyexpression.jl at master · tkoolen/Parametron.jl · GitHub](https://github.com/tkoolen/Parametron.jl/blob/master/src/lazyexpression.jl) . I wonder if there is some code (or at least some ideas) you could adapt from there.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [March 6, 2019, 4:28am UTC](https://discourse.julialang.org/t/transforming-a-function-into-a-sequence-of-mutable-operations-on-objects/21508/5 "2019-03-06T04:28:36Z")

</div>

That looks very interesting - I’ll check it out, thanks!

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [March 6, 2019, 11:15am UTC](https://discourse.julialang.org/t/transforming-a-function-into-a-sequence-of-mutable-operations-on-objects/21508/6 "2019-03-06T11:15:05Z")

</div>

This also looks similar to the macro rewritting done in JuMP in [`parse_expr.jl`](https://github.com/JuliaOpt/JuMP.jl/blob/master/src/parse_expr.jl).  
When you write `@constraint(model, x + u + y * z)`, it rewrite the expression to `JuMP.destructive_add!(JuMP.destructive_add!(copy(x), u), y, z)`.  
We plan to make this rewriting independent of JuMP as [a GSoC project](https://github.com/JuliaOpt/GSOC2019/blob/master/ideas-list.md#mutablearithmetics) as this would allow any object used in a `@constraint` expression to take advantage of the rewritting (I have a particular application in minds which is polynomials for [Sum-of-Squares](https://github.com/JuliaOpt/SumOfSquares.jl)).  
But the package would also have a, say `@expression`, macro and if your object is a subtype of, say `AbstractMutable`, and implements the mutable API, it could be constructed with `@expression` and we can share the rewritting code and not duplicate the work in JuMP, Parametron, LazyTaylorSeries, …
