python两个列表的内积_如何加快python中的多个内积的运算?

在我的机器上,C版本代码在100秒内解决了n=20的问题!import itertools

def necklaces_with_multiplicity(n):

assert isinstance(n, int)

assert n > 0

w = [1] * n

i = 1

while True:

if n % i == 0:

s = sum(w)

if s > 0:

yield (tuple(w), i * 2)

elif s == 0:

yield (tuple(w), i)

i = n - 1

while w[i] == -1:

if i == 0:

return

i -= 1

w[i] = -1

i += 1

for j in range(n - i):

w[i + j] = w[j]

def leading_zero_counts(n):

assert isinstance(n, int)

assert n > 0

assert n % 2 == 0

counts = [0] * n

necklaces = list(necklaces_with_multiplicity(n))

for combo in itertools.combinations(range(n - 1), n // 2):

for v, multiplicity in necklaces:

w = list(v)

for j in combo:

w[j] *= -1

for i in range(n):

counts[i] += multiplicity * 2

product = 0

for j in range(n):

product += v[j - (i + 1)] * w[j]

if product != 0:

break

return counts

if __name__ == '__main__':

print(leading_zero_counts(12))

c版本:#include

enum {

N = 14

};

struct Necklace {

unsigned int v;

int multiplicity;

};

static struct Necklace g_necklace[1 << (N - 1)];

static int g_necklace_count;

static void initialize_necklace(void) {

g_necklace_count = 0;

for (unsigned int v = 0; v < (1U << (N - 1)); v++) {

int multiplicity;

unsigned int w = v;

for (multiplicity = 2; multiplicity < 2 * N; multiplicity += 2) {

w = ((w & 1) << (N - 1)) | (w >> 1);

unsigned int x = w ^ ((1U << N) - 1);

if (w < v || x < v) goto nope;

if (w == v || x == v) break;

}

g_necklace[g_necklace_count].v = v;

g_necklace[g_necklace_count].multiplicity = multiplicity;

g_necklace_count++;

nope:

;

}

}

int main(void) {

initialize_necklace();

long long leading_zero_count[N + 1];

for (int i = 0; i < N + 1; i++) leading_zero_count[i] = 0;

for (unsigned int v_xor_w = 0; v_xor_w < (1U << (N - 1)); v_xor_w++) {

if (__builtin_popcount(v_xor_w) != N / 2) continue;

for (int k = 0; k < g_necklace_count; k++) {

unsigned int v = g_necklace[k].v;

unsigned int w = v ^ v_xor_w;

for (int i = 0; i < N + 1; i++) {

leading_zero_count[i] += g_necklace[k].multiplicity;

w = ((w & 1) << (N - 1)) | (w >> 1);

if (__builtin_popcount(v ^ w) != N / 2) break;

}

}

}

for (int i = 0; i < N + 1; i++) {

printf(" %lld", 2 * leading_zero_count[i]);

}

putchar('\n');

return 0;

}

您可以通过利用符号对称性(4x)和只迭代通过第一个内积测试的向量(渐变,O(sqrt(N))x)来获得一些加速。import itertools

n = 10

m = n + 1

def innerproduct(A, B):

s = 0

for k in range(n):

s += A[k] * B[k]

return s

leadingzerocounts = [0] * m

for S in itertools.product([-1, 1], repeat=n - 1):

S1 = S + (1,)

S1S1 = S1 * 2

for C in itertools.combinations(range(n - 1), n // 2):

F = list(S1)

for i in C:

F[i] *= -1

leadingzerocounts[0] += 4

for i in range(1, m):

if innerproduct(F, S1S1[i:i + n]):

break

leadingzerocounts[i] += 4

print(leadingzerocounts)

C版本,为了了解我们对PyPy损失了多少性能(PyPy为16,大致相当于C为18):#include

enum {

HALFN = 9,

N = 2 * HALFN

};

int main(void) {

long long lzc[N + 1];

for (int i = 0; i < N + 1; i++) lzc[i] = 0;

unsigned int xor = 1 << (N - 1);

while (xor-- > 0) {

if (__builtin_popcount(xor) != HALFN) continue;

unsigned int s = 1 << (N - 1);

while (s-- > 0) {

lzc[0]++;

unsigned int f = xor ^ s;

for (int i = 1; i < N + 1; i++) {

f = ((f & 1) << (N - 1)) | (f >> 1);

if (__builtin_popcount(f ^ s) != HALFN) break;

lzc[i]++;

}

}

}

for (int i = 0; i < N + 1; i++) printf(" %lld", 4 * lzc[i]);

putchar('\n');

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用`pandas`库来实现对两个多个连续单元格进行加法运算,具体步骤如下: 1. 使用`pandas`库的`read_excel`函数读取Excel文件,并将需要进行加法运算的单元格读取为`DataFrame`对象。 ```python import pandas as pd # 读取Excel文件 df = pd.read_excel('example.xlsx', sheet_name='Sheet1') # 选择需要进行加法运算的单元格 df_add = df.loc[:, ['A1', 'A2', 'A3']] ``` 2. 使用`pandas`库的`sum`函数对选定的单元格进行加法运算,并将结果保存到新的`DataFrame`对象。 ```python # 对选定的单元格进行加法运算 df_sum = pd.DataFrame(df_add.sum(axis=1), columns=['Sum']) ``` 3. 将结果保存到Excel文件。 ```python # 将结果保存到Excel文件 with pd.ExcelWriter('example.xlsx') as writer: df_sum.to_excel(writer, sheet_name='Sheet1', index=False) ``` 完整代码如下: ```python import pandas as pd # 读取Excel文件 df = pd.read_excel('example.xlsx', sheet_name='Sheet1') # 选择需要进行加法运算的单元格 df_add = df.loc[:, ['A1', 'A2', 'A3']] # 对选定的单元格进行加法运算 df_sum = pd.DataFrame(df_add.sum(axis=1), columns=['Sum']) # 将结果保存到Excel文件 with pd.ExcelWriter('example.xlsx') as writer: df_sum.to_excel(writer, sheet_name='Sheet1', index=False) ``` 其,`example.xlsx`是需要进行加法运算的Excel文件,`Sheet1`是需要进行加法运算的工作表名称,`A1`、`A2`、`A3`是需要进行加法运算的单元格。在上述代码,将选定的单元格按行求和,并将结果保存为一个新的`DataFrame`对象,最后将结果保存到Excel文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值