如何在python中调用特定的INT

问题描述 投票:-1回答:2

要计算h,我们必须有f1f2fn用于n*n矩阵。 f可以拥有所有可能的功能,从ODE到简单的等式。我选择xx+yy进行简化。如何在这里调用特定的int,这是f,例如f[2],它会自动将2放入xx[i]+yy[i]f[2]=0.14+33.8一般来说,如上所述,如何调用像f这样的特定int。

xx=np.array([0.1,0.12,0.14,0.16])
yy=np.array([32,33,33.8,34.1])
C=[[13,18],[28,-27]]


#f[i]=xx[i]+yy[i]


r1=[[f1,f2]]
r2=[[f1],[f2]]

m=np.dot(r1,C)
z=np.dot(m,r2)
h=np.linalg.det(z)
print('matrix=',h)

有可能:

A=xx[i]*squad(xx[i]/8)
B=odeint(def,0,A) #which is an ode
F[i]=B-yy[i]
python arrays int
2个回答
0
投票

只需创建一个函数来构造f,将底层函数作为参数,const函数将函数func应用于xxyy中的每一个元素,因此任何接受浮点元组的函数都应该有效。

def const(func):
    return list(map(func, zip(xx, yy)))

f = const(sum)
# f[2] = 33.939999999999998

0
投票

代码中的主要内容是def new_calculation(n):

从math import * import numpy as np from scipy.integrate import quad from scipy.integrate import odeint min = l = m = n = k = t = dec = chi = chiB = chiCMB = hh = cpl = None

xx=np.array([0.01,0.012,0.014,0.016])
yy=np.array([32.95388698,33.87900347,33.84214074,34.11856704])
z = [0.01, 0.012]  # new list

Cov=[[137,168],[28155,-2217]]


def ant(z,O_m,O_D):           
    return 1/sqrt(((1+z)**2)*(1+O_m*z)-z*(2+z)*O_D)

def HDE(y,z):
    a=1/(1+z)        
    dydz = -y*((((H0*(3*b-3+g)*O_m*(a**(3*b-3+g)))/y**2)-(g**3)*w*(a**(-2*g))/3)/(2*(1-g-0.166*(g**2)*w*(a**(-2*g))))-2*(c**2)*(1-1/(2*rc*y)))*a
    return dydz

def new_calculation(n):
    yn = odeint(HDE,y0,[z0,z[n]])  # z[n] takes values from new list
    yyn=yn[-1,0]
    O_Dn=1-O_m-(1/(2*rc*yyn))
    q=quad(ant,0,z[n],args=(O_m,O_Dn))[0]     
    h=log10((1+z[n])*(299000/70)*q)+25     
    fn=(yy[n]-M-h)
    return fn


H0=70
M=2
w=20
b=0.04
O_m=0.27
rc=0.09
c=0.7
for G in range (1,2):
        g=0.1*G  # where is this 'g' used ?

        y0=H0
        z0=0

        f_list = []
        for i in range(2):  # the value '2' reflects matrix size
            f_list.append(new_calculation(i))

        rdag=[f_list]
        rmat=[[f] for f in f_list]

        mm=np.dot(rdag,Cov)
        zz=np.dot(mm,rmat)
        hh=np.linalg.det(zz)*0.000001 

        print('Minimum chi^2 =',hh)
© www.soinside.com 2019 - 2024. All rights reserved.