[英]Same equation gives different values in Matlab and Numpy?
I'm trying to convert a function from Matlab to Python. 我正在尝试将功能从Matlab转换为Python。 The Matlab function is: Matlab函数是:
function [f,df_dr1,df_dr2,g,dg_dr1,dg_dr2] = f_eval_2eq(r1,r2,r3,z1,z2,z3,n1,n2,n3)
f = (r1)./sqrt(r1.^2 + z1.^2)...
- (n2/n1)*(r2-r1)./sqrt((r2-r1).^2 + z2.^2);
df_dr1 = 1./sqrt(r1.^2 + z1.^2)...
- r1.^2./(r1.^2 + z1.^2).^(3/2)...
+ (n2/n1)./sqrt(z2.^2 + (r1-r2).^2)...
- (n2/n1).*(r1-r2).*(2*r1-2*r2)./(2*((r1-r2).^2 + z2.^2).^(3/2));
df_dr2 = (n2/n1).*(r1-r2).*(2*r1-2*r2)./(2*((r1-r2).^2 + z2.^2).^(3/2))...
- (n2/n1)./sqrt(z2.^2 + (r1-r2).^2);
g = (r2-r1)./sqrt((r2-r1).^2 + z2.^2)...
- (n3/n2)*(r3-r2)./sqrt((r3-r2).^2 + z3.^2);
dg_dr1 = (r1-r2).*(2*r1-2*r2)./(2*((r1-r2).^2 + z2.^2).^(3/2))...
- 1./sqrt(z2.^2 + (r1-r2).^2);
dg_dr2 = 1./sqrt((r1-r2).^2 + z2.^2)...
+ (n3/n2)./sqrt(z3.^2 + (r2-r3).^2)...
- (r1-r2).*(2*r1-2*r2)./(2*((r1-r2).^2 + z2.^2).^(3/2))...
- (n3/n2).*(r2-r3).*(2*r2-2*r3)./(2*((r2-r3).^2 + z3.^2).^(3/2));
end
%test code
K>> a=[1,2,3];b=a+1;c=b+1;d=a;e=b;f=c;g=1;h=2;i=3;
K>> [f,df_dr1,df_dr2,g,dg_dr1,dg_dr2] = f_eval_2eq(a,b,c,d,e,f,g,h,i)
The Python function I wrote is: 我写的Python函数是:
def f_eval_2eq(r1,r2,r3,z1,z2,z3,n1,n2,n3):
#evaluate gradients
#n_ are scalars
f = (r1)/np.sqrt(r1**2 + z1**2) \
- (n2/n1)*(r2-r1)/np.sqrt((r2-r1)**2 + z2**2);
df_dr1 = 1/np.sqrt(r1**2 + z1**2) \
- r1**2/((r1**2 + z1**2)**(3/2)) \
+ (n2/n1)/np.sqrt(z2**2 + (r1-r2)**2) \
- (n2/n1)*(r1-r2)*(2*r1-2*r2)/(2*((r1-r2)**2 + z2**2)**(3/2));
df_dr2 = (n2/n1)*(r1-r2)*(2*r1-2*r2)/(2*((r1-r2)**2 + z2**2)**(3/2)) \
- (n2/n1)/np.sqrt(z2**2 + (r1-r2)**2);
g = (r2-r1)/np.sqrt((r2-r1)**2 + z2**2) \
- (n3/n2)*(r3-r2)/np.sqrt((r3-r2)**2 + z3**2);
dg_dr1 = (r1-r2)*(2*r1-2*r2)/(2*((r1-r2)**2 + z2**2)**(3/2)) \
- 1/np.sqrt(z2**2 + (r1-r2)**2);
dg_dr2 = 1/np.sqrt((r1-r2)**2 + z2**2) \
+ (n3/n2)/np.sqrt(z3**2 + (r2-r3)**2) \
- (r1-r2)*(2*r1-2*r2)/(2*((r1-r2)**2 + z2**2)**(3/2)) \
- (n3/n2)*(r2-r3)*(2*r2-2*r3)/(2*((r2-r3)**2 + z3**2)**(3/2));
return (f,df_dr1,df_dr2,g,dg_dr1,dg_dr2)
#test code
A=np.array([1,2,3])
B=A+1
C=B+1
D=A
E=B
F=C
G=1
H=2
I=3
[f,df_dr1,df_dr2,g,dg_dr1,dg_dr2] =f_eval_2eq(A,B,C,D,E,F,G,H,I)
print ('f= '+str(f) +'\n'+'df_dr1= '+str(df_dr1) +'\n' +'df_dr2='+str(df_dr2) +'\n'+'g= '+str(g) +'\n'+'dg_dr1= '+str(dg_dr1) +'\n'+'dg_dr2= '+str(dg_dr2) +'\n')
The output for f is the same in both, but all the other values are different and I cant figure out why??? f的输出在两者中是相同的,但是所有其他值都不同,我无法弄清楚为什么???
Any help is appreciated. 任何帮助表示赞赏。
In Python 2.x, if you divide two integers (such as 2
and 3
) the result is cast as an integer as well: 在Python 2.x中,如果将两个整数(例如2
和3
)相除,则结果也将转换为整数:
x = 3/2
# 1
type(x)
# <type 'int'>
You need to explicitly specify either the numerator or denominator to be a float rather than an integer using a decimal point and this will allow the output to be a float as well. 您需要明确指定分子或分母为浮点数,而不是使用小数点的整数,这也将使输出也为浮点数。
y = 3./2
# 1.5
type(y)
# <type 'float'>
Alternately, as suggested by @rayryeng, you can place the following at the top of your code to get the behavior you expect. 或者,按照@rayryeng的建议,可以将以下内容放在代码的顶部,以获取所需的行为。
from __future__ import division
You can also add 您也可以添加
from __future__ import division
to the top of your file, if you're using Python 2, in order to get the Python 3 behavior, ie always using float division. 如果您使用的是Python 2,则返回文件顶部,以便获得Python 3的行为,即始终使用浮点除法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.