Why can't complex integer arithmetic be done?
36 次查看(过去 30 天)
显示 更早的评论
I have been experimenting with a model, comparing fixed and floating-point. At one stage I am taking the product of a complex floating-point array and a complex int16 array. However complex integer arithmetic is not supported. Why is this and can it be changed?
3 个评论
David Goodmanson
2021-5-14
Hi Joshua,
Aside from multiplcation, what kind of operations are you interested in doing with complex integer variables?
回答(3 个)
James Tursa
2021-5-14
编辑:James Tursa
2021-5-14
I think the main reason for disallowing integer array arithmetic in MATLAB is because of the ambiguity caused by the max(min( )) behavior of integer arithmetic. Unlike other languages such as C/C++ that typically do modulo arithmetic for overflow situations, MATLAB does extreme value clipping. E.g., take this operation:
uint8(200) + uint8(200)
In C/C++ this will result in mod(200+200,256) = mod(400,256) = 144
In C/C++ you can string together several such operations and get a consistent answer regardless of the order of operations.
But in MATLAB this results in max(200+200,255) = max(400,255) = 255
This results in MATLAB depending on the order of operations. E.g., take this expression:
uint8(200) + uint8(200) - uint8(200)
Group the operations in two different ways:
(uint8(200) + uint8(200)) - uint8(200) = 255 - 200 = 55
uint8(200) + (uint8(200) - uint8(200)) = 200 + 0 = 200
So, depending on the order of operations you get two different answers. This becomes problematic when the operation involves anything other than real scalars because of the operation order ambiguity noted above. Depending on assumptions made for operation order of intermediate expressions when using arrays, you can get a completely different answer. To avoid this ambiguity, MATLAB simply disallows it.
My guess is that you want to treat this operation as a floating point operation, in which case converting the operands to floating point prior to the operation as Jan suggest is the way to go. You can always convert the result back to an integer type after the operation.
0 个评论
Andy Bartlett
2021-6-7
编辑:Andy Bartlett
2021-6-7
The integers provided in base MATLAB were design to support image processing use cases.
Integer types are also a special case of the fi objects provided by Fixed-Point Designer. Fi objects were designed to support general purpose calculations for controls, signal processing, image processing, etc. Complex operations are supported for fi objects.
The following code will produce an error due to the complex limitations of MATLAB built-in integers.
a = complex(7,-5)
b = complex(3,2)
c = a * b
ai = int16(a)
bi = int16(b)
ci = ai * bi
Producing this output
>> complex_fi_mult
a =
7 - 5i
b =
3 + 2i
c =
31 - 1i
ai =
int16
7 - 5i
bi =
int16
3 + 2i
Error using *
Complex integer arithmetic is not supported.
Error in complex_fi_mult (line 7)
ci = ai * bi
If the integer types are first converted to their fi object equivalents, then the complex math is supported.
a = complex(7,-5)
b = complex(3,2)
c = a * b
ai = int16(a)
bi = int16(b)
af = castIntToFi(ai)
bf = castIntToFi(bi)
cf = af * bf
as shown in this output
>> complex_fi_mult2
a =
7 - 5i
b =
3 + 2i
c =
31 - 1i
ai =
int16
7 - 5i
bi =
int16
3 + 2i
af =
7 - 5i
numerictype(1,16,0)
bf =
3 + 2i
numerictype(1,16,0)
cf =
31 - 1i
numerictype(1,33,0)
0 个评论
Jan
2021-5-14
What about converting the complex int16 value to a single?
a = int16(complex(2,3))
b = single(4 + 5i)
c = single(a) * b
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Fixed-Point Designer 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!