Import same module as two independent identities

Suppose I have a module

module A

x=1

function set(y)
    global x
    x=y
end

end

Is there a way to import A twice, so that I have two different variables A.x as following

import A as A1
#A1.x=41
A1.set(41)
import A as A2
#A2.x=1024
A2.set(1024)
# A2.x and A1.x are two different variables

I don’t believe so, but why do you want this?

I’d recommend using structs:

mutable struct A{T}
    x::T
end
set!(a::A, y) = a.x = y
function Base.getproperty(a::A, s::Symbol)
    s === :set && return Base.Fix1(set!, a)
    getfield(a, s)
end

yielding

julia> a1 = A(4)
A{Int64}(4)

julia> a1.set(6)
6

julia> a1
A{Int64}(6)

I’d also recommend not doing the set thing with getproperty and just calling set! directly.

1 Like

I am using the TaylorSeries package for Taylor polynomial expansions.

There is a type TaylorN in this package, which describes a polynomial with N variable. The number of variables and orders can be set via

using TaylorSeries
x, y = set_variables("x y", order=2);

However, I would like to obtain a composite type, like TaylorN{TaylorN{Float64}}. The outer TaylorN has different orders and number of variables with inner TaylorN.

Let me give you an example
f(x,y)=a_{00}+a_{10}x+a_{01}y+a_{20}x^2+a_{11}xy+a_{02}y^2
The coefficients are controlled by 3 knobs,
a_{ij}(k_1,k_2,k_3)=c_{000}+c_{100}k_1+c_{010}k_2+c_{001}k_3

The following code doesn’t work,

using TaylorSeries
x1,x2=set_variables("x1 x2",numvars=2,order=5)
y1,y2,y3=set_variables("y1 y2 y3",numvars=3,order=4) 
# x1 and x2 become invalid, because the above command 
#      changes the maximum order and number of variables

Therefore, I wonder if there is a way so that

import TaylorSeries as TS1
import TaylorSeries as TS2
x1,x2=TS1.set_variables("x1 x2",numvars=2,order=5)
y1,y2,y3=TS2.set_variables("y1 y2 y3",numvars=3,order=4)
# typeof(x1)=TS1.TaylorN{Float64}
# typeof(y1)=TS2.TaylorN{Float64}
# both x1 and y1 are valid, and their types--- TS1.TalorN and TS2.TaylorN---are different 

I have a stupid method to solve this. But it seems weird.

module TS1
      include("path to TaylorSeries.jl")
end

module TS2
      include("path to TaylorSeries.jl")
end

# Now TaylorN in TS1 and TaylorN in TS2 are two different structs
#      and they have their own method of set_variables 

Hi!

The way TaylorSeries works internally for the multivariable case, is by defining some internal tables which depend on the maximum degree of your polynomials and on the number of variables; those tables are some constant (but mutable) internal globals. That’s the reason that, if you run twice set_variables you overwrite the previous meaning of the maximum order and number of variables.

A possibility to achieve what you are after (including the variable order) is to use nested Taylor1 variables. So a polynomial of order two is a polynomial in one variable whose coefficients are polynomials in one variable, a similarly to more variables, as you describe above. While the possibility exists, we haven’t had the time to polish it and make it nicer for the user; see WIP: Nested Taylors by blas-ko · Pull Request #126 · JuliaDiff/TaylorSeries.jl · GitHub. Moreover, we have doubts that this is as performant as the TaylorN approach. In any case, I am interested in your solution!

2 Likes

Thanks for your information.