English 中文(简体)
How to set a local function to be one of two functions in the global namespace and then use that function in even deeper local namespaces in Julia?
原标题:

I have a numerical scheme which I would like to optimize as well as make more readable by organizing it as follows. I have two numerical schemes, "numeric_method_1" and "numeric_method_2" which both calculate the next step of an interation and run many, many times. They are called by the function "do_single_step" which runs fewer times and in turn is called by "run_scheme" which runs the entire scheme and is only called once. I would like to have an initializer function/wrapper called "initialize_and_run" which, given a parameter, would declare a function "chosen_numeric_method" that refers to one of "numeric_method_1" or "numeric_method_2" and is used in all functions and subscopes that follow.

In code, what I would like to have is the following:

numeric_method_1 = function(x)
    return x*x #arbitrary 
end

numeric_method_2 = function(x)
    return x*x*x #arbitrary
end

do_single_step = function(x)
    return chosen_numeric_method(x)
end

run_scheme = function(x)
    return do_single_step(x)
end

initialize_and_run = function(x,which)
    if which
        chosen_numeric_method = numeric_method_1
    else
        chosen_numeric_method = numeric_method_2
    end
    
    ##
    
    return run_scheme(x) 
end

As you can see, the "initialize_and_run" function takes a boolean parameter "which", that lets you choose which numeric method will be used deeper within the scheme. However, this does not run as one obtains the following error:

UndefVarError: chosen_numeric_method not defined

I understand, from looking into this periodically, that it is to do with the fact that the function "do_single_step" has the outermost scope, and so it can only see functions which also have the outermost scope.

I also understand that there were other options. For one, I could have rewritten the above to pass the "chosen_numeric_method" down the scope to where I need it:

numeric_method_1 = function(x)
    return x*x
end

numeric_method_2 = function(x)
    return x*x*x
end

do_single_step = function(x,chosen_numeric_method)
    return chosen_numeric_method(x)
end

run_scheme = function(x,chosen_numeric_method)
    return do_single_step(x,chosen_numeric_method)
end

initialize_and_run = function(x,which)
    if which
        chosen_numeric_method = numeric_method_1
    else
        chosen_numeric_method = numeric_method_2
    end
    
    ##
    
    return run_scheme(x,chosen_numeric_method) 
end

This runs, however, this will become problematic as I have more and more functions to pass down and as they need to be passed deeper and deeper. I could also do the if statement inside the innermost scope where I need it:

numeric_method_1 = function(x)
    return x*x
end

numeric_method_2 = function(x)
    return x*x*x
end

do_single_step = function(x,which)
    if which
        return numeric_method_1(x)
    else
        return numeric_method_2(x)
    end
end

run_scheme = function(x,which)
    return do_single_step(x,which)
end

initialize_and_run = function(x,which)
    return run_scheme(x,which) 
end

This also runs, however, the innermost function "do_single_step" is run many, many times and so I worry about this constant checking as it may make things slower. In addition, this is once again just not very pleasing style as I knew already at the initializer step which numeric function will be used for the entire run period.

Is there really no way to achieve my desired configuration (the first code block) and have it be performant by somehow telling the compiler in the initializer step Hey, I want the function "chosen_numeric_method" to be avaliable in all subscopes of this one ?

问题回答

Does this describe your problem?

numeric_method_1 = function(x)
    return x*x #arbitrary 
end
numeric_method_2 = function(x)
    return x*x*x #arbitrary
end

numeric_method = function(x, method)
    go_down = function(x, method, depth)
        if depth == 10
            return method(x)
        else
            return go_down(x, method, depth+1)
        end
    end
    var = numeric_method_1
    return go_down(x, var, 0)
end




相关问题
Optimizing a LAN server for a game

I m the network programmer on a school game project. We want to have up to 16 players at once on a LAN. I am using the Server-Client model and am creating a new thread per client that joins. ...

SQL Table Size And Query Performance

We have a number of items coming in from a web service; each item containing an unknown number of properties. We are storing them in a database with the following Schema. Items - ItemID - ...

Most optimized way to store crawler states?

I m currently writing a web crawler (using the python framework scrapy). Recently I had to implement a pause/resume system. The solution I implemented is of the simplest kind and, basically, stores ...

Do bitwise operations distribute over addition?

I m looking at an algorithm I m trying to optimize, and it s basically a lot of bit twiddling, followed by some additions in a tight feedback. If I could use carry-save addition for the adders, it ...

Improve INSERT-per-second performance of SQLite

Optimizing SQLite is tricky. Bulk-insert performance of a C application can vary from 85 inserts per second to over 96,000 inserts per second! Background: We are using SQLite as part of a desktop ...

Profiling Vim startup time

I’ve got a lot of plugins enabled when using Vim – I have collected plugins over the years. I’m a bit fed up with how long Vim takes to start now, so I’d like to profile its startup and see which of ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签