计算化学公社

标题: 请教大家两个python的小问题 [打印本页]

作者
Author:
柒月小鱼    时间: 2020-3-28 22:11
标题: 请教大家两个python的小问题
1.dd = re.compile(r'Initial\scommand:[\s\S]+Title\sCard\sRequired?')请教一下各位,这个Title\sCard\sRequired我只想匹配第一个,但是由于前面开启了贪婪模式,Title\sCard\sRequired一直匹配到了最后面的第二个
有什么办法可以解决么
2.我想求两个相同大小的矩阵的对应行之间的角度,
目前我是A×B.T=C,C的第一列即是点乘;再计算每一行的模长,这样计算的。
有什么简便的方法的,比如直接np.dot(vector1,vector2)/(np.linalg.norm(vector1)
先谢谢各位了


作者
Author:
liyuanhe211    时间: 2020-3-29 06:55
本帖最后由 liyuanhe211 于 2020-3-29 07:10 编辑

1. r'Initial\scommand:[\s\S]+?Title\sCard\sRequired?'(不确定我明白你的意思了)
2. 没懂,是求两个矩阵中行向量之间的角度?能否给出一个样例出入输出?可以看看numpy.einsum(),听起来多半能用它实现
作者
Author:
柒月小鱼    时间: 2020-3-29 11:03
liyuanhe211 发表于 2020-3-29 06:55
1. r'Initial\scommand:[\s\S]+?Title\sCard\sRequired?'(不确定我明白你的意思了)
2. 没懂,是求两个矩 ...

谢谢回复
1.Initial command:
/home/test/g16/l1.exe "/home/test/tmp/Gau-45062.inp" -scrdir="/home/test/tmp/"
Entering Link 1 = /home/test/g16/l1.exe PID=     45063.
  
Copyright (c) 1988,1990,1992,1993,1995,1998,2003,2009,2016,
            Gaussian, Inc.  All Rights Reserved.
  
This is part of the Gaussian(R) 16 program.  It is based on
the Gaussian(R) 09 system (copyright 2009, Gaussian, Inc.),
the Gaussian(R) 03 system (copyright 2003, Gaussian, Inc.),
the Gaussian(R) 98 system (copyright 1998, Gaussian, Inc.),
the Gaussian(R) 94 system (copyright 1995, Gaussian, Inc.),
the Gaussian 92(TM) system (copyright 1992, Gaussian, Inc.),
the Gaussian 90(TM) system (copyright 1990, Gaussian, Inc.),
the Gaussian 88(TM) system (copyright 1988, Gaussian, Inc.),
the Gaussian 86(TM) system (copyright 1986, Carnegie Mellon
University), and the Gaussian 82(TM) system (copyright 1983,
Carnegie Mellon University). Gaussian is a federally registered
trademark of Gaussian, Inc.
Initial command:
/home/test/g16/l1.exe "/home/test/tmp/Gau-45062.inp" -scrdir="/home/test/tmp/"
Entering Link 1 = /home/test/g16/l1.exe PID=     45063.
  
Copyright (c) 1988,1990,1992,1993,1995,1998,2003,2009,2016,
            Gaussian, Inc.  All Rights Reserved.
  
This is part of the Gaussian(R) 16 program.  It is based on
the Gaussian(R) 09 system (copyright 2009, Gaussian, Inc.),
the Gaussian(R) 03 system (copyright 2003, Gaussian, Inc.),
the Gaussian(R) 98 system (copyright 1998, Gaussian, Inc.),
the Gaussian(R) 94 system (copyright 1995, Gaussian, Inc.),
the Gaussian 92(TM) system (copyright 1992, Gaussian, Inc.),
the Gaussian 90(TM) system (copyright 1990, Gaussian, Inc.),
the Gaussian 88(TM) system (copyright 1988, Gaussian, Inc.),
the Gaussian 86(TM) system (copyright 1986, Carnegie Mellon
University), and the Gaussian 82(TM) system (copyright 1983,
Carnegie Mellon University). Gaussian is a federally registered
trademark of Gaussian, Inc.
比如我想截取第一个Initial command:到第一个trademark of Gaussian, Inc之间的内容,
dd = re.compile(r'Initial\scommand:([\s\S]+)trademark\sof\sGaussian,\sInc')
for one in dd.findall(text):
    print(one)
但是这样截取的是整个段落,我可以怎么做呢
2.谢谢提供的函数,
A = np.array([[1, 2], [3, 4],[1,4]])
B = np.array([[5, 6], [7, 8],[5,8]])
计算对应行的cosα,我自己这么计算也算蛮快了
作者
Author:
冰释之川    时间: 2020-3-29 11:22
柒月小鱼 发表于 2020-3-29 11:03
谢谢回复
1.Initial command:
/home/test/g16/l1.exe "/home/test/tmp/Gau-45062.inp" -scrd ...

在+后面加上?切换成非贪婪模式看看
作者
Author:
柒月小鱼    时间: 2020-3-29 11:35
冰释之川 发表于 2020-3-29 11:22
在+后面加上?切换成非贪婪模式看看

谢谢
我咋就没这么试一下呢,搞了我好久的说
非常谢谢
作者
Author:
liyuanhe211    时间: 2020-3-29 12:24
柒月小鱼 发表于 2020-3-29 11:03
谢谢回复
1.Initial command:
/home/test/g16/l1.exe "/home/test/tmp/Gau-45062.inp" -scrd ...

第一个问题在2L已回复
第二个问题定义一个row-wise点乘的操作就行了
  1. f = lambda x,y:np.einsum('ij,ij->i',x,y)
  2. dot_pds = f(A,B)/np.sqrt(f(A,A)*f(B,B)))
复制代码

f(A,A)等价于np.linalg.norm(A,axis=1)
作者
Author:
柒月小鱼    时间: 2020-3-29 13:08
liyuanhe211 发表于 2020-3-29 12:24
第一个问题在2L已回复
第二个问题定义一个row-wise点乘的操作就行了

非常谢谢
这个比我的简单多了
学习




欢迎光临 计算化学公社 (http://bbs.keinsci.com/) Powered by Discuz! X3.3