作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 Matlab 中有一个命令来定义一个新的数字类型,例如:
numerictype(0,16,8)
参见文档:
https://www.mathworks.com/help/fixedpoint/ref/embedded.numerictype.html
dtype=[('signed', np.bool_), ('word', np.int16), ('frac', np.int16)])
我的最终目标是实现三个独立的 reinterpertcast 语句,即:
reinterpretcast(EVMacq,numerictype(0,16,8))
reinterpretcast(Payload16c,numerictype(1,16,16))
reinterpretcast(Payload32,numerictype(1,32,32))
如果有一种方法可以更简单地做到这些,我非常乐意以不同的方式做到这一点。
reinterpretcast(Payload16c,numerictype(1,16,16))
其中 Payload16c 是由
np.complex(real,imag)
定义的复数数组.先感谢您。
i = 0
result = []
#first generate a binary number that is a one in the highest spot and zero elsewhere
comp = 2**wordlength
#next iterate through entire array
while i < array.size:
#check to see if the value of the item is near the largest value it can be
#if so its likely that it is just negative and thats why that bit is high
if(array[i:i+1] < ((2**fracbits)-1000)):
#if it is not near the largest number simply convert divide to move decimal place
real = array[i:i+1] * (2**-fracbits)
else:
#else we subtract comp so that we get the negative number this binary string was supposed to represent.
# print(np.binary_repr(np.uint16(array[i:i+1])))
real = double(array[i:i+1]) - comp
#then we divide it to move the decimal point properly
real = real * (2**-fracbits)
#same for the next number in the array which is the imaginary component
if(array[i+1:i+2] < ((2**fracbits)-2000)):
imag = array[i+1:i+2] * (2**-fracbits)
else:
imag = double(array[i+1:i+2]) - comp
imag = imag * (2**-fracbits)
result.append(np.complex(real,imag))
i+=2
return result
最佳答案
从 Python 程序员的角度来看,真正深入研究数据类型与 Python 本身的性质背道而驰。 python is dynamically typed ,这意味着缺乏效率,但易于编程。为了解决这个问题,many popular libraries are written in c ,因此您可能需要查看类似 numpy 的库得到你的打字修复。 Here is an example of setting datatypes in numpy .但据我所知,这些仅适用于预定义的 c 类型
理论上,您可以定义一个特殊的类来包含您的数据,实现 __add__
, __subtract__
,以及任何其他必要的关键功能。但是,由于 python 是动态类型的,这实际上可能具有有限的返回。
另一种选择可能是 Cython ,它允许你在 python 中定义 C 类型,但是如果你只是想要一个快速的函数来定义一个类型,那么 Python 的底层本质正在与你作对。
关于python - python中的Matlab numerictype/reinterpretcast等价物?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63293463/
我是一名优秀的程序员,十分优秀!