Why can't complex integer arithmetic be done?

58 visualizaciones (últimos 30 días)
Joshua Ford
Joshua Ford el 12 de Mayo de 2021
Editada: Andy Bartlett el 7 de Jun. de 2021
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 comentarios
David Goodmanson
David Goodmanson el 14 de Mayo de 2021
Hi Joshua,
Aside from multiplcation, what kind of operations are you interested in doing with complex integer variables?
Joshua Ford
Joshua Ford el 14 de Mayo de 2021
Hi David,
Thank you for yout reply.
Taking the product involving complex integers is the only operation I wish to do.
Cheers

Iniciar sesión para comentar.

Respuestas (3)

James Tursa
James Tursa el 14 de Mayo de 2021
Editada: James Tursa el 14 de Mayo de 2021
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.

Andy Bartlett
Andy Bartlett el 7 de Jun. de 2021
Editada: Andy Bartlett el 7 de Jun. de 2021
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)

Jan
Jan el 14 de Mayo de 2021
What about converting the complex int16 value to a single?
a = int16(complex(2,3))
a = int16 2 + 3i
b = single(4 + 5i)
b = single 4.0000 + 5.0000i
c = single(a) * b
c = single -7.0000 + 22.0000i

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by