Import same module as two independent identities

Suppose I have a module

module A


function set(y)
    global x


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

import A as A1
import A as A2
# 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}
set!(a::A, y) = a.x = y
function Base.getproperty(a::A, s::Symbol)
    s === :set && return Base.Fix1(set!, a)
    getfield(a, s)


julia> a1 = A(4)

julia> a1.set(6)

julia> a1

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
The coefficients are controlled by 3 knobs,

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")

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

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


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!


Thanks for your information.