我正在学习Fortran中的BCASTing数据类型,并且有一个代码从终端获取两个值并在每个进程上显示它们.对于整数/整数和整数/实数类型的组合value1/value2,这是有效的,但是对于组合整数/实数*8,它会失败.
代码是:
use mpi
implicit none
integer :: ierror, pid, ncpu, root = 0
integer :: counts, newtype, extent
integer, dimension(2) :: oldtypes, blockcounts, offsets
type value
integer :: value1 = 0
real*8 :: value2
end type
type (value) input
call MPI_INIT(ierror)
call MPI_COMM_RANK(MPI_COMM_WORLD, pid, ierror)
call MPI_COMM_SIZE(MPI_COMM_WORLD, ncpu, ierror)
! setup of 1 MPI_INTEGER field: value1
offsets(1) = 0
oldtypes(1) = MPI_INTEGER
blockcounts(1) = 1
! setup of 1 MPI_REAL8 field: value2
call MPI_TYPE_EXTENT(MPI_INTEGER, extent, ierror) !determine offset of …Run Code Online (Sandbox Code Playgroud) 我有一个log-log图,范围从10^-3到10^+3.我希望值在指数中?10^0有一个+符号,类似于值如何在指数中<10^0有-符号.在matplotlib中有一个简单的方法吗?
我调查了一下,FuncFormatter但实现这一目标似乎过于复杂,我也无法让它发挥作用.
使用以下命令很容易检查列表的元素是否在另一个列表中any():
any(elem in list2 for elem in list1)
Run Code Online (Sandbox Code Playgroud)
但无论如何,有没有惯用的方式来返回找到的第一个元素?
我更喜欢单行解决方案,而不是:
for elem in list1:
if elem in list2:
return elem
Run Code Online (Sandbox Code Playgroud) 我有一个矢量类:
class Vector:
def __init__(self, x, y):
self.x, self.y = x, y
def __str__(self):
return '(%s,%s)' % (self.x, self.y)
def __add__(self, n):
if isinstance(n, (int, long, float)):
return Vector(self.x+n, self.y+n)
elif isinstance(n, Vector):
return Vector(self.x+n.x, self.y+n.y)
Run Code Online (Sandbox Code Playgroud)
哪个工作正常,即我可以写:
a = Vector(1,2)
print(a + 1) # prints (2,3)
Run Code Online (Sandbox Code Playgroud)
但是,如果操作顺序颠倒,则失败:
a = Vector(1,2)
print(1 + a) # raises TypeError: unsupported operand type(s)
# for +: 'int' and 'instance'
Run Code Online (Sandbox Code Playgroud)
我理解错误:一个的加入int对象的Vector,因为我没有在定义它的对象是未定义int类.有没有办法解决这个问题而不在类int(或父int类)中定义它?
说我有一个方法:
def add_force_coupling(g1, g2, name):
print {(g1,g2): name}
Run Code Online (Sandbox Code Playgroud)
那么代码:
l = 3
for g1 in range(l):
for g2 in range(l):
name = 'G' + str(g1) + str(g2)
add_force_coupling(g1, g2, name)
Run Code Online (Sandbox Code Playgroud)
产生结果:
{(0, 0): 'G00'}, {(0, 1): 'G01'}, {(0, 2): 'G02'}, {(1, 0): 'G10'}, ..., {(2, 2): 'G22'}
Run Code Online (Sandbox Code Playgroud)
现在我想通过使用该map()函数使这更加pythonic .到目前为止我有:
list1 = sorted(l*range(l))
list2 = l*range(l)
list3 = ["".join(values) for values in zip(l*l*'G',list(map(str,l1)),list(map(str,l2)))]
map( add_force_coupling, list1, list2, list3 )
Run Code Online (Sandbox Code Playgroud)
这不正是我想要什么,但比标准的双回路丑陋,因为INT表需要转换到str在list3.此外,很难理解发生了什么.是否有一个重写的双循环的任何更好的Python的方式map(),zip()或以其他方式?
注意:不能选择更改方法
我有如下代码示例中定义的数组结构:
struct Struct {
float *field;
};
Run Code Online (Sandbox Code Playgroud)
其中 field 是具有 index 的任意长度的数组idx。目前我正在按如下方式填充这些数组:
float *field_ptr = a->field;
field_ptr[idx] = 1.0f;
Run Code Online (Sandbox Code Playgroud)
有没有没有中间 field_ptr 指针的直接填充数组的方法?我尝试了几种方法,但不幸的是我不完全是 C 或指针专家,所以我遇到了越界内存问题。
编辑 1:知道这是 (Py)Cuda 代码的一部分可能很有用。
编辑 2:代码位于具有以下(指针)声明的示例函数中:
void testfunction(Struct *a)
{
int idx = get_index();
float *field_ptr = a->field;
field_ptr[idx] = 1.0f;
}
Run Code Online (Sandbox Code Playgroud) 我有一个ExampleSim从基类继承的类Physics:
class Physics(object):
arg1 = 'arg1'
def physics_method:
print 'physics_method'
class ExampleSim(Physics):
print 'example physics sim'
Run Code Online (Sandbox Code Playgroud)
想象一下这些包含大量代码的类。现在,我Physics通过定义一个新类PhysicsMod并继承自Physics:
class PhysicsMod(Physics):
arg1 = 'modified arg1'
Run Code Online (Sandbox Code Playgroud)
但ExampleSim我为此又创建了一个新类ExampleSimMod并继承自ExampleSim:
class ExampleSimMod(ExampleSim):
print 'modified example sim'
Run Code Online (Sandbox Code Playgroud)
我的问题是,ExampleSimMod继承ExampleSim从继承Physics在那里我想有它继承PhysicsMod来代替。有没有办法做到这一点?也许通过super()或通过多重继承?
class ExampleSimMod(ExampleSim, PhysicsMod):
print 'modified example sim'
Run Code Online (Sandbox Code Playgroud) python ×5
python-2.7 ×3
class ×2
arrays ×1
c ×1
for-loop ×1
fortran ×1
inheritance ×1
map-function ×1
matplotlib ×1
mpi ×1
object ×1
pointers ×1
struct ×1
types ×1