Python operator - Standard operators as functions

Python operator - Standard operators as functions

https://docs.python.org/3/library/operator.html

The operator module exports a set of efficient functions corresponding to the intrinsic operators of Python. For example, operator.add(x, y) is equivalent to the expression x + y. Many function names are those used for special methods, without the double underscores. For backward compatibility, many of these have a variant with the double underscores kept. The variants without the double underscores are preferred for clarity.
operator 模块提供了一套与 Python 的内置运算符对应的高效率函数。例如,operator.add(x, y) 与表达式 x+y 相同。许多函数名与特殊方法名相同,只是没有双下划线。为了向后兼容性,也保留了许多包含双下划线的函数。为了表述清楚,建议使用没有双下划线的函数。

  • operator.matmul(a, b)
    operator.__matmul__(a, b)

Return a @ b.

#!/usr/bin/env python
# coding=utf-8

import numpy as np

A = np.array([[1, 2],
              [3, 4],
              [5, 6]])
B = np.array([[2, 4],
              [1, 3]])

C = A @ B
print(f"{C}")

/home/yongqiang/miniconda3/bin/python /home/yongqiang/stable_diffusion_work/stable_diffusion_diffusers/yongqiang.py 
[[ 4 10]
 [10 24]
 [16 38]]
 
Process finished with exit code 0

1. Mapping Operators to Functions

This table shows how abstract operations correspond to operator symbols in the Python syntax and the functions in the operator module.
以下表格显示了抽象运算是如何对应于 Python 语法中的运算符和 operator 模块中的函数的。

OperationSyntaxFunction
Additiona + badd(a, b)
Concatenation (字符串拼接)seq1 + seq2concat(seq1, seq2)
Containment Test (包含测试)obj in seqcontains(seq, obj)
Divisiona / btruediv(a, b)
Divisiona // bfloordiv(a, b)
Bitwise And (按位与)a & band_(a, b)
Bitwise Exclusive Or (按位异或)a ^ bxor(a, b)
Bitwise Inversion (按位取反)~ ainvert(a)
Bitwise Or (按位或)a | bor_(a, b)
Exponentiation (取幂)a ** bpow(a, b)
Identitya is bis_(a, b)
Identitya is not bis_not(a, b)
Indexed Assignmentobj[k] = vsetitem(obj, k, v)
Indexed Deletiondel obj[k]delitem(obj, k)
Indexingobj[k]getitem(obj, k)
Left Shifta << blshift(a, b)
Modulo (取模)a % bmod(a, b)
Multiplicationa * bmul(a, b)
Matrix Multiplicationa @ bmatmul(a, b)
Negation (Arithmetic)- aneg(a)
Negation (Logical)not anot_(a)
Positive+ apos(a)
Right Shifta >> brshift(a, b)
Slice Assignmentseq[i:j] = valuessetitem(seq, slice(i, j), values)
Slice Deletiondel seq[i:j]delitem(seq, slice(i, j))
Slicingseq[i:j]getitem(seq, slice(i, j))
String Formattings % objmod(s, obj)
Subtractiona - bsub(a, b)
Truth Testobjtruth(obj)
Ordering (比较)a < blt(a, b)
Orderinga <= ble(a, b)
Equalitya == beq(a, b)
Differencea != bne(a, b)
Orderinga >= bge(a, b)
Orderinga > bgt(a, b)

2. In-place Operators

Many operations have an “in-place” version. Listed below are functions providing a more primitive access to in-place operators than the usual syntax does; for example, the statement x += y is equivalent to x = operator.iadd(x, y).

  • operator.iadd(a, b)
    operator.__iadd__(a, b)

a = iadd(a, b) is equivalent to a += b.

  • operator.imul(a, b)
    operator.__imul__(a, b)

a = imul(a, b) is equivalent to a *= b.

  • operator.imatmul(a, b)
    operator.__imatmul__(a, b)

a = imatmul(a, b) is equivalent to a @= b.

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/

资源下载链接为: https://pan.quark.cn/s/abbae039bf2a DDC控制器是一种智能化的控制设备,广泛应用于建筑自动化、工业控制以及环境监控等领域。它基于先进的微处理器技术,具备强大的数据处理能力和灵活的编程功能。通过预先设定的程序,DDC控制器能够对各类传感器采集的信号进行分析处理,并根据预设的控制策略,精准地驱动执行器完成相应的操作。 在建筑自动化系统中,DDC控制器可用于控制暖通空调系统,实现对温度、湿度、风速等参数的精确调节。它能根据室内外环境的变化,自动调整空调设备的运行状态,确保室内环境的舒适性,同时优化能源消耗。此外,DDC控制器还可用于照明控制,根据自然光照强度和人员活动情况,自动调节灯光亮度,实现节能与舒适性的平衡。 在工业控制领域,DDC控制器可用于监控和控制生产线上的各种设备。它可以实时采集设备的运行数据,如温度、压力、流量等,通过分析这些数据判断设备的运行状态,并及时发出指令调整设备的运行参数,确保生产过程的稳定性和产品质量的可靠性。 DDC控制器具有高度的可靠性和稳定性,能够在恶劣的环境条件下长期稳定运行。其模块化的设计便于安装、调试和维护,用户可以根据实际需求灵活配置控制器的输入输出模块。此外,DDC控制器还具备良好的兼容性,能够与多种类型的传感器和执行器无缝对接,构建完整的自动化控制系统。 总之,DDC控制器凭借其卓越的性能和广泛的应用领域,已成为现代自动化控制系统中不可或缺的核心设备,为实现智能化、高效化和节能化的控制目标提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值