I am currently using Julia to do some fluid computation. Since I want to see the influence of some variables, like time steps, I need to store and prepare other persistent variables at first, and then pass the variable I am interested in to start the real computation. In Python I will create an object like
class Solver:
def __init__(self, space_step, ...):
self.dx = space_step
self.matrices = generate_matrix() # generate some matrices for iteration from the persistent information
def run(self, viscosity, time_step, ...):
iteration(self.dx, self.matrices, viscosity, time_step, ...) # some computation code
and then use it like
solver = Solver(space_step, ...)
result = solver.run(viscosity, time_step, ...)
This will compute the matrices only once. But since Julia does not have class, I cannot do things in this way. I have tried to write some factory function separate the persistent and temporary information, like
function solver_factory(space_step, ..., viscosity, time_step, ...)
dx = space_step
matrices = generate_matrix()
solver(viscosity, time_step, ...) = iteration(dx, matrices, viscosity, time_step, ...)
end
Then use it like
solver(viscosity, time_step, ...) = solver_factory(space_step, ..., viscosity, time_step, ...)
result = solver(viscosity, time_step, ...)
However, it will call generate_matrix()
every time when I change the temporary information, which is unnecessary as they will not change. Is there a way to avoid this redundancy, like object.method() way in Python? Thank you very much!