我正在尝试解决线性方程组。因为有很多我正在使用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)
其中函数f
(newton_krylov 方法的输入(定义如下
def f(w, n, onrec, inrec, rec):
F = [0]*3*n
for i in range(n):
F[i] = -onrec[i]
F[n+i] = -inrec[i]
F[(2*n)+i] = -rec[i]
for j in range(n):
if i == j:
None
else:
F[i] += (w[i]*w[n+j])/(1+w[i]*w[n+j]+w[j]*w[n+i]+w[2*n+i]*w[2*n+j])
F[n+i] += (w[j]*w[n+i])/(1+w[i]*w[n+j]+w[j]*w[n+i]+w[2*n+i]*w[2*n+j])
F[2*n+i] += (w[(2*n)+i]*w[(2*n)+j])/(1+w[i]*w[n+j]+w[j]*w[n+i]+w[2*n+i]*w[2*n+j])
return(F)
我已经阅读了有关全局变量的信息,但是由于有些混乱,还没有弄清楚在这种情况下如何使用它们。提前感谢您的帮助,希望您能帮助我按时交出论文!
要使用全局变量,您需要在函数中声明它们。
例如:
variable = 0
def function():
global variable
variable += 1
function()
此代码递增全局变量。但是,此函数不会:
variable = 0
def function(input):
global variable
input += 1
function(variable)
局部变量是全局变量的副本
这略有不同,因为我现在使用 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
放在f
、inrec
、rec
或onrec
函数中