Python scipy函数不接受全局变量

时间:2018-06-15 14:38:08

标签: python scipy global-variables

我正在尝试解决线性方程组。因为其中有很多我使用的是Container( decoration: BoxDecoration( image: DecorationImage( image: NetworkImage("http://via.placeholder.com/350x150")))) 方法,来自Newton-Krylov。对于那些不熟悉的人,它需要一组方程式和初始猜测。但是,方程组的定义取决于它自己的参数,但没有办法将它们输入scipy.minimize求解器。

以下是我编写的代码

Newton-Krylov

其中函数import networkx as nx import scipy as sp import numpy as np import math from scipy.optimize import newton_krylov def gen_r_scores_anderson(): datasets = [ 'WTW_decades/1960wtw.txt' ] z_scores = [] for i in range(1): data = np.genfromtxt(datasets[i], dtype=[('a','|S5'),('b','|S5'),('amount','f8')], usemask=True) #import data binary_edgelist = create_edgelist_binary(data) #create edgelist H = nx.DiGraph() #create graph H.add_edges_from(binary_edgelist) #insert edgelist in graph B = nx.adjacency_matrix(H) #make H into an adjacency matrix n = len(H.nodes()) #define number of nodes n H_nodes = np.asarray(H.nodes()) #define the name of the nodes in an array rec = recip(B.todense(),n) #counts the amount of reciprocating links between i and j onrec = out_non_recip(B.todense(),n) #links going from i to j inrec = in_non_recip(B.todense(),n) #amount of links going from j to i #now we calculate the x and y values using Newtons method u = [0.5]*3*n # initial guess s = newton_krylov(f, u) # << this is where the problem lies return(t) t_score = gen_r_scores_anderson() print(t_score) (newton_krylov方法的输入)定义如下

f

我已经读过有关全局变量的内容,但是有些麻烦,还没有想到如何在这种情况下使用它们。感谢您的帮助,希望您能按时帮助我完成论文!

2 个答案:

答案 0 :(得分:1)

要使用全局变量,您需要在函数中声明它们。

例如:

variable = 0
def function():
     global variable
     variable += 1
function()

此代码增加全局变量。但是,此功能不会:

variable = 0
def function(input):
    global variable
    input += 1
function(variable)

局部变量是全局

的副本

答案 1 :(得分:0)

这略有不同,因为我现在使用的是anderson方法,但基本相同。

start1 = time.time()

def gen_r_scores_anderson():
    datasets = [
        'WTW_decades/1960wtw.txt'
            ]
    z_scores = []
    for i in range(1):
        data = np.genfromtxt(datasets[i], dtype=[('a','|S5'),('b','|S5'),('amount','f8')], usemask=True) #import data
        binary_edgelist = create_edgelist_binary(data) #create edgelist
        H = nx.DiGraph() #create graph
        H.add_edges_from(binary_edgelist) #insert edgelist in graph
        B = nx.adjacency_matrix(H) #make H into an adjacency matrix
        global n
        n = len(H.nodes()) #define number of nodes n
        H_nodes = np.asarray(H.nodes()) #define the name of the nodes in an array

        global rec
        rec = recip(B.todense(),n) #counts the amount of reciprocating links between i and j

        global onrec
        onrec = out_non_recip(B.todense(),n) #links going from i to j

        global inrec
        inrec = in_non_recip(B.todense(),n) #amount of links going from j to i

        #now we calculate the x and y values using Newtons method

        u = [0.5]*3*n
        s = anderson(f, u)
        t = makematrixpos(s)
    return(t)

t_score = gen_r_scores_anderson()
print(t_score)

end1 = time.time()
print(end1 - start1)

所以我所做的就是将global放在gen_all_data函数中,而不是将global放入finrec,{{1} }或rec函数